%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /lib/python3.9/site-packages/dasbus/server/__pycache__/
Upload File :
Create Path :
Current File : //lib/python3.9/site-packages/dasbus/server/__pycache__/interface.cpython-39.pyc

a

F
�`�K�@s�ddlZddlZddlmZddlmZddlmZddlmZddl	m
Z
mZddlm
Z
ddlmZgd	�Zd
ZdZdd
�Zdd�ZGdd�de�Zddd�Zdd�Zdd�ZGdd�de�ZdS)�N)�	Parameter)�get_type_hints)�
get_dbus_name)�Signal)�DBusSpecificationError�DBusSpecification)�
get_dbus_type)�XMLGenerator)�
dbus_class�dbus_interface�dbus_signal�get_xml�accepts_additional_arguments�"are_additional_arguments_supportedZ__dbus_xml__Z-__dbus_handler_accepts_additional_arguments__cCst|td�|S)a[Decorator for accepting extra arguments in a DBus method.

    The decorator allows the server object handler to propagate
    additional information about the DBus call into the decorated
    method.

    Use a dictionary of keyword arguments:

    .. code-block:: python

        @accepts_additional_arguments
        def Method(x: Int, y: Str, **info):
            pass

    Or use keyword only parameters:

    .. code-block:: python

        @accepts_additional_arguments
        def Method(x: Int, y: Str, *, call_info):
            pass

    At this moment, the library provides only the call_info argument
    generated by GLibServer.get_call_info, but the additional arguments
    can be customized in the _get_additional_arguments method of the
    server object handler.

    :param method: a DBus method
    :return: a DBus method with a flag
    T)�setattr�&ACCEPTS_ADDITIONAL_ARGUMENTS_ATTRIBUTE��method�r�;/usr/lib/python3.9/site-packages/dasbus/server/interface.pyr5srcCst|td�S)zyDoes the given DBus method accept additional arguments?

    :param method: a DBus method
    :return: True or False
    F)�getattrrrrrrrXsrc@s6eZdZdZdefdd�Zdd�Zdd�Zd	d
�ZdS)ra�DBus signal.

    Can be used as:

    .. code-block:: python

        Signal = dbus_signal()

    Or as a method decorator:

    .. code-block:: python

        @dbus_signal
        def Signal(x: Int, y: Double):
            pass

    Signal is defined by the type hints of a decorated method.
    This method is accessible as: signal.definition

    If the signal is not defined by a method, it is expected to
    have no arguments and signal.definition is equal to None.
    NcCs||_||_d|_dS)z�Create a signal descriptor.

        :param definition: a definition of the emit function
        :param factory: a signal factory
        N)�
definition�factory�name)�selfrrrrr�__init__xszdbus_signal.__init__cCs.|jdurdSd�t|�j��|���|_dS)aVSet a name of the descriptor

        The descriptor has been assigned to the specified name.
        Generate a name of a private attribute that will be set
        to a signal in the ``__get__`` method.

        For example: ``__dbus_signal_my_name``

        :param owner: the owning class
        :param name: the descriptor name
        Nz__{}_{})r�format�type�__name__�lower)r�ownerrrrr�__set_name__�s
�zdbus_signal.__set_name__cCs<|dur|St||jd�}|dur8|��}t||j|�|S)a�Get a value of the descriptor.

        If the descriptor is accessed as a class attribute,
        return the descriptor.

        If the descriptor is accessed as an instance attribute,
        return a signal created by the signal factory.

        :param instance: an instance of the owning class
        :param owner: an owning class
        :return: a value of the attribute
        N)rrrr)r�instancer �signalrrr�__get__�s
zdbus_signal.__get__cCstd��dS)zSet a value of the descriptor.zCan't set DBus signal.N)�AttributeError)rr"�valuerrr�__set__�szdbus_signal.__set__)	r�
__module__�__qualname__�__doc__rrr!r$r'rrrrras

rrcs��fdd�}|S)aDBus interface.

    A new DBus interface can be defined as:

    .. code-block:: python

        @dbus_interface
        class Interface():
            ...

    The interface will be generated from the given class cls
    with a name interface_name and added to the DBus XML
    specification of the class.

    The XML specification is accessible as:
    .. code-block:: python

        Interface.__dbus_xml__

    :param interface_name: a DBus name of the interface
    :param namespace: a sequence of strings
    cs.tg����R�}t�||�}t|t|�|S)N)r�DBusSpecificationGenerator�generate_specificationr�DBUS_XML_ATTRIBUTE)�clsr�xml��interface_name�	namespacerr�	decorated�sz!dbus_interface.<locals>.decoratedr)r1r2r3rr0rr�srcCst�|�}t|t|�|S)a�DBus class.

    A new DBus class can be defined as:

    .. code-block:: python

        @dbus_class
        class Class(Interface):
            ...

    DBus class can implement DBus interfaces, but it cannot
    define a new interface.

    The DBus XML specification will be generated from
    implemented interfaces (inherited) and it will be
    accessible as:

    .. code-block:: python

        Class.__dbus_xml__

    )r+r,rr-)r.r/rrrr
�s
r
cCs&t|td�}|dur"td�t���|S)z�Return XML specification of an object.

    :param obj: an object decorated with @dbus_interface or @dbus_class
    :return: a string with XML specification
    Nz)XML specification is not defined at '{}'.)rr-rr)�objZxml_specificationrrrr
�s��r
c@s�eZdZdZeZe�d�Ze	d dd��Z
e	dd��Ze	dd	��Ze	d
d��Z
e	dd
��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��Ze	dd��ZdS)!r+z,Class for generating DBus XML specification.z[A-Z][A-Za-z0-9]*NcCsN|�|�}|r6|��}|�|�|�|||�}|||<|�||�}|j�|�S)aEGenerates DBus XML specification for given class.

        If class defines a new interface, it will be added to
        the specification.

        :param interface_cls: class object to decorate
        :param str interface_name: name of the interface defined by class
        :return str: DBus specification in XML
        )�_collect_interfaces�_collect_standard_interfaces�update�_generate_interface�_generate_node�
xml_generatorZelement_to_xml)r.�
interface_clsr1�
interfacesZall_interfaces�	interface�noderrrr,
s

�z1DBusSpecificationGenerator.generate_specificationcCs|j�tj�}|j�|�S)z�Collect standard interfaces.

        Standard interfaces are implemented by default.

        :return: a dictionary of standard interfaces
        )r:�xml_to_elementrZSTANDARD_INTERFACES�get_interfaces_from_node)r.r>rrrr6's�z7DBusSpecificationGenerator._collect_standard_interfacescCsRt�}tt�|��D]8}t|td�}|s*q|j�|�}|j�|�}|�	|�q|S)z�Collect interfaces implemented by the class.

        Returns a dictionary that maps interface names
        to interface elements.

        :param interface_cls: a class object
        :return: a dictionary of implemented interfaces
        N)
�dict�reversed�inspectZgetmrorr-r:r?r@r7)r.r;r<�memberZ
member_xmlr>Znode_interfacesrrrr54s
z.DBusSpecificationGenerator._collect_interfacescCs�|j�|�}t�|�D]�\}}|�|�s*q|�||�r8q|�|�rP|�||�}n>|�|�rh|�	||�}n&|�
|�r�|�||�}ntd�
|���|j�||�q|S)apGenerate interface defined by given class.

        :param interface_cls: a class object that defines the interface
        :param interfaces: a dictionary of implemented interfaces
        :param interface_name: a name of the new interface
        :return: a new interface element

        :raises DBusSpecificationError: if a class member cannot be exported
        z+Unsupported definition of DBus member '{}'.)r:Zcreate_interfacerCZ
getmembers�_is_exportable�_is_defined�
_is_signal�_generate_signal�_is_property�_generate_property�
_is_method�_generate_methodrr�	add_child)r.r;r<r1r=�member_namerD�elementrrrr8Ns&



��z.DBusSpecificationGenerator._generate_interfacecCst|j�|��S)z�Is the name of a class member exportable?

        The name is exportable if it follows the DBus specification.
        Only CamelCase names are allowed.
        )�bool�NAME_PATTERN�	fullmatch)r.rNrrrrExsz)DBusSpecificationGenerator._is_exportablecCs@|��D]2}|D](}|j�|�s"q|j�||�s2qdSqdS)z�Is the member name defined in given interfaces?

        :param interfaces: a dictionary of interfaces
        :param member_name: a name of the class member
        :return: True if the name is defined, otherwise False
        TF)�valuesr:Z	is_memberZhas_name)r.r<rNr=rDrrrrF�s
z&DBusSpecificationGenerator._is_definedcCs
t|t�S)z"Is the class member a DBus signal?)�
isinstancer�r.rDrrrrG�sz%DBusSpecificationGenerator._is_signalc	Cst|j�|�}|j}|s|S|�|�D]J\}}}|tjkrFtd�|���tj}|j�|t	|�|�}|j�
||�q$|S)z�Generate signal defined by a class member.

        :param member: a dbus_signal object.
        :param member_name: a name of the signal
        :return: a signal element

        raises DBusSpecificationError: if signal has defined return type
        z(Invalid return type of DBus signal '{}'.)r:Z
create_signalr�_iterate_parametersr�
DIRECTION_OUTrr�create_parameterrrM)	r.rDrNrOrr�	type_hint�	direction�	parameterrrrrH�s&

���z+DBusSpecificationGenerator._generate_signalccs�t|�}t�|�}t|j�dd�D]d}|j|j}|tjtjfvrNt	|�rNq$|tj
kr`td��||vrvtd�|���|||t
jfVq$|j|jur�dS|jdur�dSt
j|jt
jfVdS)z�Iterate over method parameters.

        For every parameter returns its name, a type hint and a direction.

        :param member: a method object
        :return: an iterator

        raises DBusSpecificationError: if parameters are invalid
        �Nz1Only positional or keyword arguments are allowed.z!Undefined type of parameter '{}'.)rrC�	signature�list�
