a
    �Ï?e#  ã                   @   sÚ   U d Z ddlZddlZddlZddlZddlmZ dZeje	df e
d< e	ejeje	 e	f dœd	d
„Zefe	eje	df dœdd„Ze	e	dœdd„Ze	e	dœdd„Ze	e	dœdd„Zeefdd„ZG dd„ dejƒZdS )aÇ  A backend module to load and dump (Java) properties files.

- Format to support: Java Properties file, e.g.
  http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Properties.html
- Requirements: None (built-in)
- Development Status :: 4 - Beta
- Limitations:

  - Key and value separator of white spaces is not supported
  - Keys contain escaped white spaces is not supported

- Special options: None

Changelog:

.. versionchanged:: 0.7.0

   - Fix handling of empty values, pointed by @ajays20078
   - Fix handling of values contain strings start with '#' or '!' by
     @ajays20078

.. versionadded:: 0.2

   - Added native Java properties parser instead of a plugin utilizes
     pyjavaproperties module.
é    Né   )Úbase)ú#ú!.Ú_COMMENT_MARKERS)ÚlineÚreturnc                 C   sX   t  d|  ¡ d¡}|d  ¡ }t|ƒdk rHt d| › �t¡ |pBddfS ||d  ¡ fS )zÊParse a line of Java properties file.

    :param line:
        A string to parse, must not start with ' ', '#' or '!' (comment)
    :return: A tuple of (key, value), both key and value may be None
    z(?:\s+)?(?:(?<!\\)[=:])r   r   é   zInvalid line found: NÚ )ÚreÚsplitÚstripÚrstripÚlenÚwarningsÚwarnÚSyntaxWarning)r   ÚpairÚkey© r   ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/anyconfig/backend/properties.pyÚ	parseline*   s    r   ©r   Úcomment_markersc                    s0   ˆ sdS t ‡ fdd„|D ƒƒr,ˆ  |¡r,dS ˆ S )a  Preprocess a line in properties; strip comments, etc.

    :param line:
        A string not starting w/ any white spaces and ending w/ line breaks.
        It may be empty. see also: :func:`load`.
    :param comment_markers: Comment markers, e.g. '#' (hash)
    Nc                 3   s   | ]}|ˆ v V  qd S ©Nr   ©Ú.0Úc©r   r   r   Ú	<genexpr>I   ó    z$_pre_process_line.<locals>.<genexpr>)ÚanyÚ
startswithr   r   r   r   Ú_pre_process_line;   s    
r#   )Úin_sr   c                 C   s   t  dd| ¡S )z;Un-escape and take out the content from given str ``in_s``.z\\(.)z\1)r   Úsub©r$   r   r   r   ÚunescapeP   s    r'   )Úin_cr   c                 C   s   | dv rd|  S | S )z9Escape some special characters in java .properties files.)ú:ú=ú\r+   r   )r(   r   r   r   Ú_escape_charU   s    r,   c                 C   s   d  dd„ | D ƒ¡S )z'Escape special characters in given str.r
   c                 s   s   | ]}t |ƒV  qd S r   )r,   r   r   r   r   r   \   r    zescape.<locals>.<genexpr>)Újoinr&   r   r   r   ÚescapeZ   s    r.   c                 C   sŒ   |ƒ }d}| D ]x}t || ¡  ¡  |ƒ}|du s|s6qd}| d¡rT|| d¡7 }qt|ƒ\}}|du rzt d|› �¡ qt|ƒ||< q|S )aV  Load data from a java properties files given as ``stream``.

    :param stream: A file or file like object of Java properties files
    :param container:
        Factory function to create a dict-like object to store properties
    :param comment_markers: Comment markers, e.g. '#' (hash)
    :return: Dict-like object holding properties
    r
   Nr+   z \zFailed to parse the line: )r#   r   r   Úendswithr   r   r   r'   )ÚstreamÚ	containerr   ÚretÚprevr   r   Úvalr   r   r   Úload_   s$    	ÿ
r5   c                   @   s8   e Zd ZdZdZdZdgZdZdgZdd„ Z	dd„ Z
d	S )
ÚParserz!Parser for Java properties files.Z
propertiesTZac_dictc                 K   s   t ||d�S )a=  Load config from given file like object 'stream'.

        :param stream: A file or file like object of Java properties files
        :param container: callble to make a container object
        :param kwargs: optional keyword parameters (ignored)

        :return: Dict-like object holding config parameters
        )r1   )r5   )Úselfr0   r1   Úkwargsr   r   r   Úload_from_stream‹   s    	zParser.load_from_streamc                 K   s4   |  ¡ D ]&\}}| |› dt|ƒ› tj› �¡ qdS )a  Dump config 'cnf' to a file or file-like object 'stream'.

        :param cnf: Java properties config data to dump
        :param stream: Java properties file or file like object
        :param kwargs: backend-specific optional keyword parameters :: dict
        z = N)ÚitemsÚwriter.   ÚosÚlinesep)r7   Zcnfr0   r8   r   r4   r   r   r   Údump_to_stream–   s    zParser.dump_to_streamN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Z_cidÚ_typeZ_extensionsZ_orderedZ
_dict_optsr9   r>   r   r   r   r   r6   ‚   s   r6   )rB   r<   r   Útypingr   r
   r   r   ÚTupleÚstrÚ__annotations__ÚOptionalr   r#   r'   r,   r.   Údictr5   ZStreamParserr6   r   r   r   r   Ú<module>   s"    þþ#