HEX
Server: LiteSpeed
System: Linux shams.tasjeel.ae 5.14.0-611.5.1.el9_7.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Nov 11 08:09:09 EST 2025 x86_64
User: infowars (1469)
PHP: 8.2.29
Disabled: NONE
Upload Files
File: //usr/lib/python3.9/site-packages/ipalib/__pycache__/frontend.cpython-39.pyc
a

}�f���@s�dZddlZddlZddlmZddlmZddlmZddl	m
Z
mZddlm
Z
mZmZmZddlmZdd	lmZmZmZdd
lmZddlmZmZmZmZmZmZmZddl m!Z!m"Z"dd
l#m$Z$m%Z%ddl&m'Z'm(Z(m)Z)ddl*m+Z+ej,r�e-Z.e�/e0�Z1dZ2dd�Z3dd�Z4dd�Z5Gdd�de
�Z6iZ7Gdd�de6�Z8Gdd�de8�Z9Gdd�de8�Z:Gdd �d e6�Z;Gd!d"�d"e
�Z<Gd#d$�d$e<e8�Z=Gd%d&�d&e
�Z>dS)'z)
Base classes for all front-end plugins.
�N)�API_VERSION)�
APIVersion)�	NameSpace)�Plugin�APINameSpace)�create_param�Param�Str�Flag)�create_signature)�Output�Entry�
ListOfEntries)�_)�ZeroArgumentError�MaxArgumentError�OverlapError�VersionError�OptionError�ValidationError�ConversionError)�errors�messages)�context�
context_frame)�
classproperty�classobjectproperty�json_serialize)�SD_IPA_API_MESSAGE_IDZvalidation_rulecCst|t�rJ�t|td�|S)NT)�hasattr�	RULE_FLAG�setattr��obj�r$�3/usr/lib/python3.9/site-packages/ipalib/frontend.py�rule8sr&cCst|�ot|td�duS)NFT)�callable�getattrr r"r$r$r%�is_rule=sr)cCsDd}|D]6}t||�tur.|t||�}q|t||�}q|S)z�
    Return the number of entries in an entry. This is primarly for the
    failed output parameter so we don't print empty values.

    We also use this to determine if a non-zero return value is needed.
    r)�type�dict�entry_count�len)�entryZnum_entries�fr$r$r%r,Asr,c@s>eZdZdZdZd
