o
    wi                  	   @   s  d Z ddlZddlZddlZddlZddlZddlZddlZddlZg dZ	e
eZdedejdejfddZd	d
dejejef deddfddZdd Zejdejejef dejfddZejd$ddZejdejejedf dejej fddZdejejedf dejej fddZdddejejedf dejejedf dejfddZdedddedeje d eje d!efd"d#ZdS )%z3Generic re-useable self-contained helper functions.    N)attachmkdirsmapping_itemspromote_pathlikepromote_pathlike_directorydeprecate_positional_argsnameobjectreturnc                   s    fdd}|S )a  Return a decorator doing ``setattr(object, name)`` with its argument.

    >>> spam = type('Spam', (object,), {})()  # doctest: +NO_EXE

    >>> @attach(spam, 'eggs')
    ... def func():
    ...     pass

    >>> spam.eggs  # doctest: +ELLIPSIS
    <function func at 0x...>
    c                    s   t  |  | S )N)setattrfuncr   r	    L/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/graphviz/_tools.py	decorator#   s   zattach.<locals>.decoratorr   )r	   r   r   r   r   r   r      s   r   i  )modefilenamer   c               C   s4   t j| }|s
dS td| t j||dd dS )zPRecursively create directories up to the path of ``filename``
        as needed.Nzos.makedirs(%r)T)r   exist_ok)ospathdirnamelogdebugmakedirs)r   r   r   r   r   r   r   *   s
   r   c                C   s(   t |  }t| tu rt t|}|S )ay  Return an iterator over the ``mapping`` items,
        sort if it's a plain dict.

    >>> list(mapping_items({'spam': 0, 'ham': 1, 'eggs': 2}))  # doctest: +NO_EXE
    [('eggs', 2), ('ham', 1), ('spam', 0)]

    >>> from collections import OrderedDict
    >>> list(mapping_items(OrderedDict(enumerate(['spam', 'ham', 'eggs']))))
    [(0, 'spam'), (1, 'ham'), (2, 'eggs')]
    )iteritemstypedictsorted)mappingresultr   r   r   r   4   s   r   filepathc                C      dS )z(Return path object for path-like-object.Nr   r"   r   r   r   r   E       r   c                C   r#   )zReturn None for None.Nr   r$   r   r   r   r   J   r%   c                C   r#   )z9Return path object or ``None`` depending on ``filepath``.Nr   r$   r   r   r   r   O   r%   c                 C   s   | dur	t | S dS )zReturn path-like object ``filepath`` promoted into a path object.

    See also:
        https://docs.python.org/3/glossary.html#term-path-like-object
    N)pathlibPathr$   r   r   r   r   U   s   )default	directoryr(   c               C   s   t | dur	| S |ptjS )zReturn path-like object ``directory`` promoted into a path object (default to ``os.curdir``).

    See also:
        https://docs.python.org/3/glossary.html#term-path-like-object
    N)r&   r'   r   curdir)r)   r(   r   r   r   r   _   s   r      )
ignore_argcategory
stacklevelsupported_numberr,   r-   r.   c                    sV   dksJ d du rdd }|S t  tsJ d7  fdd}|S )	a  Mark supported_number of positional arguments as the maximum.

    Args:
        supported_number: Number of positional arguments
            for which no warning is raised.
        ignore_arg: Name of positional argument to ignore.
        category: Type of Warning to raise
            or None to return a nulldecorator
            returning the undecorated function.
        stacklevel: See :func:`warning.warn`.

    Returns:
        Return a decorator raising a category warning
            on more than supported_number positional args.

    See also:
        https://docs.python.org/3/library/exceptions.html#FutureWarning
        https://docs.python.org/3/library/exceptions.html#DeprecationWarning
        https://docs.python.org/3/library/exceptions.html#PendingDeprecationWarning
    r   zsupported_number => 0: Nc                 S   s   | S )z Return the undecorated function.r   r   r   r   r   nulldecorator   s   z0deprecate_positional_args.<locals>.nulldecoratorr+   c              
      s   t }dd |j D  	d ur1fdd D }|s$J dt|7 d dnd 	d  }|s=J tdjj| j	
d	d
\}}r[|r]|r]J 	dkrcdndt 	f	dd}|S )Nc                 S   s"   g | ]\}}|j tjjkr|qS r   )kindinspect	ParameterPOSITIONAL_OR_KEYWORD).0r   paramr   r   r   
<listcomp>   s    z@deprecate_positional_args.<locals>.decorator.<locals>.<listcomp>c                    s   g | ]}| kr|qS r   r   )r5   r   )r,   r   r   r7      s    z#ignore_arg must be a positional argz (ignoring z)) z$deprecate positional args: %s.%s(%r)__legacyr+   sc                     s   t | krEt | }tt|}t|}|sJ ddd | D }tjd d d  dt	| d| d	 d
 | i |S )Nz, c                 s   s"    | ]\}}| d |V  qdS )=Nr   )r5   r   valuer   r   r   	<genexpr>   s    zPdeprecate_positional_args.<locals>.decorator.<locals>.wrapper.<locals>.<genexpr>zThe signature of z will be reduced to z positional arg z: pass z as keyword arg)r.   r-   )
lenzipr   	itertoolsislicejoinr   warningswarnlist)argskwargs	call_args	supported
deprecatedwanted)	argnamesr-   check_numberr   	func_namequalifications_r.   r/   r   r   wrapper   s2   

z=deprecate_positional_args.<locals>.decorator.<locals>.wrapper)r2   	signature
parametersr   r@   r   r   
__module____qualname____name__lstrip	partition	functoolswraps)r   rT   ignoredrL   seprestrS   r-   r,   r.   r/   )rN   rO   r   rP   rQ   rR   r   r      s*   

z,deprecate_positional_args.<locals>.decorator)
issubclassWarning)r/   r,   r-   r.   r0   r   r   r`   r   r   k   s   ,r   )r"   Nr
   N)__doc__r[   r2   rB   loggingr   r&   typingrE   __all__	getLoggerrX   r   strAnyCallabler   UnionPathLikeintr   r   overloadr'   r   Optionalr   PendingDeprecationWarningTyperb   r   r   r   r   r   <module>   sV    
(
"



