a F `62@sddlZddlmZddlmZddlmZmZmZm Z m Z m Z gdZ dZ GdddeZGd d d eZGd d d eZGd ddeZGdddedZddZGdddeZdddZddZdS)N)ABCMeta)get_type_hints) get_variantget_type_argumentsunwrap_variant is_base_type StructureList)DBusStructureErrorDBusDatagenerate_string_from_data compare_dataZ__dbus_fields__c@seZdZdZdS)r z,General exception for DBus structure errors.N)__name__ __module__ __qualname____doc__rr4/usr/lib/python3.9/site-packages/dasbus/structure.pyr (sr c@sjeZdZdZdddZeddZeddZed d Zed d Z d dZ ddZ ddZ ddZ dS) DBusFieldz+Description of a field in a DBus structure.cCs||_||_||_dS)zCreate a description of the field. :param name: a name of the field :param type_hint: a type hint :param description: a description N)_name _type_hint _description)selfname type_hint descriptionrrr__init__0szDBusField.__init__cCs|jS)z4Name of the field. :return: a name )rrrrrr;szDBusField.namecCs|jS)z>Type hint of the field. :return: a type hint )rrrrrrCszDBusField.type_hintcCs|jS)zBDescription of the field. :return: a description )rrrrrrKszDBusField.descriptioncCs|jddS)zQReturn name of a data attribute. :return: a data attribute name -_)rreplacerrrr data_nameSszDBusField.data_namecCst||j|dS)zaSet the data attribute. :param obj: a data object :param value: a value N)setattrr"robjvaluerrrset_data[szDBusField.set_datacCs||t|dS)ztSet the data attribute from a variant. :param obj: a data object :param variant: a variant N)r'r)rr%variantrrrset_data_variantcszDBusField.set_data_variantcCs t||jS)z\Get the data attribute. :param obj: a data object :return: a value )getattrr"rr%rrrget_datakszDBusField.get_datacCst|j||S)zkGet a variant of the data attribute. :param obj: a data object :return: a variant )rrr,r+rrrget_data_variantsszDBusField.get_data_variantN)r)rrrrrpropertyrrrr"r'r)r,r-rrrrr-s     rcsBeZdZdZd fdd ZeddZfddZd d ZZ S) DBusDataFieldz0Description of a data field in a DBus structure.rcst|t|||_dSzCreate a description of the field. :param name: a name of the field :param data_type: a subclass of DBusData :param description: a description N)superrr _data_typerr data_typer __class__rrrszDBusDataField.__init__cCs|jSzMType of the data structure. :return: a subclass of DBusData r2rrrrr4szDBusDataField.data_typecs|j|}t||dSzSet the data attribute.N)r2from_structurer1r'r$r5rrr's zDBusDataField.set_datacCs|j||}t|j|Sz$Get a variant of the data attribute.)r2 to_structurer,rrr$rrrr-szDBusDataField.get_data_variant)r rrrrrr.r4r'r- __classcell__rrr5rr/|s    r/csBeZdZdZd fdd ZeddZfddZd d ZZ S) DBusDataListFieldz5Description of a data list field in a DBus structure.rcst|tt|||_dSr0)r1rr rr2r3r5rrrszDBusDataListField.__init__cCs|jSr7r8rrrrr4szDBusDataListField.data_typecs|j|}t||dSr9)r2from_structure_listr1r'r$r5rrr's zDBusDataListField.set_datacCs|j||}t|j|Sr;)r2to_structure_listr,rrr$rrrr-sz"DBusDataListField.get_data_variant)rr=rrr5rr?s    r?csxeZdZdZfddZeedddZeeddd Zee ed d d Z ee edd dZ ddZ Z S)r zObject representation of data in a DBus structure. Classes derived from this class should represent specific types of DBus structures. They will support a conversion from a DBus structure of this type to a Python object and back. cs(tj|i|t|tt|dS)zCreate a new data class.N)r1__init_subclass__r#DBUS_FIELDS_ATTRIBUTEDBusFieldFactorygenerate_fields)clsargskwargsr5rrrBs zDBusData.__init_subclass__) structurecCslt|tstdt|j|}t|}|D]2\}}||d}|sZt d|| ||q4|S)z~Convert a DBus structure to a data object. :param structure: a DBus structure :return: a data object Invalid type '{}'.NzField '{}' doesn't exist.) isinstancedict TypeErrorformattyper get_fieldsitemsgetr r))rFrIdatafieldsrr(fieldrrrr:s  zDBusData.from_structure)returncCsNt||stdt|ji}t|}|D]\}}||||<q2|S)zYConvert this data object to a DBus structure. :return: a DBus structure rJ)rKrMrNrOrrPrQr-)rFrSrIrTrrUrrrr<s zDBusData.to_structure) structurescCs.t|tstdt|jtt|j|S)zConvert DBus structures to data objects. :param structures: a list of DBus structures :return: a list of data objects rJ)rKlistrMrNrOrmapr:)rFrWrrrr@s  zDBusData.from_structure_listcCstt|j|S)zConvert data objects to DBus structures. :param objects: a list of data objects :return: a list of DBus structures )rXrYr<)rFZobjectsrrrrA szDBusData.to_structure_listcCst|S)z%Convert this data object to a string.)r rrrr__repr__szDBusData.__repr__)rrrrrB classmethodrr:r<r r@rArZr>rrr5rr s  r ) metaclasscCs&t|td}|dur"tdt|S)zjReturn DBus fields of a data object. :param obj: a data object :return: a map of DBus fields NzFields are not defined at '{}'.)r*rCr rN)r%rTrrrrPs  rPc@sLeZdZdZeddZeddZeddZedd Zed d Z d S) rDzA DBus field factory.cCs\i}t|D]<\}}|||s$q||}|||}|||||<q|sXtd|S)aGenerate DBus fields from properties of a class. Properties of the class will be used to generate a map of a DBus fields. The property should have a getter and a setter, otherwise an error is raised. The type hint of the getter is used to define the type of the DBus field. :param data_class: a data class :return: a map of DBus fields :raise DBusStructureError: if the DBus fields cannot be generated zNo fields found.)inspectZ getmembers _is_field_get_field_name_get_member_hint _create_fieldr )rFZ data_classrT member_namememberrrrrrrE,s   z DBusFieldFactory.generate_fieldscCs |drdSt|tsdSdS)zIs the member a representation of a DBus field? :param member_name: a name of the class member :param member: a class member :return: True or False r FT) startswithrKr.)rFrbrcrrrr^Is  zDBusFieldFactory._is_fieldcCs |ddS)zGet the name of the DBus field. :param member_name: a name of the class member :return: a name of the DBus field r r)r!)rFrbrrrr_[sz DBusFieldFactory._get_field_namecCsT|jstd||js(td|t|j}|dd}|sPtd||S)zGet the type hint of the member. :param field_name: a name of the DBus field :param member: a class member :return: a type hint zField '{}' cannot be set.zField '{}' cannot be get.rVNzField '{}' has unknown type.)fsetr rNfgetrrR)rF field_namercZgetter_type_hintsrrrrr`ds  z!DBusFieldFactory._get_member_hintcCsFt|trt||St|trsz-generate_string_from_data..z{}({})z, ) rPvaluesr,r"rXpoprLsortedrQrNr6rjoin)r%skipaddZ dictionaryrUrZ attributesrrrr sr cCs(t|to&t|to&||||kS)zCompare data of the given data objects. :param obj: a data object :param other: another data object :return: True if the data is equal, otherwise False )rKr r<)r%otherrrrr s  r )NN)r]abcrtypingrZ dasbus.typingrrrrrr __all__rC Exceptionr objectrr/r?r rPrDr r rrrrs    O  ]k