dd�Zddd�Zdd	d
�Zedd��Z	dS)�HasParama�
    Base class for plugins that have `Param` `NameSpace` attributes.

    Subclasses of `HasParam` will on one or more attributes store `NameSpace`
    instances containing zero or more `Param` instances.  These parameters might
    describe, for example, the arguments and options a command takes, or the
    attributes an LDAP entry can include, or whatever else the subclass sees
    fit.

    Although the interface a subclass must implement is very simple, it must
    conform to a specific naming convention: if you want a namespace
    ``SubClass.foo``, you must define a ``Subclass.takes_foo`` attribute and a
    ``SubCLass.get_foo()`` method, and you may optionally define a
    ``SubClass.check_foo()`` method.


    A quick big-picture example
    ===========================

    Say you want the ``options`` instance attribute on your subclass to be a
    `Param` `NameSpace`... then according to the enforced naming convention,
    your subclass must define a ``takes_options`` attribute and a
    ``get_options()`` method.  For example:

    >>> from ipalib import Str, Int
    >>> class Example(HasParam):
    ...
    ...     options = None  # This will be replaced with your namespace
    ...
    ...     takes_options = (Str('one'), Int('two'))
    ...
    ...     def get_options(self):
    ...         return self._get_param_iterable('options')
    ...
    >>> eg = Example()

    The ``Example.takes_options`` attribute is a ``tuple`` defining the
    parameters you want your ``Example.options`` namespace to contain.  Your
    ``Example.takes_options`` attribute will be accessed via
    `HasParam._get_param_iterable()`, which, among other things, enforces the
    ``('takes_' + name)`` naming convention.  For example:

    >>> eg._get_param_iterable('options')
    (Str('one'), Int('two'))

    The ``Example.get_options()`` method simply returns
    ``Example.takes_options`` by calling `HasParam._get_param_iterable()`.  Your
    ``Example.get_options()`` method will be called via
    `HasParam._filter_param_by_context()`, which, among other things, enforces
    the ``('get_' + name)`` naming convention.  For example:

    >>> list(eg._filter_param_by_context('options'))
    [Str('one'), Int('two')]

    At this point, the ``eg.options`` instance attribute is still ``None``:

    >>> eg.options is None
    True

    `HasParam._create_param_namespace()` will create the ``eg.options``
    namespace from the parameters yielded by
    `HasParam._filter_param_by_context()`.  For example:

    >>> eg._create_param_namespace('options')
    >>> eg.options
    NameSpace(<2 members>, sort=False)
    >>> list(eg.options)  # Like dict.__iter__()
    ['one', 'two']

    Your subclass can optionally define a ``check_options()`` method to perform
    sanity checks.  If it exists, the ``check_options()`` method is called by
    `HasParam._create_param_namespace()` with a single value, the `NameSpace`
    instance it created.  For example:

    >>> class Example2(Example):
    ...
    ...     def check_options(self, namespace):
    ...         for param in namespace():  # Like dict.itervalues()
    ...             if param.name == 'three':
    ...                 raise ValueError("I dislike the param 'three'")
    ...         print '  ** Looks good! **'  # Note output below
    ...
    >>> eg = Example2()
    >>> eg._create_param_namespace('options')
      ** Looks good! **
    >>> eg.options
    NameSpace(<2 members>, sort=False)

    However, if we subclass again and add a `Param` named ``'three'``:

    >>> class Example3(Example2):
    ...
    ...     takes_options = (Str('one'), Int('two'), Str('three'))
    ...
    >>> eg = Example3()
    >>> eg._create_param_namespace('options')
    Traceback (most recent call last):
      ...
    ValueError: I dislike the param 'three'
    >>> eg.options is None  # eg.options was not set
    True


    The Devil and the details
    =========================

    In the above example, ``takes_options`` is a ``tuple``, but it can also be
    a param spec (see `create_param()`), or a callable that returns an iterable
    containing one or more param spec.  Regardless of how ``takes_options`` is
    defined, `HasParam._get_param_iterable()` will return a uniform iterable,
    conveniently hiding the details.

    The above example uses the simplest ``get_options()`` method possible, but
    you could instead implement a ``get_options()`` method that would, for
    example, produce (or withhold) certain parameters based on the whether
    certain plugins are loaded.

    Think of ``takes_options`` as declarative, a simple definition of *what*
    parameters should be included in the namespace.  You should only implement
    a ``takes_options()`` method if a `Param` must reference attributes on your
    plugin instance (for example, for validation rules); you should not use a
    ``takes_options()`` method to filter the parameters or add any other
    procedural behaviour.

    On the other hand, think of the ``get_options()`` method as imperative, a
    procedure for *how* the parameters should be created and filtered.  In the
    example above the *how* just returns the *what* unchanged, but arbitrary
    logic can be implemented in the ``get_options()`` method.  For example, you
    might filter certain parameters from ``takes_options`` base on some
    criteria, or you might insert additional parameters provided by other
    plugins.

    The typical use case for using ``get_options()`` this way is to procedurally
    generate the arguments and options for all the CRUD commands operating on a
    specific LDAP object: the `Object` plugin defines the possible LDAP entry
    attributes (as `Param`), and then the CRUD commands intelligently build
    their ``args`` and ``options`` namespaces based on which attribute is the
    primary key.  In this way new LDAP attributes (aka parameters) can be added
    to the single point of definition (the `Object` plugin), and all the
    corresponding CRUD commands pick up these new parameters without requiring
    modification.  For an example of how this is done, see the
    `ipalib.crud.Create` base class.

    However, there is one type of filtering you should not implement in your
    ``get_options()`` method, because it's already provided at a higher level:
    you should not filter parameters based on the value of ``api.env.context``
    nor (preferably) on any values in ``api.env``.
    `HasParam._filter_param_by_context()` already does this by calling
    `Param.use_in_context()` for each parameter.  Although the base
    `Param.use_in_context()` implementation makes a decision solely on the value
    of ``api.env.context``, subclasses can override this with implementations
    that consider arbitrary ``api.env`` values.
    F�takescCsp|d|}t||d�}t|�tur(|St|ttf�r<|fSt|�rJ|�S|durXt�Std|j||f��dS)a�
        Return an iterable of params defined by the attribute named ``name``.

        A sequence of params can be defined one of three ways: as a ``tuple``;
        as a callable that returns an iterable; or as a param spec (a `Param` or
        ``str`` instance).  This method returns a uniform iterable regardless of
        how the param sequence was defined.

        For example, when defined with a tuple:

        >>> class ByTuple(HasParam):
        ...     takes_args = (Param('foo'), Param('bar'))
        ...
        >>> by_tuple = ByTuple()
        >>> list(by_tuple._get_param_iterable('args'))
        [Param('foo'), Param('bar')]

        Or you can define your param sequence with a callable when you need to
        reference attributes on your plugin instance (for validation rules,
        etc.).  For example:

        >>> class ByCallable(HasParam):
        ...     def takes_args(self):
        ...         yield Param('foo', self.validate_foo)
        ...         yield Param('bar', self.validate_bar)
        ...
        ...     def validate_foo(self, _, value, **kw):
        ...         if value != 'Foo':
        ...             return _("must be 'Foo'")
        ...
        ...     def validate_bar(self, _, value, **kw):
        ...         if value != 'Bar':
        ...             return _("must be 'Bar'")
        ...
        >>> by_callable = ByCallable()
        >>> list(by_callable._get_param_iterable('args'))
        [Param('foo', validate_foo), Param('bar', validate_bar)]

        Lastly, as a convenience for when a param sequence contains a single
        param, your defining attribute may a param spec (either a `Param`
        or an ``str`` instance).  For example:

        >>> class BySpec(HasParam):
        ...     takes_args = Param('foo')
        ...     takes_options = 'bar?'
        ...
        >>> by_spec = BySpec()
        >>> list(by_spec._get_param_iterable('args'))
        [Param('foo')]
        >>> list(by_spec._get_param_iterable('options'))
        ['bar?']

        For information on how an ``str`` param spec is interpreted, see the
        `create_param()` and `parse_param_spec()` functions in the
        `ipalib.parameters` module.

        Also see `HasParam._filter_param_by_context()`.
        rNz0%s.%s must be a tuple, callable, or spec; got %r)	r(r*�tuple�
isinstancer�strr'�	TypeError�name)�selfr6�verbZsrc_name�srcr$r$r%�_get_param_iterable�s;��zHasParam._get_param_iterableNccs�t|d|�}d|}t||�s0td|j|f��t||�}t|�sVtd|j||f��|�D]$}t|�}|dusz|�|�r\|Vq\dS)aK
        Filter params on attribute named ``name`` by environment ``env``.

        For example:

        >>> from ipalib.config import Env
        >>> class Example(HasParam):
        ...
        ...     takes_args = (
        ...         Str('foo_only', include=['foo']),
        ...         Str('not_bar', exclude=['bar']),
        ...         'both',
        ...     )
        ...
        ...     def get_args(self):
        ...         return self._get_param_iterable('args')
        ...
        ...
        >>> eg = Example()
        >>> foo = Env(context='foo')
        >>> bar = Env(context='bar')
        >>> another = Env(context='another')
        >>> (foo.context, bar.context, another.context)
        (u'foo', u'bar', u'another')
        >>> list(eg._filter_param_by_context('args', foo))
        [Str('foo_only', include=['foo']), Str('not_bar', exclude=['bar']), Str('both')]
        >>> list(eg._filter_param_by_context('args', bar))
        [Str('both')]
        >>> list(eg._filter_param_by_context('args', another))
        [Str('not_bar', exclude=['bar']), Str('both')]
        �envZget_z%s.%s()z %s.%s must be a callable; got %rN)r(r�NotImplementedErrorr6r'r5rZuse_in_context)r7r6r;Zget_name�get�spec�paramr$r$r%�_filter_param_by_context;s 
�
�
z!HasParam._filter_param_by_contextcCsNt|�||�dd�}|j��s>t|d|d�}t|�r>||�t|||�dS)NF��sortZcheck_)rr@�apiZis_production_moder(r'r!)r7r6r;�	namespace�checkr$r$r%�_create_param_namespaceks
�
z HasParam._create_param_namespacecCstjS�N)rZ
current_frame�r7r$r$r%rvszHasParam.context)r1)N)N)
�__name__�
__module__�__qualname__�__doc__�NO_CLIr:r@rF�propertyrr$r$r$r%r0Rs
K
0
r0cseZdZdZe�Ze�Ze�d�Z	e�d�Z
e�d�Ze�d�ZdZ
dZe�d�Zd	Ze�d
�Ze�Ze�ZdZed�ZdZeZed
d��Zee�Zeedd���Ze dd��Z!dd�Z"dd�Z#dd�Z$dd�Z%dd�Z&dd�Z'dd �Z(d!d"�Z)d#d$�Z*d%d&�Z+d'd(�Z,d)d*�Z-d+d,�Z.dd-e/�dfd.d/�Z0d0d1�Z1d2d3�Z2dcd4d5�Z3d6d7�Z4d8d9�Z5d:d;�Z6d<d=�Z7d>d?�Z8d@dA�Z9dBdC�Z:�fdDdE�Z;dFdG�Z<dHdI�Z=dJdK�Z>dLdM�Z?efdNdO�Z@dPdQ�ZAdRdS�ZBdTdU�ZCdVdW�ZDdXZEdYdZ�ZFed[d\��ZGeddd]d^��ZHeded_d`��ZIdadb�ZJ�ZKS)f�Commanda�
    A public IPA atomic operation.

    All plugins that subclass from `Command` will be automatically available
    as a CLI command and as an XML-RPC method.

    Plugins that subclass from Command are registered in the ``api.Command``
    namespace. For example:

    >>> from ipalib import create_api
    >>> api = create_api()
    >>> class my_command(Command):
    ...     pass
    ...
    >>> api.add_plugin(my_command)
    >>> api.finalize()
    >>> list(api.Command)
    [<class '__main__.my_command'>]
    >>> api.Command.my_command # doctest:+ELLIPSIS
    ipalib.frontend.my_command()

    This class's subclasses allow different types of callbacks to be added and
    removed to them.
    Registering a callback is done either by ``register_callback``, or by
    defining a ``<type>_callback`` method.

    Subclasses should define the `callback_types` attribute as a tuple of
    allowed callback types.
    �args�options�params�params_by_defaultNT�output)�result�
