
    Xf                        d Z ddlZddlZddlmc 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  G d
 d          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de          Z G d de           Z! G d de           Z" G d de           Z# G d de$          Z% G d  d!e%"          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)BytesIO)urlparse)urlopen)expat)escape)StreamModeErrorc                   *    e Zd ZdZd Zd Zd Zd ZdS )NoneElementz'NCBI Entrez XML element mapped to None.c                 0    || _         || _        || _        dS )zCreate a NoneElement.N)tagkey
attributes)selfr   r   r   s       1lib/python3.11/site-packages/Bio/Entrez/Parser.py__init__zNoneElement.__init__<   s    $    c                 <    |dS |                     d          rdS dS )z(Define equality with other None objects.NTF__eq__r   others     r   r   zNoneElement.__eq__B   s*    =4\\$ 	45r   c                 <    |dS |                     d          rdS dS )zDefine non-equality.NFTr   r   s     r   __ne__zNoneElement.__ne__K   s*    =5\\$ 	54r   c                 @    	 | j         }n# t          $ r Y dS w xY wd|z  S )-Return a string representation of the object.r   zNoneElement(attributes=%r))r   AttributeError)r   r   s     r   __repr__zNoneElement.__repr__T   s=    	!JJ 	! 	! 	! ==	!+j88s   
 
N)__name__
__module____qualname____doc__r   r   r   r    r   r   r   r   9   sV        11% % %    9 9 9 9 9r   r   c                   $    e Zd ZdZd Zd Zd ZdS )IntegerElementz-NCBI Entrez XML element mapped to an integer.c                 8    t                               | |          S )zCreate an IntegerElement.)int__new__clsvalueargskwargss       r   r(   zIntegerElement.__new__`       {{3&&&r   c                 0    || _         || _        || _        dS )zInitialize an IntegerElement.Nr   r   r   r   r+   r   r   r   s        r   r   zIntegerElement.__init__d       $r   c                 ~    t                               |           }	 | j        }n# t          $ r |cY S w xY wd| d|dS )r   zIntegerElement(, attributes=))r'   r   r   r   r   textr   s      r   r   zIntegerElement.__repr__j   s^    ||D!!	JJ 	 	 	KKK	CCCJCCCCs   $ 33Nr   r    r!   r"   r(   r   r   r#   r   r   r%   r%   ]   sL        77' ' '  D D D D Dr   r%   c                   $    e Zd ZdZd Zd Zd ZdS )StringElementz+NCBI Entrez XML element mapped to a string.c                 8    t                               | |          S )zCreate a StringElement.strr(   r)   s       r   r(   zStringElement.__new__w   r.   r   c                 0    || _         || _        || _        dS )zInitialize a StringElement.Nr0   r1   s        r   r   zStringElement.__init__{   r2   r   c                 ^    t                               |           }| j        }|s|S d| d|dS )r   zStringElement(r4   r5   )r=   r   r   r6   s      r   r   zStringElement.__repr__   s@    ||D!!_
 	KBBB:BBBBr   Nr8   r#   r   r   r:   r:   t   sL        55' ' '  C C C C Cr   r:   c                   &    e Zd ZdZddZd Zd ZdS )ListElementz)NCBI Entrez XML element mapped to a list.Nc                 R    || _         ||| _        n|| _        || _        || _        dS )zCreate a ListElement.N)r   r   r   allowed_tags)r   r   r   rC   r   s        r   r   zListElement.__init__   s3    ;DHHDH$(r   c                 ^    t                               |           }| j        }|s|S d| d|dS )r   zListElement(r4   r5   listr   r   r6   s      r   r   zListElement.__repr__   @    }}T""_
 	K@d@@@@@@r   c                     |j         }| j        || j        vrt          d|z            |` |                     |           dS )-Append an element to the list, checking tags.NUnexpected item '%s' in list)r   rC   
