B
    ‰°bˆ  ã               @   s
  d 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
  mZ ddlmZ ddlmZmZ G d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G dd„ deƒZG dd„ deƒZ G dd„ de dZ!dS )a‚  Parser for XML results returned by NCBI's Entrez Utilities.

This parser is used by the read() function in Bio.Entrez, and is not
intended be used directly.

The question is how to represent an XML file as Python objects. Some
XML files returned by NCBI look like lists, others look like dictionaries,
and others look like a mix of lists and dictionaries.

My approach is to classify each possible element in the XML as a plain
string, an integer, a list, a dictionary, or a structure. The latter is a
dictionary where the same key can occur multiple times; in Python, it is
represented as a dictionary where that key occurs once, pointing to a list
of values found in the XML file.

The parser then goes through the XML and creates the appropriate Python
object for each element. The different levels encountered in the XML are
preserved on the Python side. So a subelement of a subelement of an element
is a value in a dictionary that is stored in a list which is a value in
some other dictionary (or a value in a list which itself belongs to a list
which is a value in a dictionary, and so on). Attributes encountered in
the XML are stored as a dictionary in a member .attributes of each element,
and the tag name is saved in a member .tag.

To decide which kind of Python object corresponds to each element in the
XML, the parser analyzes the DTD referred at the top of (almost) every
XML file returned by the Entrez Utilities. This is preferred over a hand-
written solution, since the number of DTDs is rather large and their
contents may change over time. About half the code in this parser deals
with parsing the DTD, and the other half with the XML itself.
é    N)ÚCounter)Úexpat)ÚBytesIO)Úescape)ÚurlopenÚurlparsec               @   s2   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZdS )ÚNoneElementz'NCBI Entrez XML element mapped to None.Nc             C   s&   || _ |dkr|| _n|| _|| _dS )zCreate a NoneElement.N)ÚtagÚkeyÚ
attributes)Úselfr	   r   r
   © r   ú0lib/python3.7/site-packages/Bio/Entrez/Parser.pyÚ__init__:   s
    zNoneElement.__init__c             C   s"   |dkrdS |  d¡rdS dS dS )z(Define equality with other None objects.NTF)Ú__eq__)r   Úotherr   r   r   r   C   s
    
zNoneElement.__eq__c             C   s"   |dkrdS |  d¡rdS dS dS )zDefine non-equality.NFT)r   )r   r   r   r   r   Ú__ne__L   s
    
zNoneElement.__ne__c             C   s(   y
| j }W n tk
r   dS X d| S )z-Return a string representation of the object.r   zNoneElement(attributes=%r))r   ÚAttributeError)r   r   r   r   r   Ú__repr__U   s
    
zNoneElement.__repr__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   7   s
   
			r   c               @   s"   e Zd ZdZddd„Zdd„ ZdS )ÚIntegerElementz-NCBI Entrez XML element mapped to an integer.Nc             C   s2   t  | |¡}||_|dkr"||_n||_||_|S )zCreate an IntegerElement.N)ÚintÚ__new__r	   r
   r   )ÚclsÚvaluer	   r   r
   r   r   r   r   r   a   s    zIntegerElement.__new__c             C   s6   t  | ¡}y
| j}W n tk
r(   |S X d||f S )z-Return a string representation of the object.z!IntegerElement(%s, attributes=%r))r   r   r   r   )r   Útextr   r   r   r   r   l   s    

zIntegerElement.__repr__)N)r   r   r   r   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 )ÚStringElementz+NCBI Entrez XML element mapped to a string.Nc             C   s2   t  | |¡}||_|dkr"||_n||_||_|S )zCreate a StringElement.N)Ústrr   r	   r
   r   )r   r   r	   r   r
   r   r   r   r   r   y   s    zStringElement.__new__c             C   s$   t  | ¡}| j}|s|S d||f S )z-Return a string representation of the object.z StringElement(%s, attributes=%r))r    r   r   )r   r   r   r   r   r   r   „   s
    