output_paramsz1Results are truncated, try a more specific search)�interactive_promptcCs|j�d�dS)N�.�)rJ�
rpartition)�clsr$r$r%Z__topic_getter�szCommand.__topic_gettercCsRt|d�r|jS|��r"d|_dS|dur@ddlm}||d�}t|�|_}|S)N�
_signaturer)rC)rr\�__subclasses__�ipalibrCr)r[r#rCZ	signaturer$r$r%�
__signature__�s

zCommand.__signature__cCs|jSrG)�	full_namerHr$r$r%�forwarded_name�szCommand.forwarded_namecOsR|��t��0ttdd�|j_|j|i|��Wd�S1sD0YdS)z�
        Perform validation and then execute the command.

        If not in a server context, the call will be forwarded over
        XML-RPC and the executed an the nearest IPA server.
        �	principalN)Zensure_finalizedrr(rrb�_Command__do_call�r7rPrQr$r$r%�__call__�szCommand.__call__c
Cs�ttdd�|krdSttdd�ddlm}ddlm}tg|jfi|����}||t	dd�}|jj
phd}t|jjdd�}	|	dur�|	j
}
nd	}
|jd
d||||
|f|j|jjjt||||d�dS)
N�audit_actionr)�journal)�json_encode_binaryF)Zpretty_printz
[autobind]Zldap2z[no_connection_id]z[%s] %s: %s: %s [%s] %szIPA.API)ZPRIORITYZSYSLOG_IDENTIFIERZ
MESSAGE_IDZIPA_API_COMMANDZIPA_API_PARAMSZIPA_API_RESULTZ
IPA_API_ACTOR)r(rr!ZsystemdrgZipalib.ipajsonrhr+�_safe_args_and_paramsrrbrC�Backend�id�sendZ
LOG_NOTICEr;Zscriptr)r7�funcrRrUrgrhZ	args_optsZjson_encodedZactorZconnZconn_idr$r$r%Z__audit_to_journal�s@���	�zCommand.__audit_to_journalc
Osbg|j_d|vr$|�t|d��nF|jjjrB|jjjsBd|d<n(|j|d<|jjjrj|�	t
j|jd��|j|i|��}t
�d|jd�|jfi|����|jjjr�|�|jfi|���|jfi|��}|jfi|��}t
�d|jd�|jfi|����|jjj�rF|jfi|��t|jdkttdd�g��rFttd|j�|jfi|��\}}z|j|i|��}WnJt�y�}z0|jjj�r�|�|j|t|�j��WYd}~n
d}~00t |t!��r�|jjD]}t
�	|d||��q�t |t!��rd	|j"v�rd	|v�r|�#|�|d	<|j$�rB|j"�s2|du�rB|�%||d�|jjj�r^|�|j|d
�|S)N�versionz2.0)Zserver_versionzraw: %s(%s)�, z%s(%s)Zconsolerf�summary�SUCCESS)&r�_Command__messages�verify_client_version�unicoderCr;Zskip_version_check�	in_server�api_version�add_messagerZVersionMissing�args_options_2_params�logger�debugr6�join�
_repr_iter�update�get_default�	normalize�convert�validate�allr(r!�params_2_args_options�run�	Exception�_Command__audit_to_journalr*rIr3r+rT�get_summary_default�use_output_validation�validate_output)r7rPrQrR�ret�e�messager$r$r%Z	__do_callsb