ValueErrorappendr   r+   r   s      r   storezListElement.store   sP    i(S8I-I-I;cABBBIEr   Nr   r    r!   r"   r   r   rN   r#   r   r   rA   rA      sO        33) ) ) )A A A    r   rA   c                   &    e Zd ZdZddZd Zd ZdS )DictionaryElementz0NCBI Entrez XML element mapped to a dictionaray.Nc                 |    || _         ||| _        n|| _        || _        || _        || _        |r
|D ]	}g | |<   dS dS )zCreate a DictionaryElement.N)r   r   r   rC   repeated_tags)r   r   attrsrC   rT   r   s         r   r   zDictionaryElement.__init__   sk    ;DHHDH(* 	$  S			 	 r   c                 ^    t                               |           }| j        }|s|S d| d|dS )r   zDictElement(r4   r5   )dictr   r   r6   s      r   r   zDictionaryElement.__repr__   rG   r   c                     |j         }|j        }| j        || j        vrt          d|z            |` | j        r&|| j        v r| |                             |           dS || |<   dS )z.Add an entry to the dictionary, checking tags.Nz"Unexpected item '%s' in dictionary)r   r   rC   rK   rT   rL   )r   r+   r   r   s       r   rN   zDictionaryElement.store   s    ii(S8I-I-IACGHHHI 	#);";";IU#####DIIIr   )NNrP   r#   r   r   rR   rR      sO        ::   A A A
 
 
 
 
r   rR   c                   &    e Zd ZdZddZd Zd ZdS )OrderedListElementa  NCBI Entrez XML element mapped to a list of lists.

    OrderedListElement is used to describe a list of repeating elements such as
    A, B, C, A, B, C, A, B, C ... where each set of A, B, C forms a group. This
    is then stored as [[A, B, C], [A, B, C], [A, B, C], ...]
    Nc                 `    || _         ||| _        n|| _        || _        || _        || _        dS )zCreate an OrderedListElement.N)r   r   r   rC   	first_tag)r   r   r   rC   r\   r   s         r   r   zOrderedListElement.__init__   s8    ;DHHDH$("r   c                 ^    t                               |           }| j        }|s|S d| d|dS )r   zOrderedListElement(r4   r5   rE   r6   s      r   r   zOrderedListElement.__repr__   s@    }}T""_
 	KGTGG
GGGGr   c                     |j         }| j        || j        vrt          d|z            || j        k    r|                     g            | d                             |           dS )rI   NrJ   )r   rC   rK   r\   rL   rM   s      r   rN   zOrderedListElement.store   sk    i(S8I-I-I;cABBB$.  KKOOORr   rO   rP   r#   r   r   rZ   rZ      sS         	# 	# 	# 	#H H H    r   rZ   c                   $    e Zd ZdZd Zd Zd ZdS )ErrorElementz4NCBI Entrez XML element containing an error message.c                 8    t                               | |          S )zCreate an ErrorElement.r<   r)   s       r   r(   zErrorElement.__new__   r.   r   c                 "    || _         || _        dS )zInitialize an ErrorElement.N)r   r   )r   r+   r   s      r   r   zErrorElement.__init__   s    r   c                 B    t                               |           }d| dS )z%Return the error message as a string.zErrorElement(r5   )r=   r   )r   r7   s     r   r   zErrorElement.__repr__   s$    ||D!!&t&&&&r   Nr8   r#   r   r   ra   ra      sG        >>' ' '  
' ' ' ' 'r   ra   c                       e Zd ZdZd Zd ZdS )NotXMLErrorzFailed to parse file as XML.c                     || _         dS Initialize the class.Nmsgr   messages     r   r   zNotXMLError.__init__      r   c                     d| j         z  S ))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.rj   r   s    r   __str__zNotXMLError.__str__      !#'8,	
r   Nr   r    r!   r"   r   rr   r#   r   r   rf   rf     s8        &&  
 
 
 
 
r   rf   c                       e Zd ZdZd Zd ZdS )CorruptedXMLErrorzCorrupted XML.c                     || _         dS rh   rj   rl   s     r   r   zCorruptedXMLError.__init__  rn   r   c                     d| j         z  S )rp   zZFailed to parse the XML data (%s). Please make sure that the input data are not corrupted.rj   rq   s    r   rr   zCorruptedXMLError.__str__  rs   r   Nrt   r#   r   r   rv   rv     s8          
 
 
 
 
r   rv   c                       e Zd ZdZd Z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                     || _         dS rh   namer   r}   s     r   r   zValidationError.__init__,  s    			r   c                     d| j         z  S )rp   zFailed 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.r|   rq   s    r   rr   zValidationError.__str__0  s    79=C	
r   Nrt   r#   r   r   rz   rz   #  s<           
 
 
 
 
r   rz   c                   N    e Zd ZdZd Zed             Zej        d             ZdS )DataHandlerMetaz;A metaclass is needed until Python supports @classproperty.c                 h    ddl m} 	 |j        | _        n# t          $ r |j        | _        Y nw xY w~dS )ri   r   EntrezN)Bior   local_cache	directoryPermissionError
_directory)r*   r,   r-   r   s       r   r   zDataHandlerMeta.__init__<  sU    	0".CMM 	0 	0 	0#/CNNN	0FFs    ..c                     | j         S )z(Directory for caching XSD and DTD files.)r   )r*   s    r   r   zDataHandlerMeta.directoryF  s     ~r   c                 >   |ddl }|                                dk    r3t          j                            t          j        d          d          }n@t          j                            d          }t          j                            |dd          }t          j                            |dd	d
          | _        t          j        | j        d           t          j                            |dd	d          | _	        t          j        | j	        d           || _
        dS )z9Set a custom directory for the local DTD/XSD directories.Nr   WindowsAPPDATA	biopython~z.configr   r   DTDsT)exist_okXSDs)platformsystemospathjoingetenv
