a
    !f,                     @   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d Zd	d
 ZG dd deZG dd deZG dd deZG dd deZG dd deZG dd deZdd ZdS )z@User friendly container for Google Cloud Bigtable Column Family.    N)duration_pb2)_total_seconds)	table_pb2)bigtable_table_admin_pb2c                 C   s@   t | }t|}|dk r$| jd }n| j}d| }tj||dS )a  Convert a Python timedelta object to a duration protobuf.

    .. note::

        The Python timedelta has a granularity of microseconds while
        the protobuf duration type has a duration of nanoseconds.

    :type timedelta_val: :class:`datetime.timedelta`
    :param timedelta_val: A timedelta object.

    :rtype: :class:`google.protobuf.duration_pb2.Duration`
    :returns: A duration object equivalent to the time delta.
    r   i@B i  )secondsnanos)r   intmicrosecondsr   ZDuration)Ztimedelta_valZseconds_decimalr   Zsigned_microsr    r
   ^/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigtable/column_family.py_timedelta_to_duration_pb   s    r   c                 C   s   t j| j| jd dS )a  Convert a duration protobuf to a Python timedelta object.

    .. note::

        The Python timedelta has a granularity of microseconds while
        the protobuf duration type has a duration of nanoseconds.

    :type duration_pb: :class:`google.protobuf.duration_pb2.Duration`
    :param duration_pb: A protobuf duration object.

    :rtype: :class:`datetime.timedelta`
    :returns: The converted timedelta object.
    g     @@)r   r	   )datetime	timedeltar   r   )Zduration_pbr
   r
   r   _duration_pb_to_timedelta7   s    r   c                   @   s   e Zd ZdZdd ZdS )GarbageCollectionRulea  Garbage collection rule for column families within a table.

    Cells in the column family (within a table) fitting the rule will be
    deleted during garbage collection.

    .. note::

        This class is a do-nothing base class for all GC rules.

    .. note::

        A string ``gc_expression`` can also be used with API requests, but
        that value would be superceded by a ``gc_rule``. As a result, we
        don't support that feature and instead support via native classes.
    c                 C   s   |  | S N__eq__selfotherr
   r
   r   __ne__\   s    zGarbageCollectionRule.__ne__N)__name__
__module____qualname____doc__r   r
   r
   r
   r   r   K   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MaxVersionsGCRulezGarbage collection limiting the number of versions of a cell.

    :type max_num_versions: int
    :param max_num_versions: The maximum number of versions
    c                 C   s
   || _ d S r   max_num_versions)r   r   r
   r
   r   __init__g   s    zMaxVersionsGCRule.__init__c                 C   s   t || jsdS |j| jkS NF)