��
�
����zCommand.__do_callcCs|jj�|�dSrG)rrr�append)r7r�r$r$r%rw5szCommand.add_messagecksh|��D]$}|�|jd�}|j|�|�fVq|��D],}|j|vrFq6||j}|j|�|�fVq6dS)z�
        Iterate through *safe* values of args and options

        This method uses `parameters.Param.safe_value()` to mask
        passwords when logging. It yields tuples of (name, value)
        of the arguments and options.
        N)rPr=r6�
safe_valuerQ�r7rR�arg�value�optionr$r$r%ri8s

zCommand._safe_args_and_paramscksj|��D]"}|�|jd�}t|�|��Vq|��D]0}|j|vrDq4||j}d|j|�|�fVq4dS)af
        Iterate through ``repr()`` of *safe* values of args and options.

        This method uses `parameters.Param.safe_value()` to mask passwords when
        logging.  Logging the exact call is extremely useful, but we obviously
        don't want to log the cleartext password.

        For example:

        >>> class my_cmd(Command):
        ...     takes_args = ('login',)
        ...     takes_options=(Password('passwd'),)
        ...
        >>> c = my_cmd()
        >>> c.finalize()
        >>> list(c._repr_iter(login=u'Okay.', passwd=u'Private!'))
        ["u'Okay.'", "passwd=u'********'"]
        Nz%s=%r)rPr=r6�reprr�rQr�r$r$r%r|Is

zCommand._repr_itercOs�|jdur>t|�|jkr>|jdkr.t|jd��t|j|jd��t|�|��}t|�dkr�t|�|��}t|��	|�}t|�dkr�t
t|�d��|�|�|S)z4
        Merge (args, options) into params.
        Nr�r6)r6�count)�names)
�max_argsr-rr6rr+�_Command__options_2_params�_Command__args_2_params�set�intersectionr�sortedr})r7rPrQrRZarg_kwr�r$r$r%rxes

zCommand.args_options_2_paramsccs�d}t|���D]�\}}|r J�t|�|kr�|jr�d}t|�|dkrlt||�ttfvrl|j||fVq�|j||d�fVq�|j||fVqq�qdS)NFT�)�	enumeraterPr-�
multivaluer*�listr2r6)r7�valuesr��ir�r$r$r%Z__args_2_paramsvs$zCommand.__args_2_paramsccsP|jD]}||vr||�|�fVqt|��|j�}|rLttd�|��d��dS)NzUnknown option: %(option)s)r�)rR�popr��
difference�internal_optionsrr)r7rQr6Zunused_keysr$r$r%Z__options_2_params�s
�zCommand.__options_2_paramscOs|j|i|��}t|�|��S)zK
        Creates a LDAP entry from attributes in args and options.
        )rxr+�_Command__attributes_2_entry)r7rPrQ�kwr$r$r%�args_options_2_entry�szCommand.args_options_2_entryccsT|jD]H}|j|jr||vr||}t|t�r@|t|�fVq|||fVqdSrG)rRZ	attributer3r2r�)r7r�r6r�r$r$r%Z__attributes_2_entry�s

zCommand.__attributes_2_entryc	Kslt�}t|�|��}d}|jD]D}z||}WntyHd}YqYn0|rZ||f7}q|||<q||fS)z4
        Split params into (args, options).
        TF)r2r+�_Command__params_2_optionsrP�KeyError)r7rRrPrQZis_argr6r�r$r$r%r��s


zCommand.params_2_args_optionsccs&|jD]}||vr|||fVqdSrG)rQ)r7rRr6r$r$r%Z__params_2_options�s
zCommand.__params_2_optionsFc
Cs�|dur|j}|jjj|||d�}|��s.dSz||fi|��WSttfy�}z&|jj�t|�t|j	��WYd}~qd}~00qdS)zm
        Prompts the user for the value of given parameter.

        Returns the parameter instance.
        N)�optional)