expanduserlocal_dtd_dirmakedirslocal_xsd_dirr   )r*   r+   r   homes       r   r   zDataHandlerMeta.directoryK  s     =OOO  I--RYy%9%9;GGw))#..T9kBBGLLxHH
C%5555GLLxHH
C%5555 r   N)r   r    r!   r"   r   propertyr   setterr#   r   r   r   r   9  sc        EE     X     r   r   c                   b   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 Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#d Z$d Z%d Z&d  Z'd! Z(dS )"DataHandlerz.Data handler for parsing NCBI XML from Entrez.r   r   r   r   Nc                    g | _         d| _        d| _        g | _        d| _        d| _        i | _        i | _        t                      | _	        t                      | _
        || _        || _        t          j        d          | _        | j                            t          j                   | j        | j        _        d| _        t+                      | _        i | _        |r| j        | _        dS | j        | _        dS )zCreate a DataHandler object.Nr    )namespace_separator)dtd_urlselementleveldatar   rC   constructorsstringssetitemserrors
validatingignore_errorsr   ParserCreateparserSetParamEntityParsingXML_PARAM_ENTITY_PARSING_ALWAYSxmlDeclHandlerXmlDeclHandlerschema_namespacer   namespace_levelnamespace_prefixcharacterDataHandlerEscapecharacterDataHandlercharacterDataHandlerRaw)r   validater   r   s       r   r   zDataHandler.__init__m  s    
	 UU
ee"*(SAAA))%*OPPP%)%8" $&yy " 	E(,(GD%%%(,(DD%%%r   c                    	 t          |d          }n;# t          $ r. |                    d          dk    rt          d          d|}Y nw xY w|                    d          dk    rt          d          	 | j                            |           nC# t          j        $ r1}| j        j        rt          |          dt          |          dd}~ww xY w	 ||ur|                                 n# ||ur|                                 w w xY w	 | j        }|`|S # t          $ r- | j        j        rt          d          dt          d          dw xY w)	2Set up the parser and let it read the XML results.rbr   r   +the XML file must be opened in binary mode.N$file should be opened in binary modeFailed 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.XML declaration not found)open	TypeErrorreadr	   r   	ParseFiler   
ExpatErrorStartElementHandlerrv   rf   closerecordr   r   RuntimeError)r   sourcestreamer   s        r   r   zDataHandler.read  s   	&$''FF 	 	 	{{1~~$$%A  FFF	 ;;q>>S  BCCC	K!!&)))) 		/ 		/ 		/{. / (**4 "!nn$.		/ * V## V## $	[F  
M!  	I 	I 	I{. I #A  	 ""=>>DH	IsE    5A
A7B C/ C!,CCC/ /D
D 7Ec              #     K   	 t          |d          }n;# t          $ r. |                    d          dk    rt          d          d|}Y nw xY w|                    d          dk    rt          d          d}	 	 |                    |          }| j                            |d	           	 | j        }n:# t          $ r- | j        j        rt          d
          dt          d          dw xY wt          |t                    st          d          |sn@t          |          dk    r,|                    d          }|V  t          |          dk    ,nC# t           j        $ r1}| j        j        rt%          |          dt          |          dd}~ww xY w	 ||ur|                                 n# ||ur|                                 w w xY wd| _        | j        t%          d          |E d{V  dS )r   r   r   r   r   Nr   i   TFr   r   zWThe XML file does not represent a list. Please use Entrez.read instead of Entrez.parse.   zPremature end of data)r   r   r   r	   r   Parser   r   r   r   rf   
isinstancerF   rK   lenpopr   r   rv   r   r   )r   r   r   BLOCKr   recordsr   r   s           r   parsezDataHandler.parse  s     	&$''FF 	 	 	{{1~~$$%A  FFF	 ;;q>>S  BCCC2	#!{{5))!!$...Q"kGG% Q Q Q{6 Q
 +X   $	$ **EFFDPQ  "'400 $?  
  'lla'' %[[^^F LLL	 'lla''?#!:   		/ 		/ 		/{. / (**4 "!nn$.		/ & V## V## $ <##$;<<< sR    5AA;1E -B5 4E 57C,,A*E F4 F&,FFF4 4Gc                     | j         | j        _        | j        | j        _        | j        | j        _        | j        | j        _        | j	        | j        _
        dS )z2Set XML handlers when an XML declaration is found.N)r   r   CharacterDataHandlerexternalEntityRefHandlerExternalEntityRefHandlerstartNamespaceDeclHandlerStartNamespaceDeclHandlerendNamespaceDeclHandlerEndNamespaceDeclHandlerhandleMissingDocumentDefinitionr   )r   versionencoding
