o
    g*                     @   sh   d Z ddlmZ ddlmZ G dd deZG dd deZd	d
 ZG dd deZ	G dd deZ
dS )a  
Contains factories to be used with the ObjectBuilderParserListener in order to build
a json tree that consists of pure python objects. With these factories we can load
json string into python object hierarchies just like the python standard json.loads()
but our parser allows an extended syntax (unquoted keys, comments, trailing commas)
and this parser have other extras, for example you can provide your own dictionary
and list objects.
    )OrderedDict   )ObjectBuilderParamsc                   @   $   e Zd ZdZefddZdd ZdS )DefaultObjectCreatorz
    A factory that creates json objects (dict like objects) when parsing the json
    string into a python object hierarchy.
    c                 C   s
   || _ dS )z
        :param dict_class: A class that will be instantiated in order to be used as
        a json object in the python object hierarchy. The instances of this class must have
        at least a __setitem__ and a __contains__.
        N
dict_class)selfr    r
   Q/home/ubuntu/cloudmapper/venv/lib/python3.10/site-packages/jsoncfg/tree_python.py__init__   s   
zDefaultObjectCreator.__init__c                       |     fdd} |fS )  
        :param listener: The parser listener that builds the python object hierarchy.
        As an example: the config parser uses listener.parser.line and listener.parser.column
        to get the line/column info for each node of the python object hierarchy.
        c                    s   | | < d S Nr
   )keyvalueobjr
   r   insert_function#   s   z6DefaultObjectCreator.__call__.<locals>.insert_functionr   )r	   listenerr   r
   r   r   __call__      zDefaultObjectCreator.__call__N)__name__
__module____qualname____doc__r   r   r   r
   r
   r
   r   r      s    r   c                   @   r   )DefaultArrayCreatorz
    A factory that creates json arrays (list like objects) when parsing the json
    string into a python object hierarchy.
    c                 C   s
   || _ d S r   
list_class)r	   r   r
   r
   r   r   -   s   
zDefaultArrayCreator.__init__c                    r   )r   c                    s     |  d S r   )append)itemarrayr
   r   append_function8   s   z5DefaultArrayCreator.__call__.<locals>.append_functionr   )r	   r   r#   r
   r!   r   r   0   r   zDefaultArrayCreator.__call__N)r   r   r   r   listr   r   r
   r
   r
   r   r   (   s    r   c                 C   s6   |  dr| dd  p|  }|rt| S t| S )z
    Converts the string representation of a json number into its python object equivalent, an
    int, long, float or whatever type suits.
    -r   N)
startswithisdigitintfloat)
number_stris_intr
   r
   r   default_number_converter=   s   "r,   c                   @   s,   e Zd ZdZedfddZe Zdd ZdS )DefaultStringToScalarConverterax  
    A callable that converts the string representation of json scalars into python objects.
    The JSONParser works only with quoted and non-quoted strings, it doesn't interpret different
    types of json scalars like bool, null, number, string. It is the responsibility of this
    callable to convert the strings (emitted by the parser) into their python equivalent.
    Nc                 C   s*   || _ |du rdddd| _dS || _dS )a[  

        :param number_converter: This number converter will be called with every non-quoted
        string that isn't present in the dictionary passed to the scalar_const_literals parameter.
        :param scalar_const_literals: A dictionary that maps non-quoted string representation of
        json scalars to any user supplied python objects.
        If you don't supply this parameter then the default dictionary
        that will be used is {'null': None, 'true': True, 'false': False}. Note that
        you can use this parameter to easily define your own "constants" in the json file.
        NTF)nulltruefalse)number_converterscalar_const_literals)r	   r1   r2   r
   r
   r   r   O   s   
z'DefaultStringToScalarConverter.__init__c                 C   s\   |r|S | j || j}|| ju r,z| |}W |S  ty+   |d|f  Y |S w |S )z
        :return: After interpreting the string representation of the parsed scalar (scalar_str, and
        scalar_str_quoted) you have to convert it into a python object that will be inserted in the
        object hierarchy.
        zInvalid json scalar: "%s")r2   get_not_scalar_const_literalr1   
ValueErrorerror)r	   r   
scalar_strscalar_str_quotedr   r
   r
   r   r   d   s   
z'DefaultStringToScalarConverter.__call__)	r   r   r   r   r,   r   objectr4   r   r
   r
   r
   r   r-   H   s    
r-   c                   @   s   e Zd Ze Ze Ze ZdS )PythonObjectBuilderParamsN)	r   r   r   r   default_object_creatorr   default_array_creatorr-   "default_string_to_scalar_converterr
   r
   r
   r   r:   v   s    
r:   N)r   collectionsr   parser_listenerr   r9   r   r   r,   r-   r:   r
   r
   r
   r   <module>   s    .