<s> """<STR_LIT>""" <EOL> from gcloud . exceptions import NotFound <EOL> from gcloud . pubsub . _helpers import topic_name_from_path <EOL> from gcloud . pubsub . iam import Policy <EOL> from gcloud . pubsub . message import Message <EOL> class Subscription ( object ) : <EOL> """<STR_LIT>""" <EOL> _DELETED_TOPIC_PATH = '<STR_LIT>' <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , topic = None , ack_deadline = None , push_endpoint = None , <EOL> client = None ) : <EOL> if client is None and topic is None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> if client is not None and topic is not None : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . name = name <EOL> self . topic = topic <EOL> self . _client = client or topic . _client <EOL> self . _project = self . _client . project <EOL> self . ack_deadline = ack_deadline <EOL> self . push_endpoint = push_endpoint <EOL> @ classmethod <EOL> def from_api_repr ( cls , resource , client , topics = None ) : <EOL> """<STR_LIT>""" <EOL> if topics is None : <EOL> topics = { } <EOL> topic_path = resource [ '<STR_LIT>' ] <EOL> if topic_path == cls . _DELETED_TOPIC_PATH : <EOL> topic = None <EOL> else : <EOL> topic = topics . get ( topic_path ) <EOL> if topic is None : <EOL> topic_name = topic_name_from_path ( topic_path , client . project ) <EOL> topic = topics [ topic_path ] = client . topic ( topic_name ) <EOL> _ , _ , _ , name = resource [ '<STR_LIT:name>' ] . split ( '<STR_LIT:/>' ) <EOL> ack_deadline = resource . get ( '<STR_LIT>' ) <EOL> push_config = resource . get ( '<STR_LIT>' , { } ) <EOL> push_endpoint = push_config . get ( '<STR_LIT>' ) <EOL> if topic is None : <EOL> return cls ( name , ack_deadline = ack_deadline , <EOL> push_endpoint = push_endpoint , client = client ) <EOL> return cls ( name , topic , ack_deadline , push_endpoint ) <EOL> @ property <EOL> def project ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . project <EOL> @ property <EOL> def full_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . project , self . name ) <EOL> @ property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . full_name , ) <EOL> def _require_client ( self , client ) : <EOL> """<STR_LIT>""" <EOL> if client is None : <EOL> client = self . _client <EOL> return client <EOL> def create ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> api . subscription_create ( <EOL> self . full_name , self . topic . full_name , self . ack_deadline , <EOL> self . push_endpoint ) <EOL> def exists ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> try : <EOL> api . subscription_get ( self . full_name ) <EOL> except NotFound : <EOL> return False <EOL> else : <EOL> return True <EOL> def reload ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> data = api . subscription_get ( self . full_name ) <EOL> self . ack_deadline = data . get ( '<STR_LIT>' ) <EOL> push_config = data . get ( '<STR_LIT>' , { } ) <EOL> self . push_endpoint = push_config . get ( '<STR_LIT>' ) <EOL> def delete ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> api . subscription_delete ( self . full_name ) <EOL> def modify_push_configuration ( self , push_endpoint , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> api . subscription_modify_push_config ( self . full_name , push_endpoint ) <EOL> self . push_endpoint = push_endpoint <EOL> def pull ( self , return_immediately = False , max_messages = <NUM_LIT:1> , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> response = api . subscription_pull ( <EOL> self . full_name , return_immediately , max_messages ) <EOL> return [ ( info [ '<STR_LIT>' ] , Message . from_api_repr ( info [ '<STR_LIT:message>' ] ) ) <EOL> for info in response ] <EOL> def acknowledge ( self , ack_ids , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> api . subscription_acknowledge ( self . full_name , ack_ids ) <EOL> def modify_ack_deadline ( self , ack_ids , ack_deadline , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . subscriber_api <EOL> api . subscription_modify_ack_deadline ( <EOL> self . full_name , ack_ids , ack_deadline ) <EOL> def get_iam_policy ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . iam_policy_api <EOL> resp = api . get_iam_policy ( self . full_name ) <EOL> return Policy . from_api_repr ( resp ) <EOL> def set_iam_policy ( self , policy , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . iam_policy_api <EOL> resource = policy . to_api_repr ( ) <EOL> resp = api . set_iam_policy ( self . full_name , resource ) <EOL> return Policy . from_api_repr ( resp ) <EOL> def check_iam_permissions ( self , permissions , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> api = client . iam_policy_api <EOL> return api . test_iam_permissions ( <EOL> self . full_name , list ( permissions ) ) </s>
