a
    !fF                     @   s  d Z ddlmZ ddlmZ ddlmZ ddlmZ	 ddlm
Z ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ e	jZdZdZejZdZdZej Z!dZ"dZ#ej$Z%eZ&eZ'dZ(dZ)dZ*dZ+d,edj-Z.G dd deeZ/G dd de0Z1dd Z2dS )a  Parent client for calling the Google Cloud Bigtable API.

This is the base from which all interactions with the API occur.

In the hierarchy of API concepts

* a :class:`Client` owns an :class:`.Instance`
* a :class:`.Instance` owns a :class:`Table <gcloud.bigtable.table.Table>`
* a :class:`Table <gcloud.bigtable.table.Table>` owns a
  :class:`ColumnFamily <.column_family.ColumnFamily>`
* a :class:`Table <gcloud.bigtable.table.Table>` owns a :class:`Row <.row.Row>`
  (and all the cells in the row)
    )get_distribution)implementations)bigtable_instance_admin_pb2)bigtable_table_admin_pb2)bigtable_pb2)operations_grpc_pb2)DEFAULT_SERVE_NODESInstance)_EXISTING_INSTANCE_LOCATION_ID)_ClientFactoryMixin)_ClientProjectMixin)get_credentialszbigtableadmin.googleapis.comi  zbigtable.googleapis.comz.https://www.googleapis.com/auth/bigtable.adminz-https://www.googleapis.com/auth/bigtable.dataz6https://www.googleapis.com/auth/bigtable.data.readonly
   zgcloud-python/{0}Zgcloudc                   @   s   e Zd ZdZddddeefddZdd Zedd	 Z	ed
d Z
edd Zedd Zedd Zedd Zdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zedefd&d'Zd(d) ZdS )*Clienta  Client for interacting with Google Cloud Bigtable API.

    .. note::

        Since the Cloud Bigtable API requires the gRPC transport, no
        ``http`` argument is accepted by this class.

    :type project: :class:`str` or :func:`unicode <unicode>`
    :param project: (Optional) The ID of the project which owns the
                    instances, tables and data. If not provided, will
                    attempt to determine from the environment.

    :type credentials:
        :class:`OAuth2Credentials <oauth2client.client.OAuth2Credentials>` or
        :data:`NoneType <types.NoneType>`
    :param credentials: (Optional) The OAuth2 Credentials to use for this
                        client. If not provided, defaults to the Google
                        Application Default Credentials.

    :type read_only: bool
    :param read_only: (Optional) Boolean indicating if the data scope should be
                      for reading only (or for writing as well). Defaults to
                      :data:`False`.

    :type admin: bool
    :param admin: (Optional) Boolean indicating if the client will be used to
                  interact with the Instance Admin or Table Admin APIs. This
                  requires the :const:`ADMIN_SCOPE`. Defaults to :data:`False`.

    :type user_agent: str
    :param user_agent: (Optional) The user agent to be used with API request.
                       Defaults to :const:`DEFAULT_USER_AGENT`.

    :type timeout_seconds: int
    :param timeout_seconds: Number of seconds for request time-out. If not
                            passed, defaults to
                            :const:`DEFAULT_TIMEOUT_SECONDS`.

    :raises: :class:`ValueError <exceptions.ValueError>` if both ``read_only``
             and ``admin`` are :data:`True`
    NFc                 C   s   t j| |d |d u rt }|r,|r,tdg }|r@|t n
|t |rX|t t|| _	z|
|}W n ty   Y n0 || _|| _|| _d | _d | _d | _d | _d S )Nprojectz=A read-only client cannot also performadministrative actions.)r   __init__r   
ValueErrorappendREAD_ONLY_SCOPE
DATA_SCOPEADMIN_SCOPEbool_admincreate_scopedAttributeError_credentials
user_agenttimeout_seconds_data_stub_internal_instance_stub_internal_operations_stub_internal_table_stub_internal)selfr   credentialsZ	read_onlyZadminr   r   scopes r'   W/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigtable/client.pyr      s.    


