a
    !f<                     @   s   d Z ddlZddlZddlmZ ddlm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 )zEContainer for Google Cloud Bigtable Cells and Streaming Row Contents.    N)_datetime_from_microseconds)	_to_bytesc                   @   s6   e Zd ZdZdddZedd Zdd Zd	d
 ZdS )CellaN  Representation of a Google Cloud Bigtable Cell.

    :type value: bytes
    :param value: The value stored in the cell.

    :type timestamp: :class:`datetime.datetime`
    :param timestamp: The timestamp when the cell was stored.

    :type labels: list
    :param labels: (Optional) List of strings. Labels applied to the cell.
     c                 C   s   || _ || _t|| _d S N)value	timestamplistlabels)selfr   r   r
   r   r   Y/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigtable/row_data.py__init__&   s    zCell.__init__c                 C   s2   t |j}|jr"| |j||jdS | |j|S dS )zCreate a new cell from a Cell protobuf.

        :type cell_pb: :class:`._generated.data_pb2.Cell`
        :param cell_pb: The protobuf to convert.

        :rtype: :class:`Cell`
        :returns: The cell corresponding to the protobuf.
        )r
   N)r   timestamp_microsr
   r   )clsZcell_pbr   r   r   r   from_pb+   s    

zCell.from_pbc                 C   s4   t || jsdS |j| jko2|j| jko2|j| jkS NF)
isinstance	__class__r   r   r
   r   otherr   r   r   __eq__;   s    

zCell.__eq__c                 C   s   |  | S r   r   r   r   r   r   __ne__B   s    zCell.__ne__N)r   )	__name__
__module____qualname____doc__r   classmethodr   r   r   r   r   r   r   r      s   

r   c                   @   s"   e Zd ZdZd	ddZdd ZdS )
PartialCellDataa  Representation of partial cell in a Google Cloud Bigtable Table.

    These are expected to be updated directly from a
    :class:`._generated.bigtable_service_messages_pb2.ReadRowsResponse`

    :type row_key: bytes
    :param row_key: The key for the row holding the (partial) cell.

    :type family_name: str
    :param family_name: The family name of the (partial) cell.

    :type qualifier: bytes
    :param qualifier: The column qualifier of the (partial) cell.

    :type timestamp_micros: int
    :param timestamp_micros: The timestamp (in microsecods) of the
                             (partial) cell.

    :type labels: list of str
    :param labels: labels assigned to the (partial) cell

    :type value: bytes
    :param value: The (accumulated) value of the (partial) cell.
    r       c                 C   s(   || _ || _|| _|| _|| _|| _d S r   )row_keyfamily_name	qualifierr   r
   r   )r   r    r!   r"   r   r
   r   r   r   r   r   _   s    zPartialCellData.__init__c                 C   s   |  j |7  _ dS )zrAppend bytes from a new chunk to value.

        :type value: bytes
        :param value: bytes to append
        N)r   )r   r   r   r   r   append_valueh   s    zPartialCellData.append_valueN)r   r   )r   r   r   r   r   r#   r   r   r   r   r   F   s    
	r   c                   @   sH   e Zd ZdZdd Zdd Zdd Zdd	 Zed
d Z	edd Z
dS )PartialRowDataa   Representation of partial row in a Google Cloud Bigtable Table.

    These are expected to be updated directly from a
    :class:`._generated.bigtable_service_messages_pb2.ReadRowsResponse`

    :type row_key: bytes
    :param row_key: The key for the row holding the (partial) data.
    c                 C   s   || _ i | _d S r   )_row_key_cells)r   r    r   r   r   r   {   s    zPartialRowData.__init__c                 C   s(   t || jsdS |j| jko&|j| jkS r   )r   r   r%   r&   r   r   r   r   r      s
    
zPartialRowData.__eq__c                 C   s   |  | S r   r   r   r   r   r   r      s    zPartialRowData.__ne__c                 C   sN   i }t | jD ]8\}}t |D ]$\}}t|d t| }|||< q"q|S )a  Convert the cells to a dictionary.

        This is intended to be used with HappyBase, so the column family and
        column qualiers are combined (with ``:``).

        :rtype: dict
        :returns: Dictionary containing all the data in the cells of this row.
           :)six	iteritemsr&   r   )r   resultZcolumn_family_idcolumnsZcolumn_qualcellskeyr   r   r   to_dict   s    	