�labelrj�textui�prompt�striprrZprint_prompt_attribute_errorrt�error)r7r?�defaultr�r�r��rawr�r$r$r%�prompt_param�s�zCommand.prompt_paramcst�fdd�|��D��S)a�
        Return a dictionary of normalized values.

        For example:

        >>> class my_command(Command):
        ...     takes_options = (
        ...         Param('first', normalizer=lambda value: value.lower()),
        ...         Param('last'),
        ...     )
        ...
        >>> c = my_command()
        >>> c.finalize()
        >>> c.normalize(first=u'JOHN', last=u'DOE')
        {'last': u'DOE', 'first': u'john'}
        c3s&|]\}}|�j|�|�fVqdSrG)rRr��.0�k�vrHr$r%�	<genexpr>�sz$Command.normalize.<locals>.<genexpr>�r+�items�r7r�r$rHr%r�s�zCommand.normalizecst�fdd�|��D��S)a�
        Return a dictionary of values converted to correct type.

        >>> from ipalib import Int
        >>> class my_command(Command):
        ...     takes_args = (
        ...         Int('one'),
        ...         'two',
        ...     )
        ...
        >>> c = my_command()
        >>> c.finalize()
        >>> c.convert(one=1, two=2)
        {'two': u'2', 'one': 1}
        c3s&|]\}}|�j|�|�fVqdSrG)rRr�r�rHr$r%r��sz"Command.convert.<locals>.<genexpr>r�r�r$rHr%r��s�zCommand.convertcs.|dur�fdd�|��D�}t|�|���S)a�
        Return a dictionary of defaults for all missing required values.

        For example:

        >>> from ipalib import Str
        >>> class my_command(Command):
        ...     takes_args = Str('color', default=u'Red')
        ...
        >>> c = my_command()
        >>> c.finalize()
        >>> c.get_default()
        {'color': u'Red'}
        >>> c.get_default(color=u'Yellow')
        {}
        Ncs(g|] }|j�vr|js|jr|j�qSr$)r6�requiredZautofill�r��p�r�r$r%�
<listcomp>s�z'Command.get_default.<locals>.<listcomp>)rRr+�_Command__get_default_iter)r7Z_paramsr�r$r�r%r~�szCommand.get_defaultcKst|�|g|��}|�|�S)z=
        Return default value for parameter `_name`.
        )r+r�r=)r7�_namer�r�r$r$r%�get_default_ofszCommand.get_default_ofc	cs"t�}t|j�D]<}|j|vs(|j|vr|jdur4q|jjD]}|�|�q<q|��D]�}d}d}|j|vr�|j|vr�|||jfi|��}|jjj	r�|j
|dd�|||j<n8|di|��}|jjj	r�|�
|�|dur�|||j<d}|j|vrV|�s|jfi|��}|durV|j|fVqVdS)z^
        Generator method used by `Command.get_default` and `Command.get_default_of`.
        NFT�Zsupplied)N)r��reversedrSr6�default_from�keys�addrCr;rur�r~)	r7rRr�Zdepr?r6r�Z
hasdefaultr�r$r$r%Z__get_default_iters6







zCommand.__get_default_itercKs4|��D]&}|�|jd�}|j||j|vd�qdS)z�
        Validate all values.

        If any value fails the validation, `ipalib.errors.ValidationError`
        (or a subclass thereof) will be raised.
        Nr�)rRr=r6r�)r7r�r?r�r$r$r%r�GszCommand.validatecCsbt|j�}zt|�}Wn&ty<t||j|jjd��Yn0|j|jkr^t||j|jjd��dS)a
        Compare the version the client provided to the version of the
        server.

        If the client major version does not match then return an error.
        If the client minor version is less than or equal to the server
        then let the request proceed.
        )ZcverZsver�serverN)rrv�
ValueErrorrr;Z
xmlrpc_uri�major)r7Zclient_versionZ
server_apiverZ
client_apiverr$r$r%rsRs	
��zCommand.verify_client_versioncOs*|jjjr|j|i|��S|j|i|��S)a�
        Dispatch to `Command.execute` or `Command.forward`.

        If running in a server context, `Command.execute` is called and the
        actually work this command performs is executed locally.

        If running in a non-server context, `Command.forward` is called,
        which forwards this call over RPC to the exact same command
        on the nearest IPA server and the actual work this command
        performs is executed remotely.
        )rCr;ru�execute�forwardrdr$r$r%r�hs
zCommand.runcOstd|j��dS)a�
        Perform the actual work this command does.

        This method should be implemented only against functionality
        in self.api.Backend.  For example, a hypothetical
        user_add.execute() might be implemented like this:

        >>> class user_add(Command):
        ...     def execute(self, **kw):
        ...         return self.api.Backend.ldap.add(**kw)
        ...
        �%s.execute()N�r<r6)r7rPr�r$r$r%r�xs
