3
5ÒÇI,$  ã               @   s®  d dl T d dlT d dlT d dlT d dlT d dlT yd dlT W n ek
rP   Y nX efdd„Z	efdd„Z
efdd„Zefdd	„Zefd
d„Zefdd„Zdd„ Zdd„ Zdedddddfdd„Zdeddddddddddfdd„Zdefdd„Zdeddddddddddddfdd„Zdefdd„Zd2dd„Zd3d d!„Zdeefd"d#„Zdeefd$d%„Zefd&d'„Zefd(d)„Zefd*d+„Zefd,d-„ZG d.d/„ d/eƒZ G d0d1„ d1e!ƒZ"dS )4é    )Ú*c             c   s$   || ƒ}x|j ƒ r|jƒ V  q
W dS )z9
    Scan a YAML stream and produce scanning tokens.
    N)Zcheck_tokenZ	get_token)ÚstreamÚLoaderÚloader© r   ú#/p/zhu/06/nlp/nltk/yaml/__init__.pyÚscan   s    
r   c             c   s$   || ƒ}x|j ƒ r|jƒ V  q
W dS )z9
    Parse a YAML stream and produce parsing events.
    N)Zcheck_eventZ	get_event)r   r   r   r   r   r   Úparse   s    
r	   c             C   s   || ƒ}|j ƒ r|jƒ S dS )zj
    Parse the first YAML document in a stream
    and produce the corresponding representation tree.
    N)Ú
check_nodeÚget_node)r   r   r   r   r   r   Úcompose    s    r   c             c   s$   || ƒ}x|j ƒ r|jƒ V  q
W dS )zb
    Parse all YAML documents in a stream
    and produce corresponsing representation trees.
    N)r
   r   )r   r   r   r   r   r   Úcompose_all)   s    
r   c             c   s$   || ƒ}x|j ƒ r|jƒ V  q
W dS )z\
    Parse all YAML documents in a stream
    and produce corresponding Python objects.
    N)Ú
check_dataÚget_data)r   r   r   r   r   r   Úload_all2   s    
r   c             C   s   || ƒ}|j ƒ r|jƒ S dS )zd
    Parse the first YAML document in a stream
    and produce the corresponding Python object.
    N)r   r   )r   r   r   r   r   r   Úload;   s    r   c             C   s
   t | tƒS )z~
    Parse all YAML documents in a stream
    and produce corresponding Python objects.
    Resolve only basic YAML tags.
    )r   Ú
SafeLoader)r   r   r   r   Úsafe_load_allD   s    r   c             C   s
   t | tƒS )z†
    Parse the first YAML document in a stream
    and produce the corresponding Python object.
    Resolve only basic YAML tags.
    )r   r   )r   r   r   r   Ú	safe_loadL   s    r   Nc             C   s„   d}|dkrJyddl m}	 W n  tk
r<   ddlm}	 Y nX |	ƒ }|j}|||||||d}
x| D ]}|
j|ƒ qdW |r€|ƒ S dS )zl
    Emit YAML parsing events into a stream.
    If stream is None, return the produced string instead.
    Nr   )ÚStringIO)Ú	canonicalÚindentÚwidthÚallow_unicodeÚ
line_break)Ú	cStringIOr   ÚImportErrorÚgetvalueÚemit)Úeventsr   ÚDumperr   r   r   r   r   r   r   ÚdumperÚeventr   r   r   r   T   s    


r   zutf-8c             C   sž   d}|dkrJyddl m} W n  tk
r<   ddlm} Y nX |ƒ }|j}|||||||||||	|
d}|jƒ  x| D ]}|j|ƒ qvW |jƒ  |rš|ƒ S dS )z…
    Serialize a sequence of representation trees into a YAML stream.
    If stream is None, return the produced string instead.
    Nr   )r   )
r   r   r   r   r   ÚencodingÚversionÚtagsÚexplicit_startÚexplicit_end)r   r   r   r   ÚopenÚ	serializeÚclose)Únodesr   r    r   r   r   r   r   r#   r&   r'   r$   r%   r   r   r!   Únoder   r   r   Úserialize_allj   s$    	


r-   c             K   s   t | g|fd|i|—ŽS )zx
    Serialize a representation tree into a YAML stream.
    If stream is None, return the produced string instead.
    r    )r-   )r,   r   r    Úkwdsr   r   r   r)   †   s    r)   c             C   s¢   d}|dkrJyddl m} W n  tk
r<   ddlm} Y nX |ƒ }|j}|||||||||	|
||||d}|jƒ  x| D ]}|j|ƒ qzW |jƒ  |rž|ƒ S dS )z
    Serialize a sequence of Python objects into a YAML stream.
    If stream is None, return the produced string instead.
    Nr   )r   )Údefault_styleÚdefault_flow_styler   r   r   r   r   r#   r$   r%   r&   r'   )r   r   r   r   r(   Z	representr*   )Ú	documentsr   r    r/   r0   r   r   r   r   r   r#   r&   r'   r$   r%   r   r   r!   Údatar   r   r   Údump_all   s(    