standalones       r   r   zDataHandler.xmlDeclHandler  sI    +/+D(/3/L,040N-.2.J+*.*N'''r   c                      t          d          )z?Raise an Exception if neither a DTD nor an XML Schema is found.zAs 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.)rK   )r   r   rU   s      r   r   z+DataHandler.handleMissingDocumentDefinition  s     y
 
 	
r   c                 0   |dk    r|| _         | j        | j        _        dS |dk    r	|dk    sJ nG|dk    r	|dk    sJ n8|dk    r|                    d          dk    sJ nt          d	| d
| d          | j        |xx         dz  cc<   || j        |<   dS )z-Handle start of an XML namespace declaration.xsimmlz"http://www.w3.org/1998/Math/MathMLxlinkzhttp://www.w3.org/1999/xlinkali/z#http://www.niso.org/schemas/ali/1.0zUnknown prefix 'z' with uri ''   N)r   schemaHandlerr   r   rstriprK   r   r   )r   prefixuris      r   r   z%DataHandler.startNamespaceDeclHandler  s    U??$'D!.2.@DK+++ BBBBBB7""<<<<<<5zz#*OOOOOO !NF!N!N!N!N!NOOO (((A-((()/D!#&&&r   c                     |dk    rf| j         |xx         dz  cc<   | j         |         dk    rB| j                                        D ]\  }}||k    r nt          d          | j        |= dS dS dS )z+Handle end of an XML namespace declaration.r   r   r   zFailed to find namespace prefixN)r   r   r   r   )r   r   r   r+   s       r   r   z#DataHandler.endNamespaceDeclHandler5  s    U?? (((A-(((#F+q00"&"7"="="?"? J JJC ' ''HIII)#... ?00r   c                    d| j         z  }||         }|                     t          j                            |                    }|st          |          }|                                }|                     t          j                            |          |           |                                 | 	                    t          j        |                     nM| 	                    t          j        |                                                     |                                 |                     ||           | j        | j        _        dS )z7Process the XML schema (before processing the element).z%s noNamespaceSchemaLocationN)r   open_xsd_filer   r   basenamer   r   save_xsd_filer   	parse_xsdET
fromstringstartElementHandlerr   r   )r   r}   rU   r   schemahandler7   s          r   r   zDataHandler.schemaHandlerA  s   ,t/DDs##BG$4$4V$<$<== 	V__F;;==Drw//77>>>LLNNNNN2=..////NN2=77888LLNNN  u---*.*B'''r   c           	      	   d}| j         r=	 |                                \  }}| j         |         }| d| }n# t          $ r Y nw xY w|| j        v r|dk    sJ |d         }|d         }|d= |dk    rn|d= t	          ||dd          }| j        }||_        ||| _        n|                    |           || _        | j	        | j
        _        | j        | j
        _        dS |dv r|d= d}	t          d	d
