Example #1
0
def process_date_specifier_to_datestr (now, spec):
        start, end = process_date_specifier (now, spec)
        if start == None and end == None:
            return None
        elif start == None and end != None:
            return end.strftime ("..%Y-%m-%d")
        elif start != None and end == None:
            return start.strftime ("%Y-%m-%d..")
        elif start == end:
            return start.strftime ("%Y-%m-%d")
        else:
            return start.strftime ("%Y-%m-%d") + '..' + end.strftime ("%Y-%m-%d")
Example #2
0
def parse_expr (tokens, type_required=BOOL_TYPE, now = now (), level = 0):
    LOGGER.debug ('parsing %s tokens: %s', level, tokens)
    tok, tokens = next_token (tokens, [TEXT, QUOTED_TEXT, NOT, OPEN_BRACE])
    (t,v) = tok
    
    # NOT
    if t == NOT:
        assert type_required == BOOL_TYPE, "expecting a ' + required_type' expression, not " + BOOL_TYPE
        expr, tokens, expr_type, expr_string = parse_expr (tokens, now=now, level=level+1)
        assert expr_type == BOOL_TYPE, "not must have a boolean argument"
        LOGGER.debug ('built %s:1 %s %s', level, expr_type, expr_string)
        return (lambda x: not expr (x)), tokens, BOOL_TYPE, 'not(' + expr_string + ')'
    
    # LHS
    if t == TEXT and v =='true':
        lhs = lambda x: True
        lhs_string = 'true'
        lhs_type = BOOL_TYPE
    elif t == TEXT and v =='false':
        lhs = lambda x: False
        lhs_string = 'true'
        lhs_type = BOOL_TYPE
    elif t == TEXT and v in ALIAS_LOOKUPS:
        field = ALIAS_LOOKUPS[v]
        lhs = lambda x: access_field(x, field)
        lhs_string = 'field:' + field
        if field in DATE_ALIAS_LOOKUPS:
            lhs_type = DATE_TYPE
        elif field in STRING_ALIAS_LOOKUPS:
            lhs_type = STRING_TYPE
        else:
            lhs_type = BOOL_TYPE
    elif t == OPEN_BRACE:
        lhs, tokens, lhs_type, lhs_string = parse_expr (tokens, now=now, level=level+1)
        tokens = next_token (tokens, [CLOSE_BRACE])[1]
    elif t == QUOTED_TEXT:
        text = v
        lhs = lambda x: unicode (text)
        lhs_string = '"' + v + '"'
        lhs_type = STRING_TYPE
        if type_required == DATE_TYPE:
            rng = process_date_specifier (now, text)
            lhs = lambda x: rng
            lhs_string = '[' + date_range_to_str(rng) + ']'
            lhs_type = DATE_TYPE
    elif t == TEXT:
        text = v
        lhs = lambda x: unicode (text)
        lhs_string = text
        lhs_type = STRING_TYPE
        if type_required == DATE_TYPE:
            rng = process_date_specifier (now, text)
            lhs = lambda x: rng
            lhs_string = '[' + date_range_to_str(rng) + ']'
            lhs_type = DATE_TYPE
    else:
        assert False, 'unexpected token: ' + v
    
    LOGGER.debug ('built %s:2 %s %s', level, lhs_type, lhs_string)
    
    # OPERATOR 
    if len(tokens) == 0:
        LOGGER.debug ('built %s:3 %s %s', level, lhs_type, lhs_string)
        assert type_required == lhs_type, "expecting a " + type_required + ' got a ' + lhs_type + ': ' + lhs_string
        return lhs, tokens, lhs_type, lhs_string
    
    tok, tokens = next_token (tokens,[AND, OR, EQUAL, NOT_EQUAL, CLOSE_BRACE])
    op,v = tok
    if op == CLOSE_BRACE:
        LOGGER.debug ('built %s:4 %s %s', level, lhs_type, lhs_string)
        assert type_required == lhs_type, "expecting a " + type_required + ' got a ' + lhs_type + ': ' + lhs_string
        return lhs, [tok] + tokens, lhs_type, lhs_string
        
    rhs, tokens, rhs_type, rhs_string = parse_expr (tokens, type_required = lhs_type, now=now, level=level+1)         
    assert lhs_type == rhs_type, "incompatible types, " + lhs_type + ' ' + rhs_type

    assert type_required == BOOL_TYPE, "expecting a " + type_required + ' but got ' + BOOL_TYPE

    if op == AND:
        expr_string = '(' + lhs_string + ')AND(' + rhs_string + ')' 
        LOGGER.debug ('built %s:5 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: and_fn(lhs, rhs, x)), tokens, BOOL_TYPE, expr_string
    elif op == OR:
        expr_string = '(' + lhs_string + ')OR(' + rhs_string + ')' 
        LOGGER.debug ('built %s:6 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: or_fn(lhs, rhs, x)), tokens, BOOL_TYPE, expr_string
    elif op == EQUAL:
        expr_string = '(' + lhs_string + ')=(' + rhs_string + ')' 
        LOGGER.debug ('built %s:7 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: eq_fn (lhs (x), rhs (x))), tokens, BOOL_TYPE, expr_string 
    elif op == NOT_EQUAL:
        expr_string = '(' + lhs_string + ')!=(' + rhs_string + ')' 
        LOGGER.debug ('built %s:8 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: ne_fn (lhs (x), rhs (x))), tokens, BOOL_TYPE, expr_string 
Example #3
0
 def __init__(self, filtr, include=True):
     TaskFilterVisitor.__init__(self, process_date_specifier (datetime.now(), filtr), match_start, include)
Example #4
0
 def __init__(self, filtr, include=True):
     ProjectFilterVisitor.__init__(self, process_date_specifier (datetime.now(), filtr), match_due, include)
Example #5
0
def process_date_specifier_to_datestr (now, spec):
        rng = process_date_specifier (now, spec)
        return date_range_to_str (rng)
Example #6
0
def process_date_specifier_to_datestr(now, spec):
    rng = process_date_specifier(now, spec)
    return date_range_to_str(rng)
Example #7
0
def parse_expr(tokens, type_required=BOOL_TYPE, now=now(), level=0):
    LOGGER.debug('parsing %s tokens: %s', level, tokens)
    tok, tokens = next_token(tokens, [TEXT, QUOTED_TEXT, NOT, OPEN_BRACE])
    (t, v) = tok

    # NOT
    if t == NOT:
        assert type_required == BOOL_TYPE, "expecting a ' + required_type' expression, not " + BOOL_TYPE
        expr, tokens, expr_type, expr_string = parse_expr(tokens,
                                                          now=now,
                                                          level=level + 1)
        assert expr_type == BOOL_TYPE, "not must have a boolean argument"
        LOGGER.debug('built %s:1 %s %s', level, expr_type, expr_string)
        return (lambda x: not expr(x)
                ), tokens, BOOL_TYPE, 'not(' + expr_string + ')'

    # LHS
    if t == TEXT and v == 'true':
        lhs = lambda x: True
        lhs_string = 'true'
        lhs_type = BOOL_TYPE
    elif t == TEXT and v == 'false':
        lhs = lambda x: False
        lhs_string = 'true'
        lhs_type = BOOL_TYPE
    elif t == TEXT and v in ALIAS_LOOKUPS:
        field = ALIAS_LOOKUPS[v]
        lhs = lambda x: access_field(x, field)
        lhs_string = 'field:' + field
        if field in DATE_ALIAS_LOOKUPS:
            lhs_type = DATE_TYPE
        elif field in STRING_ALIAS_LOOKUPS:
            lhs_type = STRING_TYPE
        else:
            lhs_type = BOOL_TYPE
    elif t == OPEN_BRACE:
        lhs, tokens, lhs_type, lhs_string = parse_expr(tokens,
                                                       now=now,
                                                       level=level + 1)
        tokens = next_token(tokens, [CLOSE_BRACE])[1]
    elif t == QUOTED_TEXT:
        text = v
        lhs = lambda x: unicode(text)
        lhs_string = '"' + v + '"'
        lhs_type = STRING_TYPE
        if type_required == DATE_TYPE:
            rng = process_date_specifier(now, text)
            lhs = lambda x: rng
            lhs_string = '[' + date_range_to_str(rng) + ']'
            lhs_type = DATE_TYPE
    elif t == TEXT:
        text = v
        lhs = lambda x: unicode(text)
        lhs_string = text
        lhs_type = STRING_TYPE
        if type_required == DATE_TYPE:
            rng = process_date_specifier(now, text)
            lhs = lambda x: rng
            lhs_string = '[' + date_range_to_str(rng) + ']'
            lhs_type = DATE_TYPE
    else:
        assert False, 'unexpected token: ' + v

    LOGGER.debug('built %s:2 %s %s', level, lhs_type, lhs_string)

    # OPERATOR
    if len(tokens) == 0:
        LOGGER.debug('built %s:3 %s %s', level, lhs_type, lhs_string)
        assert type_required == lhs_type, "expecting a " + type_required + ' got a ' + lhs_type + ': ' + lhs_string
        return lhs, tokens, lhs_type, lhs_string

    tok, tokens = next_token(tokens, [AND, OR, EQUAL, NOT_EQUAL, CLOSE_BRACE])
    op, v = tok
    if op == CLOSE_BRACE:
        LOGGER.debug('built %s:4 %s %s', level, lhs_type, lhs_string)
        assert type_required == lhs_type, "expecting a " + type_required + ' got a ' + lhs_type + ': ' + lhs_string
        return lhs, [tok] + tokens, lhs_type, lhs_string

    rhs, tokens, rhs_type, rhs_string = parse_expr(tokens,
                                                   type_required=lhs_type,
                                                   now=now,
                                                   level=level + 1)
    assert lhs_type == rhs_type, "incompatible types, " + lhs_type + ' ' + rhs_type

    assert type_required == BOOL_TYPE, "expecting a " + type_required + ' but got ' + BOOL_TYPE

    if op == AND:
        expr_string = '(' + lhs_string + ')AND(' + rhs_string + ')'
        LOGGER.debug('built %s:5 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: and_fn(lhs, rhs, x)), tokens, BOOL_TYPE, expr_string
    elif op == OR:
        expr_string = '(' + lhs_string + ')OR(' + rhs_string + ')'
        LOGGER.debug('built %s:6 %s %s', level, BOOL_TYPE, expr_string)
        return (lambda x: or_fn(lhs, rhs, x)), tokens, BOOL_TYPE, expr_string
    elif op == EQUAL:
        expr_string = '(' + lhs_string + ')=(' + rhs_string + ')'
        LOGGER.debug('built %s:7 %s %s', level, BOOL_TYPE, expr_string)
        return (
            lambda x: eq_fn(lhs(x), rhs(x))), tokens, BOOL_TYPE, expr_string
    elif op == NOT_EQUAL:
        expr_string = '(' + lhs_string + ')!=(' + rhs_string + ')'
        LOGGER.debug('built %s:8 %s %s', level, BOOL_TYPE, expr_string)
        return (
            lambda x: ne_fn(lhs(x), rhs(x))), tokens, BOOL_TYPE, expr_string
Example #8
0
 def __init__(self, filtr, include=True):
     TaskFilterVisitor.__init__(
         self, process_date_specifier(datetime.now(), filtr), match_start,
         include)
Example #9
0
 def __init__(self, filtr, include=True):
     ProjectFilterVisitor.__init__(
         self, process_date_specifier(datetime.now(), filtr), match_due,
         include)