zStringElement.__repr__)N)r   r   r   r   r   r   r   r   r   r   r   v   s   
r   c               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚListElementz)NCBI Entrez XML element mapped to a list.Nc             C   s,   || _ |dkr|| _n|| _|| _|| _dS )zCreate a ListElement.N)r	   r
   r   Úallowed_tags)r   r	   r   r"   r
   r   r   r   r      s    zListElement.__init__c             C   s$   t  | ¡}| j}|s|S d||f S )z-Return a string representation of the object.zListElement(%s, attributes=%r))Úlistr   r   )r   r   r   r   r   r   r   š   s
    
zListElement.__repr__c             C   s4   |j }| jdk	r&|| jkr&td| ƒ‚|  |¡ dS )z-Append an element to the list, checking tags.NzUnexpected item '%s' in list)r
   r"   Ú
ValueErrorÚappend)r   r   r
   r   r   r   Ústore¢   s    zListElement.store)N)r   r   r   r   r   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d„ ZdS )
ÚDictionaryElementz0NCBI Entrez XML element mapped to a dictionaray.Nc             C   sL   || _ |dkr|| _n|| _|| _|| _|| _|rHx|D ]}g | |< q8W dS )zCreate a DictionaryElement.N)r	   r
   r   r"   Úrepeated_tags)r   r	   Úattrsr"   r(   r
   r   r   r   r   ­   s    
zDictionaryElement.__init__c             C   s$   t  | ¡}| j}|s|S d||f S )z-Return a string representation of the object.zDictElement(%s, attributes=%r))Údictr   r   )r   r   r   r   r   r   r   »   s
    
zDictionaryElement.__repr__c             C   sX   |j }|j}| jdk	r,|| jkr,td| ƒ‚| jrL|| jkrL| |  |¡ n|| |< dS )z.Add an entry to the dictionary, checking tags.Nz"Unexpected item '%s' in dictionary)r
   r	   r"   r$   r(   r%   )r   r   r
   r	   r   r   r   r&   Ã   s    zDictionaryElement.store)NN)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S )ÚNotXMLErrorzFailed to parse file as XML.c             C   s
   || _ dS )zInitialize the class.N)Úmsg)r   Úmessager   r   r   r   Ò   s    zNotXMLError.__init__c             C   s
   d| j  S )z)Return a string summary of the exception.zZFailed to parse the XML data (%s). Please make sure that the input data are in XML format.)r,   )r   r   r   r   Ú__str__Ö   s    zNotXMLError.__str__N)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S )ÚCorruptedXMLErrorzCorrupted XML.c             C   s
   || _ dS )zInitialize the class.N)r,   )r   r-   r   r   r   r   á   s    zCorruptedXMLError.__init__c             C   s
   d| j  S )z)Return a string summary of the exception.zZFailed to parse the XML data (%s). Please make sure that the input data are not corrupted.)r,   )r   r   r   r   r.   å   s    zCorruptedXMLError.__str__N)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S )ÚValidationErrora_  XML tag found which was not defined in the DTD.

    Validating parsers raise this error if the parser finds a tag in the XML
    that is not defined in the DTD. Non-validating parsers do not raise this
    error. The Bio.Entrez.read and Bio.Entrez.parse functions use validating
    parsers by default (see those functions for more information).
    c             C   s
   || _ dS )zInitialize the class.N)Úname)r   r1   r   r   r   r   ö   s    zValidationError.__init__c             C   s
   d| j  S )z)Return a string summary of the exception.zžFailed to find tag '%s' in the DTD. To skip all tags that are not represented in the DTD, please call Bio.Entrez.read or Bio.Entrez.parse with validate=False.)r1   )r   r   r   r   r.   ú   s    zValidationError.__str__N)r   r   r   r   r   r.   r   r   r   r   r0   í   s   r0   c               @   s2   e Zd ZdZdd„ Zedd„ ƒZejdd„ ƒZdS )ÚDataHandlerMetaz;A metaclass is needed until Python supports @classproperty.c             O   s
   d| _ dS )zInitialize the class.N)Ú
_directory)r   ÚargsÚkwargsr   r   r   r     s    zDataHandlerMeta.__init__c             C   s   | j S )z(Directory for caching XSD and DTD files.)r3   )r   r   r   r   Ú	directory
  s    zDataHandlerMeta.directoryc             C   s¤   |dkrNddl }| ¡ dkr2tj t d¡d¡}ntj d¡}tj |dd¡}|| _tj | jdd	d
