Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
restrepo
GitHub Repository: restrepo/ComputationalMethods
Path: blob/master/input/__pycache__/google_drive_tools.cpython-34.pyc
934 views
�
�چZ��@s�ddlZddlZddlmZe�Zee_ejd�ZedZ	dd�Z
dddd�Zdd	d
d�Zdd
�Z
dd	dd�Zdd�Zdddddddd�ZdS)�N)�ConfigParserz	drive.cfg�FILEScCs)t�}t|_|jd�}|dS)Nz	drive.cfgr)r�str�optionxform�read)�cfg�tmp�r	�F/home/restrepo/Cursos/ComputationalMethods/input/google_drive_tools.py�load_drive_files_keys
s		rcCsddl}|s%d||f}nd|||f}tj|�}|jdkr{tj|j�}|j|dd�SdS)a

    Read Google spread sheet by id.
    Options:
       gss_sheet=N : if in old format select the N-sheet
       gss_query=SQL_command: Filter with some SQL command
       example 
         SQL_command: 'select B,D,E,F,I where (H contains 'GFIF') order by D desc'
    rNzDhttps://docs.google.com/spreadsheets/d/%s/gviz/tq?tqx=out:csv&gid=%szJhttps://docs.google.com/spreadsheets/d/%s/gviz/tq?tqx=out:csv&gid=%s&tq=%s���keep_default_naF)�pandas�requests�getZstatus_code�io�StringIO�text�read_csv)�id�	gss_sheet�	gss_query�pd�url�rZcsv_filer	r	r
�pandas_from_google_drive_csvs	rTcCs�d}tj�}|j|di|d6dd�}t|�}|rxi|d6|d6}|j|d|dd�}nt|d|d|�S)	a�
    Download file from google drive as binary (default) or txt file.
    If not destination the file object is returned
    Example: Let id="XXX" a txt file:
    1) fb=download_file_from_google_drive("XXX") ; fb.decode() #to convert to text file
    2) ft=download_file_from_google_drive("XXX",binary=False) # txt file
    3) fb=download_file_from_google_drive("XXX",'output_file') # always binay
    z*https://docs.google.com/uc?export=download�paramsr�streamT�confirm�destination�binary)r�Sessionr�get_confirm_token�save_response_content)rrr �URL�session�response�tokenrr	r	r
�download_file_from_google_drive's	"r(cCs7x0|jj�D]\}}|jd�r|SqWdS)NZdownload_warning)Zcookies�items�
startswith)r&�key�valuer	r	r
r">sr"cCs�d}|rt|d�}nd}x@|j|�D]/}|r4|rV|j|�qc||}q4q4W|rz|j�n&|r�tj|�Stj|j��SdS)Ni��wb�)�openZiter_content�write�closer�BytesIOr�decode)r&rr Z
CHUNK_SIZE�f�chunks�chunkr	r	r
r#Es

r#cKs�ddl}ddl}|jd|�rbtj|�rOttj|�|�S|j||�Sntj|�r�|jttj|��|�S|j||�SdS)a�Read excel from google drive
     Requieres a drive_file dictionary with the keys for the google drive
     file names.
     If the file_name is not found in the drive_file dictionary it is read locally.
     If the file_name have an extension .csv, tray to read the google spreadsheet 
     directly: check pandas_from_google_drive_csv for passed options
     WARNING: ONLY OLD Google Spread Sheet allows to load sheet different from 0
     rNz\.csv$)	r�re�search�
drive_filerrr�
read_excelr()�	file_name�kwargsrr7r	r	r
�read_drive_excelZs		r=zhttps://spreadsheets.google.com�csvZ,0AuLa_xuSIEvxdERYSGVQWDBTX1NCN19QMXVpb0lhWXcz:select B,D,E,F,I where (H contains 'GFIF') order by D descFcCsDddl}d||||t|�f}|j|jdd��S)Nrz$%s/tq?tqx=out:%s&tq=%s&key=%s&gid=%s� �+)rrr�replace)Zgss_urlZ
gss_formatZgss_keyrrZgss_keep_default_narZissn_urlr	r	r
�query_drive_csvrsrB)rr�configparserrrrrrrr9rrr(r"r#r=rBr	r	r	r
�<module>s$