~simpoir/landscape-client/py3fix-network-usage

« back to all changes in this revision

Viewing changes to landscape/lib/bpickle_dbus.py

  • Committer: Adam Collard
  • Date: 2017-01-05 11:19:41 UTC
  • mto: This revision was merged to the branch mainline in revision 930.
  • Revision ID: adam.collard@canonical.com-20170105111941-pov96h7g5hc2myb6
Remove dbus code, KIWF v1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Different versions of the Python DBus bindings return different types
3
 
to represent integers, strings, lists, etc.  Older versions return
4
 
builtin Python types: C{int}, C{str}, C{list}, etc.  Newer versions
5
 
return DBus-specific wrappers: C{Int16}, C{String}, C{Array}, etc.
6
 
Failures occur when DBus types are used because bpickle doesn't know
7
 
that an C{Int16} is really an C{int} and that an C{Array} is really a
8
 
C{list}.
9
 
 
10
 
L{install} and L{uninstall} can install and remove extensions that
11
 
make bpickle work with DBus types.
12
 
"""
13
 
 
14
 
import dbus
15
 
 
16
 
from landscape.lib import bpickle
17
 
 
18
 
 
19
 
def install():
20
 
    """Install bpickle extensions for DBus types."""
21
 
    for type, function in get_dbus_types():
22
 
        bpickle.dumps_table[type] = function
23
 
 
24
 
 
25
 
def uninstall():
26
 
    """Uninstall bpickle extensions for DBus types."""
27
 
    for type, function in get_dbus_types():
28
 
        del bpickle.dumps_table[type]
29
 
 
30
 
 
31
 
def dumps_utf8string(obj):
32
 
    """
33
 
    Convert the specified L{dbus.types.UTF8String} to bpickle's
34
 
    representation for C{unicode} data.
35
 
    """
36
 
    return "u%s:%s" % (len(obj), obj)
37
 
 
38
 
 
39
 
def dumps_double(obj):
40
 
    """
41
 
    Convert a dbus.types.Double into a floating point representation.
42
 
    """
43
 
    return "f%r;" % float(obj)
44
 
 
45
 
 
46
 
def get_dbus_types():
47
 
    """
48
 
    Generator yields C{(type, bpickle_function)} for available DBus
49
 
    types.
50
 
    """
51
 
    for (type_name, function) in [("Boolean", bpickle.dumps_bool),
52
 
                                  ("Int16", bpickle.dumps_int),
53
 
                                  ("UInt16", bpickle.dumps_int),
54
 
                                  ("Int32", bpickle.dumps_int),
55
 
                                  ("UInt32", bpickle.dumps_int),
56
 
                                  ("Int64", bpickle.dumps_int),
57
 
                                  ("UInt64", bpickle.dumps_int),
58
 
                                  ("Double", dumps_double),
59
 
                                  ("Array", bpickle.dumps_list),
60
 
                                  ("Dictionary", bpickle.dumps_dict),
61
 
                                  ("String", bpickle.dumps_unicode),
62
 
                                  ("UTF8String", dumps_utf8string)]:
63
 
        type = getattr(dbus.types, type_name, None)
64
 
        if type is not None:
65
 
            yield type, function