into-dbus-python


Nameinto-dbus-python JSON
Version 0.8.2 PyPI version JSON
download
home_pagehttps://github.com/stratis-storage/into-dbus-python
Summary"transforms values into properly wrapped dbus-python objects"
upload_time2023-05-04 13:08:41
maintainer
docs_urlNone
authorAnne Mulhern
requires_python
licenseApache-2.0
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            A transformer to dbus-python types
==================================

Facilities for converting an object that inhabits core Python types, e.g.,
lists, ints, dicts, to an object that inhabits dbus-python types, e.g.,
dbus.Array, dbus.UInt32, dbus.Dictionary based on a specified dbus signature.

Motivation
----------

The dbus-python library is a library of python bindings for libdbus. It does
not provide facilities to ensure that the types of the values that client code
places on the D-Bus conform to the required signature. The client code may
either be a D-Bus service, so that the values that it places on the D-Bus
should conform to the signature that it specifies, or, in some cases, a client
of the service, which must conform to the specifications of the service.

If a service implements the Introspectable interface on its objects,
dbus-python will use the signature information to massage client messages
into the correct dbus types. If the Introspectable interface is unavailable,
dbus-python will guess the signature by recursively examining the values of
the arguments, and will then proceed the same as before. If the signature
contains a 'v', indicating a variant type, dbus-python must guess the type
of the correspdoning value. dbus-python can be instructed not to make use of
dbus introspection by setting the introspect parameter to false in the
appropriate methods.

This library provides facilities to ensure that values placed on the D-Bus
conform to a given signature, by wrapping the values in the appropriate
constructors for this signature. It generates correct functions for any
valid signature.

Usage and Implementation Hints
------------------------------

Usage of the library is fairly straightforward::

   >>> from into_dbus_python import xformers
   >>> funcs = xformers("adq")
   >>> len(funcs)
   2

Note that the length of the list of functions is the same as the number of
complete types in the signature. Each element in the list of functions is
a tuple. ::

    >>> funcs[0]
    (<function ToDbusXformer._handleArray.<locals>.the_func at 0x7f4542f2d730>, 'ad')

The first element is the function itself, the second is a string which
matches the complete type signature for which this function yields values of
the correct type. Applying this function yields the actual value ::

    >>> funcs[0][0]([2.3, 37.5])
    (dbus.Array([dbus.Double(2.3), dbus.Double(37.5)], signature=dbus.Signature('d')), 0)

In this example, the signature was "ad" so the resulting value is a dbus.Array
of dbus.Double objects. The signature parameter has the appropriate value;
it is just 'd', the symbol for the type of the elements in the array,
double. Note that the function also yields a tuple, the converted value and
an int, which represents the variant level. Since there was no "v" in the
signature, the variant level is 0.

The generated functions will fail with an IntoDPError if passed invalid
arguments. ::

    >>> try:
    ...     funcs[0][0](True)
    ... except IntoDPError as err:
    ...     print("bad arg")
    ...
    bad arg

If any of the functions raises an exception that is not a subtype of
IntoDPError this constitutes a bug and is not part of the public API.

Conveniences
------------
The parser itself returns a list of tuples, of which generally only the first
element in the tuple is of interest to the client. The second element, the
string matched, is a necessary result for the recursive implementation,
but is not generally useful to the client. The resulting functions each
return a tuple of the transformed value and the variant level, generally only
the transformed value is of interest to the client.

For this reason, the library provides a convenience function, xformer(),
which takes a signature and returns a function, which takes a list of objects
and returns the list, transformed to appropriate dbus types. It can be used
in the following way::

    >>> from into_dbus_python import xformer
    >>> func = xformer("adq")
    >>> func([[2.3, 34.0], 3])
    [dbus.Array([dbus.Double(2.3), dbus.Double(34.0)], signature=dbus.Signature('d')), dbus.UInt16(3)]