g          }
t	          |||	|
|          }| j        }||_        ||| _        n|                    |           || _        | j	        | j
        _        | j        | j
        _        dS |dk    rt|d= d}	t          |||	|          }| j        }||_        | j        || _        n|                    |           || _        | j	        | j
        _        | j        | j
        _        dS |dk    r+| j        | j
        _        | j        | j
        _        || _        dS |dv rE| j        J || _        | j        | j
        _        | j        | j
        _        | j        | j
        _        dS t          d|z            || j        v r$| j        | j
        _        | j        | j
        _        dS || j        v r`| j        | j
        _        | j        | j
        _        | j        | j
        _        | j        J | j        |         | _        | j        J || _        dS || j        v rr| j        |         \  }}	 |||g|	R  }| j        }||_        ||| _        n|                    |           || _        | j	        | j
        _        | j        | j
        _        dS |dk    rg }d}|d         dk    r|                    d           |d         dk    r.d|                    dd          z   }|                    |           |                    d          dk    r|                    d           d                    |          }d|d          d|d          d| d| d 	}|                     ddd           |                      dd|d           n| j!        rtE          |          | j#        | j
        _        | j$        | j
        _        | j        | j
        _        d!| _%        dS )"zHandle start of an XML element.N:ItemNameType	Structure)rC   rT   )
ArticleIdsHistorypubmedmedline)rC   rT   r   ListInteger)StringUnknownDate
EnumeratorzUnknown item type %szprocessing-metaz1.3ztagset-familyjatsJATSzbase-tagset	archivingarchivearticle.-zmathml-versionz3.0mathml3zhttps://z.nlm.nih.gov/r   z.dtdr   )&r   splitrK   r   rR   r   parentr   rN   endElementHandlerr   EndElementHandlerskipCharacterDataHandlerr   	frozensetrA   endIntegerElementHandlerr   r   startRawElementHandlerr   endStringElementHandlerr   endErrorElementHandlerr   rC   r   rL   replacegetr   r   r   r   rz   startSkipElementHandlerendSkipElementHandlerr   )r   r   rU   r   r   r}   itemtyper   r  rC   rT   r*   termsdtd_versiontermr   urls                    r   r   zDataHandler.startElementHandlerU  si     	))IIKK	T .s3(($((	    
 $*&=====DV}Hf;&&&M+%d$   !'>")DKKLL)))&040F-373P000222&M# )8Y*? @ @+!-"/   !'>")DKKLL)))&040F-373P000V##&M#%c5,EE!'<'")DKKLL)))&040F-373P000Y&&040M-373L0"'HHH..."'262M/040L-373L000 !7$!>???DK,0,GDK)/3/HDK,,,DL  .2.IDK+,0,HDK)/3/HDK,$,,, $S 1D?***#DOOOD%%% $ 1# 6Cc#u4|444G\F#GN~%W%%%"DL,0,BDK)/3/LDK,,, '''#)V33LL(((';66+k.A.A#s.K.KKDLL&&&99-..%77LL+++88E??y!7yyeMFZyy]hyyksyyy##D$555--dD#tDDDD +%c***.2.JDK+,0,FDK)/3/LDK,DJJJs   7 
AAc                    d}| j         rK	 |                                \  }}| j         |         }| j        |         dk    rd|i}n# t          $ r Y nw xY w|r| d| }n|}d|z  }|                                D ]\  }}|d| d| dz  }|d	z  }| j                            |           | j        | j        _	        | xj
        dz  c_
        dS )
z#Handle start of an XML raw element.Nr   xmlnsr   z<%sr   z="">)r   r  r   rK   r   r   rL   endRawElementHandlerr   r  r   )r   r}   rU   r   r   r   r   r+   s           r   r  z"DataHandler.startRawElementHandler  s!      	++ JJLL	T .s3'/144$cNE      	$$d$$CCC dl++-- 	' 	'JC&s&&e&&&&CCs
	(,(A%

a



s   A 
AAc                 &    | xj         dz  c_         dS )z$Handle start of an XML skip element.r   N)r   )r   r}   rU   s      r   r   z#DataHandler.startSkipElementHandler  s    

a



r   c                    | j         }|3| j        | j        _        | j        | j        _        | j        | j        _        d                    | j	                  }g | _	        | j
        }d| _
        | j        r=	 |                                \  }}| j        |         }| d| }n# t          $ r Y nw xY w|| j        v r|dk    sJ |d         }|d= n|}t          ||||          }	||| _        n|                    |	           d| _        dS )z$Handle end of an XML string element.N r   r   r   )r   r   r   r   r  r  r  r   r   r   r   r   r  rK   r   r:   r   rN   rC   )
r   r   r   r   r   r   r}   r   r   r+   s
             r   r  z#DataHandler.endStringElementHandler  s<   ,.2.FDK+,0,BDK)/3/LDK,wwty!!	_
  	))IIKK	T .s3(($((	    
 $*&====V$C6""CdCS99?!DKKMM%    s   4B   
B-,B-c                     | xj         dz  c_         | j         dk    r| j        | j        _        | j        r)	 |                                \  }}n# t          $ r Y nw xY wd|z  }| j                            |           dS )z!Handle end of an XML raw element.r   r   z</%s>N)	r   r  r   r  r   r  rK   r   rL   )r   r}   r   r   s       r   r+  z DataHandler.endRawElementHandler  s    

a

:??,0,HDK)  	 JJLL	TT   n	s   A 
AAc                     | xj         dz  c_         | j         dk    r$| j        | j        _        | j        | j        _        dS dS )z"Handle end of an XML skip element.r   r   N)r   r   r   r   r  r  r~   s     r   r!  z!DataHandler.endSkipElementHandler   sC    

a

:??.2.FDK+,0,BDK))) ?r   c                 `   | j         }|3| j        | j        _        | j        | j        _        | j        | j        _        d                    | j	                  }|dk    rdS | j
        du rt          |          g | _	        t          ||          }|	|| _        dS |                    |           dS )z#Handle end of an XML error element.Nr.  F)r   r   r   r   r  r  r  r   r   r   r   r   ra   r   rN   )r   r   r   r   r+   s        r   r  z"DataHandler.endErrorElementHandler'  s    ,.2.FDK+,0,BDK)/3/LDK,wwty!!2::F&&t$$$	T3''?!DKKKMM%     r   c                 0    | j         }|j        | _         |`dS )zHandle end of an XML element.N)r   r  )r   r}   r   s      r   r  zDataHandler.endElementHandler:  s    ,~NNNr   c                    | j         }d| _         |dk    sJ |d         }|d= | j        rAt          d                    | j                            }g | _        t	          ||||          }nt          |||          }| j        }|	|| _        dS | j        | j	        _
        | j        | j	        _        |dS |                    |           dS )z%Handle end of an XML integer element.Nr   r   r.  )r   r   r'   r   r%   r   r   r   r  r   r  r  r   rN   )r   r   r   r   r+   r   s         r   r  z$DataHandler.endIntegerElementHandler@  s    _
