a F `vK@sddlZddlmZmZddlmZddlmZddlm Z ddl m Z m Z ddl mZmZddlmZmZddlZed d dd lmZeeZgd ZGd ddeZGdddedZGdddeZdS)N)ABCMetaabstractmethod)partial) ErrorMapper)Signal)get_xml"are_additional_arguments_supported)DBusSpecificationDBusSpecificationError) get_variantunwrap_variantGioz2.0)r ) GLibServerAbstractServerObjectHandlerServerObjectHandlerc@sheZdZdZedddZedddZedd Zed d Zed d Z eddZ eddZ dS)rz0The low-level DBus server library based on GLib.NcCs||||||dS)zEmit a DBus signal.N) emit_signal)cls connection object_pathinterface_name signal_name parametersZ destinationr9/usr/lib/python3.9/site-packages/dasbus/server/handler.pyr/szGLibServer.emit_signalrc Csftj|}t|j||fd}g}|js0td|jD] } ||| |dd} || q6t|j ||S)zRegister an object on DBus.) user_dataz$No DBus interfaces for registration.N) r Z DBusNodeInfoZ new_for_xmlr_object_callbackZ interfacesr register_objectappend_unregister_object) rrrZ object_xmlcallback callback_argsZ node_infoZmethod_call_closure registrationsZinterface_inforegistration_idrrrr;s4  zGLibServer.register_objectcCs|D]}||qdS)zUnregister an object from DBus.N)Zunregister_object)rrr!r"rrrr]szGLibServer._unregister_objectc Cs"|\} } | ||||g| RdS)Nr) rrsenderrr method_namer invocationrrr rrrrcszGLibServer._object_callbackcCs d|iS)a9Get information about the DBus call. Supported items: sender str: The bus name that invoked the method There can be more supported items in the future. :param invocation: an invocation of a DBus call :return: a dictionary of information about the DBus call r#)Z get_sender)rr%rrr get_call_infosszGLibServer.get_call_infocCs|||dS)zSet the error of the DBus call. :param invocation: an invocation of a DBus call :param error_name: a DBus name of the error :param error_message: an error message N)Zreturn_dbus_error)rr% error_nameZ error_messagerrrset_call_errorszGLibServer.set_call_errorcCs&d}|durt||f}||dS)zSet the reply of the DBus call. :param invocation: an invocation of a DBus call :param out_type: a type of the reply :param out_value: a value of the reply N)r Z return_value)rr%out_typeZ out_valueZ reply_valuerrrset_call_replys zGLibServer.set_call_reply)N)r) __name__ __module__ __qualname____doc__ classmethodrrrrr&r(r*rrrrr,s"  !    rc@seZdZdZgdZddZeddZddZe d d Z e d d Z e d dZ e ddZ e ddZddZddZddZddZe ddZdS)rz+The abstract handler of a published object. _message_bus _object_path_object_specificationcCs||_||_||_d|_dS)zCreate a new handler. :param message_bus: a message bus :param object_path: a DBus path of the object :param obj: a Python instance of the object Nr0)self message_busrobjrrr__init__sz$AbstractServerObjectHandler.__init__cCs|js||_|jS)zDBus specification.)r4_get_specificationr5rrr specifications z)AbstractServerObjectHandler.specificationcCst|S)zKGet the DBus specification. :return: a DBus specification )r Zfrom_xml_get_xml_specificationr:rrrr9sz.AbstractServerObjectHandler._get_specificationcCsdS)IGet the XML specification. :return: a XML specification rr:rrrr<sz2AbstractServerObjectHandler._get_xml_specificationcCsdS)zConnect the object to DBus. Handle emitted signals of the object with the _emit_signal method and handle incoming DBus calls with the _handle_call method. Nrr:rrrconnect_objectsz*AbstractServerObjectHandler.connect_objectcCsdS)zdDisconnect the object from DBus. Unregister the object and disconnect all signals. Nrr:rrrdisconnect_objectsz-AbstractServerObjectHandler.disconnect_objectcCsdS)zConnect a DBus signal. :param interface_name: a DBus interface name :param signal_name: a DBus signal name Nrr5rrrrr_connect_signalsz+AbstractServerObjectHandler._connect_signalcGsdS)zHandle a DBus signal. :param interface_name: a DBus interface name :param signal_name: a DBus name of the signal :param parameters: a signal parameters Nr)r5rrrrrr _emit_signalsz(AbstractServerObjectHandler._emit_signalcOs&|||}t|si}||i|S)a*Handle a DBus call. :param interface_name: a name of the interface :param method_name: a name of the called method :param parameters: parameters of the call :param additional_args: additional arguments of the call :return: a result of the DBus call ) _find_handlerr)r5rr$radditional_argshandlerrrr _handle_calls z(AbstractServerObjectHandler._handle_callcCs|j||S)zFind a specification of the DBus member. :param interface_name: a name of the interface :param member_name: a name of the member :return: a specification of the member )r;Z get_memberr5r member_namerrr_find_member_specsz-AbstractServerObjectHandler._find_member_speccCs0|||p|||}|s,td|||S)zFind a handler of a DBus member. :param interface_name: a name of the interface :param member_name: a name of the method :return: a handler z The member {}.{} has no handler.)_find_object_handler_find_default_handlerAttributeErrorformat)r5rrIrFrrrrDs  z)AbstractServerObjectHandler._find_handlercCst|j|dS)aeGet an object handler of a DBus call. By default, DBus interfaces with members of the same name are not supported, so the given interface name is not used to find the object handler. :param interface_name: a name of the interface. :param member_name: a name of the member :return: a handler or None N)getattrr3rHrrrrK s z0AbstractServerObjectHandler._find_object_handlercCsdS)Find a default handler of a DBus call. :param interface_name: a name of the interface :param member_name: a name of the member :return: a handler or None NrrHrrrrL-sz1AbstractServerObjectHandler._find_default_handlerN)r+r,r-r. __slots__r8propertyr;r9rr<r?r@rBrCrGrJrDrKrLrrrrrs,          r) metaclasscseZdZdZgdZdeeffdd ZddZdd Z d d Z d d Z ddZ ddZ ddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zed(d)ZZS)*rz+The handler of an object published on DBus.)_server_signal_factory _error_mapper_registrationsNcs2t|||||_||_|p$t|_g|_dS)aACreate a new handler. :param message_bus: a message bus :param object_path: a DBus path of the object :param obj: a Python instance of the object :param error_mapper: a DBus error mapper :param server: a DBus server library :param signal_factory: a signal factory N)superr8rTrUrrVrW)r5r6rr7Z error_mapperZserverZsignal_factory __class__rrr8Bs  zServerObjectHandler.__init__cCs t|jS)r=)rr3r:rrrr<Ssz*ServerObjectHandler._get_xml_specificationcCs||dS)zConnect the object to DBus.N)_register_object_connect_signalsr:rrrr?Zsz"ServerObjectHandler.connect_objectcCs|jr|j}|qdS)z Disconnect the object from DBus.N)rWpop)r5rrrrr@_s z%ServerObjectHandler.disconnect_objectcCs.|j|jj|j||j}|j|dS)zLRegister to DBus calls. :return: an unregistering callback N) rTrr1rr2r<_method_callbackrWr)r5Z unregisterrrrr[esz$ServerObjectHandler._register_objectcCs0|jjD]"}t|tjsq||j|jqdS)zConnect all DBus signals.N)r;members isinstancer rrBrname)r5memberrrrr\ss  z$ServerObjectHandler._connect_signalscCs>|||}|||}||t|j|}|j|dS)zConnect a DBus signal. :param interface_name: a DBus interface name :param signal_name: a DBus signal name :return: a disconnecting callback N) _find_emitterrDZconnectr disconnectrWr)r5rrrsignalrdrrrrB~s     z#ServerObjectHandler._connect_signalcCst|j||S)zFind an emitter of a DBus signal. :param interface_name: a DBus interface name :param signal_name: a DBus signal name :return: a callback )rrCrArrrrcsz!ServerObjectHandler._find_emittercGsH|||}|sd}|jdur*t|j|}|j|jj|j|||dS)zHandle a DBus signal. :param interface_name: a DBus interface name :param signal_name: a DBus signal name :param parameters: a signal parameters N)rJtyper rTrr1rr2)r5rrrrbrrrrCs   z ServerObjectHandler._emit_signalc CszL|||||}|||}|j||gt|Ri|}||||Wn4ty}z|||||WYd}~n d}~00dS)zThe callback for a DBus call. :param invocation: an invocation of the DBus call :param interface_name: a DBus interface name :param method_name: a DBus method name :param parameters: a variant of DBus arguments N)_get_additional_argumentsrJrGr _handle_method_result Exception_handle_method_error) r5r%rr$rrErbresulterrorrrrr^s> z$ServerObjectHandler._method_callbackcCsd|j|iS)aBGet additional arguments of a DBus call. Supported items: call_info dict: Information about the DBus call This method is useful for customizations. It shouldn't be changed in this library unless we make sure that the change won't break the existing use cases. :param invocation: an invocation of the DBus call :param interface_name: a DBus interface name :param method_name: a DBus method name :param parameters: a variant of DBus arguments :return: a dictionary of additional info Z call_info)rTr&)r5r%rr$rrrrrgs z-ServerObjectHandler._get_additional_argumentscCs:tjd||dd|jt|}|j||t|dS)aHandle an error of a DBus call. :param invocation: an invocation of the DBus call :param interface_name: a DBus interface name :param method_name: a DBus method name :param error: an exception raised during the call z,The call %s.%s has failed with an exception:T)exc_infoN)logZwarningrVZget_error_namerfrTr(str)r5r%rr$rlr'rrrrjs z(ServerObjectHandler._handle_method_errorcCs|j||j|dS)zHandle a result of a DBus call. :param invocation: an invocation of a DBus call :param method_spec: a method specification :param method_reply: a method reply N)rTr*r))r5r%Z method_specZ method_replyrrrrhs z)ServerObjectHandler._handle_method_resultcCsD|dkr@|dkr|jS|dkr$|jS|dkr2|jS|dkr@|jSdS)rPzorg.freedesktop.DBus.PropertiesZGetSetZGetAllZPropertiesChangedN) _get_property _set_property_get_all_properties_properties_changedrHrrrrL sz)ServerObjectHandler._find_default_handlercCs:|||}|js"td||t|j|}t|j|S)zThe default handler of the Get method. :param interface_name: an interface name :param property_name: a property name :return: a variant with a property value z#The property {}.{} is not readable.)rJreadablerMrNrOr3r rf)r5r property_namerbvaluerrrrqs  z!ServerObjectHandler._get_propertycCs8|||}|js"td||t|j|t|dS)zThe default handler of the Set method. :param interface_name: an interface name :param property_name: a property name :param property_value: a variant with a property value z#The property {}.{} is not writable.N)rJwritablerMrNsetattrr3r )r5rrvZproperty_valuerbrrrrr0s  z!ServerObjectHandler._set_propertycsfdd|jjDS)zFind all properties of the given interface. :param interface_name: an interface name :return: a list of property names cs.g|]&}t|tjr|jkr|jr|jqSr)r`r ZPropertyrrura).0rbrrr Fs   z.)r;r_r5rrr{r_find_all_properties@s z(ServerObjectHandler._find_all_propertiescsfddDS)zThe default handler of the GetAll method. :param interface_name: an interface name :return: a dictionary of properties csi|]}||qSr)rq)rzrvrr5rr Ssz;ServerObjectHandler._get_all_properties..)r~r}rrrrsMs z'ServerObjectHandler._get_all_propertiescCs|S)zXThe default handler of the PropertiesChanged method. :return: a signal )rUr:rrrrtXsz'ServerObjectHandler._properties_changed)r+r,r-r.rQrrr8r<r?r@r[r\rBrcrCr^rgrjrhrLrqrrr~rsrRrt __classcell__rrrYrr8s0  '   r)Zloggingabcrr functoolsrZ dasbus.errorrZ dasbus.signalrZdasbus.server.interfacerrZdasbus.specificationr r Z dasbus.typingr r ZgiZrequire_versionZ gi.repositoryr Z getLoggerr+rn__all__objectrrrrrrrs       r