B
    ÔùÓ`ÈD  ã               @   sl  d Z ddlZddlZddlZddlZddlZddlm  mZ	 ddl
mZ ddlmZ ddlmZmZmZmZ edd	d
d�Ze d¡Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zefdd„Zdd„ Zd7dd„Zefdd„Zedfdd „Z ed
fd!d"„Z!efd#d$„Z"d%d&„ Z#edfd'd(„Z$d)d*„ Z%d+d,„ Z&d-d.„ Z'd8d/d0„Z(dZ)d9d1d2„Z*d3d4„ Z+G d5d6„ d6ej,ej-ej.ƒZ,dS ):a}  XML backend:

- Format to support: XML, e.g. http://www.w3.org/TR/xml11/
- Requirements: one of the followings

  - xml.etree.cElementTree in standard lib if python >= 2.5
  - xml.etree.ElementTree in standard lib if python >= 2.5
  - elementtree.ElementTree (otherwise)

- Development Status :: 4 - Beta
- Limitations:

  - special node '@attrs', '@text' and '@children' are used to keep XML
    structure of original data. You have to cusomize them with 'tags' keyword
    option to avoid any config parameters conflict with some of them.

  - Some data or structures of original XML file may be lost if make it backed
    to XML file; XML file - (anyconfig.load) -> config - (anyconfig.dump) ->
    XML file

  - XML specific features (namespace, etc.) may not be processed correctly.

- Special Options:

  - ac_parse_value: Try to parse values, elements' text and attributes.

  - merge_attrs: Merge attributes and mix with children nodes. Please note that
    information of attributes are lost after load if this option is used.

  - tags: A dict provide special parameter names to distinguish between
    attributes, text and children nodes. Default is {"attrs": "@attrs", "text":
    "@text", "children": "@children"}.

Changelog:

.. versionchanged:: 0.8.2

   - Add special options, tags, merge_attrs and ac_parse_value
   - Remove special option, pprefix which conflicts with another option tags

.. versionchanged:: 0.8.0

   - Try to make a nested dict w/o extra dict having keys of attrs, text and
     children from XML string/file as much as possible.
   - Support namespaces partially.

.. versionchanged:: 0.1.0

   - Added XML dump support.
é    Né   )Úbaseé   )Úparse_single)Úget_path_from_streamÚis_dict_likeÚis_iterableÚnoopz@attrsú@textz	@children)ÚattrsÚtextÚchildrenz^{(\S+)}(\S+)$c             C   s2   yt j| dd�S  tk
r,   t j| dd�S X dS )z~
    Avoid bug in python 3.{2,3}. See http://bugs.python.org/issue9257.

    :param xmlfile: XML file or file-like object
    )zstart-ns)Úevents)s   start-nsN)ÚETÚ	iterparseÚ	TypeError)Úxmlfile© r   ú9/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/backend/xml.pyÚ
_iterparseM   s    r   c             C   s   | d | d fS )z%
    >>> flip((1, 2))
    (2, 1)
    r   r   r   )Ztplr   r   r   ÚflipY   s    r   c             C   s   t dd„ t| ƒD ƒƒS )zk
    :param xmlfile: XML file or file-like object
    :return: {namespace_uri: namespace_prefix} or {}
    c             s   s   | ]\}}t |ƒV  qd S )N)r   )Ú.0Ú_Útr   r   r   ú	<genexpr>f   s    z(_namespaces_from_file.<locals>.<genexpr>)Údictr   )r   r   r   r   Ú_namespaces_from_filea   s    r   c             K   sP   |  dd¡}|dk	rLt | ¡}|rL| ¡ \}} |  |d¡}|rL|› d| › �S | S )aÓ  
    :param tag: XML tag element
    :param nspaces: A namespaces dict, {uri: prefix}
    :param options: Extra keyword options may contain 'nspaces' keyword option
        provide a namespace dict, {uri: prefix}

    >>> _tweak_ns("a", nspaces={})
    'a'
    >>> _tweak_ns("a", nspaces={"http://example.com/ns/val/": "val"})
    'a'
    >>> _tweak_ns("{http://example.com/ns/val/}a",
    ...           nspaces={"http://example.com/ns/val/": "val"})
    'val:a'
    ÚnspacesNFú:)ÚgetÚ	_ET_NS_REÚmatchÚgroups)ÚtagÚoptionsr   ÚmatchedÚuriÚprefixr   r   r   Ú	_tweak_nsi   s    
r(   c             C   s4   t j dd„ | D ƒ¡}tt|ƒƒtdd„ | D ƒƒkS )aZ  
    :param dics: [<dict or dict-like object>], must not be [] or [{...}]
    :return: True if all keys of each dict of 'dics' are unique

    # Enable the followings if to allow dics is [], [{...}]:
    # >>> all(_dicts_have_unique_keys([d]) for [d]
    # ...     in ({}, {'a': 0}, {'a': 1, 'b': 0}))
    # True
    # >>> _dicts_have_unique_keys([{}, {'a': 1}, {'b': 2, 'c': 0}])
    # True

    >>> _dicts_have_unique_keys([{}, {'a': 1}, {'a': 2}])
    False
    >>> _dicts_have_unique_keys([{}, {'a': 1}, {'b': 2}, {'b': 3, 'c': 0}])
    False
    >>> _dicts_have_unique_keys([{}, {}])
    True
    c             s   s   | ]}|  ¡ V  qd S )N)Úkeys)r   Údr   r   r   r   —   s    z*_dicts_have_unique_keys.<locals>.<genexpr>c             s   s   | ]}t |ƒV  qd S )N)Úlen)r   r*   r   r   r   r   ˜   s    )Ú	itertoolsÚchainÚfrom_iterabler+   ÚsetÚsum)ÚdicsZkey_itrr   r   r   Ú_dicts_have_unique_keys„   s    r2   c             C   s$   t j dd„ | D ƒ¡}|t |¡ƒS )a^  
    :param dics: [<dict/-like object must not have same keys each other>]
    :param container: callble to make a container object
    :return: <container> object

    >>> _merge_dicts(({}, ))
    {}
    >>> _merge_dicts(({'a': 1}, ))
    {'a': 1}
    >>> sorted(kv for kv in _merge_dicts(({'a': 1}, {'b': 2})).items())
    [('a', 1), ('b', 2)]
    c             s   s   | ]}|  ¡ V  qd S )N)Úitems)r   r*   r   r   r   r   ¨   s    z_merge_dicts.<locals>.<genexpr>)r,   r-   r.   ÚcollectionsÚOrderedDict)r1   Ú	containerZdic_itrr   r   r   Ú_merge_dicts›   s    r7   c             K   s   | r|  dd¡rt| ƒS | S )zK
    :return: Parsed value or value itself depends on 'ac_parse_value'
    Úac_parse_valueF)r   r   )Úvalr$   r   r   r   Ú_parse_text¬   s    r:   c             K   sF   | j  ¡ | _ | j rBt| j f|Ž}t| ƒs.| jr8|||< n
||| j< dS )a�  
    :param elem: ET Element object which has elem.text
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating elem.text, dic and subdic as side effects
    N)r   Ústripr:   r+   Úattribr#   )ÚelemÚdicÚsubdicr   r$   Zetextr   r   r   Ú_process_elem_text¶   s    
r@   c                sJ   t ‡ fdd„| j ¡ D ƒƒ}ˆ  dd¡rB|t dd„ | ¡ D ƒƒƒS ||ƒS )zÄ
    :param elem: ET Element object has attributes (elem.attrib)
    :param container: callble to make a container object
    :return: Parsed value or value itself depends on 'ac_parse_value'
    c             3   s"   | ]\}}t |fˆ Ž|fV  qd S )N)r(   )r   ÚaÚv)r$   r   r   r   Ð   s    z_parse_attrs.<locals>.<genexpr>r8   Fc             s   s   | ]\}}|t |ƒfV  qd S )N)r   )r   ÚkrB   r   r   r   r   Ò   s   )r   r<   r3   r   )r=   r6   r$   Úadicr   )r$   r   Ú_parse_attrsÊ   s
    
rE   c             K   sD   t | fd|i|—Ž}| js8t| ƒs8| d¡r8||| j< n|||< dS )aj  
    :param elem: ET Element object or None
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating dic and subdic as side effects
    r6   Úmerge_attrsN)rE   r   r+   r   r#   )r=   r>   r?   r6   r   r$   rD   r   r   r   Ú_process_elem_attrsØ   s    rG   c       	         sn   ‡ ‡fdd„| D ƒ}ˆ  dd¡}|r.ˆ | jƒn|g| }t|ƒrRt|ˆ ƒ|| j< n|sb||| j< n|||< dS )aÏ  
    :param elem: ET Element object or None
    :param dic: <container> (dict[-like]) object converted from elem
    :param subdic: Sub <container> object converted from elem
    :param container: callble to make a container object
    :param children: Tag for children nodes
    :param options:
        Keyword options, see the description of :func:`elem_to_container` for
        more details.

    :return: None but updating dic and subdic as side effects
    c                s    g | ]}t |fd ˆ iˆ—Ž‘qS )r6   )Úelem_to_container)r   Úc)r6   r$   r   r   ú
<listcomp>ù   s   z+_process_children_elems.<locals>.<listcomp>rF   FN)r   r<   r2   r7   r#   )	r=   r>   r?   r6   r   r$   ZcdicsrF   Zsdicsr   )r6   r$   r   Ú_process_children_elemsë   s    rK   c             K   sš   |ƒ }| dkr|S t | jf|Ž| _|ƒ  }|| j< ||d< | jrPt| ||f|Ž | jrft| ||f|Ž t| ƒr€t| ||f|Ž n| js–| js–d|| j< |S )as  
    Convert XML ElementTree Element to a collection of container objects.

    Elements are transformed to a node under special tagged nodes, attrs, text
    and children, to store the type of these elements basically, however, in
    some special cases like the followings, these nodes are attached to the
    parent node directly for later convenience.

    - There is only text element
    - There are only children elements each has unique keys among all

    :param elem: ET Element object or None
    :param container: callble to make a container object
    :param options: Keyword options

        - nspaces: A namespaces dict, {uri: prefix} or None
        - attrs, text, children: Tags for special nodes to keep XML info
        - merge_attrs: Merge attributes and mix with children nodes, and the
          information of attributes are lost after its transformation.
    Nr6   )r(   r#   r   r@   r<   rG   r+   rK   )r=   r6   r$   r>   r?   r   r   r   rH     s    
rH   c                sL   t ‡ fdd„tD ƒƒsHˆ  di ¡}x$t ¡ D ]\}}| ||¡ˆ |< q,W ˆ S )aL  
    :param options: Keyword options :: dict

    >>> ref = _TAGS.copy()
    >>> ref["text"] = "#text"
    >>> opts = _complement_tag_options({"tags": {"text": ref["text"]}})
    >>> del opts["tags"]  # To simplify comparison.
    >>> sorted(opts.items())
    [('attrs', '@attrs'), ('children', '@children'), ('text', '#text')]
    c             3   s   | ]}|ˆ kV  qd S )Nr   )r   Únt)r$   r   r   r   <  s    z*_complement_tag_options.<locals>.<genexpr>Útags)ÚallÚ_TAGSr   r3   )r$   rM   Zntyper#   r   )r$   r   Ú_complement_tag_options1  s
    rP   c             K   sb   |ƒ }| dkr|S |dk	rHx,|  ¡ D ] \}}|| j|r@d|› �nd< q$W t| f||dœt|ƒ—ŽS )a¶  
    Convert XML ElementTree Root Element to a collection of container objects.

    :param root: etree root object or None
    :param container: callble to make a container object
    :param nspaces: A namespaces dict, {uri: prefix} or None
    :param options: Keyword options,

        - tags: Dict of tags for special nodes to keep XML info, attributes,
          text and children nodes, e.g. {"attrs": "@attrs", "text": "#text"}
    Nzxmlns:Úxmlns)r6   r   )r3   r<   rH   rP   )Úrootr6   r   r$   Útreer&   r'   r   r   r   Úroot_to_containerD  s    rT   c              K   s   |   d¡rtS tS )z€
    :param options: Keyword options might have 'ac_parse_value' key
    :param to_str: Callable to convert value to string
    r8   )r   Ústrr	   )r$   r   r   r   Ú
_to_str_fn\  s    rV   c             C   s*   x$|   ¡ D ]\}}| |||ƒ¡ q
W dS )a0  
    :param obj: Container instance gives attributes of XML Element
    :param parent: XML ElementTree parent node object
    :param to_str: Callable to convert value to string or None
    :param options: Keyword options, see :func:`container_to_etree`

    :return: None but parent will be modified
    N)r3   r/   )ÚobjÚparentÚto_strÚattrr9   r   r   r   Ú_elem_set_attrsd  s    	r[   c             k   sL   xF| D ]>}x8|  ¡ D ],\}}t |¡}t|fd|i|—Ž |V  qW qW dS )z�
    :param children_nodes: A list of child dict objects
    :param options: Keyword options, see :func:`container_to_etree`
    rX   N)r3   r   ÚElementÚcontainer_to_etree)Zchildren_nodesr$   ÚchildZckeyÚcvalÚcelemr   r   r   Ú_elem_from_descendantsq  s
    

ra   c             K   sZ   t  | ¡}t|ƒr|n|g}x"|D ]}t|f||dœ|—Ž q"W |dkrL|S | |¡ |S )aF  
    :param key: Key of current child (dict{,-like} object)
    :param val: Value of current child (dict{,-like} object or [dict{,...}])
    :param to_str: Callable to convert value to string
    :param parent: XML ElementTree parent node object or None
    :param options: Keyword options, see :func:`container_to_etree`
    )rX   rY   N)r   r\   r   r]   Úappend)Úkeyr9   rY   rX   r$   r=   ÚvalsZval_r   r   r   Ú_get_or_update_parent}  s    


re   c       
      K   sÜ   |dkrt f |Ž}t| ƒs4|dk	r0| r0|| ƒ|_|S t|ƒ}tjtŽ |ƒ\}}}x€|  ¡ D ]t\}}||krxt|||ƒ qZ||krŒ||ƒ|_qZ||kr¶x8t	|f|ŽD ]}	| 
|	¡ q¢W qZt|||fd|i|—Ž}qZW t |¡S )a§  
    Convert a dict-like object to XML ElementTree.

    :param obj: Container instance to convert to
    :param parent: XML ElementTree parent node object or None
    :param to_str: Callable to convert value to string or None
    :param options: Keyword options,

        - tags: Dict of tags for special nodes to keep XML info, attributes,
          text and children nodes, e.g. {"attrs": "@attrs", "text": "#text"}
    NrX   )rV   r   r   rP   ÚoperatorÚ
itemgetterÚ_ATCr3   r[   ra   rb   re   r   ÚElementTree)
rW   rX   rY   r$   r   r   r   rc   r9   r`   r   r   r   r]   •  s&    