parameters�kindrZVAR_KEYWORDZKEYWORD_ONLYrZPOSITIONAL_OR_KEYWORDrrrZDIRECTION_INZreturn_annotation�emptyZRETURN_PARAMETERrW)r.rDZ
type_hintsr]rr`rrrrV�s2
�
��
�z.DBusSpecificationGenerator._iterate_parameterscCs
t|t�S)z$Is the class member a DBus property?)rT�propertyrUrrrrI�sz'DBusSpecificationGenerator._is_propertycCs�d}d}zD|jr*|�|j�\\}}}tj}|jrJ|�|j�\\}}}tj}Wn"tyntd�|��d�Yn0|jr�|jr�tj	}|dur�td�|���|j
�|t|�|�S)z�Generate DBus property defined by class member.

        :param member: a property object
        :param member_name: a property name
        :return: a property element

        raises DBusSpecificationError: if the property is invalid
        Nz%Undefined type of DBus property '{}'.z%DBus property '{}' is not accessible.)
�fsetrVrZACCESS_WRITE�fgetZACCESS_READ�
ValueErrorrrZACCESS_READWRITEr:Zcreate_propertyr)r.rDrN�accessrY�_rrrrJ�s4

����z-DBusSpecificationGenerator._generate_propertycCst�|�pt�|�S)aIs the class member a DBus method?

        Ignore the difference between instance method and class method.

        For example:

        .. code-block:: python

            class Foo(object):
                def bar(cls, x):
                    pass

            inspect.isfunction(Foo.bar) # True
            inspect.isfunction(Foo().bar) # False

            inspect.ismethod(Foo.bar) # False
            inspect.ismethod(Foo().bar) # True

            _is_method(Foo.bar) # True
            _is_method(Foo().bar) # True

        )rCZismethodZ
isfunctionrUrrrrK'sz%DBusSpecificationGenerator._is_methodcCsH|j�|�}|�|�D],\}}}|j�|t|�|�}|j�||�q|S)z�Generate method defined by given class member.

        :param member: a method object
        :param member_name: a name of the method
        :return: a method element
        )r:Z
create_methodrVrXrrM)r.rDrNrrrYrZr[rrrrLAs
�z+DBusSpecificationGenerator._generate_methodcCsH|j��}|j�|d�|j��t|���D]}|j�|||�q,|S)z�Generate node element that specifies the given class.

        :param interface_cls: a class object
        :param interfaces: a dictionary of interfaces
        :return: a node element
        zSpecifies {})r:Zcreate_nodeZadd_commentrr�sorted�keysrM)r.r;r<r>r1rrrr9Vs
�z)DBusSpecificationGenerator._generate_node)N)rr(r)r*r	r:�re�compilerQ�classmethodr,r6r5r8rErFrGrHrVrIrJrKrLr9rrrrr+s>



)



#
6

+

r+)r)rCrjr�typingrZdasbus.namespacerZ
dasbus.signalrZdasbus.specificationrrZ
dasbus.typingrZ
dasbus.xmlr	�__all__r-rrr�objectrrr
r
r+rrrr�<module>s&
�#	R
 

Zerion Mini Shell 1.0