¡| _tj	| jdd tj | jdd	d¡| _
tj	| j
dd dS )z9Set a custom directory for the local DTD/XSD directories.Nr   ZWindowsÚAPPDATAZ	biopythonú~z.configÚBioÚEntrezÚDTDsT)Úexist_okÚXSDs)ÚplatformÚsystemÚosÚpathÚjoinÚgetenvÚ
expanduserr3   Úlocal_dtd_dirÚmakedirsÚlocal_xsd_dir)r   r   r>   Úhomer   r   r   r6     s    N)r   r   r   r   r   Úpropertyr6   Úsetterr   r   r   r   r2     s   r2   c               @   s&  e Zd ZdZddlmZ ej ej	d d¡Z
ej ej	d d¡ZdZ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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-d.„ Z!d/d0„ Z"d1d2„ Z#d3d4„ Z$d5d6„ Z%d7d8„ Z&d9d:„ Z'd;d<„ Z(d=S )>ÚDataHandlerz.Data handler for parsing NCBI XML from Entrez.r   )r:   r;   r=   Ú c             C   s    g | _ d| _d| _g | _d| _d| _i | _i | _i | _t	ƒ | _
t	ƒ | _|| _tjdd| _| j tj¡ | j| j_d| _tƒ | _i | _|r”| j| _n| j| _dS )zCreate a DataHandler object.Nr   ú )Znamespace_separator)Údtd_urlsÚelementÚlevelÚdatar   r"   ÚstringsÚlistsÚdictionariesÚsetÚitemsÚerrorsÚ
validatingr   ZParserCreateÚparserZSetParamEntityParsingZXML_PARAM_ENTITY_PARSING_ALWAYSÚxmlDeclHandlerZXmlDeclHandlerÚschema_namespacer   Únamespace_levelÚnamespace_prefixÚcharacterDataHandlerEscapeÚcharacterDataHandlerÚcharacterDataHandlerRaw)r   Zvalidater   r   r   r   r   /  s*    

zDataHandler.__init__c          
   C   s¨   |  d¡dkrtdƒ‚y| j |¡ W nB tjk
rh } z"| jjrNt|ƒd‚n
t|ƒd‚W dd}~X Y nX y| j	S  t
k
r¢   | jjr”tdƒd‚n
tdƒd‚Y nX dS )z3Set up the parser and let it parse the XML results.r   ó    z$file should be opened in binary modeNz£Failed to parse the XML file correctly, possibly due to a bug in Bio.Entrez. Please contact the Biopython developers via the mailing list or GitHub for assistance.zXML declaration not found)ÚreadÚ	TypeErrorrY   Ú	ParseFiler   Ú
ExpatErrorÚStartElementHandlerr/   r+   Úrecordr   ÚRuntimeError)r   ÚhandleÚer   r   r   rb   H  s     zDataHandler.readc          
   c   s   |  d¡dkrtdƒ‚d}xÜ|  |¡}y| j |d¡ W nB tjk
rz } z"| jjr`t|ƒd‚n
t|ƒd‚W dd}~X Y nX y
| j	}W n2 t
k
r¸   | jjrªtdƒd‚n
tdƒd‚Y nX t|tƒsÌtd	ƒ‚|sÒP x t|ƒd
krò| d¡}|V  qÔW qW d| _| jdk	rtdƒ‚|E dH  dS )z'Parse the XML in the given file handle.r   ra   z$file should be opened in binary modei   FNz£Failed to parse the XML file correctly, possibly due to a bug in Bio.Entrez. Please contact the Biopython developers via the mailing list or GitHub for assistance.zXML declaration not foundzVThe XML file does not represent a list. Please use Entrez.read instead of Entrez.parseé   zPremature end of data)rb   rc   rY   ZParser   re   rf   r/   r+   rg   r   rh   Ú
isinstancer#   r$   ÚlenÚpoprO   )r   ri   ZBLOCKrQ   rj   Zrecordsrg   r   r   r   Úparsek  s>    



