o
    )=jÔ?  ã                   @   s–   d Z ddlZddlmZ ddlmZ ddlmZ ddlm	Z	 dZ
dZd	Zd
ZdZdZdZdZdZe ee¡Ze e¡ZdZG dd„ deƒZdd„ ZdS )zÇConfigure bucket notification resources to interact with Google Cloud Pub/Sub.

See [Cloud Pub/Sub Notifications for Google Cloud Storage](https://cloud.google.com/storage/docs/pubsub-notifications)
é    N)ÚNotFound)Úcreate_trace_span)Ú_DEFAULT_TIMEOUT)ÚDEFAULT_RETRYZOBJECT_FINALIZEZOBJECT_METADATA_UPDATEZOBJECT_DELETEZOBJECT_ARCHIVEZJSON_API_V1ÚNONEz-//pubsub.googleapis.com/projects/{}/topics/{}z)(?P<project>[a-z][a-z0-9-]{4,28}[a-z0-9])z (?P<name>[A-Za-z](\w|[-_.~+%])+)zpResource has invalid topic: {}; see https://cloud.google.com/storage/docs/json_api/v1/notifications/insert#topicc                   @   s  e Zd ZdZdddddedf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e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edfd#d$„Zdeefd%d&„Zdeefd'd(„Zdeefd)d*„ZdS )+ÚBucketNotificationaÏ  Represent a single notification resource for a bucket.

    See: https://cloud.google.com/storage/docs/json_api/v1/notifications

    :type bucket: :class:`google.cloud.storage.bucket.Bucket`
    :param bucket: Bucket to which the notification is bound.

    :type topic_name: str
    :param topic_name:
        (Optional) Topic name to which notifications are published.

    :type topic_project: str
    :param topic_project:
        (Optional) Project ID of topic to which notifications are published.
        If not passed, uses the project ID of the bucket's client.

    :type custom_attributes: dict
    :param custom_attributes:
        (Optional) Additional attributes passed with notification events.

    :type event_types: list(str)
    :param event_types:
        (Optional) Event types for which notification events are published.

    :type blob_name_prefix: str
    :param blob_name_prefix:
        (Optional) Prefix of blob names for which notification events are
        published.

    :type payload_format: str
    :param payload_format:
        (Optional) Format of payload for notification events.

    :type notification_id: str
    :param notification_id:
        (Optional) The ID of the notification.
    Nc	           	      C   sŽ   || _ || _|d u r|jj}|d u rtdƒ‚|| _i | _|d ur%|| jd< |d ur.|| jd< |d ur7|| jd< |d ur@|| jd< || jd< d S )Nz8Client project not set:  pass an explicit topic_project.Úcustom_attributesÚevent_typesÚobject_name_prefixÚidÚpayload_format)Ú_bucketÚ_topic_nameÚclientÚprojectÚ
ValueErrorÚ_topic_projectÚ_properties)	ÚselfÚbucketÚ
topic_nameÚtopic_projectr   r	   Úblob_name_prefixr   Únotification_id© r   úkC:\Users\psant\Downloads\Anu Raj Backend\school\venv\Lib\site-packages\google/cloud/storage/notification.pyÚ__init__X   s"   



zBucketNotification.__init__c                 C   s>   |  d¡}|du rtdƒ‚t|ƒ\}}| |||d�}||_|S )aÎ  Construct an instance from the JSON repr returned by the server.

        See: https://cloud.google.com/storage/docs/json_api/v1/notifications

        :type resource: dict
        :param resource: JSON repr of the notification

        :type bucket: :class:`google.cloud.storage.bucket.Bucket`
        :param bucket: Bucket to which the notification is bound.

        :rtype: :class:`BucketNotification`
        :returns: the new notification instance
        ÚtopicNzResource has no topic)r   )Úgetr   Ú_parse_topic_pathr   )ÚclsÚresourcer   Ú
