a
    !f                     @   s   d Z ddlZddlZddl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lmZ ddlmZ dd	lmZ dd
lmZ ddlmZ e ZG dd deZG dd deZdd Zdd Zdd ZG dd deZG dd deZdS )zDefine API Datasets.    N)_datetime_from_microseconds)_microseconds_from_datetime)_millis_from_datetime)NotFound)Request)make_api_request)RESUMABLE_UPLOAD)Upload)_rows_from_jsonc                   @   s"   e Zd ZdZdddZdd ZdS )	SchemaFieldah  Describe a single field within a table schema.

    :type name: str
    :param name: the name of the field

    :type field_type: str
    :param field_type: the type of the field (one of 'STRING', 'INTEGER',
                       'FLOAT', 'BOOLEAN', 'TIMESTAMP' or 'RECORD')

    :type mode: str
    :param mode: the type of the field (one of 'NULLABLE', 'REQUIRED',
                 or 'REPEATED')

    :type description: str
    :param description: optional description for the field

    :type fields: list of :class:`SchemaField`, or None
    :param fields: subfields (requires ``field_type`` of 'RECORD').
    NULLABLENc                 C   s"   || _ || _|| _|| _|| _d S N)name
field_typemodedescriptionfields)selfr   r   r   r   r    r   V/var/www/html/python-backend/venv/lib/python3.9/site-packages/gcloud/bigquery/table.py__init__9   s
    zSchemaField.__init__c                 C   sD   | j |j koB| j |j koB| j|jkoB| j|jkoB| j|jkS r   )r   r   lowerr   r   r   )r   otherr   r   r   __eq__A   s    


zSchemaField.__eq__)r   NN)__name__
__module____qualname____doc__r   r   r   r   r   r   r   %   s
     
r   c                   @   s  e Zd ZdZdZdKddZedd Zedd	 Zed
d Z	edd Z
e
j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jd!d  Zed"d# Zejd$d# Zed%d& Zejd'd& Zed(d) Zejd*d) Zed+d, Zejd-d, Zejd.d, Zed/d0 Zd1d2 Zd3d4 Zd5d6 ZdLd7d8ZdMd9d:Z dNd;d<Z!de"e"e"e"e"e"fd=d>Z#dOd?d@Z$dPdAdBZ%dQdCdDZ&dRdEdFZ'dSdIdJZ(dS )TTablea  Tables represent a set of rows whose values correspond to a schema.

    See:
    https://cloud.google.com/bigquery/docs/reference/v2/tables

    :type name: str
    :param name: the name of the table

    :type dataset: :class:`gcloud.bigquery.dataset.Dataset`
    :param dataset: The dataset which contains the table.

    :type schema: list of :class:`SchemaField`
    :param schema: The table's schema
    Nr   c                 C   s   || _ || _i | _|| _d S r   )r   _dataset_propertiesschema)r   r   datasetr!   r   r   r   r   \   s    zTable.__init__c                 C   s   | j jS )zsProject bound to the table.

        :rtype: str
        :returns: the project (derived from the dataset).
        )r   projectr   r   r   r   r#   c   s    zTable.projectc                 C   s   | j jS )zxName of dataset containing the table.

        :rtype: str
        :returns: the ID (derived from the dataset).
        )r   r   r$   r   r   r   dataset_namel   s    zTable.dataset_namec                 C   s   d| j j| jf S )zzURL path for the table's APIs.

        :rtype: str
        :returns: the path based on project and dataste name.
        z%s/tables/%s)r   pathr   r$   r   r   r   r&   u   s    z
Table.pathc                 C   s
   t | jS )zuTable's schema.

        :rtype: list of :class:`SchemaField`
        :returns: fields describing the schema
        )list_schemar$   r   r   r   r!   ~   s    zTable.schemac                 C   s(   t dd |D stdt|| _dS )a  Update table's schema

        :type value: list of :class:`SchemaField`
        :param value: fields describing the schema

        :raises: TypeError if 'value' is not a sequence, or ValueError if
                 any item in the sequence is not a SchemaField
        c                 s   s   | ]}t |tV  qd S r   )
