diff options
Diffstat (limited to 'gnunet/_dbus_utils.py')
-rw-r--r-- | gnunet/_dbus_utils.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/gnunet/_dbus_utils.py b/gnunet/_dbus_utils.py new file mode 100644 index 0000000..4e08c2a --- /dev/null +++ b/gnunet/_dbus_utils.py | |||
@@ -0,0 +1,65 @@ | |||
1 | import dbus | ||
2 | import threading | ||
3 | import datetime | ||
4 | from gi.repository import Gtk | ||
5 | |||
6 | from dbus.mainloop.glib import DBusGMainLoop, threads_init | ||
7 | threads_init() | ||
8 | DBusGMainLoop(set_as_default=True) | ||
9 | sysbus = dbus.SystemBus() | ||
10 | |||
11 | class MainLoop(threading.Thread): | ||
12 | def __init__(self): | ||
13 | threading.Thread.__init__(self) | ||
14 | self.daemon = True | ||
15 | self.start() | ||
16 | |||
17 | def run(self): | ||
18 | Gtk.main() | ||
19 | |||
20 | MainLoop() | ||
21 | |||
22 | from gnunet import _Key, GNUNetDaemonError | ||
23 | import gnunet.strings as strings | ||
24 | |||
25 | def pythonize(arg, argtype): | ||
26 | if argtype is datetime.datetime: | ||
27 | if isinstance(arg, str): | ||
28 | return strings.string_to_absolute_time(arg) | ||
29 | if isinstance(arg. dbus.UInt64): | ||
30 | return datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=arg) | ||
31 | return datatime.datetime(arg) | ||
32 | |||
33 | def dbusize(arg, pretty): | ||
34 | if isinstance(arg, _Key): | ||
35 | if pretty: | ||
36 | return dbus.String(arg, variant_level=1) | ||
37 | else: | ||
38 | return dbus.Array(arg._data[:], variant_level=1, signature="y") | ||
39 | |||
40 | #if type(arg) is gnsrecord.Data: | ||
41 | #return dbus.Struct([arg._recordtype, | ||
42 | |||
43 | if isinstance(arg, datetime.datetime): | ||
44 | if pretty: | ||
45 | return dbus.String(strings.absolute_time_to_string(arg), variant_level=1) | ||
46 | else: | ||
47 | return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1) | ||
48 | |||
49 | def handle_exception(e, daemon, daemon_address): | ||
50 | name = e.get_dbus_name() | ||
51 | message = e.get_dbus_message() | ||
52 | if not name.startswith("org.freedesktop.DBus.Error."): | ||
53 | raise e | ||
54 | name = name[len("org.freedesktop.DBus.Error."):] | ||
55 | |||
56 | if name == "Failed" or name == "InvalidArgs": | ||
57 | raise GNUNetDaemonError(message) | ||
58 | if name == "NoMemory": | ||
59 | raise MemoryError(message) | ||
60 | if name == "ServiceUnknown" or name == "NameHasNoOwner": | ||
61 | raise GNUNetDaemonError("Failed to contact " + daemon + " daemon at " + daemon_address) | ||
62 | if name == "NoReply" or name == "Timeout": | ||
63 | raise GNUNetDaemonError("Did not receive reply from " + daemon + " daemon at " + daemon_address + ". Daemon might of crashed") | ||
64 | raise e | ||
65 | |||