zDataHandler.parsec             C   s6   | j | j_| j| j_| j| j_| j| j_| j	| j_
dS )z2Set XML handlers when an XML declaration is found.N)r_   rY   ÚCharacterDataHandlerÚexternalEntityRefHandlerZExternalEntityRefHandlerÚstartNamespaceDeclHandlerZStartNamespaceDeclHandlerÚendNamespaceDeclHandlerZEndNamespaceDeclHandlerÚhandleMissingDocumentDefinitionrf   )r   ÚversionÚencodingZ
standaloner   r   r   rZ   ¶  s
    



zDataHandler.xmlDeclHandlerc             C   s   t dƒ‚dS )z?Raise an Exception if neither a DTD nor an XML Schema is found.zêAs the XML data contained neither a Document Type Definition (DTD) nor an XML Schema, Bio.Entrez is unable to parse these data. We recommend using a generic XML parser from the Python standard library instead, for example ElementTree.N)r$   )r   r	   r)   r   r   r   rt   ¾  s    z+DataHandler.handleMissingDocumentDefinitionc             C   sv   |dkr|| _ | j| j_nX|dkr0|dksVt‚n&|dkrF|dksVt‚ntd||f ƒ‚| j|  d7  < || j|< dS )	z-Handle start of an XML namespace declaration.ÚxsiZmmlz"http://www.w3.org/1998/Math/MathMLZxlinkzhttp://www.w3.org/1999/xlinkz!Unknown prefix '%s' with uri '%s'é   N)r[   ÚschemaHandlerrY   rf   ÚAssertionErrorr$   r\   r]   )r   ÚprefixÚurir   r   r   rr   Ä  s    	z%DataHandler.startNamespaceDeclHandlerc             C   s^   |dkrZ| j |  d8  < | j | dkrZx(| j ¡ D ]\}}||kr4P q4W tdƒ‚| j|= dS )z+Handle end of an XML namespace declaration.rw   rx   r   zFailed to find namespace prefixN)r\   r]   rV   rh   )r   r{   r
   r   r   r   r   rs   Û  s    z#DataHandler.endNamespaceDeclHandlerc             C   sœ   d| j  }|| }|  tj |¡¡}|sft|ƒ}| ¡ }|  tj |¡|¡ | ¡  |  	t
 |¡¡ n|  	t
 | ¡ ¡¡ | ¡  |  ||¡ | j| j_dS )z7Process the XML schema (before processing the element).z%s noNamespaceSchemaLocationN)r[   Úopen_xsd_filer@   rA   Úbasenamer   rb   Úsave_xsd_fileÚcloseÚ	parse_xsdÚETZ
fromstringÚstartElementHandlerrY   rf   )r   r1   r)   r
   Zschemari   r   r   r   r   ry   ç  s    
zDataHandler.schemaHandlerc       	      C   sr  || j krâ|dkst‚|d }|d }|d= |dkr|d= t||ddd}| j}||_|dkrh|| _n
| |¡ || _| j| j_	| j
| j_qn|dkr|d= d}tdd	gƒ}t|||||d
}| j}||_|dkrÞ|| _n
| |¡ || _| j| j_	| j
| j_nÚ|dkrl|d= d}t||||ƒ}| j}||_| jdkrF|| _n
| |¡ || _| j| j_	| j
| j_nr|dkr’| j| j_	| j| j_|| _nL|dkrÒ| jdks¬t‚|| _| j| j_| j| j_	| j| j_ntd| ƒ‚nŒ|| jkr| j| j_	| j| j_nh|| jkrf| j| j_| j| j_	| j| j_| jdks@t‚| j| | _| jdks\t‚|| _n|| jkrÒ| j| \}}t||||ƒ}| j}||_|dkr¬|| _n
| |¡ || _| j| j_	| j
| j_nœ|| jkr8| j| }t|||ƒ}| j}||_|dkr|| _n
| |¡ || _| j| j_	| j
| j_n6| jrJt|ƒ‚n$| j| j_| j| j_	| j
| j_d| _dS )zHandle start of an XML element.ÚItemÚNameZTypeZ	StructureN)r"   r(   )Z
ArticleIdsZHistoryZpubmedZmedline)r"   r(   r
   ZListZInteger)ÚStringZUnknownZDateZ