r]   c             C   s>   y| j |ddd� W n$ tk
r8   | j |ddd� Y nX dS )zŸ
    Write XML ElementTree 'root' content into 'stream'.

    :param tree: XML ElementTree object
    :param stream: File or file-like object can write to
    zutf-8T)ÚencodingÚxml_declarationÚunicodeN)Úwriter   )rS   Ústreamr   r   r   Úetree_write»  s    ro   c               @   s^   e Zd ZdZdZdZdgZdddg ZZdZ	dgZ
dd	„ Zd
d„ Zdd„ Zdd„ Zdd„ ZdS )ÚParserz
    Parser for XML files.
    ÚxmlrM   rF   r8   TZac_dictc             K   s2   t  |¡}t |¡}t|ƒ}t|f||dœ|—ŽS )a\  
        Load config from XML snippet (a string 'content').

        :param content:
            XML snippet string of str (python 2) or bytes (python 3) type
        :param container: callble to make a container object
        :param opts: optional keyword parameters passed to

        :return: Dict-like object holding config parameters
        )r6   r   )r   Ú
fromstringÚioÚBytesIOr   rT   )ÚselfÚcontentr6   ÚoptsrR   rn   r   r   r   r   Úload_from_stringÓ  s
    

zParser.load_from_stringc             K   s,   t  |¡ ¡ }t|ƒ}t|f||dœ|—ŽS )zë
        :param filepath: XML file path
        :param container: callble to make a container object
        :param opts: optional keyword parameters to be sanitized

        :return: Dict-like object holding config parameters
        )r6   r   )r   ÚparseÚgetrootr   rT   )ru   Úfilepathr6   rw   rR   r   r   r   r   Úload_from_pathä  s    zParser.load_from_pathc             K   s4   t  |¡ ¡ }t|ƒ}t|ƒ}t|f||dœ|—ŽS )zø
        :param stream: XML file or file-like object
        :param container: callble to make a container object
        :param opts: optional keyword parameters to be sanitized

        :return: Dict-like object holding config parameters
        )r6   r   )r   ry   rz   r   r   rT   )ru   rn   r6   rw   rR   Úpathr   r   r   r   Úload_from_streamñ  s
    zParser.load_from_streamc             K   s&   t |f|Ž}t ¡ }t||ƒ | ¡ S )zŸ
        :param cnf: Configuration data to dump
        :param opts: optional keyword parameters

        :return: string represents the configuration
        )r]   rs   rt   ro   Úgetvalue)ru   Úcnfrw   rS   Úbufr   r   r   Údump_to_stringÿ  s    
zParser.dump_to_stringc             K   s   t |f|Ž}t||ƒ dS )z©
        :param cnf: Configuration data to dump
        :param stream: Config file or file like object write to
        :param opts: optional keyword parameters
        N)r]   ro   )ru   r€   rn   rw   rS   r   r   r   Údump_to_stream  s    zParser.dump_to_streamN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_cidÚ_typeZ_extensionsZ
_load_optsZ
_dump_optsZ_orderedZ
_dict_optsrx   r|   r~   r‚   rƒ   r   r   r   r   rp   È  s   rp   )r
   )N)NN)/r‡   r4   rs   r,   rf   ÚreÚxml.etree.ElementTreeÚetreeri   r   Ú r   Úparserr   Úutilsr   r   r   r	   r   rO   Úcompiler    r   r   r   r(   r2   r7   r:   r@   rE   rG   rK   rH   rP   rT   rV   r[   ra   re   rh   r]   ro   rp   ZToStreamDumperMixinZBinaryFilesMixinr   r   r   r   Ú<module>:   sD   


+

&