<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> import base64 <EOL> import sys <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api import datastore_types <EOL> from google . appengine . api . prospective_search import error_pb <EOL> from google . appengine . api . prospective_search import prospective_search_admin <EOL> from google . appengine . api . prospective_search import prospective_search_pb <EOL> from google . appengine . runtime import apiproxy_errors <EOL> from google . appengine . datastore import entity_pb <EOL> DEFAULT_RESULT_BATCH_SIZE = <NUM_LIT:100> <EOL> DEFAULT_LEASE_DURATION_SEC = <NUM_LIT:0> <EOL> DEFAULT_LIST_SUBSCRIPTIONS_MAX_RESULTS = <NUM_LIT:1000> <EOL> DEFAULT_LIST_TOPICS_MAX_RESULTS = <NUM_LIT:1000> <EOL> _doc_class = prospective_search_pb . MatchRequest <EOL> _schema_type = prospective_search_pb . SchemaEntry <EOL> _entity_meaning = entity_pb . Property <EOL> class SubscriptionState : <EOL> OK = <NUM_LIT:0> <EOL> PENDING = <NUM_LIT:1> <EOL> ERROR = <NUM_LIT:2> <EOL> _State_NAMES = { <EOL> <NUM_LIT:0> : "<STR_LIT:OK>" , <EOL> <NUM_LIT:1> : "<STR_LIT>" , <EOL> <NUM_LIT:2> : "<STR_LIT>" , <EOL> } <EOL> def subscription_state_name ( x ) : <EOL> return SubscriptionState . _State_NAMES . get ( x , "<STR_LIT>" ) <EOL> def _GetSchemaEntryForPropertyType ( property_type ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> _MODEL_TYPE_TO_SCHEMA_ENTRY = { <EOL> db . StringProperty : ( _schema_type . STRING , None ) , <EOL> db . IntegerProperty : ( _schema_type . INT32 , None ) , <EOL> db . BooleanProperty : ( _schema_type . BOOLEAN , None ) , <EOL> db . FloatProperty : ( _schema_type . DOUBLE , None ) , <EOL> db . TextProperty : ( _schema_type . STRING , None ) <EOL> } <EOL> return _MODEL_TYPE_TO_SCHEMA_ENTRY . get ( property_type , ( None , None ) ) <EOL> def _GetModelTypeForListPropertyType ( property_type ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> _LISTPROPERTY_TYPE_TO_SCHEMA_ENTRY = { <EOL> basestring : db . StringProperty , <EOL> str : db . StringProperty , <EOL> unicode : db . StringProperty , <EOL> bool : db . BooleanProperty , <EOL> int : db . IntegerProperty , <EOL> float : db . FloatProperty , <EOL> db . Text : db . TextProperty , <EOL> } <EOL> return _LISTPROPERTY_TYPE_TO_SCHEMA_ENTRY . get ( property_type , None ) <EOL> def _GetModelTypeForEntityType ( python_type ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> _PYTHON_TYPE_TO_MODEL_TYPE = { <EOL> basestring : db . StringProperty , <EOL> str : db . StringProperty , <EOL> unicode : db . StringProperty , <EOL> int : db . IntegerProperty , <EOL> bool : db . BooleanProperty , <EOL> float : db . FloatProperty , <EOL> db . Text : db . TextProperty , <EOL> } <EOL> return _PYTHON_TYPE_TO_MODEL_TYPE . get ( python_type , None ) <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class TopicNotSpecified ( Error ) : <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class SubscriptionDoesNotExist ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , topic , sub_id ) : <EOL> Exception . __init__ ( self ) <EOL> self . topic = topic <EOL> self . sub_id = sub_id <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . sub_id , <EOL> self . topic ) <EOL> class DocumentTypeError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> class SchemaError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , detail ) : <EOL> Exception . __init__ ( self ) <EOL> self . detail = detail <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % self . detail <EOL> class QuerySyntaxError ( Error ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , topic , sub_id , query , detail ) : <EOL> Exception . __init__ ( self ) <EOL> self . topic = topic <EOL> self . sub_id = sub_id <EOL> self . query = query <EOL> self . detail = detail <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> self . topic , self . sub_id , self . query , self . detail ) <EOL> def _get_document_topic ( document , topic ) : <EOL> if topic : <EOL> return topic <EOL> return document . kind ( ) <EOL> def _make_sync_call ( service , call , request , response ) : <EOL> """<STR_LIT>""" <EOL> resp = apiproxy_stub_map . MakeSyncCall ( service , call , request , response ) <EOL> if resp is not None : <EOL> return resp <EOL> return response <EOL> def _add_schema_entry ( model_type , name , add_entry ) : <EOL> """<STR_LIT>""" <EOL> schema_type , entity_meaning = _GetSchemaEntryForPropertyType ( model_type ) <EOL> if not schema_type : <EOL> return <EOL> entry = add_entry ( ) <EOL> entry . set_name ( name ) <EOL> entry . set_type ( schema_type ) <EOL> if entity_meaning : <EOL> entry . set_meaning ( entity_meaning ) <EOL> def _entity_schema_to_prospective_search_schema ( schema , add_entry ) : <EOL> """<STR_LIT>""" <EOL> all_names = [ ] <EOL> for python_type , names in schema . items ( ) : <EOL> all_names . extend ( names ) <EOL> for name in names : <EOL> model_type = _GetModelTypeForEntityType ( python_type ) <EOL> if not model_type : <EOL> continue <EOL> _add_schema_entry ( model_type , name , add_entry ) <EOL> if len ( all_names ) != len ( set ( all_names ) ) : <EOL> duplicate_names = all_names <EOL> for name in set ( all_names ) : <EOL> duplicate_names . remove ( name ) <EOL> raise SchemaError ( '<STR_LIT>' % duplicate_names ) <EOL> def _model_to_prospective_search_schema ( model , add_entry ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> for name , model_property in model . properties ( ) . iteritems ( ) : <EOL> model_class = model_property . __class__ <EOL> if issubclass ( model_class , db . ListProperty ) : <EOL> model_class = _GetModelTypeForListPropertyType ( model_property . item_type ) <EOL> _add_schema_entry ( model_class , name , add_entry ) <EOL> def subscribe ( document_class , <EOL> query , <EOL> sub_id , <EOL> schema = None , <EOL> topic = None , <EOL> lease_duration_sec = DEFAULT_LEASE_DURATION_SEC ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> request = prospective_search_pb . SubscribeRequest ( ) <EOL> request . set_sub_id ( sub_id ) <EOL> request . set_vanilla_query ( unicode ( query ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> request . set_lease_duration_sec ( lease_duration_sec ) <EOL> if issubclass ( document_class , db . Model ) : <EOL> topic = _get_document_topic ( document_class , topic ) <EOL> _model_to_prospective_search_schema ( document_class , <EOL> request . add_schema_entry ) <EOL> elif issubclass ( document_class , datastore . Entity ) : <EOL> if not topic : <EOL> raise TopicNotSpecified ( ) <EOL> _entity_schema_to_prospective_search_schema ( schema , <EOL> request . add_schema_entry ) <EOL> else : <EOL> raise DocumentTypeError ( ) <EOL> request . set_topic ( topic ) <EOL> response = prospective_search_pb . SubscribeResponse ( ) <EOL> try : <EOL> _make_sync_call ( '<STR_LIT>' , '<STR_LIT>' , request , response ) <EOL> except apiproxy_errors . ApplicationError , e : <EOL> if e . application_error is error_pb . Error . BAD_REQUEST : <EOL> raise QuerySyntaxError ( sub_id , topic , query , e . error_detail ) <EOL> raise e <EOL> def unsubscribe ( document_class , sub_id , topic = None ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> request = prospective_search_pb . UnsubscribeRequest ( ) <EOL> if issubclass ( document_class , db . Model ) : <EOL> topic = _get_document_topic ( document_class , topic ) <EOL> elif issubclass ( document_class , datastore . Entity ) : <EOL> if not topic : <EOL> raise TopicNotSpecified ( ) <EOL> else : <EOL> raise DocumentTypeError ( ) <EOL> request . set_topic ( topic ) <EOL> request . set_sub_id ( sub_id ) <EOL> response = prospective_search_pb . UnsubscribeResponse ( ) <EOL> _make_sync_call ( '<STR_LIT>' , '<STR_LIT>' , request , response ) <EOL> def get_subscription ( document_class , sub_id , topic = None ) : <EOL> """<STR_LIT>""" <EOL> subscriptions = list_subscriptions ( document_class , sub_id , topic = topic , <EOL> max_results = <NUM_LIT:1> ) <EOL> if len ( subscriptions ) and subscriptions [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == sub_id : <EOL> return subscriptions [ <NUM_LIT:0> ] <EOL> raise SubscriptionDoesNotExist ( topic , sub_id ) <EOL> def list_subscriptions ( document_class , <EOL> sub_id_start = '<STR_LIT>' , <EOL> topic = None , <EOL> max_results = DEFAULT_LIST_SUBSCRIPTIONS_MAX_RESULTS , <EOL> expires_before = None ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> if issubclass ( document_class , db . Model ) : <EOL> topic = _get_document_topic ( document_class , topic ) <EOL> elif issubclass ( document_class , datastore . Entity ) : <EOL> if not topic : <EOL> raise TopicNotSpecified ( ) <EOL> else : <EOL> raise DocumentTypeError ( ) <EOL> return prospective_search_admin . list_subscriptions ( topic , max_results , None , <EOL> sub_id_start , <EOL> expires_before ) <EOL> def list_topics ( max_results = DEFAULT_LIST_TOPICS_MAX_RESULTS , <EOL> topic_start = None ) : <EOL> """<STR_LIT>""" <EOL> return prospective_search_admin . list_topics ( max_results , <EOL> topic_start = topic_start ) <EOL> def match ( document , <EOL> topic = None , <EOL> result_key = None , <EOL> result_relative_url = '<STR_LIT>' , <EOL> result_task_queue = '<STR_LIT:default>' , <EOL> result_batch_size = DEFAULT_RESULT_BATCH_SIZE , <EOL> result_return_document = True ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> request = prospective_search_pb . MatchRequest ( ) <EOL> if isinstance ( document , db . Model ) : <EOL> topic = _get_document_topic ( document , topic ) <EOL> doc_pb = db . model_to_protobuf ( document ) <EOL> if result_return_document : <EOL> request . set_result_python_document_class ( _doc_class . MODEL ) <EOL> elif isinstance ( document , datastore . Entity ) : <EOL> topic = _get_document_topic ( document , topic ) <EOL> doc_pb = document . ToPb ( ) <EOL> if result_return_document : <EOL> request . set_result_python_document_class ( _doc_class . ENTITY ) <EOL> else : <EOL> raise DocumentTypeError ( ) <EOL> request . set_topic ( topic ) <EOL> request . mutable_document ( ) . CopyFrom ( doc_pb ) <EOL> if result_key : <EOL> request . set_result_key ( result_key ) <EOL> request . set_result_relative_url ( result_relative_url ) <EOL> request . set_result_task_queue ( result_task_queue ) <EOL> request . set_result_batch_size ( result_batch_size ) <EOL> response = prospective_search_pb . MatchResponse ( ) <EOL> _make_sync_call ( '<STR_LIT>' , '<STR_LIT>' , request , response ) <EOL> def get_document ( request ) : <EOL> """<STR_LIT>""" <EOL> from google . appengine . ext import db <EOL> doc_class = request . get ( '<STR_LIT>' ) <EOL> if not doc_class : <EOL> return None <EOL> entity = entity_pb . EntityProto ( ) <EOL> entity . ParseFromString ( base64 . urlsafe_b64decode ( <EOL> request . get ( '<STR_LIT>' ) . encode ( '<STR_LIT:utf-8>' ) ) ) <EOL> doc_class = int ( doc_class ) <EOL> if doc_class is _doc_class . ENTITY : <EOL> return datastore . Entity ( '<STR_LIT>' ) . FromPb ( entity ) <EOL> elif doc_class is _doc_class . MODEL : <EOL> return db . model_from_protobuf ( entity ) <EOL> else : <EOL> raise DocumentTypeError ( ) </s>