isinstancer   ).0fieldr   r   r   	<genexpr>       zTable.schema.<locals>.<genexpr>zSchema items must be fieldsN)all
ValueErrortupler(   r   valuer   r   r   r!      s    
c                 C   s$   | j d}|dur td| S dS )zDatetime at which the table was created.

        :rtype: ``datetime.datetime``, or ``NoneType``
        :returns: the creation time (None until set from the server).
        creationTimeN     @@r    getr   )r   Zcreation_timer   r   r   created   s    zTable.createdc                 C   s   | j dS )zETag for the table resource.

        :rtype: str, or ``NoneType``
        :returns: the ETag (None until set from the server).
        etagr    r6   r$   r   r   r   r8      s    z
Table.etagc                 C   s$   | j d}|dur td| S dS )zDatetime at which the table was last modified.

        :rtype: ``datetime.datetime``, or ``NoneType``
        :returns: the modification time (None until set from the server).
        lastModifiedTimeNr4   r5   )r   Zmodified_timer   r   r   modified   s    zTable.modifiedc                 C   s    | j d}|durt|S dS )zThe size of the table in bytes.

        :rtype: integer, or ``NoneType``
        :returns: the byte count (None until set from the server).
        ZnumBytesNr    r6   int)r   Znum_bytes_as_strr   r   r   	num_bytes   s    zTable.num_bytesc                 C   s    | j d}|durt|S dS )zThe number of rows in the table.

        :rtype: integer, or ``NoneType``
        :returns: the row count (None until set from the server).
        ZnumRowsNr<   )r   Znum_rows_as_strr   r   r   num_rows   s    zTable.num_rowsc                 C   s   | j dS )zURL for the table resource.

        :rtype: str, or ``NoneType``
        :returns: the URL (None until set from the server).
        ZselfLinkr9   r$   r   r   r   	self_link   s    zTable.self_linkc                 C   s   | j dS )zID for the table resource.

        :rtype: str, or ``NoneType``
        :returns: the ID (None until set from the server).
        idr9   r$   r   r   r   table_id   s    zTable.table_idc                 C   s   | j dS )zThe type of the table.

        Possible values are "TABLE" or "VIEW".

        :rtype: str, or ``NoneType``
        :returns: the URL (None until set from the server).
        typer9   r$   r   r   r   
table_type   s    	zTable.table_typec                 C   s   | j dS )zDescription of the table.

        :rtype: str, or ``NoneType``
        :returns: The description as set by the user, or None (the default).
        r   r9   r$   r   r   r   r      s    zTable.descriptionc                 C   s*   t |tjs|durtd|| jd< dS )zUpdate description of the table.

        :type value: str, or ``NoneType``
        :param value: new description

        :raises: ValueError for invalid value types.
        NPass a string, or Noner   r)   sixstring_typesr/   r    r1   r   r   r   r      s    	c                 C   s$   | j d}|dur td| S dS )zDatetime at which the table will be removed.

        :rtype: ``datetime.datetime``, or ``NoneType``
        :returns: the expiration time, or None
        expirationTimeNr4   r5   )r   Zexpiration_timer   r   r   expires   s    zTable.expiresc                 C   s.   t |tjs|durtdt|| jd< dS )zUpdate datetime at which the table will be removed.

        :type value: ``datetime.datetime``, or ``NoneType``
        :param value: the new expiration time, or None
        NPass a datetime, or NonerI   )r)   datetimer/   r   r    r1   r   r   r   rJ     s    c                 C   s   | j dS )zTitle of the table.

        :rtype: str, or ``NoneType``
        :returns: The name as set by the user, or None (the default).
        friendlyNamer9   r$   r   r   r   friendly_name  s    zTable.friendly_namec                 C   s*   t |tjs|durtd|| jd< dS )zUpdate title of the table.

        :type value: str, or ``NoneType``
        :param value: new title

        :raises: ValueError for invalid value types.
        NrE   rM   rF   r1   r   r   r   rN     s    	c                 C   s   | j dS )zLocation in which the table is hosted.

        :rtype: str, or ``NoneType``
        :returns: The location as set by the user, or None (the default).
        locationr9   r$   r   r   r   rO   ,  s    zTable.locationc                 C   s*   t |tjs|durtd|| jd< dS )zUpdate location in which the table is hosted.

        :type value: str, or ``NoneType``
        :param value: new location

        :raises: ValueError for invalid value types.
        NrE   rO   rF   r1   r   r   r   rO   5  s    	c                 C   s"   | j d}|dur|dS dS )zSQL query defining the table as a view.

        :rtype: str, or ``NoneType``
        :returns: The query as set by the user, or None (the default).
        viewNqueryr9   )r   rP   r   r   r   