zPartialRowData.to_dictc                 C   s   t | jS )a  Property returning all the cells accumulated on this partial row.

        :rtype: dict
        :returns: Dictionary of the :class:`Cell` objects accumulated. This
                  dictionary has two-levels of keys (first for column families
                  and second for column names/qualifiers within a family). For
                  a given column, a list of :class:`Cell` objects is stored.
        )copydeepcopyr&   r   r   r   r   r,      s    
zPartialRowData.cellsc                 C   s   | j S )z~Getter for the current (partial) row's key.

        :rtype: bytes
        :returns: The current (partial) row's key.
        )r%   r1   r   r   r   r       s    zPartialRowData.row_keyN)r   r   r   r   r   r   r   r.   propertyr,   r    r   r   r   r   r$   q   s   	
r$   c                   @   s   e Zd ZdZdS )InvalidReadRowsResponsez;Exception raised to to invalid response data from back-end.Nr   r   r   r   r   r   r   r   r3      s   r3   c                   @   s   e Zd ZdZdS )InvalidChunkz8Exception raised to to invalid chunk data from back-end.Nr4   r   r   r   r   r5      s   r5   c                   @   s   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
d Z
edd Zedd Zdd Zd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%d& Zd'd( Zd)d* ZdS ),PartialRowsDataa#  Convenience wrapper for consuming a ``ReadRows`` streaming response.

    :type response_iterator:
        :class:`grpc.framework.alpha._reexport._CancellableIterator`
    :param response_iterator: A streaming iterator returned from a
                              ``ReadRows`` request.
    ZStartzNew rowzRow in progresszCell in progressc                 C   s4   || _ i | _d| _d | _d | _d | _d | _d | _d S )Nr   )_response_iterator_rows_counter_last_scanned_row_key_row_previous_row_cell_previous_cell)r   Zresponse_iteratorr   r   r   r      s    zPartialRowsData.__init__c                 C   s   t || jsdS |j| jkS r   )r   r   r7   r   r   r   r   r      s    zPartialRowsData.__eq__c                 C   s   |  | S r   r   r   r   r   r   r      s    zPartialRowsData.__ne__c                 C   sb   | j du r| jS | jdu r<| jdu s(J | jdu s6J | jS | jdurL| jS | jdur\| jS | jS )zState machine state.

        :rtype: str
        :returns:  name of state corresponding to currrent row / chunk
                   processing.
        N)r:   STARTr;   r=   r>   NEW_ROWCELL_IN_PROGRESSROW_IN_PROGRESSr1   r   r   r   state   s    



zPartialRowsData.statec                 C   s   | j S )zProperty returning all rows accumulated from the stream.

        :rtype: dict
        :returns: row_key -> :class:`PartialRowData`.
        )r8   r1   r   r   r   rows   s    	zPartialRowsData.rowsc                 C   s   | j   dS )z)Cancels the iterator, closing the stream.N)r7   cancelr1   r   r   r   rE      s    zPartialRowsData.cancelc                 C   s  t | j}|  jd7  _| jdu r0|jr0t |j| _| j}| j}|j	D ]}| 
| |jrzd }| _d } | _| _qJ|du rt|j }| _|du rt|j|jj|jj|j|j|j }| _| | n||j |jr|   d }}qJ|jdkrJ|   d}qJdS )zConsume the next ``ReadRowsResponse`` from the stream.

        Parse the response and its chunks into a new/existing row in
        :attr:`_rows`
           Nr   )r(   nextr7   r9   r:   Zlast_scanned_row_keyr3   r;   r=   chunks_validate_chunk	reset_rowr>   r$   r    r   r!   r   r"   r   r
   _copy_from_previousr#   
commit_row_save_current_row
value_size_save_current_cell)r   responserowcellchunkr   r   r   consume_next   sD    