EnumeratorzUnknown item type %srx   ) rV   rz   r'   rO   Úparentrg   r&   ÚendElementHandlerrY   ÚEndElementHandlerÚskipCharacterDataHandlerrp   Ú	frozensetr!   ÚendIntegerElementHandlerr_   r   ÚstartRawElementHandlerrf   ÚendStringElementHandlerr$   rW   ÚendErrorElementHandlerrR   r"   rT   rS   rX   r0   ÚstartSkipElementHandlerÚendSkipElementHandlerrP   )	r   r	   r)   r1   ZitemtyperO   r‡   r"   r(   r   r   r   rƒ   û  sÂ    





























zDataHandler.startElementHandlerc             C   sÄ   d}| j rPy| ¡ \}}W n tk
r.   Y n"X | j | }| j| dkrPd|i}|rbd||f }n|}d| }x$| ¡ D ]\}}|d||f 7 }qxW |d7 }| j |¡ | j| j_	|  j
d7  _
dS )z#Handle start of an XML raw element.Nrx   Zxmlnsz%s:%sz<%sz %s="%s"ú>)r]   Úsplitr$   r\   rV   rQ   r%   ÚendRawElementHandlerrY   r‰   rP   )r   r1   r)   r{   r|   r
   r	   r   r   r   r   r   p  s&    

z"DataHandler.startRawElementHandlerc             C   s   |  j d7  _ dS )z$Handle start of an XML skip element.rx   N)rP   )r   r1   r)   r   r   r   r   ‹  s    z#DataHandler.startSkipElementHandlerc             C   s¦   | j }|dk	r,| j| j_| j| j_| j| j_d | j	¡}g | _	| j
}d| _
|| jkrp|dks`t‚|d }|d= n|}t||||ƒ}|dkr’|| _n
| |¡ d| _dS )z$Handle end of an XML string element.NrL   r„   r…   )rO   rƒ   rY   rf   rˆ   r‰   rŠ   rp   rB   rQ   r   rV   rz   r   rg   r&   r"   )r   r	   rO   r   r   r
   r   r   r   rŽ     s&    




z#DataHandler.endStringElementHandlerc             C   sf   |  j d8  _ | j dkr"| j| j_| jrNy| ¡ \}}W n tk
rL   Y nX d| }| j |¡ dS )z#Handle start of an XML raw element.rx   r   z</%s>N)	rP   rŽ   rY   r‰   r]   r“   r$   rQ   r%   )r   r1   r|   r	   r   r   r   r”   §  s    

z DataHandler.endRawElementHandlerc             C   s0   |  j d8  _ | j dkr,| j| j_| j| j_dS )z$Handle start of an XML skip element.rx   r   N)rP   rƒ   rY   rf   rˆ   r‰   )r   r1   r   r   r   r‘   ´  s    

z!DataHandler.endSkipElementHandlerc             C   s<   | j rd | j ¡}t|ƒ‚| jdk	r8| j| j_| j| j_dS )z%Handle start of an XML error element.rL   N)	rQ   rB   rh   rO   rˆ   rY   r‰   rŠ   rp   )r   r1   r   r   r   r   r   »  s    

z"DataHandler.endErrorElementHandlerc             C   s   | j }|j| _ |`dS )zHandle end of an XML element.N)rO   r‡   )r   r1   rO   r   r   r   rˆ   Æ  s    zDataHandler.endElementHandlerc             C   s¢   | j }d| _ |dkst‚|d }|d= | jrRtd | j¡ƒ}g | _t||||ƒ}nt|||ƒ}| j}|dkrt|| _n*| j	| j
_| j| j
_|dkr”dS | |¡ dS )z%Handle end of an XML integer element.Nr„   r…   rL   )r   rz   rQ   r   rB   r   r   rO   rg   rˆ   rY   r‰   rŠ   rp   r&   )r   r	   r   r
   r   rO   r   r   r   rŒ   Ì  s$    