zClient.__init__c                 C   s4   | j }||j}| | j|t|jv | j| j| jS )a  Make a copy of this client.

        Copies the local data stored as simple types but does not copy the
        current state of any open connections with the Cloud Bigtable API.

        :rtype: :class:`.Client`
        :returns: A copy of the current client.
        )	r   r   r&   	__class__r   r   r   r   r   )r$   r%   Zcopied_credsr'   r'   r(   copy   s    	zClient.copyc                 C   s   | j S )zGetter for client's credentials.

        :rtype:
            :class:`OAuth2Credentials <oauth2client.client.OAuth2Credentials>`
        :returns: The credentials stored on the client.
        )r   r$   r'   r'   r(   r%      s    zClient.credentialsc                 C   s
   d| j  S )a  Project name to be used with Instance Admin API.

        .. note::

            This property will not change if ``project`` does not, but the
            return value is not cached.

        The project name is of the form

            ``"projects/{project}"``

        :rtype: str
        :returns: The project name to be used with the Cloud Bigtable Admin
                  API RPC service.
        z	projects/r   r+   r'   r'   r(   project_name   s    zClient.project_namec                 C   s   | j du rtd| j S )a  Getter for the gRPC stub used for the Data API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        :raises: :class:`ValueError <exceptions.ValueError>` if the current
                 client has not been :meth:`start`-ed.
        NClient has not been started.)r    r   r+   r'   r'   r(   
_data_stub   s    	
zClient._data_stubc                 C   s&   | j std| jdu r td| jS )aX  Getter for the gRPC stub used for the Instance Admin API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        :raises: :class:`ValueError <exceptions.ValueError>` if the current
                 client is not an admin client or if it has not been
                 :meth:`start`-ed.
        Client is not an admin client.Nr-   )r   r   r!   r+   r'   r'   r(   _instance_stub   s
    

zClient._instance_stubc                 C   s&   | j std| jdu r td| jS )aT  Getter for the gRPC stub used for the Operations API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        :raises: :class:`ValueError <exceptions.ValueError>` if the current
                 client is not an admin client or if it has not been
                 :meth:`start`-ed.
        r/   Nr-   )r   r   r"   r+   r'   r'   r(   _operations_stub   s
    

zClient._operations_stubc                 C   s&   | j std| jdu r td| jS )aU  Getter for the gRPC stub used for the Table Admin API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        :raises: :class:`ValueError <exceptions.ValueError>` if the current
                 client is not an admin client or if it has not been
                 :meth:`start`-ed.
        r/   Nr-   )r   r   r#   r+   r'   r'   r(   _table_stub	  s
    

zClient._table_stubc                 C   s   t | tttS )zCreates gRPC stub to make requests to the Data API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        )
_make_stubDATA_STUB_FACTORY_V2DATA_API_HOST_V2DATA_API_PORT_V2r+   r'   r'   r(   _make_data_stub  s    zClient._make_data_stubc                 C   s   t | tttS )zCreates gRPC stub to make requests to the Instance Admin API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        )r3   INSTANCE_STUB_FACTORY_V2INSTANCE_ADMIN_HOST_V2INSTANCE_ADMIN_PORT_V2r+   r'   r'   r(   _make_instance_stub"  s    zClient._make_instance_stubc                 C   s   t | tttS )a  Creates gRPC stub to make requests to the Operations API.

        These are for long-running operations of the Instance Admin API,
        hence the host and port matching.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        )r3   OPERATIONS_STUB_FACTORY_V2OPERATIONS_API_HOST_V2OPERATIONS_API_PORT_V2r+   r'   r'   r(   _make_operations_stub+  s    	zClient._make_operations_stubc                 C   s   t | tttS )zCreates gRPC stub to make requests to the Table Admin API.

        :rtype: :class:`grpc.beta._stub._AutoIntermediary`
        :returns: A gRPC stub object.
        )r3   TABLE_STUB_FACTORY_V2TABLE_ADMIN_HOST_V2TABLE_ADMIN_PORT_V2r+   r'   r'   r(   _make_table_stub7  s    zClient._make_table_stubc                 C   s
   | j duS )zCheck if the client has been started.

        :rtype: bool
        :returns: Boolean indicating if the client has been started.
        N)r    r+   r'   r'   r(   
is_started@  s    zClient.is_startedc                 C   sf   |   rdS |  | _| j  | jrb|  | _|  | _| 	 | _
| j  | j  | j
  dS )zPrepare the client to make requests.

        Activates gRPC contexts for making requests to the Bigtable
        Service(s).
        N)rD   r7   r    	__enter__r   r;   r!   r?   r"   rC   r#   r+   r'   r'   r(   startH  s    