r3   c             K   s   t | g|fd|i|—ŽS )zr
    Serialize a Python object into a YAML stream.
    If stream is None, return the produced string instead.
    r    )r3   )r2   r   r    r.   r   r   r   Údump¬   s    r4   c             K   s   t | |fdti|—ŽS )z¡
    Serialize a sequence of Python objects into a YAML stream.
    Produce only basic YAML tags.
    If stream is None, return the produced string instead.
    r    )r3   Ú
SafeDumper)r1   r   r.   r   r   r   Úsafe_dump_all³   s    r6   c             K   s   t | g|fdti|—ŽS )z”
    Serialize a Python object into a YAML stream.
    Produce only basic YAML tags.
    If stream is None, return the produced string instead.
    r    )r3   r5   )r2   r   r.   r   r   r   Ú	safe_dump»   s    r7   c             C   s    |j | ||ƒ |j | ||ƒ dS )zÙ
    Add an implicit scalar detector.
    If an implicit scalar value matches the given regexp,
    the corresponding tag is assigned to the scalar.
    first is a sequence of possible initial characters or None.
    N)Úadd_implicit_resolver)ÚtagÚregexpÚfirstr   r    r   r   r   r8   Ã   s    r8   c             C   s    |j | ||ƒ |j | ||ƒ dS )zÁ
    Add a path based resolver for the given tag.
    A path is a list of keys that forms a path
    to a node in the representation tree.
    Keys can be string values, integers, or None.
    N)Úadd_path_resolver)r9   ÚpathÚkindr   r    r   r   r   r<   Î   s    r<   c             C   s   |j | |ƒ dS )z¯
    Add a constructor for the given tag.
    Constructor is a function that accepts a Loader instance
    and a node object and produces the corresponding Python object.
    N)Úadd_constructor)r9   Úconstructorr   r   r   r   r?   Ø   s    r?   c             C   s   |j | |ƒ dS )a  
    Add a multi-constructor for the given tag prefix.
    Multi-constructor is called for a node if its tag starts with tag_prefix.
    Multi-constructor accepts a Loader instance, a tag suffix,
    and a node object and produces the corresponding Python object.
    N)Úadd_multi_constructor)Z
tag_prefixZmulti_constructorr   r   r   r   rA   à   s    rA   c             C   s   |j | |ƒ dS )zÍ
    Add a representer for the given type.
    Representer is a function accepting a Dumper instance
    and an instance of the given data type
    and producing the corresponding representation node.
    N)Úadd_representer)Ú	data_typeZrepresenterr    r   r   r   rB   é   s    rB   c             C   s   |j | |ƒ dS )zÞ
    Add a representer for the given type.
    Multi-representer is a function accepting a Dumper instance
    and an instance of the given data type or subtype
    and producing the corresponding representation node.
    N)Úadd_multi_representer)rC   Zmulti_representerr    r   r   r   rD   ò   s    rD   c                   s    e Zd ZdZ‡ fdd„Z‡  ZS )ÚYAMLObjectMetaclassz'
    The metaclass for YAMLObject.
    c                sN   t t| ƒj|||ƒ d|krJ|d d k	rJ| jj| j| jƒ | jj| | j	ƒ d S )NÚyaml_tag)
ÚsuperrE   Ú__init__Úyaml_loaderr?   rF   Ú	from_yamlÚyaml_dumperrB   Úto_yaml)ÚclsÚnameÚbasesr.   )Ú	__class__r   r   rH   ÿ   s    zYAMLObjectMetaclass.__init__)Ú__name__Ú
__module__Ú__qualname__Ú__doc__rH   Ú__classcell__r   r   )rP   r   rE   û   s   rE   c               @   sD   e Zd ZdZeZeZeZ	dZ
dZdd„ ZeeƒZdd„ ZeeƒZdS )Ú
YAMLObjectza
    An object that can dump itself to a YAML stream
    and load itself from a YAML stream.
    Nc             C   s   |j || ƒS )zC
        Convert a representation node to a Python object.
        )Zconstruct_yaml_object)rM   r   r,   r   r   r   rJ     s    zYAMLObject.from_yamlc             C   s   |j | j|| | jdS )zC
        Convert a Python object to a representation node.
        )Z
flow_style)Zrepresent_yaml_objectrF   Úyaml_flow_style)rM   r!   r2   r   r   r   rL     s    zYAMLObject.to_yaml)rQ   rR   rS   rT   rE   Ú__metaclass__r   rI   r    rK   rF   rW   rJ   ÚclassmethodrL   r   r   r   r   rV     s   rV   )N)N)#ÚerrorÚtokensr   r+   r   r!   Zcyamlr   r   r   r	   r   r   r   r   r   r   r    r   r-   r)   r3   r4   r6   r7   r8   r<   r?   rA   rB   rD   ÚtyperE   ÚobjectrV   r   r   r   r   Ú<module>   sX   				



			