f}}}} v9 	6	**++EDI"5#z3??EEZ55E,?DKKK,0,BDK)/3/LDK,}MM%     r   c                 :    | j                             |           dS )z"Handle character data as-is (raw).N)r   rL   r   contents     r   r   z#DataHandler.characterDataHandlerRawW  s    	!!!!!r   c                 X    t          |          }| j                            |           dS )z%Handle character data by encoding it.N)r   r   rL   r5  s     r   r   z&DataHandler.characterDataHandlerEscape[  s)    //	!!!!!r   c                     dS )z%Handle character data by skipping it.Nr#   r5  s     r   r  z$DataHandler.skipCharacterDataHandler`  s      r   c                 j   d}|D ],}d}g }g }g }|j         |dz   k    sJ |j        d         }t          |          dk    sJ |d         }	|	j         |dz   k    sJ |	D ]J}
|
j         }||dz   k    r!|                    |
j        d                    4||d	z   k    r|
j                            d
d          }|
D ]n}|j         |dz   k    sJ |j        d         }|                    |           |dk    s|j                            d
d          dk    r|                    |           o||dz   k    rwt          |
          dk    sJ |
d         }|j         |dz   k    sJ |j        d         dk    sJ |D ]2}|j         |dz   k    sJ |                    |j        d                    3d}Lt          |          }t          |          dk    r ||k    r|rJ |f}t          |f| j        |<   t          |          dk    r*|rJ t          |          }||f}t          |f| j        |<   "|| j	        |<   .dS )zParse an XSD file.z"{http://www.w3.org/2001/XMLSchema}Fr   r}   r   r   complexType	attributesequence	maxOccurs1refsimpleContent	extensionbasez	xs:stringTN)
r   attribr   rL   r  r  rA   r   rR   r   )r   rootr   r   isSimpleContentattribute_keyskeysmultipler}   r:  	componentr   r=  r   r?  rA  r;  rC   r,   rT   s                       r   r   zDataHandler.parse_xsdc  s   5 ,	2 ,	2G#ONDH;&9"44444>&)Dw<<1$$$$!!*K?f}&<<<<<( + +	m&;...")))*:6*BCCCCFZ/// ) 0 4 4[# F FI( 1 1"w&9*<<<<<!j/C((($++sz~~k3/O/OSV/V/V$OOC0001 F_444y>>Q.... )!I$=F[,@@@@@$+F3{BBBB%. H H	(}0DDDDD&--i.>v.FGGGG&*O$T??L4yyA~~$("2"2****$+6*=!$''Ta**** )( 3 3$m4+<d*C!$''%1T""Y,	2 ,	2r   c                 @  	
 |                                 dk    r| j                            |           dS |dk    rg|t          j        j        t          j        j        dt          j        j        t          j        j        ddfffk    r| j	                            |           dS |d         t          j        j
        t          j        j        fv r|d         t          j        j        t          j        j        fv rt          |d                   dk    r{|d         d         }|d         t          j        j
        t          j        j        fv rC|d         t          j        j        t          j        j        fv rt          |d                   dk    {|d         t          j        j        t          j        j        fv rW|d         t          j        j        k    r"|d         }t          d |D                       }nt                      }|| j        |<   dS |t          j        j        t          j        j        ddfk    rd}d}||f}t$          |f| j        |<   dS |d         t          j        j        t          j        j
        fv r|d         t          j        j        t          j        j        fv r|d         }t          d	 |D                       }|d         t          j        j
        k    rt          |          dk    ra|d         t          j        j        k    sJ |d         }|d         t          j        j        k    sJ |d
         }||f}t*          |f| j        |<   dS t          |          dk    sJ t,          |ff| j        |<   dS g g g 
	