isinstance	__class__r   r   r
   r
   r   r   j   s    zMaxVersionsGCRule.__eq__c                 C   s   t j| jdS )Converts the garbage collection rule to a protobuf.

        :rtype: :class:`.table_v2_pb2.GcRule`
        :returns: The converted current object.
        r   )table_v2_pb2GcRuler   r   r
   r
   r   to_pbo   s    zMaxVersionsGCRule.to_pbNr   r   r   r   r   r   r'   r
   r
   r
   r   r   `   s   r   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	MaxAgeGCRulezGarbage collection limiting the age of a cell.

    :type max_age: :class:`datetime.timedelta`
    :param max_age: The maximum age allowed for a cell in the table.
    c                 C   s
   || _ d S r   max_ager   r+   r
   r
   r   r      s    zMaxAgeGCRule.__init__c                 C   s   t || jsdS |j| jkS r    )r!   r"   r+   r   r
   r
   r   r      s    zMaxAgeGCRule.__eq__c                 C   s   t | j}tj|dS )r#   r*   )r   r+   r$   r%   r,   r
   r
   r   r'      s    
zMaxAgeGCRule.to_pbNr(   r
   r
   r
   r   r)   x   s   r)   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	GCRuleUnionzxUnion of garbage collection rules.

    :type rules: list
    :param rules: List of :class:`GarbageCollectionRule`.
    c                 C   s
   || _ d S r   rulesr   r/   r
   r
   r   r      s    zGCRuleUnion.__init__c                 C   s   t || jsdS |j| jkS r    r!   r"   r/   r   r
   r
   r   r      s    zGCRuleUnion.__eq__c                 C   s&   t jjdd | jD d}t j|dS )zConverts the union into a single GC rule as a protobuf.

        :rtype: :class:`.table_v2_pb2.GcRule`
        :returns: The converted current object.
        c                 S   s   g | ]}|  qS r
   r'   .0ruler
   r
   r   
<listcomp>       z%GCRuleUnion.to_pb.<locals>.<listcomp>r.   )union)r$   r%   Unionr/   )r   r8   r
   r
   r   r'      s    zGCRuleUnion.to_pbNr(   r
   r
   r
   r   r-      s   r-   c                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )	GCRuleIntersectionzIntersection of garbage collection rules.

    :type rules: list
    :param rules: List of :class:`GarbageCollectionRule`.
    c                 C   s
   || _ d S r   r.   r0   r
   r
   r   r      s    zGCRuleIntersection.__init__c                 C   s   t || jsdS |j| jkS r    r1   r   r
   r
   r   r      s    zGCRuleIntersection.__eq__c                 C   s&   t jjdd | jD d}t j|dS )zConverts the intersection into a single GC rule as a protobuf.

        :rtype: :class:`.table_v2_pb2.GcRule`
        :returns: The converted current object.
        c                 S   s   g | ]}|  qS r
   r2   r3   r
   r
   r   r6      r7   z,GCRuleIntersection.to_pb.<locals>.<listcomp>r.   )intersection)r$   r%   ZIntersectionr/   )r   r;   r
   r
   r   r'      s    zGCRuleIntersection.to_pbNr(   r
   r
   r
   r   r:      s   r:   c                   @   sV   e Zd ZdZd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S )ColumnFamilya  Representation of a Google Cloud Bigtable Column Family.

    We can use a :class:`ColumnFamily` to:

    * :meth:`create` itself
    * :meth:`update` itself
    * :meth:`delete` itself

    :type column_family_id: str
    :param column_family_id: The ID of the column family. Must be of the
                             form ``[_a-zA-Z0-9][-_.a-zA-Z0-9]*``.

    :type table: :class:`Table <gcloud.bigtable.table.Table>`
    :param table: The table that owns the column family.

    :type gc_rule: :class:`GarbageCollectionRule`
    :param gc_rule: (Optional) The garbage collection settings for this
                    column family.
    Nc                 C   s   || _ || _|| _d S r   )column_family_id_tablegc_rule)r   r=   tabler?   r
   r
   r   r      s    zColumnFamily.__init__c                 C   s   | j jd | j S )ao  Column family name used in requests.

        .. note::

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

        The table name is of the form

            ``"projects/../zones/../clusters/../tables/../columnFamilies/.."``

        :rtype: str
        :returns: The column family name.
        z/columnFamilies/)r>   namer=   r&   r
   r
   r   rA      s    zColumnFamily.namec                 C   s4   t || jsdS |j| jko2|j| jko2|j| jkS r    )r!   r"   r=   r>   r?   r   r
   r
   r   r      s    

zColumnFamily.__eq__c                 C   s   |  | S r   r   r   r
   r
   r   r      s    zColumnFamily.__ne__c                 C   s(   | j du rt S tj| j  dS dS )zConverts the column family to a protobuf.

        :rtype: :class:`.table_v2_pb2.ColumnFamily`
        :returns: The converted current object.
        N)r?   )r?   r$   r<   r'   r&   r
   r
   r   r'      s    
zColumnFamily.to_pbc                 C   sH   |   }tj| jjd}|jj| j|d | jjj	}|j
||j dS )zCreate this column family.rA   )idcreateNr'   table_admin_v2_pb2ModifyColumnFamiliesRequestr>   rA   modificationsaddr=   	_instance_client_table_stubModifyColumnFamiliestimeout_secondsr   Zcolumn_family
request_pbclientr
   r
   r   rD     s    
zColumnFamily.createc                 C   sH   |   }tj| jjd}|jj| j|d | jjj	}|j
||j dS )zUpdate this column family.

        .. note::

            Only the GC rule can be updated. By changing the column family ID,
            you will simply be referring to a different column family.
        rB   )rC   updateNrE   rO   r
   r
   r   rR     s    
zColumnFamily.updatec                 C   s@   t j| jjd}|jj| jdd | jjj}|j	
||j dS )zDelete this column family.rB   T)rC   ZdropN)rF   rG   r>   rA   rH   rI   r=   rJ   rK   rL   rM   rN   )r   rP   rQ   r
   r
   r   delete+  s    
zColumnFamily.delete)N)r   r   r   r   r   propertyrA   r   r   r'   rD   rR   rS   r
   r
   r
   r   r<      s   

r<   c                 C   s   |  d}|du rdS |dkr(t| jS |dkrBt| j}t|S |dkr`tdd | jjD S |dkrd	d | j	jD }t
|S td
|dS )a  Convert a protobuf GC rule to a native object.

    :type gc_rule_pb: :class:`.table_v2_pb2.GcRule`
    :param gc_rule_pb: The GC rule to convert.

    :rtype: :class:`GarbageCollectionRule` or :data:`NoneType <types.NoneType>`
    :returns: An instance of one of the native rules defined
              in :module:`column_family` or :data:`None` if no values were
              set on the protobuf passed in.
    :raises: :class:`ValueError <exceptions.ValueError>` if the rule name
             is unexpected.
    r5   Nr   r+   r8   c                 S   s   g | ]}t |qS r
   _gc_rule_from_pbr3   r
   r
   r   r6   O  s   z$_gc_rule_from_pb.<locals>.<listcomp>r;   c                 S   s   g | ]}t |qS r
   rU   r3   r
   r
   r   r6   R  s   zUnexpected rule name)Z
WhichOneofr   r   r   r+   r)   r-   r8   r/   r;   r:   
ValueError)Z
gc_rule_pbZ	rule_namer+   r/   r
   r
   r   rV   8  s$    


rV   )r   r   Zgoogle.protobufr   Zgcloud._helpersr   Zgcloud.bigtable._generatedr   r$   r   rF   r   r   objectr   r   r)   r-   r:   r<   rV   r
   r
   r
   r   <module>   s   s