zCommand.executec
Os�z |jjj|jg|�Ri|��WStjy�}zP|jjjdkrB�t	|dd�}|dus`||j
vrb�tj|j
|jd��WYd}~n
d}~00dS)zG
        Forward call over RPC to this same command on server.
        Zclir6Nr�)rjZ	rpcclientr�rarZRequirementErrorrCr;rr(rR�cli_name)r7rPr�r�r6r$r$r%r��s��zCommand.forwardc
s$|�d�t|j�dks$|jdjs2t|j�|_nd|_|�d�t|���t|���}dd�}tt||d�d	d
�|_	g}|D]f}t|�}|D]H}|j
dur�q�|j|j
jvr�q�zt
||�|��}Wq�ty�Yq�0q�|�||�q�t|d	d
�|_t|��d	d
�|_|�d�tt|���dS)a�
        Finalize plugin initialization.

        This method creates the ``args``, ``options``, and ``params``
        namespaces.  This is not done in `Command.__init__` because
        subclasses (like `crud.Add`) might need to access other plugins
        loaded in self.api to determine what their custom `Command.get_args`
        and `Command.get_options` methods should yield.
        rPr���NrQcSs,|jr(|jdkr|jS|jdur$dSdSdS)Nrr�rY)r�Z	sortorderr�)r�r$r$r%�get_key�s

z%Command._on_finalize.<locals>.get_key)�keyFrArV)rFr-rPr�r�r2rQrr�rRr�r6r��min�indexr��insertrS�_iter_outputrT�superrO�_on_finalize)r7Z
params_nosortr�rRr��pos�j��	__class__r$r%r��s8



�

zCommand._on_finalizeccs�t|j�tur,td|jtt|j�|jf��t|j�D]J\}}t|t�rPt|�}t|t�sztd|j|ttft|�|f��|Vq6dS)Nz&%s.has_output: need a %r; got a %r: %rz*%s.has_output[%d]: need a %r; got a %r: %r)	r*�
has_outputr2r5r6r�r3r4r)r7r��or$r$r%r��s�

�zCommand._iter_outputccs|�d�EdHdS)aM
        Iterate through parameters for ``Command.args`` namespace.

        This method gets called by `HasParam._create_param_namespace()`.

        Subclasses can override this to customize how the arguments are
        determined.  For an example of why this can be useful, see the
        `ipalib.crud.Create` subclass.
        rPN�r:rHr$r$r%�get_args�s
zCommand.get_argscCsjd}d}|�D]V}|r>|jr>td|j|jdd�|�D�f��|rPtd|j��|jsZd}|jrd}qdS)z{
        Sanity test for args namespace.

        This method gets called by `HasParam._create_param_namespace()`.
        Fz7%s: required argument after optional in %s arguments %scSsg|]
}|j�qSr$)Z
param_spec)r��xr$r$r%r���z&Command.check_args.<locals>.<listcomp>z)%s: only final argument can be multivalueTN)r�r�r6r�)r7rPr�r�r�r$r$r%�
check_args�s"

���zCommand.check_argsccs�|�d�EdH|jD]J}t|ttf�rtddtd�ddgd�Vtddtd	�ddgd�Vqbqtd
td�dddgd
�VdS)a'
        Iterate through parameters for ``Command.options`` namespace.

        This method gets called by `HasParam._create_param_namespace()`.

        For commands that return entries two special options are generated:
        --all   makes the command retrieve/display all attributes
        --raw   makes the command display attributes as they are stored

        Subclasses can override this to customize how the arguments are
        determined.  For an example of why this can be useful, see the
        `ipalib.crud.Create` subclass.
        rQNr�zJRetrieve and print all attributes from the server. Affects command output.Zwebui�	no_output)r��doc�exclude�flagsr�zBPrint entries as stored on the server. Only affects output format.zversion?z@Client version. Used to determine if server will accept request.Z	no_option)r�r�r�)r:r�r3r
rr
rr	)r7r�r$r$r%�get_options�s*
���zCommand.get_optionsc
Cs�d|j}t|t�s,td|tt|�|f��t|j�}t|�tdg�}||kr�||}|rrtd|t|�|f��||}|r�td|t|�|f��|��D]Z}||j}	|jdus�t|	|j�s�td||j|jt|	�|	f��t	|j
�r�|�
||	|�q�dS)zY
        Validate the return value to make sure it meets the interface contract.
        z%s.validate_output()z%s: need a %r; got a %r: %rrz%s: missing keys %r in %rz%s: unexpected keys %r in %rNz%%s:
  output[%r]: need %r; got %r: %r)r6r3r+r5r*r�rTr�r�r'r�)
r7rTrn�niceZexpected_setZ
actual_set�missingZextrar�r�r$r$r%r�s4

�

�
�
�
zCommand.validate_outputccs|jddd�EdHdS)NrVZhas)r8r�rHr$r$r%�get_output_params<szCommand.get_output_paramscCs|jr|j|SdSdSrG)�msg_summary)r7rTr$r$r%r�?s
zCommand.get_summary_defaultc	Cspttjtjtjtjd�}|�dd�D]F}z||d}Wn"tyZt�d�tj}Yn0||�d��q$dS)N)rz�info�warningr�rr$r*z'Server sent a message with a wrong typer�)r+ryrzr�r�r�r=r�)r7rTZlogger_functionsr��functionr$r$r%�log_messagesEs�
zCommand.log_messagescOs�t|t�sdSd}|�|�g}i}i}|��D],}	|�|	j�t|	j�||	j<|	j||	j<q0|�	dd�r||�
dd�d}
nd}
|�	dd�r�d}|jD�]L}|j|}d|jvr�q�|�	|�}
|d	kr�q�|��d
kr�|
dkr�d}n|��dkr�t
|
�dkr�q�d}t|t��r|�|
||||
�q�t|
ttf��rB|�|
||||
�q�t|t��rb|�|
||||
�q�t|
t��r�|�|
||||
�q�t|
t��r�|d
k�r�|�|
�n
|�|
�q�t|
t��r�q�t|
t�r�|�|
dt|j|j��q�|S)a
        Generic output method. Prints values the output argument according
        to their type and self.output.

        Entry attributes are labeled and printed in the order specified in
        self.output_params. Attributes that aren't present in
        self.output_params are not printed unless the command was invokend
        with the --all option. Attribute labelling is disabled if the --raw
        option was given.

        Subclasses can override this method, if custom output is needed.
        Nrr�FZdnTr�Z
no_displayr�r�r�Zfailedrpz%s %%d)r3r+r�rVr�r6rtr�r�r=r�rT�lowerr,rZ
print_entriesr2r�r
Zprint_entryZ
print_summaryZprint_indented�bool�intZprint_countr�)r7r�rTrPrQ�rv�order�labelsr�r�Z	print_allr�ZoutprUr$r$r%�output_for_cliTs^






zCommand.output_for_cli)r6r�rMcs<t�fdd��jD��}t����|d<t����|d<|S)Nc3s|]}|t�|�fVqdSrG)r(�r��arHr$r%r��sz#Command.__json__.<locals>.<genexpr>�
takes_args�
takes_options)r+�json_friendly_attributesr�r�r��r7Z	json_dictr$rHr%�__json__�s�zCommand.__json__c	cs\t�|i��|dg�}|D]<}|durPzt|d|�VWqVtyLYqV0q|VqdS)z!Yield callbacks of the given typeNz%s_callback)�_callback_registryr=r(�AttributeError)r[�
callback_type�	callbacks�callbackr$r$r%�
get_callbacks�szCommand.get_callbackscCs|||jvsJ�t|�sJ�t�|i�zt||}Wn$tyZdg}t||<Yn0|rn|�d|�n
|�|�dS)a�Register a callback

        :param callback_type: The callback type (e.g. 'pre', 'post')
        :param callback: The callable added
        :param first: If true, the new callback will be added before all
            existing callbacks; otherwise it's added after them

        Note that callbacks registered this way will be attached to this class
        only, not to its subclasses.
        Nr)�callback_typesr'r�
setdefaultr�r�r�)r[rr�firstrr$r$r%�register_callback�szCommand.register_callbackcCs|�d||�dS)z9Shortcut for register_callback('interactive_prompt', ...)rWN)r	)r[rrr$r$r%�$register_interactive_prompt_callback�sz,Command.register_interactive_prompt_callbackcCsdSrGr$r�r$r$r%�interactive_prompt_callback�sz#Command.interactive_prompt_callback)N)F)F)LrIrJrKrLr2r�r�r�
finalize_attrrPrQrRrSr#r�rTr�rVZhas_output_paramsr�r�rZ
msg_truncatedrrrv�classmethodZ_Command__topic_getterrZtopicrr_rNrarer�rcrwrir|rxr�r�r�r�r�r�r+r�rr�r~r�r�r�rsr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rr	r
r�
__classcell__r$r$r�r%rO~s�







#3
	�

-2$M

rOc@s*eZdZdZeded�d�fZdd�ZdS)�
LocalOrRemotea[
    A command that is explicitly executed locally or remotely.

    This is for commands that makes sense to execute either locally or
    remotely to return a perhaps different result.  The best example of
    this is the `ipalib.plugins.f_misc.env` plugin which returns the
    key/value pairs describing the configuration state: it can be
    zserver?z,Forward to server instead of running locally)r�cOs4|�dd�r$|jjs$|j|i|��S|j|i|��S)aw
        Dispatch to forward() or execute() based on ``server`` option.

        When running in a client context, this command is executed remotely if
        ``options['server']`` is true; otherwise it is executed locally.

        When running in a server context, this command is always executed
        locally and the value of ``options['server']`` is ignored.
        r�F)r=r;rur�r�rdr$r$r%r��s
zLocalOrRemote.runN)rIrJrKrLr
rr�r�r$r$r$r%r�s
��rc@s eZdZdZdd�Zdd�ZdS)�Localz�
    A command that is explicitly executed locally.

    This is for commands that makes sense to execute only locally
    such as the help command.
    cOs|j|i|��S)z.
        Dispatch to forward() onlly.
        )r�rdr$r$r%r�sz	Local.runcOs|j|i|��SrG)r�rdr$r$r%r�sz
Local.forwardN)rIrJrKrLr�r�r$r$r$r%r�srcs�eZdZe�d�Ze�d�Ze�d�Ze�d�Ze�d�Z	dZ
e�Z�fdd�Z
d	d
�Zdd�Zd
d�Zdd�ZdZdd�Z�ZS)�Object�backend�methodsrR�primary_key�params_minus_pkNcs�t|�d�ddd�|_|�d�dd�|��D�}t|�dkr`td	|jd
�dd�|D��f��t|�dkr�|d
|_	tdd�|��D�dd�|_
nd|_	|j|_
d|jvr�|j|jj
vr�|jj
|j|_tt|���dS)N�MethodF�	attr_name)rBZ	name_attrrRcSsg|]}|jr|�qSr$�rr�r$r$r%r�!r�z'Object._on_finalize.<locals>.<listcomp>r�z)%s (Object) has multiple primary keys: %srocss|]}|jVqdSrGr�r�r$r$r%r�&r�z&Object._on_finalize.<locals>.<genexpr>rcSsg|]}|js|�qSr$rr�r$r$r%r�,r�rArj)r�_Object__get_attrsrrFrRr-r�r6r{rrrC�backend_namerjrr�rr�)r7Zpkeysr�r$r%r�s,�
��
�
zObject._on_finalizecgsZt|�dkr&t|dttf�s&|d}t|�}|��D]}|j|vs6||vrNq6|Vq6dS)zA
        Yield all Param whose name is not in ``names``.
        r�rN)r-r3rr4�	frozensetrRr6)r7r�Zminusr?r$r$r%�params_minus7szObject.params_minuscOstd|j��dS)z'
        Construct an LDAP DN.
        z%s.get_dn()Nr�)r7rP�kwargsr$r$r%�get_dnCsz
