%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python2.7/site-packages/salt/utils/decorators/
Upload File :
Create Path :
Current File : //lib/python2.7/site-packages/salt/utils/decorators/__init__.pyo

�
���^c@@s�dZddlmZmZmZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZddlZddlZddlmZmZddlmZddlmZeZee	d	e�r�eZneje�Zd
e fd��YZ!e!Z"d�Z#d
�Z$de fd��YZ%de%fd��YZ&e&Z'de%fd��YZ(e(Z)d�Z*d�Z+dS(u'
Helpful decorators for module writing
i(tabsolute_importtprint_functiontunicode_literalsN(twraps(tdefaultdict(tCommandExecutionErrortSaltConfigurationError(t
LOG_LEVELS(tsixugetwindowsversiontDependscB@sMeZdZed��Zd�Zd�Zed��Ze	d��Z
RS(u�
    This decorator will check the module when it is loaded and check that the
    dependencies passed in are in the globals of the module. If not, it will
    cause the function to be unloaded (or replaced).
    cC@s
tt�S(N(Rtdict(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt<lambda>(scO@s)tjd||�||_||_dS(uz
        The decorator is instantiated with a list of dependencies (string of
        global name)

        An example use of this would be:

        .. code-block:: python

            @depends('modulename')
            def test():
                return 'foo'

            OR

            @depends('modulename', fallback_function=function)
            def test():
                return 'foo'

        .. code-block:: python

        This can also be done with the retcode of a command, using the
        ``retcode`` argument:

            @depends('/opt/bin/check_cmd', retcode=0)
            def test():
                return 'foo'

        It is also possible to check for any nonzero retcode using the
        ``nonzero_retcode`` argument:

            @depends('/opt/bin/check_cmd', nonzero_retcode=True)
            def test():
                return 'foo'

        .. note::
            The command must be formatted as a string, not a list of args.
            Additionally, I/O redirection and other shell-specific syntax are
            not supported since this uses shell=False when calling
            subprocess.Popen().

        u@Depends decorator instantiated with dep list of %s and kwargs %sN(tlogttracetdependenciestparams(tselfRtkwargs((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt__init__*s
*
	c	C@s�y{tj�dd}|jdjdd�\}}}|j}x5|jD]*}||jf|j||||f<qLWWn tk
r�}t	j
d�nX|S(u�
        The decorator is "__call__"d with the function, we take that function
        and determine which module and function name it is to store in the
        class wide dependency_dict
        iiu__name__u.iuNException encountered when attempting to inspect frame in dependency decorator(tinspecttstackt	f_globalstrsplitt__name__RRtdependency_dictt	ExceptionRt	exception(	Rtfunctiontframet_tkindtmod_nametfun_nametdeptexc((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt__call__[s"	,cC@s�dj||�}tjd||�trItjjj|dt�}ntjjj|�}tjd|�t	j
|dtdt	jdt	j�}|j
�d}|j}tjd	||�tjd
||�|S(Nu{0}.{1}uRunning '%s' for '%s'tposixuCommand after shlex_split: %stshelltstdouttstderriuOutput from '%s': %suRetcode from '%s': %d(tformatRR
t
IS_WINDOWStsalttutilstargstshlex_splittFalset
subprocesstPopentPIPEtSTDOUTtcommunicatet
returncode(t
dependencyRt	func_namet	full_nameR,tproctoutputtretcode((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytrun_commandss		c
C@s�x}tj|j|�D]e\}}xVtj|�D]E\\}}\}}	||kr]q3nd|	ksud|	krPy|j|||�}
WnWtk
r�}|jtjkr�tjd||j	�ntjd||�d}
nXd|	kr
|	d|
krMq3qMq�d|	kr�|	dr8|
dkrJq3qJqM|
dkrMq3qMq�n\|t
krutjd||�q3n7||jks�||jkr�tjd||�q3ntjd	|||�|r3y|jd
|}Wnt
tfk
r�nXdj||�}||krq3ny3|	jd�}
|
dk	rF|
||<n||=Wqxt
k
rttjd
|�q3qxXq3q3WqWdS(u�
        This is a class global method to enforce the dependencies that you
        currently know about.
        It will modify the "functions" dict and remove/replace modules that
        are missing dependencies.
        uretcodeunonzero_retcodeu&Failed to run command %s, %s not founduFailed to run command '%s': %si����iu*Dependency for %s.%s exists, not unloadingu2Dependency (%s) already loaded inside %s, skippingu2Unloading %s.%s because dependency (%s) is not metu__func_alias__u{0}.{1}ufallback_functionu%s already removed, skippingN(Rt	iteritemsRR;tOSErrorterrnotENOENTRR
tfilenametTrueRtf_localstAttributeErrortKeyErrorR(tgettNone(tclst	functionsRttgt_modR5tdependent_dictRR6RRR:R"tmod_keytfallback_function((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytenforce_dependencies�sl#(

	
	




(Rt
__module__t__doc__RRRR#tstaticmethodR;tclassmethodRM(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR	!s	1	c@st���fd��}|S(uI
    Decorator wrapper to log execution time, for profiling purposes
    c@s�tj�}�|tjjj|��}tj�}�jjd�rX�jd}n	�j}djtj	j