fd	 	|           t                    dk    r9t                    dk    r&t          
z             }t,          |ff| j        |<   dS t          z   
z             }t                    }||f}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   r      c              3   &   K   | ]}|d          V  dS r   Nr#   .0childs     r   	<genexpr>z*DataHandler.elementDecl.<locals>.<genexpr>  s&      (H(Heq(H(H(H(H(H(Hr   c              3   &   K   | ]}|d          V  dS rN  r#   rO  s     r   rR  z*DataHandler.elementDecl.<locals>.<genexpr>  s&      $D$D%U1X$D$D$D$D$D$Dr   r   c                 "   | dd          \  }}}|X|t           j        j        t           j        j        fv r"|D ]}                    |d                    d S |D ]} |           d S |                                dk    r                    |           d S |t           j        j        t           j        j        fv r                    |           d S |t           j        j        t           j        j        fv r                    |           d S d S )Nr   r   rK  )r   modelXML_CQUANT_PLUSXML_CQUANT_REPrL   upperXML_CQUANT_NONEXML_CQUANT_OPT)	rU  
quantifierr   childrenrQ  countr   rH  singles	        r   r]  z&DataHandler.elementDecl.<locals>.count  sI   (-abb	%JX{K/K."   "* 2 2 a11112 2 "* % %e% %''c"""""K/K."   MM#&&&&&K/K.$   OOC(((((	 r   )rX  r   addr   rU  XML_CTYPE_MIXEDrW  XML_CTYPE_NAMErY  r   XML_CTYPE_SEQXML_CTYPE_CHOICErZ  r   XML_CTYPE_EMPTYr  r   XML_CTYPE_ANYrR   r   rV  rZ   rA   )r   r}   rU  r\  rC   rT   r,   first_childr\   r]  r   rH  r^  s            @@@@r   elementDeclzDataHandler.elementDecl  s	    ::<<7""KOOD!!!F6>>eK'K&k(%+*EvrRT	(
 
 
 JNN4   F !H2EK4PQQQaU[8%+:TUUUE!H""!HQKE	 !H2EK4PQQQaU[8%+:TUUUE!H"" 83U[5PQQQQx5;555 8((H(Hx(H(H(HHH({{!-DLFU[.0KTSUVVVL M -0D'8$&?Dd#F8K(K%
 
 
 Ah5;68RSSSQxH$$D$D8$D$D$DDDLQx5;444x==1$$ 8u{'BBBBB"*1+K&q>U[-HHHHH +AI'2D/A4.HD%d+F8}}))))'2\O&DDd#F 	) 	) 	) 	) 	) 	) 	) 	)4 	ev;;!H 2 2$X%677L'2\O&DDd###$Vh%6%?@@L%h//M -0D'8$&?Dd###r   c                 R   t           j        Mt          j                            t           j        |          }	 t          |d          }|S # t          $ r Y nw xY wt          j                            t           j        |          }	 t          |d          }|S # t          $ r Y nw xY wdS )zOpen specified DTD file.Nr   )r   r   r   r   r   r   FileNotFoundErrorglobal_dtd_dirr   filenamer   r   s       r   open_dtd_filezDataHandler.open_dtd_file      $07<< 98DDDdD))  %    w||K6AA	$%%F M ! 	 	 	D	 t#   A
 

AAB 
B$#B$c                 R   t           j        Mt          j                            t           j        |          }	 t          |d          }|S # t          $ r Y nw xY wt          j                            t           j        |          }	 t          |d          }|S # t          $ r Y nw xY wdS )zOpen specified XSD file.Nr   )r   r   r   r   r   r   ri  global_xsd_dirrk  s       r   r   zDataHandler.open_xsd_file  rn  ro  c                 @   t           j        dS t          j                            t           j        |          }	 t          |d          }|                    |           |                                 dS # t          $ r t          j
        d| d|            Y dS w xY w)zSave DTD file to cache.NwbFailed to save  at )r   r   r   r   r   r   writer   OSErrorwarningswarnr   rl  r7   r   r   s        r   save_dtd_filezDataHandler.save_dtd_file-      $,Fw||K5x@@	$%%F LLLLNNNNN	  	B 	B 	BM@H@@$@@AAAAAA	B   A5 5$BBc                 @   t           j        dS t          j                            t           j        |          }	 t          |d          }|                    |           |                                 dS # t          $ r t          j
        d| d|            Y dS w xY w)zSave XSD file to cache.Nrs  rt  ru  )r   r   r   r   r   r   rv  r   rw  rx  ry  rz  s        r   r   zDataHandler.save_xsd_file:  r|  r}  c                    t          |          }|j        dv r|}n~|j        dk    r\	 | j        d         }t          j                            |          }n# t          $ r d}Y nw xY w|                    d          dz   |z   }nt          d|j        z            | j        	                    |           t          j        
                    |          \  }}	|                     |	          }
|
s	 t          |          }
n$# t          $ r t          d|	 d|           d	w xY w|
                                }|
                                 |                     |	|           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.
        )httphttpsftpr.  r_   z http://www.ncbi.nlm.nih.gov/dtd/r   zUnexpected URL scheme %rzFailed to access ru  Nr   )r   schemer   r   r   dirname
IndexErrorr   rK   rL   r  rm  r   rw  r   r   r   r{  r   r   ExternalEntityParserCreaterg  ElementDeclHandlerr   r   r   r   )r   contextrB  systemIdpublicIdurlinfor&  r   locationrl  r   r7   r   s                r   r   z$DataHandler.externalEntityRefHandlerG  s    8$$>555CC^r!!1r* 00  < < < << --$$s*X5CC7'.HIIIS!!!W]]844(##H-- 
	#V  V V V"#Jx#J#JS#J#JKKQUUV;;==DLLNNNx...T]]F77@@$($4!   *.*B'qs   A A$#A$.C> >!D))r   r    r!   r"   r   r   r   r   r   __path__rj  rq  r   r   r   r   r   r   r   r   r   r   r   r  r   r  r+  r!  r  r  r  r   r   r  r   rg  rm  r   r{  r   r   r#   r   r   r   r   a  s$       88W\\&/!"4f==NW\\&/!"4f==NMME E E2/ / /bU U UnO O O
 
 
0 0 02
/ 
/ 
/C C C(} } }~  6  ! ! !@  C C C! ! !&  ! ! !." " "" " "
4 4 4/2 /2 /2bq@ q@ q@f  &  &    2 2 2 2 2r   r   )	metaclass)'r"   r   rx  xml.etree.ElementTreeetreeElementTreer   collectionsr   ior   urllib.parser   urllib.requestr   xml.parsersr   xml.sax.saxutilsr   r   r	   r   r'   r%   r=   r:   rF   rA   rW   rR   rZ   ra   rK   rf   rv   rz   typer   r   r#   r   r   <module>r     s   @ 
			  " " " " " " " " "             ! ! ! ! ! ! " " " " " "       # # # # # #      !9 !9 !9 !9 !9 !9 !9 !9HD D D D DS D D D.C C C C CC C C C,    $   <# # # # # # # #L" " " " " " " "J' ' ' ' '3 ' ' '$
 
 
 
 
* 
 
 

 
 
 
 

 
 
 

 
 
 
 
j 
 
 
,% % % % %d % % %PX X X X XO X X X X X Xr   