zClient.startc                 C   s   |    | S )z'Starts the client as a context manager.)rF   r+   r'   r'   r(   rE   a  s    zClient.__enter__c                 C   sn   |   sdS | jddd | jrR| jddd | jddd | jddd d| _d| _d| _d| _dS )z!Closes all the open gRPC clients.N)rD   r    __exit__r   r!   r"   r#   r+   r'   r'   r(   stopf  s    zClient.stopc                 C   s   |    dS )z&Stops the client as a context manager.N)rH   )r$   exc_typeexc_valZexc_tr'   r'   r(   rG   x  s    zClient.__exit__c                 C   s   t || |||dS )a  Factory to create a instance associated with this client.

        :type instance_id: str
        :param instance_id: The ID of the instance.

        :type location: string
        :param location: location name, in form
                         ``projects/<project>/locations/<location>``; used to
                         set up the instance's cluster.

        :type display_name: str
        :param display_name: (Optional) The display name for the instance in
                             the Cloud Console UI. (Must be between 4 and 30
                             characters.) If this value is not set in the
                             constructor, will fall back to the instance ID.

        :type serve_nodes: int
        :param serve_nodes: (Optional) The number of nodes in the instance's
                            cluster; used to set up the instance's cluster.

        :rtype: :class:`.Instance`
        :returns: an instance owned by this client.
        )display_nameserve_nodesr	   )r$   Zinstance_idlocationrK   rL   r'   r'   r(   instance|  s    zClient.instancec                    s<   t j jd} j| j} fdd|jD }||jfS )a  List instances owned by the project.

        :rtype: tuple
        :returns: A pair of results, the first is a list of
                  :class:`.Instance` objects returned and the second is a
                  list of strings (the failed locations in the request).
        )parentc                    s   g | ]}t | qS r'   )r
   Zfrom_pb).0Zinstance_pbr+   r'   r(   
<listcomp>  s   z)Client.list_instances.<locals>.<listcomp>)instance_admin_v2_pb2ZListInstancesRequestr,   r0   ZListInstancesr   	instancesZfailed_locations)r$   Z
request_pbresponserS   r'   r+   r(   list_instances  s    
zClient.list_instances)__name__
__module____qualname____doc__DEFAULT_USER_AGENTDEFAULT_TIMEOUT_SECONDSr   r*   propertyr%   r,   r.   r0   r1   r2   r7   r;   r?   rC   rD   rF   rE   rH   rG   r   r   rN   rU   r'   r'   r'   r(   r   ]   s>   *
#
	




			
r   c                   @   s    e Zd ZdZdd Zdd ZdS )_MetadataPluginzCallable class to transform metadata for gRPC requests.

    :type client: :class:`.client.Client`
    :param client: The client that owns the instance.
                   Provides authorization and user agent.
    c                 C   s   |j | _|j| _d S )N)r%   r   r   _user_agent)r$   clientr'   r'   r(   r     s    z_MetadataPlugin.__init__c                 C   s0   | j  j}dd| fd| jfg}||d dS )z.Adds authorization header to request metadata.AuthorizationzBearer z
User-agentN)r   Zget_access_tokenaccess_tokenr^   )r$   Zunused_contextcallbackra   headersr'   r'   r(   __call__  s
    
z_MetadataPlugin.__call__N)rV   rW   rX   rY   r   rd   r'   r'   r'   r(   r]     s   r]   c           	      C   sF   t ddd}t| }t j|dd}t ||}t |||}||S )a  Makes a stub for an RPC service.

    Uses / depends on the beta implementation of gRPC.

    :type client: :class:`.client.Client`
    :param client: The client that owns the instance.
                   Provides authorization and user agent.

    :type stub_factory: callable
    :param stub_factory: A factory which will create a gRPC stub for
                         a given service.

    :type host: str
    :param host: The host for the service.

    :type port: int
    :param port: The port for the service.

    :rtype: :class:`grpc.beta._stub._AutoIntermediary`
    :returns: The stub object used to make gRPC requests to a given API.
    NZgoogle_creds)name)r   Zssl_channel_credentialsr]   Zmetadata_call_credentialsZcomposite_channel_credentialsZsecure_channel)	r_   Zstub_factoryhostportZtransport_credsZcustom_metadata_pluginZ
auth_credsZchannel_credsZchannelr'   r'   r(   r3     s    r3   N)3rY   pkg_resourcesr   Z	grpc.betar   Zgcloud.bigtable._generatedr   rR   r   Ztable_admin_v2_pb2r   Zdata_v2_pb2r   Zoperations_grpc_v2_pb2Zgcloud.bigtable.clusterr   Zgcloud.bigtable.instancer
   r   Zgcloud.clientr   r   Zgcloud.credentialsr   Z#beta_create_BigtableTableAdmin_stubr@   rA   rB   Z&beta_create_BigtableInstanceAdmin_stubr8   r9   r:   Zbeta_create_Bigtable_stubr4   r5   r6   Zbeta_create_Operations_stubr<   r=   r>   r   r   r   r[   formatversionrZ   r   objectr]   r3   r'   r'   r'   r(   <module>   sL     P