Note that the function must take a list of values, one for each complete type
in the signature. Here, there are two complete types "ad", and "q", and there
are two resulting values.

If the signature contains a "v", for a variant type, the value must be a pair
of a signature and a value that inhabits that type. For example, ::

    >>> func = xformer("v")
    >>> func([("aq", [0, 1])])
    [dbus.Array([dbus.UInt16(0), dbus.UInt16(1)], signature=dbus.Signature('q'), variant_level=1)]

Note that the variant level of the constructed Array object is 1. A non-zero
variant level in the dbus object indicates that the object is a variant.
In this example the variant level is just 1. Further nesting of variants is
permitted, the variant level increases by one with each level. ::

    >>> func([("av", [("q", 0)])])
    [dbus.Array([dbus.UInt16(0, variant_level=1)], signature=dbus.Signature('v'), variant_level=2)]

Here the variant level of the variant element in the array, 0, is 1, but the
variant level of the whole array is 2, since the array inhabits a variant type
and contains a variant element.

Restrictions on Core Types
--------------------------
The generated functions place as few restrictions as possible on the types
of the values to be transformed. Generally speaking, a tuple is as good as a
list, since both are iterable. ::

    >>> func = xformer("adq")
    >>> func([(2.3, 34.0), 3])
    [dbus.Array([dbus.Double(2.3), dbus.Double(34.0)], signature=dbus.Signature('d')), dbus.UInt16(3)]

However, the inhabitant of a dbus.Dictionary type must be an object with an
items() method which yields pairs of keys and values, e.g., a dict.

The signature() function
------------------------
This library also exposes a function, signature(), which, given a value in
dbus-python types, calculates its signature. It has the following relation
to the xformer() function.

Let S be a signature. Let C be a list of values in Python core types.
Let V = xformer(S)(C). Then "".join(signature(v) for v in V) is equal to S.

Technical Remarks
-----------------