topic_pathÚnamer   Úinstancer   r   r   Úfrom_api_repr~   s   
z BucketNotification.from_api_reprc                 C   ó   | j S )z*Bucket to which the notification is bound.N)r   ©r   r   r   r   r   —   ó   zBucketNotification.bucketc                 C   r&   )z0Topic name to which notifications are published.N)r   r'   r   r   r   r   œ   r(   zBucketNotification.topic_namec                 C   r&   )z9Project ID of topic to which notifications are published.N)r   r'   r   r   r   r   ¡   r(   z BucketNotification.topic_projectc                 C   ó   | j  d¡S )z2Custom attributes passed with notification events.r   N©r   r   r'   r   r   r   r   ¦   ó   z$BucketNotification.custom_attributesc                 C   r)   )z8Event types for which notification events are published.r	   Nr*   r'   r   r   r   r	   «   r+   zBucketNotification.event_typesc                 C   r)   )zAPrefix of blob names for which notification events are published.r
   Nr*   r'   r   r   r   r   °   r+   z#BucketNotification.blob_name_prefixc                 C   r)   )z)Format of payload of notification events.r   Nr*   r'   r   r   r   r   µ   r+   z!BucketNotification.payload_formatc                 C   r)   )z'Server-set ID of notification resource.r   Nr*   r'   r   r   r   r   º   r+   z"BucketNotification.notification_idc                 C   r)   )ú)Server-set ETag of notification resource.ÚetagNr*   r'   r   r   r   r-   ¿   r+   zBucketNotification.etagc                 C   r)   )r,   ZselfLinkNr*   r'   r   r   r   Ú	self_linkÄ   r+   zBucketNotification.self_linkc                 C   s   | j jS )z%The client bound to this notfication.N)r   r   r'   r   r   r   r   É   s   zBucketNotification.clientc                 C   s   d| j j› d| j› �S )z#The URL path for this notification.ú/b/z/notificationConfigs/N)r   r#   r   r'   r   r   r   ÚpathÎ   s   zBucketNotification.pathc                 C   s   |du r| j }|S )a:  Check client or verify over-ride.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: the client to use.

        :rtype: :class:`google.cloud.storage.client.Client`
        :returns: The client passed in or the bucket's client.
        N)r   )r   r   r   r   r   Ú_require_clientÓ   s   
z"BucketNotification._require_clientc                 C   s   | j  ¡  | j  |¡ dS )zwHelper for :meth:`reload`.

        :type response: dict
        :param response: resource mapping from server
        N)r   ÚclearÚupdate)r   Úresponser   r   r   Ú_set_propertiesá   s   
z"BucketNotification._set_propertiesc                 C   sÔ   t dd��[ | jdurtd| j› d�ƒ‚|  |¡}i }| jjdur'| jj|d< d| jj› d�}| j ¡ }| j	du rCt
 | jd	¡|d
< n
t
 | j| j	¡|d
< |j|||||d�| _W d  ƒ dS 1 scw   Y  dS )a¢  API wrapper: create the notification.

        See:
        https://cloud.google.com/storage/docs/json_api/v1/notifications/insert

        If :attr:`user_project` is set on the bucket, bills the API request
        to that project.

        :type client: :class:`~google.cloud.storage.client.Client`
        :param client: (Optional) The client to use.  If not passed, falls back
                       to the ``client`` stored on the notification's bucket.
        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :raises ValueError: if the notification already exists.
        z!Storage.BucketNotification.create©r#   Nznotification_id already set to z(; must be None to create a Notification.ÚuserProjectr/   z/notificationConfigsÚ r   ©Úquery_paramsÚtimeoutÚretry)r   r   r   r1   r   Úuser_projectr#   r   Úcopyr   Ú_TOPIC_REF_FMTÚformatr   Z_post_resource)r   r   r;   r<   r:   r0   Z
propertiesr   r   r   Úcreateê   s0   
ÿ


ÿ
û"êzBucketNotification.createc              	   C   s¤   t dd��C | jdu rtdƒ‚|  |¡}i }| jjdur"| jj|d< z|j| j|||d� W n ty>   Y W d  ƒ dS w 	 W d  ƒ dS 1 sKw   Y  dS )	a  Test whether this notification exists.

        See:
        https://cloud.google.com/storage/docs/json_api/v1/notifications/get

        If :attr:`user_project` is set on the bucket, bills the API request
        to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: (Optional) The client to use.  If not passed, falls back
                       to the ``client`` stored on the current bucket.
        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :rtype: bool
        :returns: True, if the notification exists, else False.
        :raises ValueError: if the notification has no ID.
        z!Storage.BucketNotification.existsr6   Nú8Notification ID not set: set an explicit notification_idr7   r9   FT)	r   r   r   r1   r   r=   Ú_get_resourcer0   r   ©r   r   r;   r<   r:   r   r   r   Úexists  s,   
ÿ

