<s> import datetime <EOL> import re <EOL> import ply . yacc as yacc <EOL> from sqlalchemy . sql . expression import and_ , or_ , not_ , select , func <EOL> import gertty . db <EOL> import gertty . search <EOL> from gertty . search . tokenizer import tokens <EOL> def age_to_delta ( delta , unit ) : <EOL> if unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:s>' ] : <EOL> pass <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:m>' ] : <EOL> delta = delta * <NUM_LIT> <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:h>' ] : <EOL> delta = delta * <NUM_LIT> * <NUM_LIT> <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:d>' ] : <EOL> delta = delta * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:w>' ] : <EOL> delta = delta * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> delta = delta * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:30> <EOL> elif unit in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:y>' ] : <EOL> delta = delta * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> <EOL> return delta <EOL> def SearchParser ( ) : <EOL> precedence = ( <EOL> ( '<STR_LIT:left>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ) <EOL> def p_terms ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_list_expr ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = and_ ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:2> ] ) <EOL> def p_paren_expr ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:2> ] <EOL> def p_boolean_expr ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . lower ( ) == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = and_ ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ) <EOL> elif p [ <NUM_LIT:2> ] . lower ( ) == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = or_ ( p [ <NUM_LIT:1> ] , p [ <NUM_LIT:3> ] ) <EOL> else : <EOL> raise gertty . search . SearchSyntaxError ( "<STR_LIT>" % p [ <NUM_LIT:2> ] ) <EOL> def p_negative_expr ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = not_ ( p [ <NUM_LIT:2> ] ) <EOL> def p_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_string ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = p [ <NUM_LIT:1> ] <EOL> def p_age_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> now = datetime . datetime . utcnow ( ) <EOL> delta = p [ <NUM_LIT:2> ] <EOL> unit = p [ <NUM_LIT:3> ] <EOL> delta = age_to_delta ( delta , unit ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . updated < ( now - datetime . timedelta ( seconds = delta ) ) <EOL> def p_recentlyseen_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> now = datetime . datetime . utcnow ( ) <EOL> delta = p [ <NUM_LIT:2> ] <EOL> unit = p [ <NUM_LIT:3> ] <EOL> delta = age_to_delta ( delta , unit ) <EOL> s = select ( [ func . datetime ( func . max ( gertty . db . change_table . c . last_seen ) , '<STR_LIT>' % delta ) ] , <EOL> correlate = False ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . last_seen >= s <EOL> def p_change_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if type ( p [ <NUM_LIT:2> ] ) == int : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . number == p [ <NUM_LIT:2> ] <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . change_id == p [ <NUM_LIT:2> ] <EOL> def p_owner_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> username = p . parser . username <EOL> p [ <NUM_LIT:0> ] = gertty . db . account_table . c . username == username <EOL> else : <EOL> p [ <NUM_LIT:0> ] = or_ ( gertty . db . account_table . c . username == p [ <NUM_LIT:2> ] , <EOL> gertty . db . account_table . c . email == p [ <NUM_LIT:2> ] , <EOL> gertty . db . account_table . c . name == p [ <NUM_LIT:2> ] ) <EOL> def p_reviewer_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> filters = [ ] <EOL> filters . append ( gertty . db . approval_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . approval_table . c . account_key == gertty . db . account_table . c . key ) <EOL> try : <EOL> number = int ( p [ <NUM_LIT:2> ] ) <EOL> except : <EOL> number = None <EOL> if number is not None : <EOL> filters . append ( gertty . db . account_table . c . id == number ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> username = p . parser . username <EOL> filters . append ( gertty . db . account_table . c . username == username ) <EOL> else : <EOL> filters . append ( or_ ( gertty . db . account_table . c . username == p [ <NUM_LIT:2> ] , <EOL> gertty . db . account_table . c . email == p [ <NUM_LIT:2> ] , <EOL> gertty . db . account_table . c . name == p [ <NUM_LIT:2> ] ) ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> def p_commit_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> filters = [ ] <EOL> filters . append ( gertty . db . revision_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . revision_table . c . commit == p [ <NUM_LIT:2> ] ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> def p_project_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . startswith ( '<STR_LIT>' ) : <EOL> p [ <NUM_LIT:0> ] = func . matches ( p [ <NUM_LIT:2> ] , gertty . db . project_table . c . name ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . project_table . c . name == p [ <NUM_LIT:2> ] <EOL> def p_project_key_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . project_key == p [ <NUM_LIT:2> ] <EOL> def p_branch_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . startswith ( '<STR_LIT>' ) : <EOL> p [ <NUM_LIT:0> ] = func . matches ( p [ <NUM_LIT:2> ] , gertty . db . change_table . c . branch ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . branch == p [ <NUM_LIT:2> ] <EOL> def p_topic_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . startswith ( '<STR_LIT>' ) : <EOL> p [ <NUM_LIT:0> ] = func . matches ( p [ <NUM_LIT:2> ] , gertty . db . change_table . c . topic ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . topic == p [ <NUM_LIT:2> ] <EOL> def p_ref_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . startswith ( '<STR_LIT>' ) : <EOL> p [ <NUM_LIT:0> ] = func . matches ( p [ <NUM_LIT:2> ] , '<STR_LIT>' + gertty . db . change_table . c . branch ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . branch == p [ <NUM_LIT:2> ] [ len ( '<STR_LIT>' ) : ] <EOL> label_re = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> def p_label_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> args = label_re . match ( p [ <NUM_LIT:2> ] ) <EOL> label = args . group ( '<STR_LIT:label>' ) <EOL> op = args . group ( '<STR_LIT>' ) or '<STR_LIT:=>' <EOL> value = int ( args . group ( '<STR_LIT:value>' ) ) <EOL> user = args . group ( '<STR_LIT:user>' ) <EOL> filters = [ ] <EOL> filters . append ( gertty . db . approval_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . approval_table . c . category == label ) <EOL> if op == '<STR_LIT:=>' : <EOL> filters . append ( gertty . db . approval_table . c . value == value ) <EOL> elif op == '<STR_LIT>' : <EOL> filters . append ( gertty . db . approval_table . c . value >= value ) <EOL> elif op == '<STR_LIT>' : <EOL> filters . append ( gertty . db . approval_table . c . value <= value ) <EOL> if user is not None : <EOL> filters . append ( gertty . db . approval_table . c . account_key == gertty . db . account_table . c . key ) <EOL> if user == '<STR_LIT>' : <EOL> filters . append ( gertty . db . account_table . c . username == p . parser . username ) <EOL> else : <EOL> filters . append ( <EOL> or_ ( gertty . db . account_table . c . username == user , <EOL> gertty . db . account_table . c . email == user , <EOL> gertty . db . account_table . c . name == user ) ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> def p_message_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> filters = [ ] <EOL> filters . append ( gertty . db . revision_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . revision_table . c . message . like ( '<STR_LIT>' % p [ <NUM_LIT:2> ] ) ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> def p_comment_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> filters = [ ] <EOL> filters . append ( gertty . db . revision_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . revision_table . c . message == p [ <NUM_LIT:2> ] ) <EOL> revision_select = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> filters = [ ] <EOL> filters . append ( gertty . db . revision_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . comment_table . c . revision_key == gertty . db . revision_table . c . key ) <EOL> filters . append ( gertty . db . comment_table . c . message == p [ <NUM_LIT:2> ] ) <EOL> comment_select = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = or_ ( gertty . db . change_table . c . key . in_ ( comment_select ) , <EOL> gertty . db . change_table . c . key . in_ ( revision_select ) ) <EOL> def p_has_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> filters = [ ] <EOL> filters . append ( gertty . db . revision_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . message_table . c . revision_key == gertty . db . revision_table . c . key ) <EOL> filters . append ( gertty . db . message_table . c . draft == True ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> else : <EOL> raise gertty . search . SearchSyntaxError ( '<STR_LIT>' % p [ <NUM_LIT:2> ] ) <EOL> def p_is_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> username = p . parser . username <EOL> if p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> filters = [ ] <EOL> filters . append ( gertty . db . approval_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . approval_table . c . value != <NUM_LIT:0> ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status . notin_ ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status . in_ ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status == '<STR_LIT>' <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status == '<STR_LIT>' <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status == '<STR_LIT>' <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . account_table . c . username == username <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . starred == True <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . held == True <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> filters = [ ] <EOL> filters . append ( gertty . db . approval_table . c . change_key == gertty . db . change_table . c . key ) <EOL> filters . append ( gertty . db . approval_table . c . account_key == gertty . db . account_table . c . key ) <EOL> filters . append ( gertty . db . account_table . c . username == username ) <EOL> s = select ( [ gertty . db . change_table . c . key ] , correlate = False ) . where ( and_ ( * filters ) ) <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . key . in_ ( s ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . project_table . c . subscribed == True <EOL> else : <EOL> raise gertty . search . SearchSyntaxError ( '<STR_LIT>' % p [ <NUM_LIT:2> ] ) <EOL> def p_file_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] . startswith ( '<STR_LIT>' ) : <EOL> p [ <NUM_LIT:0> ] = and_ ( or_ ( func . matches ( p [ <NUM_LIT:2> ] , gertty . db . file_table . c . path ) , <EOL> func . matches ( p [ <NUM_LIT:2> ] , gertty . db . file_table . c . old_path ) ) , <EOL> gertty . db . file_table . c . status is not None ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = and_ ( or_ ( gertty . db . file_table . c . path == p [ <NUM_LIT:2> ] , <EOL> gertty . db . file_table . c . old_path == p [ <NUM_LIT:2> ] ) , <EOL> gertty . db . file_table . c . status is not None ) <EOL> def p_status_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> if p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status . notin_ ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> elif p [ <NUM_LIT:2> ] == '<STR_LIT>' : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status . in_ ( [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> else : <EOL> p [ <NUM_LIT:0> ] = gertty . db . change_table . c . status == p [ <NUM_LIT:2> ] . upper ( ) <EOL> def p_limit_term ( p ) : <EOL> '''<STR_LIT>''' <EOL> p [ <NUM_LIT:0> ] = ( True == True ) <EOL> def p_op_term ( p ) : <EOL> '<STR_LIT>' <EOL> raise SyntaxError ( ) <EOL> def p_error ( p ) : <EOL> if p : <EOL> raise gertty . search . SearchSyntaxError ( '<STR_LIT>' % ( <EOL> p . lexer . lexdata [ p . lexpos : ] , p . lexer . lexdata , p . lexpos ) ) <EOL> else : <EOL> raise gertty . search . SearchSyntaxError ( '<STR_LIT>' ) <EOL> return yacc . yacc ( debug = <NUM_LIT:0> , write_tables = <NUM_LIT:0> ) </s>
