<s> """<STR_LIT>""" <EOL> import calendar <EOL> import datetime <EOL> import itertools <EOL> import logging <EOL> import re <EOL> import time <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_errors <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . api import users <EOL> MultiQuery = datastore . MultiQuery <EOL> LOG_LEVEL = logging . DEBUG - <NUM_LIT:1> <EOL> _EPOCH = datetime . datetime . utcfromtimestamp ( <NUM_LIT:0> ) <EOL> _EMPTY_LIST_PROPERTY_NAME = '<STR_LIT>' <EOL> def Execute ( query_string , * args , ** keyword_args ) : <EOL> """<STR_LIT>""" <EOL> app = keyword_args . pop ( '<STR_LIT>' , None ) <EOL> proto_query = GQL ( query_string , _app = app ) <EOL> return proto_query . Bind ( args , keyword_args ) . Run ( ) <EOL> class GQL ( object ) : <EOL> """<STR_LIT>""" <EOL> TOKENIZE_REGEX = re . compile ( r"""<STR_LIT>""" , re . VERBOSE | re . IGNORECASE ) <EOL> RESERVED_KEYWORDS = ( frozenset ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> frozenset ( ) ) <EOL> MAX_ALLOWABLE_QUERIES = datastore . MAX_ALLOWABLE_QUERIES <EOL> __ANCESTOR = - <NUM_LIT:1> <EOL> _kind = None <EOL> _keys_only = False <EOL> __projection = None <EOL> __distinct = False <EOL> __has_ancestor = False <EOL> __offset = - <NUM_LIT:1> <EOL> __limit = - <NUM_LIT:1> <EOL> __hint = '<STR_LIT>' <EOL> def __init__ ( self , query_string , _app = None , _auth_domain = None , <EOL> namespace = None ) : <EOL> """<STR_LIT>""" <EOL> self . __app = _app <EOL> self . __namespace = namespace <EOL> self . __auth_domain = _auth_domain <EOL> self . __symbols = self . TOKENIZE_REGEX . findall ( query_string ) <EOL> initial_error = None <EOL> for backwards_compatibility_mode in xrange ( len ( self . RESERVED_KEYWORDS ) ) : <EOL> self . __InitializeParseState ( ) <EOL> self . __active_reserved_words = self . __GenerateReservedWords ( <EOL> backwards_compatibility_mode ) <EOL> try : <EOL> self . __Select ( ) <EOL> except datastore_errors . BadQueryError , error : <EOL> logging . log ( LOG_LEVEL , initial_error ) <EOL> if not initial_error : <EOL> initial_error = error <EOL> else : <EOL> break <EOL> else : <EOL> raise initial_error <EOL> def __InitializeParseState ( self ) : <EOL> self . _kind = None <EOL> self . _keys_only = False <EOL> self . __projection = None <EOL> self . __distinct = False <EOL> self . __has_ancestor = False <EOL> self . __offset = - <NUM_LIT:1> <EOL> self . __limit = - <NUM_LIT:1> <EOL> self . __hint = '<STR_LIT>' <EOL> self . __filters = { } <EOL> self . __orderings = [ ] <EOL> self . __next_symbol = <NUM_LIT:0> <EOL> def Bind ( self , args , keyword_args , cursor = None , end_cursor = None ) : <EOL> """<STR_LIT>""" <EOL> num_args = len ( args ) <EOL> input_args = frozenset ( xrange ( num_args ) ) <EOL> used_args = set ( ) <EOL> queries = [ ] <EOL> enumerated_queries = self . EnumerateQueries ( used_args , args , keyword_args ) <EOL> if enumerated_queries : <EOL> query_count = len ( enumerated_queries ) <EOL> else : <EOL> query_count = <NUM_LIT:1> <EOL> for _ in xrange ( query_count ) : <EOL> queries . append ( datastore . Query ( self . _kind , <EOL> _app = self . __app , <EOL> keys_only = self . _keys_only , <EOL> projection = self . __projection , <EOL> distinct = self . __distinct , <EOL> namespace = self . __namespace , <EOL> cursor = cursor , <EOL> end_cursor = end_cursor ) ) <EOL> logging . log ( LOG_LEVEL , <EOL> '<STR_LIT>' , <EOL> len ( args ) , args , len ( keyword_args ) , keyword_args ) <EOL> for ( identifier , condition ) , value_list in self . __filters . iteritems ( ) : <EOL> for operator , params in value_list : <EOL> value = self . __Operate ( args , keyword_args , used_args , operator , params ) <EOL> if not self . __IsMultiQuery ( condition ) : <EOL> for query in queries : <EOL> self . __AddFilterToQuery ( identifier , condition , value , query ) <EOL> unused_args = input_args - used_args <EOL> if unused_args : <EOL> unused_values = [ unused_arg + <NUM_LIT:1> for unused_arg in unused_args ] <EOL> raise datastore_errors . BadArgumentError ( '<STR_LIT>' % <EOL> unused_values ) <EOL> if enumerated_queries : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , enumerated_queries ) <EOL> for ( query , enumerated_query ) in zip ( queries , enumerated_queries ) : <EOL> query . update ( enumerated_query ) <EOL> if self . __orderings : <EOL> for query in queries : <EOL> query . Order ( * tuple ( self . __orderings ) ) <EOL> if query_count > <NUM_LIT:1> : <EOL> return MultiQuery ( queries , self . __orderings ) <EOL> else : <EOL> return queries [ <NUM_LIT:0> ] <EOL> def EnumerateQueries ( self , used_args , args , keyword_args ) : <EOL> """<STR_LIT>""" <EOL> enumerated_queries = [ ] <EOL> for ( identifier , condition ) , value_list in self . __filters . iteritems ( ) : <EOL> for operator , params in value_list : <EOL> value = self . __Operate ( args , keyword_args , used_args , operator , params ) <EOL> self . __AddMultiQuery ( identifier , condition , value , enumerated_queries ) <EOL> return enumerated_queries <EOL> def __CastError ( self , operator , values , error_message ) : <EOL> """<STR_LIT>""" <EOL> raise datastore_errors . BadQueryError ( <EOL> '<STR_LIT>' % <EOL> ( values , operator . upper ( ) , error_message ) ) <EOL> def __CastNop ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) != <NUM_LIT:1> : <EOL> self . __CastError ( values , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> return values [ <NUM_LIT:0> ] <EOL> def __CastList ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if values : <EOL> return values <EOL> else : <EOL> return None <EOL> def __CastKey ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if not len ( values ) % <NUM_LIT:2> : <EOL> return datastore_types . Key . from_path ( _app = self . __app , <EOL> namespace = self . __namespace , <EOL> * values ) <EOL> elif len ( values ) == <NUM_LIT:1> and isinstance ( values [ <NUM_LIT:0> ] , basestring ) : <EOL> return datastore_types . Key ( values [ <NUM_LIT:0> ] ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def __CastGeoPt ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) != <NUM_LIT:2> : <EOL> self . __CastError ( '<STR_LIT>' , values , '<STR_LIT>' ) <EOL> return datastore_types . GeoPt ( * values ) <EOL> def __CastUser ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) != <NUM_LIT:1> : <EOL> self . __CastError ( '<STR_LIT:user>' , values , '<STR_LIT>' ) <EOL> elif values [ <NUM_LIT:0> ] is None : <EOL> self . __CastError ( '<STR_LIT:user>' , values , '<STR_LIT>' ) <EOL> else : <EOL> return users . User ( email = values [ <NUM_LIT:0> ] , _auth_domain = self . __auth_domain ) <EOL> def __EncodeIfNeeded ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT:utf8>' ) <EOL> else : <EOL> return value <EOL> def __CastDate ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> value = self . __EncodeIfNeeded ( values [ <NUM_LIT:0> ] ) <EOL> if isinstance ( value , str ) : <EOL> try : <EOL> time_tuple = time . strptime ( value , '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:6> ] <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , '<STR_LIT>' ) <EOL> elif len ( values ) == <NUM_LIT:3> : <EOL> time_tuple = ( values [ <NUM_LIT:0> ] , values [ <NUM_LIT:1> ] , values [ <NUM_LIT:2> ] , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> return datetime . datetime ( * time_tuple ) <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> def __CastTime ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> value = self . __EncodeIfNeeded ( values [ <NUM_LIT:0> ] ) <EOL> if isinstance ( value , str ) : <EOL> try : <EOL> time_tuple = time . strptime ( value , '<STR_LIT>' ) <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> time_tuple = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) + time_tuple [ <NUM_LIT:3> : ] <EOL> time_tuple = time_tuple [ <NUM_LIT:0> : <NUM_LIT:6> ] <EOL> elif isinstance ( value , int ) : <EOL> time_tuple = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> , value ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , <EOL> '<STR_LIT>' ) <EOL> elif len ( values ) <= <NUM_LIT:4> : <EOL> time_tuple = ( <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:1> ) + tuple ( values ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , <EOL> '<STR_LIT>' ) <EOL> try : <EOL> return datetime . datetime ( * time_tuple ) <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> def __CastDatetime ( self , values ) : <EOL> """<STR_LIT>""" <EOL> if len ( values ) == <NUM_LIT:1> : <EOL> value = self . __EncodeIfNeeded ( values [ <NUM_LIT:0> ] ) <EOL> if isinstance ( value , str ) : <EOL> try : <EOL> time_tuple = time . strptime ( str ( value ) , '<STR_LIT>' ) [ <NUM_LIT:0> : <NUM_LIT:6> ] <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> else : <EOL> self . __CastError ( '<STR_LIT>' , values , '<STR_LIT>' ) <EOL> else : <EOL> time_tuple = values <EOL> try : <EOL> return datetime . datetime ( * time_tuple ) <EOL> except ValueError , err : <EOL> self . __CastError ( '<STR_LIT>' , values , err ) <EOL> def __Operate ( self , args , keyword_args , used_args , operator , params ) : <EOL> """<STR_LIT>""" <EOL> if not params : <EOL> return None <EOL> param_values = [ ] <EOL> for param in params : <EOL> if isinstance ( param , Literal ) : <EOL> value = param . Get ( ) <EOL> else : <EOL> value = self . __GetParam ( param , args , keyword_args , used_args = used_args ) <EOL> if isinstance ( param , int ) : <EOL> used_args . add ( param - <NUM_LIT:1> ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> param , value ) <EOL> param_values . append ( value ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> operator , repr ( param_values ) ) <EOL> if operator in self . __cast_operators : <EOL> result = self . __cast_operators [ operator ] ( self , param_values ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' % operator ) <EOL> return result <EOL> def __IsMultiQuery ( self , condition ) : <EOL> """<STR_LIT>""" <EOL> return condition . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __GetParam ( self , param , args , keyword_args , used_args = None ) : <EOL> """<STR_LIT>""" <EOL> num_args = len ( args ) <EOL> if isinstance ( param , int ) : <EOL> if param <= num_args : <EOL> return args [ param - <NUM_LIT:1> ] <EOL> else : <EOL> raise datastore_errors . BadArgumentError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' % ( param , num_args ) ) <EOL> elif isinstance ( param , basestring ) : <EOL> if param in keyword_args : <EOL> return keyword_args [ param ] <EOL> else : <EOL> raise datastore_errors . BadArgumentError ( <EOL> '<STR_LIT>' % <EOL> param ) <EOL> elif isinstance ( param , tuple ) and len ( param ) == <NUM_LIT:2> : <EOL> cast_op , params = param <EOL> return self . __Operate ( args , keyword_args , used_args , cast_op , params ) <EOL> else : <EOL> assert False , '<STR_LIT>' % param <EOL> def __AddMultiQuery ( self , identifier , condition , value , enumerated_queries ) : <EOL> """<STR_LIT>""" <EOL> if condition . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) and self . _keys_only : <EOL> raise datastore_errors . BadQueryError ( <EOL> '<STR_LIT>' ) <EOL> def CloneQueries ( queries , n ) : <EOL> """<STR_LIT>""" <EOL> if not enumerated_queries : <EOL> for _ in xrange ( n ) : <EOL> queries . append ( { } ) <EOL> return <NUM_LIT:1> <EOL> else : <EOL> old_size = len ( queries ) <EOL> tmp_queries = [ ] <EOL> for _ in xrange ( n - <NUM_LIT:1> ) : <EOL> [ tmp_queries . append ( filter_map . copy ( ) ) for filter_map in queries ] <EOL> queries . extend ( tmp_queries ) <EOL> queries . sort ( ) <EOL> return old_size <EOL> if condition == '<STR_LIT>' : <EOL> if len ( enumerated_queries ) * <NUM_LIT:2> > self . MAX_ALLOWABLE_QUERIES : <EOL> raise datastore_errors . BadArgumentError ( <EOL> '<STR_LIT>' ) <EOL> num_iterations = CloneQueries ( enumerated_queries , <NUM_LIT:2> ) <EOL> for i in xrange ( num_iterations ) : <EOL> enumerated_queries [ <NUM_LIT:2> * i ] [ '<STR_LIT>' % identifier ] = value <EOL> enumerated_queries [ <NUM_LIT:2> * i + <NUM_LIT:1> ] [ '<STR_LIT>' % identifier ] = value <EOL> elif condition . lower ( ) == '<STR_LIT>' : <EOL> if not isinstance ( value , list ) : <EOL> raise datastore_errors . BadArgumentError ( '<STR_LIT>' ) <EOL> in_list_size = len ( value ) <EOL> if len ( enumerated_queries ) * in_list_size > self . MAX_ALLOWABLE_QUERIES : <EOL> raise datastore_errors . BadArgumentError ( <EOL> '<STR_LIT>' ) <EOL> if in_list_size == <NUM_LIT:0> : <EOL> num_iterations = CloneQueries ( enumerated_queries , <NUM_LIT:1> ) <EOL> for clone_num in xrange ( num_iterations ) : <EOL> enumerated_queries [ clone_num ] [ _EMPTY_LIST_PROPERTY_NAME ] = True <EOL> return <EOL> num_iterations = CloneQueries ( enumerated_queries , in_list_size ) <EOL> for clone_num in xrange ( num_iterations ) : <EOL> for value_num in xrange ( len ( value ) ) : <EOL> list_val = value [ value_num ] <EOL> query_num = in_list_size * clone_num + value_num <EOL> filt = '<STR_LIT>' % identifier <EOL> enumerated_queries [ query_num ] [ filt ] = list_val <EOL> def __AddFilterToQuery ( self , identifier , condition , value , query ) : <EOL> """<STR_LIT>""" <EOL> if identifier != self . __ANCESTOR : <EOL> filter_condition = '<STR_LIT>' % ( identifier , condition ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> filter_condition , value . __class__ ) <EOL> datastore . _AddOrAppend ( query , filter_condition , value ) <EOL> else : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , value ) <EOL> query . Ancestor ( value ) <EOL> def Run ( self , * args , ** keyword_args ) : <EOL> """<STR_LIT>""" <EOL> bind_results = self . Bind ( args , keyword_args ) <EOL> offset = self . offset ( ) <EOL> if self . __limit == - <NUM_LIT:1> : <EOL> it = bind_results . Run ( ) <EOL> try : <EOL> for _ in xrange ( offset ) : <EOL> it . next ( ) <EOL> except StopIteration : <EOL> pass <EOL> return it <EOL> else : <EOL> res = bind_results . Get ( self . __limit , offset ) <EOL> return res <EOL> def filters ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __filters <EOL> def hint ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __hint <EOL> def limit ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __limit <EOL> def offset ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __offset == - <NUM_LIT:1> : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return self . __offset <EOL> def orderings ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __orderings <EOL> def is_keys_only ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _keys_only <EOL> def projection ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __projection <EOL> def is_distinct ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __distinct <EOL> def kind ( self ) : <EOL> return self . _kind <EOL> @ property <EOL> def _entity ( self ) : <EOL> logging . error ( '<STR_LIT>' ) <EOL> return self . _kind <EOL> __iter__ = Run <EOL> __result_type_regex = re . compile ( r'<STR_LIT>' ) <EOL> __quoted_string_regex = re . compile ( r'<STR_LIT>' ) <EOL> __ordinal_regex = re . compile ( r'<STR_LIT>' ) <EOL> __named_regex = re . compile ( r'<STR_LIT>' ) <EOL> __identifier_regex = re . compile ( r'<STR_LIT>' ) <EOL> __quoted_identifier_regex = re . compile ( r'<STR_LIT>' ) <EOL> __conditions_regex = re . compile ( r'<STR_LIT>' , re . IGNORECASE ) <EOL> __number_regex = re . compile ( r'<STR_LIT>' ) <EOL> __cast_regex = re . compile ( <EOL> r'<STR_LIT>' , re . IGNORECASE ) <EOL> __cast_operators = { <EOL> '<STR_LIT>' : __CastGeoPt , <EOL> '<STR_LIT:user>' : __CastUser , <EOL> '<STR_LIT:key>' : __CastKey , <EOL> '<STR_LIT>' : __CastDatetime , <EOL> '<STR_LIT:date>' : __CastDate , <EOL> '<STR_LIT:time>' : __CastTime , <EOL> '<STR_LIT:list>' : __CastList , <EOL> '<STR_LIT>' : __CastNop , <EOL> } <EOL> def __Error ( self , error_message ) : <EOL> """<STR_LIT>""" <EOL> if self . __next_symbol >= len ( self . __symbols ) : <EOL> raise datastore_errors . BadQueryError ( <EOL> '<STR_LIT>' % error_message ) <EOL> else : <EOL> raise datastore_errors . BadQueryError ( <EOL> '<STR_LIT>' % <EOL> ( error_message , self . __symbols [ self . __next_symbol ] ) ) <EOL> def __Accept ( self , symbol_string ) : <EOL> """<STR_LIT>""" <EOL> if self . __next_symbol < len ( self . __symbols ) : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , self . __symbols ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> symbol_string , self . __symbols [ self . __next_symbol ] . upper ( ) ) <EOL> if self . __symbols [ self . __next_symbol ] . upper ( ) == symbol_string : <EOL> self . __next_symbol += <NUM_LIT:1> <EOL> return True <EOL> return False <EOL> def __Expect ( self , symbol_string ) : <EOL> """<STR_LIT>""" <EOL> if not self . __Accept ( symbol_string ) : <EOL> self . __Error ( '<STR_LIT>' % symbol_string ) <EOL> def __AcceptRegex ( self , regex ) : <EOL> """<STR_LIT>""" <EOL> if self . __next_symbol < len ( self . __symbols ) : <EOL> match_symbol = self . __symbols [ self . __next_symbol ] <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , regex , match_symbol ) <EOL> match = regex . match ( match_symbol ) <EOL> if match : <EOL> self . __next_symbol += <NUM_LIT:1> <EOL> if match . groups ( ) : <EOL> matched_string = match . group ( <NUM_LIT:1> ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , matched_string ) <EOL> return matched_string <EOL> return None <EOL> def __AcceptTerminal ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __Accept ( '<STR_LIT:;>' ) <EOL> if self . __next_symbol < len ( self . __symbols ) : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> return True <EOL> def __Select ( self ) : <EOL> """<STR_LIT>""" <EOL> self . __Expect ( '<STR_LIT>' ) <EOL> if ( '<STR_LIT>' in self . __active_reserved_words <EOL> and self . __Accept ( '<STR_LIT>' ) ) : <EOL> self . __distinct = True <EOL> if not self . __Accept ( '<STR_LIT:*>' ) : <EOL> props = [ self . __ExpectIdentifier ( ) ] <EOL> while self . __Accept ( '<STR_LIT:U+002C>' ) : <EOL> props . append ( self . __ExpectIdentifier ( ) ) <EOL> if props == [ '<STR_LIT>' ] : <EOL> self . _keys_only = True <EOL> else : <EOL> self . __projection = tuple ( props ) <EOL> return self . __From ( ) <EOL> def __From ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> self . _kind = self . __ExpectIdentifier ( ) <EOL> return self . __Where ( ) <EOL> def __Where ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> return self . __FilterList ( ) <EOL> return self . __OrderBy ( ) <EOL> def __FilterList ( self ) : <EOL> """<STR_LIT>""" <EOL> identifier = self . __Identifier ( ) <EOL> if not identifier : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> condition = self . __AcceptRegex ( self . __conditions_regex ) <EOL> if not condition : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> self . __CheckFilterSyntax ( identifier , condition ) <EOL> if not self . __AddSimpleFilter ( identifier , condition , self . __Reference ( ) ) : <EOL> if not self . __AddSimpleFilter ( identifier , condition , self . __Literal ( ) ) : <EOL> type_cast = self . __TypeCast ( ) <EOL> if ( not type_cast or <EOL> not self . __AddProcessedParameterFilter ( identifier , condition , <EOL> * type_cast ) ) : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> return self . __FilterList ( ) <EOL> return self . __OrderBy ( ) <EOL> def __GetValueList ( self , values_intended_for_list = False ) : <EOL> """<STR_LIT>""" <EOL> params = [ ] <EOL> while True : <EOL> reference = self . __Reference ( ) <EOL> if reference : <EOL> params . append ( reference ) <EOL> else : <EOL> literal = self . __Literal ( ) <EOL> if literal : <EOL> params . append ( literal ) <EOL> elif values_intended_for_list : <EOL> type_cast = self . __TypeCast ( can_cast_list = False ) <EOL> if type_cast is None : <EOL> self . __Error ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> params . append ( type_cast ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> if not self . __Accept ( '<STR_LIT:U+002C>' ) : <EOL> break <EOL> return params <EOL> def __CheckFilterSyntax ( self , identifier , condition ) : <EOL> """<STR_LIT>""" <EOL> if identifier . lower ( ) == '<STR_LIT>' : <EOL> if condition . lower ( ) == '<STR_LIT>' : <EOL> if self . __has_ancestor : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> elif condition . lower ( ) == '<STR_LIT>' : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> def __AddProcessedParameterFilter ( self , identifier , condition , <EOL> operator , parameters ) : <EOL> """<STR_LIT>""" <EOL> if parameters is None : <EOL> return False <EOL> if parameters [ <NUM_LIT:0> ] is None : <EOL> return False <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> identifier , condition , repr ( parameters ) ) <EOL> filter_rule = ( identifier , condition ) <EOL> if identifier . lower ( ) == '<STR_LIT>' : <EOL> self . __has_ancestor = True <EOL> filter_rule = ( self . __ANCESTOR , '<STR_LIT>' ) <EOL> assert condition . lower ( ) == '<STR_LIT>' <EOL> if operator == '<STR_LIT:list>' and condition . lower ( ) != '<STR_LIT>' : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> self . __filters . setdefault ( filter_rule , [ ] ) . append ( ( operator , parameters ) ) <EOL> return True <EOL> def __AddSimpleFilter ( self , identifier , condition , parameter ) : <EOL> """<STR_LIT>""" <EOL> return self . __AddProcessedParameterFilter ( identifier , condition , <EOL> '<STR_LIT>' , [ parameter ] ) <EOL> def __Identifier ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' ) <EOL> identifier = self . __AcceptRegex ( self . __identifier_regex ) <EOL> if identifier : <EOL> if identifier . upper ( ) in self . __active_reserved_words : <EOL> self . __next_symbol -= <NUM_LIT:1> <EOL> self . __Error ( '<STR_LIT>' ) <EOL> else : <EOL> identifier = self . __AcceptRegex ( self . __quoted_identifier_regex ) <EOL> if identifier : <EOL> identifier = identifier [ <NUM_LIT:1> : - <NUM_LIT:1> ] . replace ( '<STR_LIT>' , '<STR_LIT:">' ) <EOL> return identifier <EOL> def __ExpectIdentifier ( self ) : <EOL> id = self . __Identifier ( ) <EOL> if not id : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> return id <EOL> def __Reference ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' ) <EOL> reference = self . __AcceptRegex ( self . __ordinal_regex ) <EOL> if reference : <EOL> return int ( reference ) <EOL> else : <EOL> reference = self . __AcceptRegex ( self . __named_regex ) <EOL> if reference : <EOL> return reference <EOL> return None <EOL> def __Literal ( self ) : <EOL> """<STR_LIT>""" <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' ) <EOL> literal = None <EOL> try : <EOL> literal = int ( self . __symbols [ self . __next_symbol ] ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . __next_symbol += <NUM_LIT:1> <EOL> if literal is None : <EOL> try : <EOL> literal = float ( self . __symbols [ self . __next_symbol ] ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> self . __next_symbol += <NUM_LIT:1> <EOL> if literal is None : <EOL> literal = self . __AcceptRegex ( self . __quoted_string_regex ) <EOL> if literal : <EOL> literal = literal [ <NUM_LIT:1> : - <NUM_LIT:1> ] . replace ( "<STR_LIT>" , "<STR_LIT:'>" ) <EOL> if literal is None : <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> literal = True <EOL> elif self . __Accept ( '<STR_LIT>' ) : <EOL> literal = False <EOL> if literal is not None : <EOL> return Literal ( literal ) <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> return Literal ( None ) <EOL> else : <EOL> return None <EOL> def __TypeCast ( self , can_cast_list = True ) : <EOL> """<STR_LIT>""" <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' ) <EOL> cast_op = self . __AcceptRegex ( self . __cast_regex ) <EOL> if not cast_op : <EOL> if can_cast_list and self . __Accept ( '<STR_LIT:(>' ) : <EOL> cast_op = '<STR_LIT:list>' <EOL> else : <EOL> return None <EOL> else : <EOL> cast_op = cast_op . lower ( ) <EOL> self . __Expect ( '<STR_LIT:(>' ) <EOL> params = self . __GetValueList ( values_intended_for_list = ( cast_op == '<STR_LIT:list>' ) ) <EOL> self . __Expect ( '<STR_LIT:)>' ) <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , <EOL> cast_op , repr ( params ) ) <EOL> return ( cast_op , params ) <EOL> def __OrderBy ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> self . __Expect ( '<STR_LIT>' ) <EOL> return self . __OrderList ( ) <EOL> return self . __Limit ( ) <EOL> def __OrderList ( self ) : <EOL> """<STR_LIT>""" <EOL> identifier = self . __Identifier ( ) <EOL> if identifier : <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> self . __orderings . append ( ( identifier , datastore . Query . DESCENDING ) ) <EOL> elif self . __Accept ( '<STR_LIT>' ) : <EOL> self . __orderings . append ( ( identifier , datastore . Query . ASCENDING ) ) <EOL> else : <EOL> self . __orderings . append ( ( identifier , datastore . Query . ASCENDING ) ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> logging . log ( LOG_LEVEL , self . __orderings ) <EOL> if self . __Accept ( '<STR_LIT:U+002C>' ) : <EOL> return self . __OrderList ( ) <EOL> return self . __Limit ( ) <EOL> def __Limit ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> maybe_limit = self . __AcceptRegex ( self . __number_regex ) <EOL> if maybe_limit : <EOL> if self . __Accept ( '<STR_LIT:U+002C>' ) : <EOL> self . __offset = int ( maybe_limit ) <EOL> if self . __offset < <NUM_LIT:0> : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> else : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , self . __offset ) <EOL> maybe_limit = self . __AcceptRegex ( self . __number_regex ) <EOL> self . __limit = int ( maybe_limit ) <EOL> if self . __limit < <NUM_LIT:1> : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> else : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , self . __limit ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> return self . __Offset ( ) <EOL> def __Offset ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> if self . __offset != - <NUM_LIT:1> : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> offset = self . __AcceptRegex ( self . __number_regex ) <EOL> if offset : <EOL> self . __offset = int ( offset ) <EOL> if self . __offset < <NUM_LIT:0> : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> else : <EOL> logging . log ( LOG_LEVEL , '<STR_LIT>' , self . __offset ) <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> return self . __Hint ( ) <EOL> def __Hint ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> if self . __Accept ( '<STR_LIT>' ) : <EOL> self . __hint = '<STR_LIT>' <EOL> elif self . __Accept ( '<STR_LIT>' ) : <EOL> self . __hint = '<STR_LIT>' <EOL> elif self . __Accept ( '<STR_LIT>' ) : <EOL> self . __hint = '<STR_LIT>' <EOL> else : <EOL> self . __Error ( '<STR_LIT>' ) <EOL> return self . __AcceptTerminal ( ) <EOL> def __GenerateReservedWords ( self , level ) : <EOL> return frozenset ( itertools . chain ( * self . RESERVED_KEYWORDS [ level : ] ) ) <EOL> class Literal ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value ) : <EOL> self . __value = value <EOL> def Get ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __value <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % repr ( self . __value ) </s>