üì$êzBucketNotification.existsc                 C   s†   t dd��4 | jdu rtdƒ‚|  |¡}i }| jjdur"| jj|d< |j| j|||d�}|  |¡ W d  ƒ dS 1 s<w   Y  dS )aÌ  Update this notification from the server configuration.

        See:
        https://cloud.google.com/storage/docs/json_api/v1/notifications/get

        If :attr:`user_project` is set on the bucket, bills the API request
        to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: (Optional) The client to use.  If not passed, falls back
                       to the ``client`` stored on the current bucket.
        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`


        :raises ValueError: if the notification has no ID.
        z!Storage.BucketNotification.reloadr6   NrB   r7   r9   )	r   r   r   r1   r   r=   rC   r0   r5   )r   r   r;   r<   r:   r4   r   r   r   ÚreloadQ  s"   
ÿ
ü"îzBucketNotification.reloadc                 C   s|   t dd��/ | jdu rtdƒ‚|  |¡}i }| jjdur"| jj|d< |j| j|||d� W d  ƒ dS 1 s7w   Y  dS )a  Delete this notification.

        See:
        https://cloud.google.com/storage/docs/json_api/v1/notifications/delete

        If :attr:`user_project` is set on the bucket, bills the API request
        to that project.

        :type client: :class:`~google.cloud.storage.client.Client` or
                      ``NoneType``
        :param client: (Optional) The client to use.  If not passed, falls back
                       to the ``client`` stored on the current bucket.
        :type timeout: float or tuple
        :param timeout:
            (Optional) The amount of time, in seconds, to wait
            for the server response.  See: :ref:`configuring_timeouts`

        :type retry: google.api_core.retry.Retry or google.cloud.storage.retry.ConditionalRetryPolicy
        :param retry:
            (Optional) How to retry the RPC. See: :ref:`configuring_retries`

        :raises: :class:`google.api_core.exceptions.NotFound`:
            if the notification does not exist.
        :raises ValueError: if the notification has no ID.
        z!Storage.BucketNotification.deleter6   NrB   r7   r9   )r   r   r   r1   r   r=   Z_delete_resourcer0   rD   r   r   r   Údelete~  s    
ÿ
ü"ôzBucketNotification.delete)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚNONE_PAYLOAD_FORMATr   Úclassmethodr%   Úpropertyr   r   r   r   r	   r   r   r   r-   r.   r   r0   r1   r5   r   rA   r   rE   rF   rG   r   r   r   r   r   1   sT    )
÷&












	52-r   c                 C   s4   t  | ¡}|du rtt | ¡ƒ‚| d¡| d¡fS )aë  Verify that a topic path is in the correct format.

    Expected to be of the form:

        //pubsub.googleapis.com/projects/{project}/topics/{topic}

    where the ``project`` value must be "6 to 30 lowercase letters, digits,
    or hyphens. It must start with a letter. Trailing hyphens are prohibited."
    (see [`resource manager docs`](https://cloud.google.com/resource-manager/reference/rest/v1beta1/projects#Project.FIELDS.project_id))
    and ``topic`` must have length at least two,
    must start with a letter and may only contain alphanumeric characters or
    ``-``, ``_``, ``.``, ``~``, ``+`` or ``%`` (i.e characters used for URL
    encoding, see [`topic spec`](https://cloud.google.com/storage/docs/json_api/v1/notifications/insert#topic)).

    Args:
        topic_path (str): The topic path to be verified.

    Returns:
        Tuple[str, str]: The ``project`` and ``topic`` parsed from the
        ``topic_path``.

    Raises:
        ValueError: If the topic path is invalid.
    Nr#   r   )Ú_TOPIC_REF_REÚmatchr   Ú
_BAD_TOPICr@   Úgroup)r"   rP   r   r   r   r   ¬  s   
r   )rK   ÚreZgoogle.api_core.exceptionsr   Z+google.cloud.storage._opentelemetry_tracingr   Zgoogle.cloud.storage.constantsr   Zgoogle.cloud.storage.retryr   ZOBJECT_FINALIZE_EVENT_TYPEZ!OBJECT_METADATA_UPDATE_EVENT_TYPEZOBJECT_DELETE_EVENT_TYPEZOBJECT_ARCHIVE_EVENT_TYPEZJSON_API_V1_PAYLOAD_FORMATrL   r?   Z_PROJECT_PATTERNZ_TOPIC_NAME_PATTERNr@   Z_TOPIC_REF_PATTERNÚcompilerO   rQ   Úobjectr   r   r   r   r   r   Ú<module>   s.   
ÿ  }