Mini Shell

Direktori : /usr/lib/python3.6/site-packages/dasbus/server/__pycache__/
Upload File :
Current File : //usr/lib/python3.6/site-packages/dasbus/server/__pycache__/interface.cpython-36.pyc

3

��^�D�@s�ddlZddlZddlmZddlmZddlmZddlmZddl	m
Z
mZddlm
Z
ddlmZd	d
ddgZd
ZGdd�de�Zffdd
�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_xmlZ__dbus_xml__c@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)�selfrr�r�/usr/lib/python3.6/interface.py�__init__Fszdbus_signal.__init__cCs.|jdk	rdSdjt|�jj�|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__Ps

zdbus_signal.__set_name__cCs<|dkr|St||jd�}|dkr8|j�}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)�getattrrr�setattr)r�instancer�signalrrr�__get__ds
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__rrrrr"rrrrr/s

cs��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*t��f��}tj||�}t|t|�|S)N)r�DBusSpecificationGenerator�generate_specificationr�DBUS_XML_ATTRIBUTE)�clsr�xml)�interface_name�	namespacerr�	decorated�sz!dbus_interface.<locals>.decoratedr)r+r,r-r)r+r,rr�scCstj|�}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
cCs&t|td�}|dkr"tdjt���|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c@s�eZdZdZeZejd�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|j|�}|r6|j�}|j|�|j|||�}|||<|j||�}|jj|�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_clsr+�
interfacesZall_interfaces�	interface�noderrrr'�s

z1DBusSpecificationGenerator.generate_specificationcCs|jjtj�}|jj|�S)z�Collect standard interfaces.

        Standard interfaces are implemented by default.

        :return: a dictionary of standard interfaces
        )r4�xml_to_elementrZSTANDARD_INTERFACES�get_interfaces_from_node)r)r8rrrr0�sz7DBusSpecificationGenerator._collect_standard_interfacescCsVt�}xJttj|��D]8}t|td�}|s,q|jj|�}|jj|�}|j	|�qW|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(r4r9r:r1)r)r5r6�memberZ
member_xmlr8Znode_interfacesrrrr/s
z.DBusSpecificationGenerator._collect_interfacescCs�|jj|�}x�tj|�D]�\}}|j|�s,q|j||�r:q|j|�rR|j||�}n>|j|�rj|j	||�}n&|j
|�r�|j||�}ntdj
|���|jj||�qW|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 '{}'.)r4Zcreate_interfacer=Z
getmembers�_is_exportable�_is_defined�
_is_signal�_generate_signal�_is_property�_generate_property�
_is_method�_generate_methodrr�	add_child)r)r5r6r+r7�member_namer>�elementrrrr2s"



z.DBusSpecificationGenerator._generate_interfacecCst|jj|��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)rHrrrr?Fsz)DBusSpecificationGenerator._is_exportablecCsDx>|j�D]2}x,|D]$}|jj|�s&q|jj||�s6qdSWq
WdS)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)�valuesr4Z	is_memberZhas_name)r)r6rHr7r>rrrr@Os

z&DBusSpecificationGenerator._is_definedcCs
t|t�S)z"Is the class member a DBus signal?)�
isinstancer)r)r>rrrrAdsz%DBusSpecificationGenerator._is_signalc	Csx|jj|�}|j}|s|SxX|j|�D]J\}}}|tjkrHtdj|���tj}|jj|t	|�|�}|jj
||�q&W|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 '{}'.)r4Z
create_signalr�_iterate_parametersr�
DIRECTION_OUTrr�create_parameterrrG)	r)r>rHrI�methodr�	type_hint�	direction�	parameterrrrrBis 

z+DBusSpecificationGenerator._generate_signalccs�t|�}tj|�}x`t|j�dd�D]J}|j|j}|tjkrHtd��||kr^tdj	|���|||t
jfVq&W|j|j
kr�dS|jdkr�dSt
j|jt
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 '{}'.)rr=�	signature�listZ
parameters�kindrZPOSITIONAL_OR_KEYWORDrrrZDIRECTION_INZreturn_annotation�emptyZRETURN_PARAMETERrP)r)r>Z
type_hintsrWrrYrrrrO�s$


z.DBusSpecificationGenerator._iterate_parameterscCs
t|t�S)z$Is the class member a DBus property?)rN�property)r)r>rrrrC�sz'DBusSpecificationGenerator._is_propertycCs�d}d}yD|jr*|j|j�\\}}}tj}|jrJ|j|j�\\}}}tj}Wn"tk
rntdj|���YnX|jr�|jr�tj	}|dkr�tdj|���|j
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.)
�fsetrOrZACCESS_WRITE�fgetZACCESS_READ�
ValueErrorrrZACCESS_READWRITEr4Zcreate_propertyr)r)r>rH�accessrS�_rrrrD�s*

z-DBusSpecificationGenerator._generate_propertycCstj|�ptj|�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

        )r=ZismethodZ
isfunction)r)r>rrrrE�sz%DBusSpecificationGenerator._is_methodcCsL|jj|�}x:|j|�D],\}}}|jj|t|�|�}|jj||�qW|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
        )r4Z
create_methodrOrQrrG)r)r>rHrRrrSrTrUrrrrFsz+DBusSpecificationGenerator._generate_methodcCsL|jj�}|jj|dj|j��x&t|j��D]}|jj|||�q.W|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 {})r4Zcreate_nodeZadd_commentrr�sorted�keysrG)r)r5r6r8r+rrrr3s
z)DBusSpecificationGenerator._generate_node)N)rr#r$r%r	r4�re�compilerK�classmethodr'r0r/r2r?r@rArBrOrCrDrErFr3rrrrr&�s$

*	$0,r&)r=rcrZtypingrZdasbus.namespacerZ
dasbus.signalrZdasbus.specificationrrZ
dasbus.typingrZ
dasbus.xmlr	�__all__r(�objectrrr
r
r&rrrr�<module>s$R