view_queryB  s    zTable.view_queryc                 C   s&   t |tjstdd|i| jd< dS )zUpdate SQL query defining the table as a view.

        :type value: str
        :param value: new query

        :raises: ValueError for invalid value types.
        zPass a stringrQ   rP   NrF   r1   r   r   r   rR   M  s    	c                 C   s   | j dd dS )z.Delete SQL query defining the table as a view.rP   N)r    popr$   r   r   r   rR   Z  s    c                 C   sB   d|vsd|d vrt d|d d }| ||d}|| |S )a  Factory:  construct a table given its API representation

        :type resource: dict
        :param resource: table resource representation returned from the API

        :type dataset: :class:`gcloud.bigquery.dataset.Dataset`
        :param dataset: The dataset containing the table.

        :rtype: :class:`gcloud.bigquery.table.Table`
        :returns: Table parsed from ``resource``.
        tableReferencetableIdzJResource lacks required identity information:["tableReference"]["tableId"])r"   )KeyError_set_properties)clsresourcer"   Z
table_nametabler   r   r   from_api_repr_  s    

zTable.from_api_reprc                 C   s   |du r| j j}|S )a  Check client or verify over-ride.

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: :class:`gcloud.bigquery.client.Client`
        :returns: The client passed in or the currently bound client.
        N)r   Z_clientr   clientr   r   r   _require_clientu  s    
zTable._require_clientc                 C   s   | j   | }|dddi}t|| _d|v rDt|d |d< d|v r\t|d |d< d|v rtt|d |d< | j | dS )zUpdate properties from resource in body of ``api_response``

        :type api_response: httplib2.Response
        :param api_response: response returned from an API call
        r!   r   r   r3   r:   rI   N)r    clearcopyrS   _parse_schema_resourcer!   floatupdate)r   api_responsecleanedr!   r   r   r   rW     s    

zTable._set_propertiesc                 C   s   d| j j| j j| jdi}| jdur.| j|d< | jdurJt| j}||d< | jdur^| j|d< | jdurr| j|d< | jduri  }|d< | j|d	< n"| j	rd
t
| j	i|d< ntd|S )z1Generate a resource for ``create`` or ``update``.rT   Z	projectIdZ	datasetIdrU   Nr   rI   rM   rO   rP   rQ   r   r!   z$Set either 'view_query' or 'schema'.)r   r#   r   r   rJ   r   rN   rO   rR   r(   _build_schema_resourcer/   )r   rY   r2   rP   r   r   r   _build_resource  s.    










zTable._build_resourcec                 C   sB   |  |}d| jj| jjf }|jjd||  d}| | dS )ak  API call:  create the dataset via a PUT request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/tables/insert

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.
        z/projects/%s/datasets/%s/tablesPOSTmethodr&   dataN)r^   r   r#   r   
connectionapi_requestrh   rW   )r   r]   r&   rd   r   r   r   create  s    


zTable.createc                 C   sD   |  |}z|jjd| jddid W n ty:   Y dS 0 dS dS )a  API call:  test for the existence of the table via a GET request

        See
        https://cloud.google.com/bigquery/docs/reference/v2/tables/get

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: bool
        :returns: Boolean indicating existence of the table.
        GETr   rA   rk   r&   query_paramsFTN)r^   rm   rn   r&   r   r\   r   r   r   exists  s    

zTable.existsc                 C   s*   |  |}|jjd| jd}| | dS )am  API call:  refresh table properties via a GET request

        See
        https://cloud.google.com/bigquery/docs/reference/v2/tables/get

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.
        rp   rk   r&   N)r^   rm   rn   r&   rW   r   r]   rd   r   r   r   reload  s
    

zTable.reloadc           
      C   s   |  |}i }|tur>t|tjs2|dur2tdt||d< |turN||d< |tur^||d< |turn||d< |tur|du rd|d< nd|i|d< |tur|du rd|d	< nd