Object.get_dnccsZ||jvrdS|j|}t|�tus(J�|�D]&}|||jurBq.|j|jkr.|Vq.dSrG)rCr*rr6�obj_name)r7r6rDZpluginr$r$r%Z__get_attrsIs


zObject.__get_attrsccs0|�d�D] }t|ttf�s J�t|�Vq
dS)zR
        This method gets called by `HasParam._create_param_namespace()`.
        rRN)r:r3r4rr)r7r>r$r$r%�
get_paramsTszObject.get_params)r6�takes_paramscs<t�fdd��jD��}�jr*�jj|d<t�j�|d<|S)Nc3s |]}|tt�|��fVqdSrG)rr(r�rHr$r%r�as�z"Object.__json__.<locals>.<genexpr>rr)r+r�rr6r�rr�r$rHr%r�`s�zObject.__json__)rIrJrKrrrrrRrrrr2r!r�rrrr r�r�rr$r$r�r%rs




rc@sDeZdZdZdZedd��Zedd��Zedd��Zed	d
��Z	dS)�	Attributea]
    Base class implementing the attribute-to-object association.

    `Attribute` plugins are associated with an `Object` plugin to group
    a common set of commands that operate on a common set of parameters.

    The association between attribute and object is done using a simple
    naming convention: the first part of the plugin class name (up to the
    first underscore) is the object name, and rest is the attribute name,
    as this table shows:

    ===============  ===========  ==============
    Class name       Object name  Attribute name
    ===============  ===========  ==============
    noun_verb        noun         verb
    user_add         user         add
    user_first_name  user         first_name
    ===============  ===========  ==============

    For example:

    >>> class user_add(Attribute):
    ...     pass
    ...
    >>> instance = user_add()
    >>> instance.obj_name
    'user'
    >>> instance.attr_name
    'add'

    In practice the `Attribute` class is not used directly, but rather is
    only the base class for the `Method` class.  Also see the `Object` class.
    �1cCs|j�d�dS)Nrr)r6�	partitionrHr$r$r%r�szAttribute.obj_namecCs|jdur|jjSdSdSrG)r#r`rHr$r$r%�
