
     i+                         d dl Z d dlmZmZ 	 d dlZd dlmZ n#  d dlmZ d dlm	Z Y nxY wd dl
Z
ddlmZ ddlmZmZ d Zd	 Zd
 ZddZddZd Zd Zd Zd Zd ZdS )    N)BASE_URLBASE_URL_REDUCED)StringIO)BytesIO   )MMTFDecoder)MMTFEncoderTemplateEncoderc                 n    	 t          j        | d           dS # t           j        $ r}Y d}~dS d}~ww xY w)z
    Check to see if the internet is on by pinging a set address.
    :param address: the IP or address to hit
    :return: a boolean - true if can be reached, false if not.
    r   )timeoutTNF)urllib2urlopenURLError)addresserrs     ^/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/mmtf/api/default_api.py_internet_onr      sP    ++++t   uuuuus    44c                 n    t                      }t          |||           |                    |            dS )a  API function to write data as MMTF to a file

    :param file_path the path of the file to write
    :param input_data the input data in any user format
    :param input_function a function to converte input_data to an output format. Must contain all methods in TemplateEncoder
    N)r	   pass_data_on
write_file)	file_path
input_datainput_functionmmtf_encoders       r   
write_mmtfr      s9     ==L^\:::I&&&&&    c                      || |           |S )a0  Helper to pass data from one data structure to another.

    :param input_data the input data in any user format
    :param input_function a function to converte input_data to an output format. Must contain all methods in TemplateEncoder.
    :param output_data a data holder for the data to be put into. )r   r   output_datas      r   r   r   &   s     N:{+++r   Fc                 ~   t          | |          }t          j        |          }|                    dd           t          j        |          }|                                                    d          dk    r"t          |                                          }n|                                }t          |          S )z|" Get the msgpack unpacked data given a PDB id.

    :param pdb_id: the input PDB id
    :return the unpacked data (a dict) zAccept-encodinggzipzContent-Encoding)
get_urlr   Request
add_headerr   infogetungzip_dataread_unpack)pdb_idreducedurlrequestresponsedatas         r   get_raw_data_from_urlr0   0   s    
 &
!
!Coc""G(&111w''H}}-..&888==??++}}4==r   c                 .    |r
t           | z   S t          | z   S )zuGet the URL for the data for a given PDB id.

    :param pdb_id: the input PDB id
    :return the URL for this PDB id)r   r   )r*   r+   s     r   r"   r"   ?   s"    
  !&((&  r   c                 V    t          j        |                                 d          }|S )NF)raw)msgpackunpackbr(   )r/   out_datas     r   r)   r)   I   s#    tyy{{666HOr   c                 f    t                      }|                    t          |                      |S )zxReturn a decoded API to the data from a PDB id.

    :param pdb_id: the input PDB id
    :return an API to decoded data )r   decode_datar0   )r*   decoders     r   fetchr:   N   s/    
 mmG-f55666Nr   c                     t                      }t          | d          5 }|                    t          |                     ddd           n# 1 swxY w Y   |S )zReturn a decoded API to the data from a file path.

    :param file_path: the input file path. Data is not entropy compressed (e.g. gzip)
    :return an API to decoded data rbN)r   openr8   r)   )r   
newDecoderfhs      r   parser@   W   s    
 J	i		 ,"wr{{+++, , , , , , , , , , , , , , ,s   #AAAc                     t                      }|                    t          t          j        | d                               |S )zReturn a decoded API to the data from a file path. File is gzip compressed.
    :param file_path: the input file path. Data is gzip compressed.
    :return an API to decoded datar<   )r   r8   r)   r!   r=   )r   r>   s     r   
parse_gziprB   b   s;     J749Y#=#=>>???r   c                 N    t          |           }t          j        |          }|S )zpReturn a string of data after gzip decoding

    :param the input gziped data
    :return  the gzip decoded data)fileobj)r   r!   GzipFile)r   buffs      r   r'   r'   k   s(    
 :

Cc"""AHr   )F)r!   mmtf.utils.constantsr   r   r   r   urllib.requestr-   ior   r4   mmtf_readerr   mmtf_writerr	   r
   r   r   r   r0   r"   r)   r:   r@   rB   r'   r   r   r   <module>rM      sY    ; ; ; ; ; ; ; ;'NNN!!!!!!!'$$$$$$&&&&&&&&  $ $ $ $ $ $ 4 4 4 4 4 4 4 4
 
 
	' 	' 	'     ! ! ! !  
          s   
 )