�}tj||�j
||�|S(Nusalt.loaded.int.iu-Function %s.%s took %.{0}f seconds to execute(ttimeR*R+R,tclean_kwargsRNt
startswithR(tsyst
float_infotdigRtprofileR(R,Rt
start_timetrettend_timeRtfstr(R(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytwrapped�s	(R(RR]((RsB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyttiming�sc@s(i�t����fd��}|S(u�
    Memoize aka cache the return output of a function
    given a specific set of arguments

    .. versionedited:: 2016.3.4

    Added **kwargs support.
    c@s�g}xF|D]>}t|tj�s>|jtj|��q
|j|�q
Wdjt|�gt|�D]}dj|||�^qk�}|�kr��||��|<n�|S(Nu,u{0}={1}(	t
isinstanceRtstring_typestappendt	text_typetjointlisttsortedR((R,Rtstr_argstargtktargs_(tcachetfunc(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt_memoize�s
B(R(RkRl((RjRksB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytmemoize�s	t_DeprecationDecoratorcB@s>eZdZdZdZd�Zd�Zd�Zd�ZRS(u}
    Base mix-in class for the deprecation decorator.
    Takes care of a common functionality, used in its derivatives.
    iicC@shddlm}m}||_||_|j|j�|_|j|_d|_
d|_d|_dS(u�
        Constructor.

        :param globals: Module globals. Important for finding out replacement functions
        :param version: Expiration version
        :return:
        i(tSaltStackVersiont__saltstack_version__N(
tsalt.versionRoRpt_globalst_exp_version_namet	from_namet_exp_versiontinfot
_curr_versionRFt_raise_latert	_functiont_orig_f_name(RtglobalstversionRoRp((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyRs				cC@s(t�}tjjj|�}||fS(u
        Discard all keywords which aren't function-specific from the kwargs.

        :param kwargs:
        :return:
        (RdR*R+R,RS(RRt_argst_kwargs((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt	_get_args)s	cC@s�|jr|j�n|jr�|j|�\}}y|j||�SWq�tk
r�}tj|�j|j|j�}tj	d|j|�|jj
Stk
r�}tj	d|jj|�|�q�Xnt
d��dS(uQ
        Call target function that has been decorated.

        :return:
        u)Function "%s" was not properly called: %su0Unhandled exception occurred in function "%s: %suAFunction is deprecated, but the successor function was not found.N(RxRyRt	TypeErrorRRbtreplaceRzRterrorRORRR(RRR,R�((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt_call_function5s$		!


cC@s||_|jj|_dS(u�
        Callable method of the decorator object when
        the decorated function is gets called.

        :param function:
        :return:
        N(RyRRz(RR((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR#Rs	(	RRNROtOPT_INtOPT_OUTRRR�R#(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyRns			t
_IsDeprecatedcB@s#eZdZdd�Zd�ZRS(u_
    This decorator should be used only with the deprecated functions
    to mark them as deprecated and alter its behavior a corresponding way.
    The usage is only suitable if deprecation process is renaming
    the function from one to another. In case function name or even function
    signature stays the same, please use 'with_deprecated' decorator instead.

    It has the following functionality:

    1. Put a warning level message to the log, informing that
       the deprecated function has been in use.

    2. Raise an exception, if deprecated function is being called,
       but the lifetime of it already expired.

    3. Point to the successor of the deprecated function in the
       log messages as well during the blocking it, once expired.

    Usage of this decorator as follows. In this example no successor
    is mentioned, hence the function "foo()" will be logged with the
    warning each time is called and blocked completely, once EOF of
    it is reached:

        from salt.util.decorators import is_deprecated

        @is_deprecated(globals(), "Beryllium")
        def foo():
            pass

    In the following example a successor function is mentioned, hence
    every time the function "bar()" is called, message will suggest
    to use function "baz()" instead. Once EOF is reached of the function
    "bar()", an exception will ask to use function "baz()", in order
    to continue:

        from salt.util.decorators import is_deprecated

        @is_deprecated(globals(), "Beryllium", with_successor="baz")
        def bar():
            pass

        def baz():
            pass
    cC@s tj|||�||_dS(u�
        Constructor of the decorator 'is_deprecated'.

        :param globals: Module globals
        :param version: Version to be deprecated
        :param with_successor: Successor function (optional)
        :return:
        N(RnRt
_successor(RR{R|twith_successor((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR�s	c@s/tj�|�t|��fd��}|S(u�
        Callable method of the decorator object when
        the decorated function is gets called.

        :param function:
        :return:
        c@s��j�jkrwdjd�jjd�j�g}�jr^|jdjd�j��ntj	dj
|��nndjd�jj�g}�jr�|jdjd�j��ntj	dj
|��tdj
|����j|�S(	uw
            Decorator function.

            :param args:
            :param kwargs:
            :return:
            uRThe function "{f_name}" is deprecated and will expire in version "{version_name}".tf_nametversion_nameu$Use successor "{successor}" instead.t	successoru u0The lifetime of the function "{f_name}" expired.u/Please use its successor "{successor}" instead.(
RwRuR(RyRRsR�RaRtwarningRcRR�(R,Rtmsg(R(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt	_decorate�s				(RnR#R(RRR�((RsB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR#�sN(RRNRORFRR#(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR�^s,t_WithDeprecatedcB@sMeZdZdZdZdZdejd�Z	d�Z
d�Zd�ZRS(	uf	
    This decorator should be used with the successor functions
    to mark them as a new and alter its behavior in a corresponding way.
    It is used alone if a function content or function signature
    needs to be replaced, leaving the name of the function same.
    In case function needs to be renamed or just dropped, it has
    to be used in pair with 'is_deprecated' decorator.

    It has the following functionality:

    1. Put a warning level message to the log, in case a component
       is using its deprecated version.

    2. Switch between old and new function in case an older version
       is configured for the desired use.

    3. Raise an exception, if deprecated version reached EOL and
       point out for the new version.

    Usage of this decorator as follows. If 'with_name' is not specified,
    then the name of the deprecated function is assumed with the "_" prefix.
    In this case, in order to deprecate a function, it is required:

    - Add a prefix "_" to an existing function. E.g.: "foo()" to "_foo()".

    - Implement a new function with exactly the same name, just without
      the prefix "_".

    Example:

        from salt.util.decorators import with_deprecated

        @with_deprecated(globals(), "Beryllium")
        def foo():
            "This is a new function"

        def _foo():
            "This is a deprecated function"


    In case there is a need to deprecate a function and rename it,
    the decorator should be used with the 'with_name' parameter. This
    parameter is pointing to the existing deprecated function. In this
    case deprecation process as follows:

    - Leave a deprecated function without changes, as is.

    - Implement a new function and decorate it with this decorator.

    - Set a parameter 'with_name' to the deprecated function.

    - If a new function has a different name than a deprecated,
      decorate a deprecated function with the  'is_deprecated' decorator
      in order to let the function have a deprecated behavior.

    Example:

        from salt.util.decorators import with_deprecated

        @with_deprecated(globals(), "Beryllium", with_name="an_old_function")
        def a_new_function():
            "This is a new function"

        @is_deprecated(globals(), "Beryllium", with_successor="a_new_function")
        def an_old_function():
            "This is a deprecated function"

    u__virtualname__uuse_deprecateduuse_supersededcC@s)tj|||�||_||_dS(u�
        Constructor of the decorator 'with_deprecated'

        :param globals:
        :param version:
        :param with_name:
        :param policy:
        :return:
        N(RnRt
_with_namet_policy(RR{R|t	with_nametpolicy((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyRs
	cC@s�djd|jj|jd�p8|jdjd�dd|j�}|jd�rwtdjd|j��|_n|jjd	d
�}|jjdd
�}||j|j	t
��kp�||j|j	t
��k}||j|jt
��kp||j|jt
��k}|r=|r=tdj|���n|jj|j
p^d
j|j��}|j|jkr�|r�|n||_n|r�|n||_dS(ug
        Based on the configuration, set to execute an old or a new function.
        :return:
        u{m_name}.{f_name}tm_nameuu__name__u.i����R�u(Module not found for function "{f_name}"u__opts__u{}u
__pillar__ubFunction '{0}' is mentioned both in deprecated and superseded sections. Please remove any of that.u_{0}N(R(RrREtMODULE_NAMEtsplitRRTRRxtCFG_USE_DEPRECATEDRdtCFG_USE_SUPERSEDEDRR�R�R�Ry(RRR7toptstpillartuse_deprecatedtuse_supersededtold_function((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt
_set_functions&	2	'c	C@sdjd|jj|jd�p8|jdjd�dd|j�}||jjd�j|jt��kp||jjd	�j|jt��kp|j|j	ko||jjdi�j|j
t��ko||jjd	i�j|j
t��k|fS(
u�
        Returns True, if a component configuration explicitly is
        asking to use an old version of the deprecated function.

        :return:
        u{m_name}.{f_name}R�uu__name__u.i����R�u__opts__u
__pillar__(R(RrRER�R�RzR�RdR�R�R�(Rt	func_path((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt_is_used_deprecated4s	2*'c@sMtj���t����fd��}�jj|_�j|_|S(u�
        Callable method of the decorator object when
        the decorated function is gets called.

        :param function:
        :return:
        c@s��j���j�\}}|r��j�jkr�t�}�jr�|jdjd�jjd�rs�j	py�jd�j
��|jdjd�j	��n"|jdjd|d�j
��tjdj
|��q�d	}d�j	�jjkr |jd�j	�d
g}nWdjd�j	d�jpA�j	�|jd�jp\�j	�d
jd�j	�g}tjdj
|��tdj
|���n�j|�S(uw
            Decorator function.

            :param args:
            :param kwargs:
            :return:
            uRThe function "{f_name}" is deprecated and will expire in version "{version_name}".R�u_R�u(Use its successor "{successor}" instead.R�udThe function "{f_name}" is using its deprecated version and will expire in version "{version_name}".u u0The lifetime of the function "{f_name}" expired.u;Please turn off its deprecated version in the configurationueAlthough function "{f_name}" is called, an alias "{f_alias}" is configured as its deprecated version.tf_aliasu/Please use its successor "{successor}" instead.(R�R�RwRuRdR�RaR(RTRzRsRR�RcRyRR�RR�(R,Rt
is_deprecatedR�R�tmsg_patt(RR(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR�Qs2	
		$

	(RnR#RRyROt__wrapped__(RRR�((RRsB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR#Gs
(N(
RRNROR�R�R�RFRnR�RR�R�R#(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR��sD		c@s�fd�}|S(u�
    Decorator to filter out unexpected keyword arguments from the call

    kwarg_names:
        List of argument names to ignore
    c@s"t����fd��}|S(Nc@s@|j�}x$�D]}||kr||=qqW�||�S(N(tcopy(R,Rtkwargs_filteredtname(tfntkwarg_names(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt__ignores_kwargs�s

(R(R�R�(R�(R�sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt_ignores_kwargs�s((R�R�((R�sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytignores_kwargs�s	c@st���fd��}|S(u>
    Decodes all arguments passed to the wrapped function
    c@sEtjr4�tjjj|�tjjj|��S�||�SdS(N(RtPY2R*R+tdatatdecode_listtdecode_dict(R,R(R(sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyR]�s
	(R(RR]((RsB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pytensure_unicode_args�s	(,ROt
__future__RRRR>RtloggingR/RURRt	functoolsRtcollectionsRtsalt.utils.argsR*tsalt.utils.datatsalt.exceptionsRRtsalt.logRtsalt.extRR.R)tgetattrRAt	getLoggerRRtobjectR	tdependsR^RmRnR�R�R�twith_deprecatedR�R�(((sB/usr/lib/python2.7/site-packages/salt/utils/decorators/__init__.pyt<module>s:	�		O^�	

Zerion Mini Shell 1.0