o
    tBhH                      @   s   d dl mZmZmZ d dlmZ d dlZddlmZ ddl	m
Z
 G dd dejZee efd	d
ZefddZefddZG dd dejZeeej eeej eedej eeej dd ZdS )    )divisionabsolute_importprint_function)OrderedDictN   )ConfigReadError)
BASESTRINGc                       sF   e Zd ZdZdd Zdd ZdddZ fd	d
Zedd Z	  Z
S )LoaderzA customized YAML loader. This loader deviates from the official
    YAML spec in a few convenient ways:

    - All strings as are Unicode objects.
    - All maps are OrderedDicts.
    - Strings can begin with % without quotation.
    c                 C   s
   |  |S N)construct_scalar)selfnode r   h/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/confuse/yaml_util.py_construct_unicode   s   
zLoader._construct_unicodec                 c   s&    t  }|V  | |}|| d S r
   )r   construct_mappingupdate)r   r   datavaluer   r   r   construct_yaml_map   s
   
zLoader.construct_yaml_mapFc           	      C   s   t |tjr| | ntjd d d|j |jt }|j	D ]6\}}| j
||d}zt| W n tyI } ztjd|jd| |jd }~ww | j
||d}|||< q|S )Nz%expected a mapping node, but found %s)deepzwhile constructing a mappingzfound unacceptable key (%s))
isinstanceyamlMappingNodeflatten_mappingconstructorConstructorErrorid
start_markr   r   construct_objecthash	TypeError)	r   r   r   mappingkey_node
value_nodekeyexcr   r   r   r   r      s.   

zLoader.construct_mappingc                    s   t t|  }|p|  dkS )N%)superr	   check_plainpeek)r   plain	__class__r   r   r)   9   s   zLoader.check_plainc                 C   s.   |  dtj |  dtj |  dtj dS )zModify a PyYAML Loader class to add extra constructors for strings
        and maps. Call this method on a custom Loader class to make it behave
        like Confuse's own Loader
        ztag:yaml.org,2002:strztag:yaml.org,2002:mapztag:yaml.org,2002:omapN)add_constructorr	   r   r   )loaderr   r   r   add_constructors=   s   zLoader.add_constructors)F)__name__
__module____qualname____doc__r   r   r   r)   staticmethodr0   __classcell__r   r   r,   r   r	      s    
r	   c              
   C   sl   z t | d}tj||dW  d   W S 1 sw   Y  W dS  ttjjfy5 } zt| |d}~ww )a  Read a YAML document from a file. If the file cannot be read or
    parsed, a ConfigReadError is raised.
    loader is the PyYAML Loader class to use to parse the YAML. By default,
    this is Confuse's own Loader class, which is like SafeLoader with
    extra constructors.
    rbr	   N)openr   loadIOErrorerror	YAMLErrorr   )filenamer/   fr&   r   r   r   	load_yamlN   s   (
r@   c              
   C   s8   zt j| |dW S  t jjy } zt||d}~ww )a  Read a YAML document from a string. If the string cannot be parsed,
    a ConfigReadError is raised.
    `yaml_string` is a string to be parsed as a YAML document.
    `name` is the name to use in error messages.
    `loader` is the PyYAML Loader class to use to parse the YAML. By default,
    this is Confuse's own Loader class, which is like SafeLoader with
    extra constructors.
    r8   N)r   r:   r<   r=   r   )yaml_stringnamer/   r&   r   r   r   load_yaml_string\   s   	
rC   c                 C   sZ   t | ts| S z|d}|tj| d}t|| }||W S  tjjy,   |  Y S w )a  Parse a value as if it were a YAML scalar to perform type conversion
    that is consistent with YAML documents.
    `value` should be a string. Non-string inputs or strings that raise YAML
    errors will be returned unchanged.
    `Loader` is the PyYAML Loader class to use for parsing, defaulting to
    Confuse's own Loader class.

    Examples with the default Loader:
      - '1' will return 1 as an integer
      - '1.0' will return 1 as a float
      - 'true' will return True
      - The empty string '' will return None
     )TF)r   r   resolver   
ScalarNoder   r<   r=   )r   r/   tagr   r   r   r   parse_as_scalark   s   
rH   c                       s:   e Zd ZdZdddZ fddZdd Zd	d
 Z  ZS )DumperzaA PyYAML Dumper that represents OrderedDicts as ordinary mappings
    (in order, of course).
    Nc                 C   s   g }t j|||d}| jd ur|| j| j< d}t|dr"t| }|D ]+\}}| |}	| |}
t|	t j	r;|	j
r=d}t|
t j	rF|
j
rHd}||	|
f q$|d u rb| jd ur_| j|_|S ||_|S )N)
flow_styleFitems)r   r   	alias_keyrepresented_objectshasattrlistrK   represent_datar   rF   styleappenddefault_flow_stylerJ   )r   rG   r"   rJ   r   r   
best_styleitem_key
item_valuenode_key
node_valuer   r   r   represent_mapping   s2   




zDumper.represent_mappingc                    sH   t t| |}t|}| jdu r|dk rd|_|S | jdu r"d|_|S )z~If a list has less than 4 items, represent it in inline style
        (i.e. comma separated, within square brackets).
        N   TF)r(   rI   represent_listlenrS   rJ   )r   r   r   lengthr,   r   r   r[      s   
zDumper.represent_listc                 C   s   |rd}nd}|  d|S )zFRepresent bool as 'yes' or 'no' instead of 'true' or 'false'.
        yesnoztag:yaml.org,2002:boolrepresent_scalar)r   r   r   r   r   r   represent_bool   s   zDumper.represent_boolc                 C   s   |  ddS )z?Represent a None value with nothing instead of 'none'.
        ztag:yaml.org,2002:nullrD   r`   )r   r   r   r   r   represent_none   s   zDumper.represent_noner
   )	r1   r2   r3   r4   rY   r[   rb   rc   r6   r   r   r,   r   rI      s    
	rI   c           	      C   s   t  }t| }|D ]5}|sd}n|drd|}nq	 t|}|r+|ds+n|d|7 }q|dd  }|||< qt|  }d}|D ]}|dd  }||v r`||| 7 }|d|7 }qK|S )zScan default_data for comments (we include empty lines in our
    definition of comments) and place them before the same keys in data.
    Only works with comments that are on one or more own lines, i.e.
    not next to a yaml mapping.
    
#z{0}
T:r   rD   )dictiter
splitlines
startswithformatnextsplitstrip)	r   default_datacomment_mapdefault_lineslinecommentr%   	out_linesout_datar   r   r   restore_yaml_comments   s0   

rv   )
__future__r   r   r   collectionsr   r   
exceptionsr   utilr   
SafeLoaderr	   r0   r@   rC   rH   
SafeDumperrI   add_representerrepresent_dictboolrb   typerc   rO   r[   rv   r   r   r   r   <module>   s     
@9