obj_full_name�s
zAttribute.obj_full_namecCs.d�|j�}|j�|�sJ�|jt|�d�S)Nz{}_)�formatrr6�
startswithr-)r7�prefixr$r$r%r�szAttribute.attr_namecCs0|jdur(|jdur(|jj|j|jfSdSdSrG)r�obj_versionrCrrHr$r$r%r#�sz
Attribute.objN)
rIrJrKrLr)rNrr%rr#r$r$r$r%r"ks!


r"cs(eZdZdZdZdZ�fdd�Z�ZS)ra�
    A command with an associated object.

    A `Method` plugin must have a corresponding `Object` plugin.  The
    association between object and method is done through a simple naming
    convention: the first part of the method name (up to the first under
    score) is the object name, as the examples in this table show:

    =============  ===========  ==============
    Method name    Object name  Attribute name
    =============  ===========  ==============
    user_add       user         add
    noun_verb      noun         verb
    door_open_now  door         open_now
    =============  ===========  ==============

    There are three different places a method can be accessed.  For example,
    say you created a `Method` plugin and its corresponding `Object` plugin
    like this:

    >>> from ipalib import create_api
    >>> api = create_api()
    >>> class user_add(Method):
    ...     def run(self, **options):
    ...             return dict(result='Added the user!')
    ...
    >>> class user(Object):
    ...     pass
    ...
    >>> api.add_plugin(user_add)
    >>> api.add_plugin(user)
    >>> api.finalize()

    First, the ``user_add`` plugin can be accessed through the ``api.Method``
    namespace:

    >>> list(api.Method)
    [<class '__main__.user_add'>]
    >>> api.Method.user_add(version=u'2.88')  # Will call user_add.run()
    {'result': 'Added the user!'}

    (The "version" argument is the API version to use.
    The current API version can be found in ipalib.version.API_VERSION.)

    Second, because `Method` is a subclass of `Command`, the ``user_add``
    plugin can also be accessed through the ``api.Command`` namespace:

    >>> list(api.Command)
    [<class '__main__.user_add'>]
    >>> api.Command.user_add(version=u'2.88') # Will call user_add.run()
    {'result': 'Added the user!'}

    And third, ``user_add`` can be accessed as an attribute on the ``user``
    `Object`:

    >>> list(api.Object)
    [<class '__main__.user'>]
    >>> list(api.Object.user.methods)
    ['add']
    >>> api.Object.user.methods.add(version=u'2.88') # Will call user_add.run()
    {'result': 'Added the user!'}

    The `Attribute` base class implements the naming convention for the
    attribute-to-object association.  Also see the `Object` class.
    Fc#sD|jdur,|j��D]}d|jvr$q|Vqtt|���EdHdS)Nr�)r#rRr�r�rr�)r7r?r�r$r%r��s

zMethod.get_output_params)rIrJrKrLZextra_options_firstZextra_args_firstr�rr$r$r�r%r�sArc@s eZdZdZdd�Zdd�ZdS)�Updatera�
    An LDAP update with an associated object (always update).

    All plugins that subclass from `Updater` will be automatically available
    as a server update function.

    Plugins that subclass from Updater are registered in the ``api.Updater``
    namespace. For example:

    >>> from ipalib import create_api
    >>> api = create_api()
    >>> class my(Object):
    ...     pass
    ...
    >>> api.add_plugin(my)
    >>> class my_update(Updater):
    ...     pass
    ...
    >>> api.add_plugin(my_update)
    >>> api.finalize()
    >>> list(api.Updater)
    [<class '__main__.my_update'>]
    >>> api.Updater.my_update # doctest:+ELLIPSIS
    ipalib.frontend.my_update()
    cKstd|j��dS)Nr�r��r7rQr$r$r%r�szUpdater.executecKst�d|j�|jfi|��S)Nzraw: %s)ryrzr6r�r+r$r$r%res�zUpdater.__call__N)rIrJrKrLr�rer$r$r$r%r*�sr*)?rLZloggingZsixZipapython.versionrZipapython.ipautilrZipalib.baserZipalib.plugablerrZipalib.parametersrrr	r
rZ
ipalib.outputrr
rZipalib.textrZ
ipalib.errorsrrrrrrrr^rrZipalib.requestrrZipalib.utilrrrZipalib.constantsrZPY3r4rtZ	getLoggerrIryr r&r)r,r0rrOrrrr"rr*r$r$r$r%�<module>sN$	
+g[=N