zPartialRowsData.consume_nextNc                 C   sN   d}|du rt d}||k rJ|d7 }z|   W q tyF   Y qJY q0 qdS )a  Consume the streamed responses until there are no more.

        This simply calls :meth:`consume_next` until there are no
        more to consume.

        :type max_loops: int
        :param max_loops: (Optional) Maximum number of times to try to consume
                          an additional ``ReadRowsResponse``. You can use this
                          to avoid long wait times.
        r   NinfrF   )floatrT   StopIteration)r   Z	max_loopsZ	curr_loopr   r   r   consume_all3  s    zPartialRowsData.consume_allc                 C   sz   | j rTt| j t| d t| d t| j t| j t| j t| j t| jod| jdk t| jdk  dS )z8Helper for :meth:`_validate_chunk_row_in_progress`, etc.r!   r"   r   N)	rJ   	_raise_ifr    HasFieldr   r
   rN   r   rL   )rS   r   r   r   _validate_chunk_statusH  s    




z&PartialRowsData._validate_chunk_statusc                 C   sv   | j | jksJ t|j t|j  t|j  t|j  t|jdkoR|jdu t| j	duon|j| j	jk dS )z#Helper for :meth:`_validate_chunk`.r   FN)
rC   r@   rY   rJ   r    r!   r"   rN   rL   r<   r   rS   r   r   r   _validate_chunk_new_rowY  s    
z'PartialRowsData._validate_chunk_new_rowc                 C   s6   | j }|j|jko4|j|jko4|j|jko4|j|jkS )z2Helper for :meth:`_validate_chunk_row_in_progress`)r>   r    r!   r"   r
   r   rS   previousr   r   r   _same_as_previousf  s    


z!PartialRowsData._same_as_previousc                 C   s   | j | jksJ | | |ds>|js>t|j p:|j  t|joR|j| j	jk t|dol|d  | j
}t| |o|j|jk dS )"Helper for :meth:`_validate_chunk`rL   r!   r"   N)rC   rB   r[   rZ   rJ   rY   r   r   r    r;   r>   r`   r^   r   r   r   _validate_chunk_row_in_progressn  s    


z/PartialRowsData._validate_chunk_row_in_progressc                 C   s(   | j | jksJ | | | | dS )ra   N)rC   rA   r[   _copy_from_currentr\   r   r   r    _validate_chunk_cell_in_progress|  s    
z0PartialRowsData._validate_chunk_cell_in_progressc                 C   sF   | j | jkr| | | j | jkr,| | | j | jkrB| | dS  Helper for :meth:`consume_next`.N)rC   r@   r]   rB   rb   rA   rd   r\   r   r   r   rI     s    

zPartialRowsData._validate_chunkc                 C   sT   | j | j }}|j|ji }||jg }t| j}|| d| | _| _	dS re   )
r;   r=   r&   
setdefaultr!   r"   r   r   appendr>   )r   rQ   rR   familyZ	qualifiedZcompleter   r   r   rO     s    
z"PartialRowsData._save_current_cellc                 C   sj   | j }|durf|js|j|_|ds0|j|j_|dsD|j|j_|jsR|j|_|jsf|j|j dS )rf   Nr!   r"   )	r=   r    rZ   r!   r   r"   r   r
   extend)r   rS   currentr   r   r   rc     s    



z"PartialRowsData._copy_from_currentc                 C   s<   | j }|dur8|js|j|_|js*|j|_|js8|j|_dS re   )r>   r    r!   r"   )r   rR   r_   r   r   r   rK     s    z#PartialRowsData._copy_from_previousc                 C   s8   | j r|   | j| j| jj< d| j | _| _d| _dS re   )r=   rO   r;   r8   r    r<   r>   r1   r   r   r   rM     s
    z!PartialRowsData._save_current_row)N)r   r   r   r   r?   r@   rB   rA   r   r   r   r2   rC   rD   rE   rT   rX   staticmethodr[   r]   r`   rb   rd   rI   rO   rc   rK   rM   r   r   r   r   r6      s4   


3

		r6   c                 G   s   | rt | dS )zHelper for validation methods.N)r5   )	predicateargsr   r   r   rY     s    rY   )r   r/   r(   Zgcloud._helpersr   r   objectr   r   r$   RuntimeErrorr3   r5   r6   rY   r   r   r   r   <module>   s   -+>  