t|i|d	< |jjd| j	|d}	| 
|	 dS )a_  API call:  update individual table properties via a PATCH request

        See
        https://cloud.google.com/bigquery/docs/reference/v2/tables/patch

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :type friendly_name: str or ``NoneType``
        :param friendly_name: point in time at which the table expires.

        :type description: str or ``NoneType``
        :param description: point in time at which the table expires.

        :type location: str or ``NoneType``
        :param location: point in time at which the table expires.

        :type expires: :class:`datetime.datetime` or ``NoneType``
        :param expires: point in time at which the table expires.

        :type view_query: str
        :param view_query: SQL query defining the table as a view

        :type schema: list of :class:`SchemaField`
        :param schema: fields describing the schema

        :raises: ValueError for invalid value types.
        NrK   rI   r   rM   rO   rP   rQ   r!   r   PATCHrj   )r^   _MARKERr)   rL   r/   r   rg   rm   rn   r&   rW   )
r   r]   rN   r   rO   rJ   rR   r!   partialrd   r   r   r   patch  s6    %


zTable.patchc                 C   s0   |  |}|jjd| j|  d}| | dS )ao  API call:  update table properties via a PUT request

        See
        https://cloud.google.com/bigquery/docs/reference/v2/tables/update

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.
        PUTrj   N)r^   rm   rn   r&   rh   rW   ru   r   r   r   rc   7  s
    

zTable.updatec                 C   s    |  |}|jjd| jd dS )al  API call:  delete the table via a DELETE request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/tables/delete

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.
        DELETErt   N)r^   rm   rn   r&   r\   r   r   r   deleteF  s    