z$DataHandler.endIntegerElementHandlerc             C   s   | j  |¡ dS )z"Handle character data as-is (raw).N)rQ   r%   )r   Úcontentr   r   r   r`   ã  s    z#DataHandler.characterDataHandlerRawc             C   s   t |ƒ}| j |¡ dS )z%Handle character data by encoding it.N)r   rQ   r%   )r   r•   r   r   r   r^   ç  s    z&DataHandler.characterDataHandlerEscapec             C   s   dS )z%Handle character data by skipping it.Nr   )r   r•   r   r   r   rŠ   ì  s    z$DataHandler.skipCharacterDataHandlerc             C   s  d}x|D ]þ}d}g }g }g }|j |d ks4t‚|jd }t|ƒdksNt‚|d }	|	j |d ksht‚x0|	D ]&}
|
j }||d krš| |
jd ¡ qp||d	 kr|
j d
d¡}xà|
D ]P}|j |d ksÒt‚|jd }| |¡ |dks|j d
d¡dkr¼| |¡ q¼W qp||d krpt|
ƒdks0t‚|
d }|j |d ksLt‚|jd dks`t‚x2|D ]*}|j |d ks~t‚| |jd ¡ qfW d}qpW t|ƒ}t|ƒdkrÒ||krÒ|rÆt‚|| j|< qt|ƒdkr|rêt‚t|ƒ}||f| j|< q|| j	|< qW dS )zParse an XSD file.z"{http://www.w3.org/2001/XMLSchema}FrO   r1   rx   r   ÚcomplexTypeÚ	attributeZsequenceÚ	maxOccursÚ1ÚrefZsimpleContentÚ	extensionÚbasez	xs:stringTN)
r	   rz   Zattribrm   r%   Úgetr‹   rS   rT   rR   )r   Úrootr{   rO   ZisSimpleContentZattribute_keysÚkeysÚmultipler1   r–   Z	componentr	   r˜   r
   rš   r›   r—   r"   r(   r   r   r   r   ï  sR    






zDataHandler.parse_xsdc                s   |  ¡ dkr| j |¡ dS |dkr^|tjjtjjdtjjtjjddfffkr^| j	 |¡ dS xP|d tjj
tjjfkr®|d tjjtjjfkr®t|d ƒdkr®|d d }q`W |d tjjtjjfkr
|d tjjkrö|d }tdd	„ |D ƒƒ}ntƒ }|| j|< dS |d tjjtjj
fkrŠ|d tjjtjjfkrŠ|d }|d tjj
krjt|ƒdksjt‚td
d	„ |D ƒƒ}|| j|< dS g ‰g ‰‡ ‡‡fdd„‰ ˆ |ƒ tˆƒdkrÚtˆƒdkrÚtˆƒ}|| j|< n"tˆˆ ƒ}tˆƒ}||f| j|< dS )ai  Call a call-back function for each element declaration in a DTD.

        This is used for each element declaration in a DTD like::

            <!ELEMENT       name          (...)>

        The purpose of this function is to determine whether this element
        should be regarded as a string, integer, list, dictionary, structure,
        or error.
        ÚERRORNr„   r   r   rx   é   c             s   s   | ]}|d  V  qdS )rk   Nr   )Ú.0Úchildr   r   r   ú	<genexpr>A  s    z*DataHandler.elementDecl.<locals>.<genexpr>c             s   s   | ]}|d  V  qdS )rk   Nr   )r£   r¤   r   r   r   r¥   N  s    c                s²   | dd … \}}}|d krd|t jjt jjfkrLx2|D ]}ˆ |d ¡ q4W q®x`|D ]}ˆ |ƒ qRW nJ| ¡ dkr®|t jjt jjfkrˆ |¡ n|t jjt jjfkr®ˆ |¡ d S )Nrx   rk   r¡   )r   ÚmodelÚXML_CQUANT_PLUSÚXML_CQUANT_REPr%   ÚupperÚXML_CQUANT_NONEÚXML_CQUANT_OPT)r¦   Z
quantifierr
   Úchildrenr¤   )Úcountr    Úsingler   r   r­   ^  s$    

z&DataHandler.elementDecl.<locals>.count)r©   rW   Úaddr   r¦   ZXML_CTYPE_MIXEDr¨   ZXML_CTYPE_NAMErª   rV   ZXML_CTYPE_SEQZXML_CTYPE_CHOICEr«   rm   ZXML_CTYPE_EMPTYr‹   rR   r§   rz   rS   rT   )r   r1   r¦   r¬   r"   r(   r   )r­   r    r®   r   ÚelementDecl  sT    


zDataHandler.elementDeclc             C   st   t j tj|¡}yt|dƒ}W n tk
r2   Y nX |S t j tj|¡}yt|dƒ}W n tk
rj   Y nX |S dS )zOpen specified DTD file.ÚrbN)r@   rA   rB   rK   rE   ÚopenÚFileNotFoundErrorÚglobal_dtd_dir)r   ÚfilenamerA   ri   r   r   r   Úopen_dtd_file  s    zDataHandler.open_dtd_filec             C   st   t j tj|¡}yt|dƒ}W n tk
r2   Y nX |S t j tj|¡}yt|dƒ}W n tk
rj   Y nX |S dS )zOpen specified XSD file.r±   N)r@   rA   rB   rK   rG   r²   r³   Úglobal_xsd_dir)r   rµ   rA   ri   r   r   r   r}   ‘  s    zDataHandler.open_xsd_filec             C   s\   t j tj|¡}yt|dƒ}W n& tk
rD   t d||f ¡ Y nX | 	|¡ | 
¡  dS )zSave DTD file to cache.ÚwbzFailed to save %s at %sN)r@   rA   rB   rK   rE   r²   ÚOSErrorÚwarningsÚwarnÚwriter€   )r   rµ   r   rA   ri   r   r   r   Úsave_dtd_file£  s    
zDataHandler.save_dtd_filec             C   s\   t j tj|¡}yt|dƒ}W n& tk
rD   t d||f ¡ Y nX | 	|¡ | 
¡  dS )zSave XSD file to cache.r¸   zFailed to save %s at %sN)r@   rA   rB   rK   rG   r²   r¹   rº   r»   r¼   r€   )r   rµ   r   rA   ri   r   r   r   r   ®  s    
zDataHandler.save_xsd_filec             C   s8  t |ƒ}|jdkr|}n`|jdkrjy| jd }W n tk
rH   d}Y nX tj |¡}| d¡d | }ntd|j ƒ‚| j 	|¡ tj 
|¡\}}	|  |	¡}
|
súyt|ƒ}
W n& tk
rÔ   td|	|f ƒd‚Y nX |
 ¡ }|
 ¡  |  |	|¡ t|ƒ}
