a
    �Ï?e4  ã                   @   sÖ   d Z ddlZzddlmZmZ W n" eyB   ddlmZmZ Y n0 ddlm	Z	 ddl
mZ dd	lmZ d
dlmZ ejjjZeefdd„Zefdd„Zdd„ Zdd„ Zefdd„Zefdd„ZG dd„ dejƒZdS )aÕ  A backend module to load and dump YAML data files using yaml lib.

- Format to support: YAML, http://yaml.org
- Requirements: PyYAML (yaml), http://pyyaml.org
- Development Status :: 5 - Production/Stable
- Limitations:

  - Resuls is not ordered even if 'ac_ordered' or 'ac_dict' was given.

- Special options:

  - All keyword options of yaml.safe_load, yaml.load, yaml.safe_dump and
    yaml.dump should work.

  - Use 'ac_safe' boolean keyword option if you prefer to call yaml.safe_load
    and yaml.safe_dump instead of yaml.load and yaml.dump. Please note that
    this option conflicts with 'ac_dict' option and these options cannot be
    used at the same time.

  - See also: http://pyyaml.org/wiki/PyYAMLDocumentation

Changelog:

.. versionchanged:: 0.9.6

   - Add support of loading primitives other than mapping objects.

.. versionchanged:: 0.3

   - Changed special keyword option 'ac_safe' from 'safe' to avoid
     possibility of option conflicts in the future.
é    N)ÚCSafeLoaderÚCDumper)Ú
SafeLoaderÚDumperé   )Ú
convert_to)Úis_dict_likeé   )Úbaseé   )Úcommonc                    sZ   d‡ fdd„	}d}dd„ }z|  ||¡ W n ty<   Y n0 tˆ ƒtkrV|  ||¡ |S )a  Get the customized loader.

    Create or update loader with making given callble 'container' to make
    mapping objects such as dict and OrderedDict, used to construct python
    object from yaml mapping node internally.

    :param container: Set container used internally
    Fc           
         sÆ   |   |¡ t|tjƒs2tj ddd|j› �|j¡‚ˆ ƒ }|jD ]‚\}}| j	||d�}zt
|ƒ W nH ty¨ } z0d|jd|›d�|jf}tjj|Ž ‚W Y d}~n
d}~0 0 | j	||d�}	|	||< q>|S )z˜Construct python object from yaml mapping node.

        It is based on :meth:`yaml.BaseConstructor.construct_mapping` in PyYAML
        (MIT).
        Nz#expected a mapping node, but found )Údeepzwhile constructing a mappingzfound unacceptable key (ú))Zflatten_mappingÚ
isinstanceÚyamlZMappingNodeÚconstructorZConstructorErrorÚidZ
start_markÚvalueZconstruct_objectÚhashÚ	TypeError)
ÚloaderÚnoder   ÚmappingZkey_nodeZ
value_nodeÚkeyÚexcZeargsr   ©Ú	container© ú`/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/backend/yaml/pyyaml.pyÚconstruct_mapping?   s*    
þ
ý"
z-_customized_loader.<locals>.construct_mappingú tag:yaml.org,2002:python/unicodec                 S   s
   |   |¡S )zUnicode string constructor.)Zconstruct_scalar)r   r   r   r   r   Úconstruct_ustr]   s    z*_customized_loader.<locals>.construct_ustr)F)Zadd_constructorÚ	NameErrorÚtypeÚdict)r   r   Úmapping_tagr   Útagr!   r   r   r   Ú_customized_loader6   s    	r'   c                 C   sT   t fdd„}dd„ }z| t|¡ W n ty6   Y n0 t| ƒtkrP| | |¡ |S )z6Counterpart of :func:`_customized_loader` for dumpers.c                 S   s   |   || ¡ ¡S )zContainer representer.)Zrepresent_mappingÚitems)ÚdumperÚdatar%   r   r   r   Úcontainer_representerm   s    z1_customized_dumper.<locals>.container_representerc                 S   s   d}|   ||¡S )zUnicode string representer.r    )Zrepresent_scalar)r)   r*   r&   r   r   r   Úustr_representerq   s    z,_customized_dumper.<locals>.ustr_representer)Ú_MAPPING_TAGZadd_representerÚunicoder"   r#   r$   )r   r)   r+   r,   r   r   r   Ú_customized_dumperk   s    r/   c                 K   s   t t| d¡rd| › �n| ƒS )zÿGet yaml loading/dumping function by name.

    :param fname:
        "load" or "dump", not checked but it should be OK.
        see also :func:`yml_load` and :func:`yml_dump`
    :param options: keyword args may contain "ac_safe" to load/dump safely
    Úac_safeZsafe_)Úgetattrr   Úget)ÚfnameÚoptionsr   r   r   Úyml_fnc_by_name€   s    r5   c                 O   s&   t | fi |¤Ž}||i t d|¡¤ŽS )aO  Call yaml.safe_load, yaml.load, yaml.safe_dump and yaml.dump.

    :param fname:
        "load" or "dump", not checked but it should be OK.
        see also :func:`yml_load` and :func:`yml_dump`
    :param args: [stream] for load or [cnf, stream] for dump
    :param options: keyword args may contain "ac_safe" to load/dump safely
    r0   )r5   r   Úfilter_from_options)r3   Úargsr4   Zfncr   r   r   Úyml_fnc_‹   s    	r8   c                 K   sx   |  dd¡rtdd�}n4|  dd¡sL|  dd¡}|r@t|ƒr@|}t|ƒ|d< |d| fi t d|¡¤Ž}|du rt|ƒ S |S )	zÁCall yaml.safe_load and yaml.load.

    :param stream: a file or file-like object to load YAML content
    :param container: callble to make a container object

    :return: Mapping object
    r0   FT©r0   ÚLoaderÚac_dictÚloadN)r2   r$   Úcallabler'   r   r6   )Ústreamr   Úyml_fncr4   Úmaybe_containerÚretr   r   r   Úyml_load˜   s    

ÿrB   c                 K   s|   t | ƒ}| dd¡r tdd�}n,| dd¡sL|rL| dt| ƒ¡}t|ƒ|d< |rht| td�} t d|¡}|d| |fi |¤ŽS )	z‹Call yaml.safe_dump and yaml.dump.

    :param data: Some data to dump
    :param stream: a file or file-like object to dump YAML data
    r0   FTr9   r   r;   )r;   Údump)r   r2   r$   r#   r/   r   r   r6   )r*   r>   r?   r4   Z_is_dictr@   r   r   r   Úyml_dump³   s    rD   c                   @   s<   e Zd ZdZdZdZg d¢Zg d¢Ze 	e
¡Ze 	e¡ZdS )ÚParserzParser for YAML files.Zpyyamlé   )r:   r0   r;   )r>   r0   r   Zdefault_styleZdefault_flow_styleÚ	canonicalÚindentÚwidthZallow_unicodeZ
line_breakÚencodingZexplicit_startZexplicit_endÚversionÚtagsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_cidZ	_priorityZ
_load_optsZ
_dump_optsr
   Z	to_methodrB   Zload_from_streamrD   Zdump_to_streamr   r   r   r   rE   Ë   s   
rE   )rP   r   r   r:   r   r   ÚImportErrorr   Zdictsr   Úutilsr   Ú r
   r   ÚresolverÚBaseResolverZDEFAULT_MAPPING_TAGr-   r'   r/   r5   r8   rB   rD   rE   r   r   r   r   Ú<module>   s"    
5