zTable.deletec                 C   s   |  |}i }|dur||d< |dur.||d< |jjdd| j |d}|d}|dur`t|}|d}t|dd	| j}|||fS )
a  API call:  fetch the table data via a GET request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/tabledata/list

        .. note::

           This method assumes that its instance's ``schema`` attribute is
           up-to-date with the schema as defined on the back-end:  if the
           two schemas are not identical, the values returned may be
           incomplete.  To ensure that the local copy of the schema is
           up-to-date, call the table's ``reload`` method.

        :type max_results: integer or ``NoneType``
        :param max_results: maximum number of rows to return.

        :type page_token: str or ``NoneType``
        :param page_token: token representing a cursor into the table's rows.

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: tuple
        :returns: ``(row_data, total_rows, page_token)``, where ``row_data``
                  is a list of tuples, one per result row, containing only
                  the values;  ``total_rows`` is a count of the total number
                  of rows in the table;  and ``page_token`` is an opaque
                  string which can be used to fetch the next batch of rows
                  (``None`` if no further batches can be fetched).
        NZ
maxResultsZ	pageTokenrp   z%s/datarq   Z	totalRowsrowsr   )r^   rm   rn   r&   r6   r=   r
   r(   )r   Zmax_resultsZ
page_tokenr]   paramsresponseZ
total_rowsZ	rows_datar   r   r   
fetch_dataS  s      


zTable.fetch_datac                 C   s  |  |}g }d|i}t|D ]p\}	}
i }t| j|
D ]0\}}|jdkr\|dur\t|d }|||j< q6d|i}|dur||	 |d< || q|dur||d< |dur||d< |dur||d	< |jj	d
d| j
 |d}g }|ddD ] }|t|d |d d q|S )a  API call:  insert table data via a POST request

        See:
        https://cloud.google.com/bigquery/docs/reference/v2/tabledata/insertAll

        :type rows: list of tuples
        :param rows: Row data to be inserted. Each tuple should contain data
                     for each schema field on the current table and in the
                     same order as the schema fields.

        :type row_ids: list of string
        :param row_ids: Unique ids, one per row being inserted.  If not
                        passed, no de-duplication occurs.

        :type skip_invalid_rows: boolean or ``NoneType``
        :param skip_invalid_rows: skip rows w/ invalid data?

        :type ignore_unknown_values: boolean or ``NoneType``
        :param ignore_unknown_values: ignore columns beyond schema?

        :type template_suffix: str or ``NoneType``
        :param template_suffix: treat ``name`` as a template table and provide
                                a suffix. BigQuery will create the table
                                ``<name> + <template_suffix>`` based on the
                                schema of the template table. See:
                                https://cloud.google.com/bigquery/streaming-data-into-bigquery#template-tables

        :type client: :class:`gcloud.bigquery.client.Client` or ``NoneType``
        :param client: the client to use.  If not passed, falls back to the
                       ``client`` stored on the current dataset.

        :rtype: list of mappings
        :returns: One mapping per row with insert errors:  the "index" key
                  identifies the row, and the "errors" key contains a list
                  of the mappings describing one or more problems with the
                  row.
        r~   	TIMESTAMPNgư>jsonZinsertIdZskipInvalidRowsignoreUnknownValuesZtemplateSuffixri   z%s/insertAllrj   ZinsertErrorsr   indexerrors)r   r   )r^   	enumeratezipr(   r   r   r   appendrm   rn   r&   r6   r=   )r   r~   Zrow_idsZskip_invalid_rowsignore_unknown_valuesZtemplate_suffixr]   Z	rows_inforl   r   rowZrow_infor+   r2   infor   r   errorr   r   r   insert_data  s<    ,

zTable.insert_dataF   c           !      C   s  |  |}|j}d}|r&|dtj t|dd}|durJ|dkrJtd|}|du rzt|drrt|	 j
}ntdd	d
|jd	d}dd|dt| ji| jj| jj| jddii}t|||||	|
||||| t|||dd}t }t }|jd }d| jjf }|j||d}t|d|t|d}|||| |j}|jd }|j|||d|_|||j |jt kr|j!dd}nt"|j||d}|j#} t$| t%j&s| 'd} |(t)| S )al  Upload the contents of this table from a file-like object.

        The content type of the upload will either be
        - The value passed in to the function (if any)
        - ``text/csv``.

        :type file_obj: file
        :param file_obj: A file handle opened in binary mode for reading.

        :type source_format: str
        :param source_format: one of 'CSV' or 'NEWLINE_DELIMITED_JSON'.
                              job configuration option; see
                              :meth:`gcloud.bigquery.job.LoadJob`

        :type rewind: boolean
        :param rewind: If True, seek to the beginning of the file handle before
                       writing the file to Cloud Storage.

        :type size: int
        :param size: The number of bytes to read from the file handle.
                     If not provided, we'll try to guess the size using
                     :func:`os.fstat`. (If the file handle is not from the
                     filesystem this won't be possible.)

        :type num_retries: integer
        :param num_retries: Number of upload retries. Defaults to 6.

        :type allow_jagged_rows: boolean
        :param allow_jagged_rows: job configuration option;  see
                                  :meth:`gcloud.bigquery.job.LoadJob`

        :type allow_quoted_newlines: boolean
        :param allow_quoted_newlines: job configuration option; see
                                      :meth:`gcloud.bigquery.job.LoadJob`

        :type create_disposition: str
        :param create_disposition: job configuration option; see
                                   :meth:`gcloud.bigquery.job.LoadJob`

        :type encoding: str
        :param encoding: job configuration option; see
                         :meth:`gcloud.bigquery.job.LoadJob`

        :type field_delimiter: str
        :param field_delimiter: job configuration option; see
                                :meth:`gcloud.bigquery.job.LoadJob`

        :type ignore_unknown_values: boolean
        :param ignore_unknown_values: job configuration option; see
                                      :meth:`gcloud.bigquery.job.LoadJob`

        :type max_bad_records: integer
        :param max_bad_records: job configuration option; see
                                :meth:`gcloud.bigquery.job.LoadJob`

        :type quote_character: str
        :param quote_character: job configuration option; see
                                :meth:`gcloud.bigquery.job.LoadJob`

        :type skip_leading_rows: integer
        :param skip_leading_rows: job configuration option; see
                                  :meth:`gcloud.bigquery.job.LoadJob`

        :type write_disposition: str
        :param write_disposition: job configuration option; see
                                  :meth:`gcloud.bigquery.job.LoadJob`

        :type client: :class:`gcloud.storage.client.Client` or ``NoneType``
        :param client: Optional. The client to use.  If not passed, falls back
                       to the ``client`` stored on the current dataset.

        :rtype: :class:`gcloud.bigquery.jobs.LoadTableFromStorageJob`
        :returns: the job instance used to load the data (e.g., for
                  querying status)
        :raises: :class:`ValueError` if ``size`` is not passed in and can not
                 be determined, or if the ``file_obj`` can be detected to be
                 a file opened in text mode.
        zapplication/octet-streamr   r   NrbzGCannot upload files opened in text mode:  use open(filename, mode='rb')filenozBtotal bytes could not be determined. Please pass an explicit size.zapplication/jsonzgzip, deflate)AcceptzAccept-Encodingz
User-Agentzcontent-typeconfigurationloadr   rf   )ZsourceFormatr!   ZdestinationTableF)Zauto_transferz/uploadz/projects/%s/jobs)api_base_urlr&   ri   )body)r   r&   rr   T)Z
use_chunks)retrieszutf-8)*r^   rm   seekosSEEK_SETgetattrr/   hasattrfstatr   st_sizeZ
USER_AGENTrg   r(   r   r#   r   _configure_job_metadatar	   _UrlBuilder_UploadConfigZAPI_BASE_URLZbuild_api_urlr   r   dumpsZconfigure_requestrr   urlZinitialize_uploadhttpZstrategyr   Zstream_filer   contentr)   rG   rH   decodeZjob_from_resourceloads)!r   Zfile_objZsource_formatrewindsizeZnum_retriesallow_jagged_rowsallow_quoted_newlinescreate_dispositionencodingfield_delimiterr   max_bad_recordsquote_characterskip_leading_rowswrite_dispositionr]   rm   content_typer   total_bytesheadersmetadatauploadZurl_builderZupload_configbase_urlr&   Z
upload_urlrequestrr   http_responseZresponse_contentr   r   r   upload_from_file  s    _





zTable.upload_from_file)r   )N)N)N)N)N)NNN)NNNNN)FNr   NNNNNNNNNNN))r   r   r   r   r(   r   propertyr#   r%   r&   r!   setterr7   r8   r;   r>   r?   r@   rB   rD   r   rJ   rN   rO   rR   deleterclassmethodr[   r^   rW   rh   ro   rs   rv   rx   rz   rc   r}   r   r   r   r   r   r   r   r   J   s   






























!



I


6     
Z              r   c                 C   s   | d d }|dur||d< |dur,||d< |dur<||d< |durL||d< |dur\||d< |durl||d	< |dur|||d
< |dur||d< |	dur|	|d< |
dur|
|d< dS )z*Helper for :meth:`Table.upload_from_file`.r   r   NZallowJaggedRowsZallowQuotedNewlinesZcreateDispositionr   ZfieldDelimiterr   ZmaxBadRecordsquoteZskipLeadingRowsZwriteDispositionr   )r   r   r   r   r   r   r   r   r   r   r   Zload_configr   r   r   r     s*    r   c              	   C   sf   d| vrdS g }| d D ]H}|d }|d }| dd}| d}t|}|t||||| q|S )a1  Parse a resource fragment into a schema field.

    :type info: mapping
    :param info: should contain a "fields" key to be parsed

    :rtype: list of :class:`SchemaField`, or ``NoneType``
    :returns: a list of parsed fields, or ``None`` if no "fields" key is
                present in ``info``.
    r   Nr   rC   r   r   r   )r6   ra   r   r   )r   r!   Zr_fieldr   r   r   r   Z
sub_fieldsr   r   r   ra     s    

ra   c                 C   sZ   g }| D ]L}|j |j|jd}|jdur2|j|d< |jdurJt|j|d< || q|S )zGenerate a resource fragment for a schema.

    :type fields: sequence of :class:`SchemaField`
    :param fields: schema to be dumped

    :rtype: mapping
    :returns: a mapping describing the schema of the supplied fields.
    )r   rC   r   Nr   r   )r   r   r   r   r   rg   r   )r   infosr+   r   r   r   r   rg     s    	


rg   c                   @   s*   e Zd ZdZdgZdZdZdZdZdZ	dS )r   z/Faux message FBO apitools' 'configure_request'.z*/*NTz+/upload/bigquery/v2/projects/{project}/jobs)
r   r   r   r   acceptmax_sizeZresumable_multipartZresumable_pathZsimple_multipartZsimple_pathr   r   r   r   r     s   r   c                   @   s   e Zd ZdZdd ZdS )r   z.Faux builder FBO apitools' 'configure_request'c                 C   s   i | _ d| _d S )N )rr   Z_relative_pathr$   r   r   r   r     s    z_UrlBuilder.__init__N)r   r   r   r   r   r   r   r   r   r     s   r   )r   rL   r   r   rG   Zgcloud._helpersr   r   r   Zgcloud.exceptionsr   Zgcloud.streaming.http_wrapperr   r   Zgcloud.streaming.transferr   r	   Zgcloud.bigquery._helpersr
   objectrx   r   r   r   ra   rg   r   r   r   r   r   r   <module>   s6   %      Q-