| j |¡}| j|_| |
¡ |
 ¡  | j ¡  | j| j_d	S )
a¾  Handle external entity reference in order to cache DTD locally.

        The purpose of this function is to load the DTD locally, instead
        of downloading it from the URL specified in the XML. Using the local
        DTD results in much faster parsing. If the DTD is not found locally,
        we try to download it. If new DTDs become available from NCBI,
        putting them in Bio/Entrez/DTDs will allow the parser to see them.
        )ZhttpZhttpsZftprL   éÿÿÿÿz http://www.ncbi.nlm.nih.gov/dtd/ú/zUnexpected URL scheme %rzFailed to access %s at %sNrx   )r   ZschemerN   Ú
IndexErrorr@   rA   ÚdirnameÚrstripr$   r%   r“   r¶   r   r¹   rh   rb   r€   r½   r   rY   ZExternalEntityParserCreater°   ZElementDeclHandlerrd   rn   rƒ   rf   )r   Úcontextrœ   ZsystemIdZpublicIdZurlinfoZurlÚsourceÚlocationrµ   ri   r   rY   r   r   r   rq   ¹  s@    	







z$DataHandler.externalEntityRefHandlerN))r   r   r   r   r9   r:   r@   rA   rB   Ú__path__r´   r·   rE   rG   r   rb   ro   rZ   rt   rr   rs   ry   rƒ   r   r   rŽ   r”   r‘   r   rˆ   rŒ   r`   r^   rŠ   r   r°   r¶   r}   r½   r   rq   r   r   r   r   rK   #  sD   #Ku/arK   )Ú	metaclass)"r   r@   rº   Úcollectionsr   Zxml.parsersr   Úior   Zxml.etree.ElementTreeZetreeZElementTreer‚   Zxml.sax.saxutilsr   Zurllib.requestr   r   r   r   r   r    r   r#   r!   r*   r'   r$   r+   r/   r0   Útyper2   rK   r   r   r   r   Ú<module>'   s$   '% 