This package extends the parser for dbus signatures implemented in the
dbus-signature-pyparsing package
(https://github.com/stratis-storage/dbus-signature-pyparsing)
by adding actions to the individual parsers using the setParseAction() method.

The package has undergone significant testing using the Hypothesis testing
library (http://hypothesis.works/) and the external Hypothesis strategy
implemented in the hs-dbus-signature package
(https://github.com/stratis-storage/hs-dbus-signature).

Downstream packagers, if incorporating testing into their packaging, are
encouraged to use only the tests in the test_deterministic.py module, to
avoid testing failures that may arise due to the non-deterministic behavior
of Hypothesis tests.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/stratis-storage/into-dbus-python",
    "name": "into-dbus-python",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Anne Mulhern",
    "author_email": "amulhern@redhat.com",
    "download_url": "https://files.pythonhosted.org/packages/8e/fa/b2bd260802048ce8e67d924038335956bbcdb2e913505c925240c4a279ce/into-dbus-python-0.8.2.tar.gz",
    "platform": "Linux",
    "description": "A transformer to dbus-python types\n==================================\n\nFacilities for converting an object that inhabits core Python types, e.g.,\nlists, ints, dicts, to an object that inhabits dbus-python types, e.g.,\ndbus.Array, dbus.UInt32, dbus.Dictionary based on a specified dbus signature.\n\nMotivation\n----------\n\nThe dbus-python library is a library of python bindings for libdbus. It does\nnot provide facilities to ensure that the types of the values that client code\nplaces on the D-Bus conform to the required signature. The client code may\neither be a D-Bus service, so that the values that it places on the D-Bus\nshould conform to the signature that it specifies, or, in some cases, a client\nof the service, which must conform to the specifications of the service.\n\nIf a service implements the Introspectable interface on its objects,\ndbus-python will use the signature information to massage client messages\ninto the correct dbus types. If the Introspectable interface is unavailable,\ndbus-python will guess the signature by recursively examining the values of\nthe arguments, and will then proceed the same as before. If the signature\ncontains a 'v', indicating a variant type, dbus-python must guess the type\nof the correspdoning value. dbus-python can be instructed not to make use of\ndbus introspection by setting the introspect parameter to false in the\nappropriate methods.\n\nThis library provides facilities to ensure that values placed on the D-Bus\nconform to a given signature, by wrapping the values in the appropriate\nconstructors for this signature. It generates correct functions for any\nvalid signature.\n\nUsage and Implementation Hints\n------------------------------\n\nUsage of the library is fairly straightforward::\n\n   >>> from into_dbus_python import xformers\n   >>> funcs = xformers(\"adq\")\n   >>> len(funcs)\n   2\n\nNote that the length of the list of functions is the same as the number of\ncomplete types in the signature. Each element in the list of functions is\na tuple. ::\n\n    >>> funcs[0]\n    (<function ToDbusXformer._handleArray.<locals>.the_func at 0x7f4542f2d730>, 'ad')\n\nThe first element is the function itself, the second is a string which\nmatches the complete type signature for which this function yields values of\nthe correct type. Applying this function yields the actual value ::\n\n    >>> funcs[0][0]([2.3, 37.5])\n    (dbus.Array([dbus.Double(2.3), dbus.Double(37.5)], signature=dbus.Signature('d')), 0)\n\nIn this example, the signature was \"ad\" so the resulting value is a dbus.Array\nof dbus.Double objects. The signature parameter has the appropriate value;\nit is just 'd', the symbol for the type of the elements in the array,\ndouble. Note that the function also yields a tuple, the converted value and\nan int, which represents the variant level. Since there was no \"v\" in the\nsignature, the variant level is 0.\n\nThe generated functions will fail with an IntoDPError if passed invalid\narguments. ::\n\n    >>> try:\n    ...     funcs[0][0](True)\n    ... except IntoDPError as err:\n    ...     print(\"bad arg\")\n    ...\n    bad arg\n\nIf any of the functions raises an exception that is not a subtype of\nIntoDPError this constitutes a bug and is not part of the public API.\n\nConveniences\n------------\nThe parser itself returns a list of tuples, of which generally only the first\nelement in the tuple is of interest to the client. The second element, the\nstring matched, is a necessary result for the recursive implementation,\nbut is not generally useful to the client. The resulting functions each\nreturn a tuple of the transformed value and the variant level, generally only\nthe transformed value is of interest to the client.\n\nFor this reason, the library provides a convenience function, xformer(),\nwhich takes a signature and returns a function, which takes a list of objects\nand returns the list, transformed to appropriate dbus types. It can be used\nin the following way::\n\n    >>> from into_dbus_python import xformer\n    >>> func = xformer(\"adq\")\n    >>> func([[2.3, 34.0], 3])\n    [dbus.Array([dbus.Double(2.3), dbus.Double(34.0)], signature=dbus.Signature('d')), dbus.UInt16(3)]\n\nNote that the function must take a list of values, one for each complete type\nin the signature. Here, there are two complete types \"ad\", and \"q\", and there\nare two resulting values.\n\nIf the signature contains a \"v\", for a variant type, the value must be a pair\nof a signature and a value that inhabits that type. For example, ::\n\n    >>> func = xformer(\"v\")\n    >>> func([(\"aq\", [0, 1])])\n    [dbus.Array([dbus.UInt16(0), dbus.UInt16(1)], signature=dbus.Signature('q'), variant_level=1)]\n\nNote that the variant level of the constructed Array object is 1. A non-zero\nvariant level in the dbus object indicates that the object is a variant.\nIn this example the variant level is just 1. Further nesting of variants is\npermitted, the variant level increases by one with each level. ::\n\n    >>> func([(\"av\", [(\"q\", 0)])])\n    [dbus.Array([dbus.UInt16(0, variant_level=1)], signature=dbus.Signature('v'), variant_level=2)]\n\nHere the variant level of the variant element in the array, 0, is 1, but the\nvariant level of the whole array is 2, since the array inhabits a variant type\nand contains a variant element.\n\nRestrictions on Core Types\n--------------------------\nThe generated functions place as few restrictions as possible on the types\nof the values to be transformed. Generally speaking, a tuple is as good as a\nlist, since both are iterable. ::\n\n    >>> func = xformer(\"adq\")\n    >>> func([(2.3, 34.0), 3])\n    [dbus.Array([dbus.Double(2.3), dbus.Double(34.0)], signature=dbus.Signature('d')), dbus.UInt16(3)]\n\nHowever, the inhabitant of a dbus.Dictionary type must be an object with an\nitems() method which yields pairs of keys and values, e.g., a dict.\n\nThe signature() function\n------------------------\nThis library also exposes a function, signature(), which, given a value in\ndbus-python types, calculates its signature. It has the following relation\nto the xformer() function.\n\nLet S be a signature. Let C be a list of values in Python core types.\nLet V = xformer(S)(C). Then \"\".join(signature(v) for v in V) is equal to S.\n\nTechnical Remarks\n-----------------\n\nThis package extends the parser for dbus signatures implemented in the\ndbus-signature-pyparsing package\n(https://github.com/stratis-storage/dbus-signature-pyparsing)\nby adding actions to the individual parsers using the setParseAction() method.\n\nThe package has undergone significant testing using the Hypothesis testing\nlibrary (http://hypothesis.works/) and the external Hypothesis strategy\nimplemented in the hs-dbus-signature package\n(https://github.com/stratis-storage/hs-dbus-signature).\n\nDownstream packagers, if incorporating testing into their packaging, are\nencouraged to use only the tests in the test_deterministic.py module, to\navoid testing failures that may arise due to the non-deterministic behavior\nof Hypothesis tests.\n",
    "bugtrack_url": null,
    "license": "Apache-2.0",
    "summary": "\"transforms values into properly wrapped dbus-python objects\"",
    "version": "0.8.2",
    "project_urls": {
        "Homepage": "https://github.com/stratis-storage/into-dbus-python"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d6e3df3f99589f4374976ccfd90e87871970ce98e5faeb5bcf46d23540654426",
                "md5": "b9fcbcc2d768b78f74f45d0069142065",
                "sha256": "32963ab73f32c4130a7e1cd6e0f15a7b3bf6587b0ec0682640946f2d7e17f631"
            },
            "downloads": -1,
            "filename": "into_dbus_python-0.8.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b9fcbcc2d768b78f74f45d0069142065",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 15021,
            "upload_time": "2023-05-04T13:08:39",
            "upload_time_iso_8601": "2023-05-04T13:08:39.313227Z",
            "url": "https://files.pythonhosted.org/packages/d6/e3/df3f99589f4374976ccfd90e87871970ce98e5faeb5bcf46d23540654426/into_dbus_python-0.8.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "8efab2bd260802048ce8e67d924038335956bbcdb2e913505c925240c4a279ce",
                "md5": "dd39fc4e9d22511c70087312f44c7111",
                "sha256": "f3b5c85e547294f0a28bafa16a3f6a2205e2f12c99ecac1677bd370edd21de8b"
            },
            "downloads": -1,
            "filename": "into-dbus-python-0.8.2.tar.gz",
            "has_sig": false,
            "md5_digest": "dd39fc4e9d22511c70087312f44c7111",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 17870,
            "upload_time": "2023-05-04T13:08:41",
            "upload_time_iso_8601": "2023-05-04T13:08:41.149104Z",
            "url": "https://files.pythonhosted.org/packages/8e/fa/b2bd260802048ce8e67d924038335956bbcdb2e913505c925240c4a279ce/into-dbus-python-0.8.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-04 13:08:41",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "stratis-storage",
    "github_project": "into-dbus-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "into-dbus-python"
}
        
Elapsed time: 0.14621s