B
    ÔùÓ`Õ  ã               @   sN  U d Z ddlZddlZddlZe d¡Zejed< e dej¡Z	ejed< e d¡Z
ejed< ejeeef Zeje Zeed	œd
d„Zdeeedœdd„Zejeejeef f Zdeeeeeje dœdd„Zdeeeeeje dœdd„Zejeejeef f Zdeeeeedœdd„Zejeeef Zd eeeeeedœdd„ZdS )!zMisc parsers
é    Nz^(\d|([1-9]\d+))$ÚINT_PATTERNz^(true|false)$ÚBOOL_PATTERNz^['\"](.*)['\"]$ÚSTR_PATTERN)Ústr_Úreturnc             C   sf   | dkrdS |   ¡ } | sdS t | ¡dk	r2t| ƒS t | ¡dk	rHt| ƒS t | ¡dk	rb| dd… S | S )aJ  
    Very simple parser to parse expressions represent some single values.

    :param str_: a string to parse
    :return: Int | Bool | String

    >>> parse_single(None)
    ''
    >>> parse_single('0')
    0
    >>> parse_single('123')
    123
    >>> parse_single('True')
    True
    >>> parse_single('a string')
    'a string'
    >>> parse_single('"a string"')
    'a string'
    >>> parse_single("'a string'")
    'a string'
    >>> parse_single('0.1')
    '0.1'
    >>> parse_single('    a string contains extra whitespaces     ')
    'a string contains extra whitespaces'
    NÚ é   éÿÿÿÿ)Ústripr   ÚmatchÚboolr   Úintr   )r   © r   ú4/tmp/pip-unpacked-wheel-bpftjxoo/anyconfig/parser.pyÚparse_single   s    r   ú,)r   Úsepr   c             C   s   dd„ |   |¡D ƒS )av  
    Simple parser to parse expressions reprensent some list values.

    :param str_: a string to parse
    :param sep: Char to separate items of list
    :return: [Int | Bool | String]

    >>> parse_list('')
    []
    >>> parse_list('1')
    [1]
    >>> parse_list('a,b')
    ['a', 'b']
    >>> parse_list('1,2')
    [1, 2]
    >>> parse_list('a,b,')
    ['a', 'b']
    c             S   s   g | ]}|rt |ƒ‘qS r   )r   )Ú.0Úxr   r   r   ú
<listcomp>V   s    zparse_list.<locals>.<listcomp>)Úsplit)r   r   r   r   r   Ú
parse_listC   s    r   ú:ú;)r   Úavs_sepÚvs_sepÚas_sepr   c          
   c   s²   x¬t | |ƒD ]ž}t t|¡}||ks| |¡r0qt ||ƒ^}}}|rbt d|›d|›d|› d�¡ t t|¡}|t|ƒkr”|t t t|¡|ƒfV  q|r|t t|¡fV  qW dS )a	  
    Atrribute and value pair parser.

    :param str_: String represents a list of pairs of attribute and value
    :param avs_sep: char to separate attribute and values
    :param vs_sep: char to separate values
    :param as_sep: char to separate attributes
    zExtra strings z in z It should be in the form of attrzvalue.N)r   ÚtypingÚcastÚstrÚendswithÚwarningsÚwarnÚ
PrimitiveT)r   r   r   r   ÚrelZ_attrZ_valuesÚ_restr   r   r   Úattr_val_itr\   s    
r&   c             C   s   t t| |||ƒƒS )a"  
    Simple parser to parse expressions in the form of
    [ATTR1:VAL0,VAL1,...;ATTR2:VAL0,VAL2,..].

    :param str_: input string
    :param avs_sep:  char to separate attribute and values
    :param vs_sep:  char to separate values
    :param as_sep:  char to separate attributes

    :return:
        a list of tuples of (key, value | [value])
            where key = (Int | String | ...),
            value = (Int | Bool | String | ...) | [Int | Bool | String | ...]

    >>> parse_attrlist_0('a:1')
    [('a', 1)]
    >>> parse_attrlist_0('a:1;b:xyz')
    [('a', 1), ('b', 'xyz')]
    >>> parse_attrlist_0('requires:bash,zsh')
    [('requires', ['bash', 'zsh'])]
    >>> parse_attrlist_0('obsoletes:sysdata;conflicts:sysdata-old')
    [('obsoletes', 'sysdata'), ('conflicts', 'sysdata-old')]
    )Úlistr&   )r   r   r   r   r   r   r   Úparse_attrlist_0y   s    r(   c             C   s   t t| |||ƒƒS )an  
    Simple parser to parse expressions in the form of
    [ATTR1:VAL0,VAL1,...;ATTR2:VAL0,VAL2,..].

    :param str_: input string
    :param avs_sep:  char to separate attribute and values
    :param vs_sep:  char to separate values
    :param as_sep:  char to separate attributes

    >>> parse_attrlist('requires:bash,zsh')
    {'requires': ['bash', 'zsh']}
    )Údictr(   )r   r   r   r   r   r   r   Úparse_attrlist˜   s    r*   )r   ÚlsepÚavsepÚvssepÚavssepr   c             C   s0   || krt | |||ƒS || kr(t| |ƒS t| ƒS )zGeneric parser)r*   r   r   )r   r+   r,   r-   r.   r   r   r   Úparse°   s
    
r/   )r   )r   r   r   )r   r   r   )r   r   r   )r   r   r   r   )Ú__doc__Úrer   r!   Úcompiler   ÚPatternÚ__annotations__ÚIr   r   ÚUnionr   r   r   r#   ZListZPrimitivesTr   r   ZTupleZ	AttrValsTÚIteratorr&   r(   ÚDictZAttrValsDictTr*   ZResultsTr/   r   r   r   r   Ú<module>   s0   
.    