aboutsummaryrefslogtreecommitdiff
path: root/gnunet/_dbus_utils.py
blob: 4e08c2a8c74d8df65b08804cab81f317a904f381 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import dbus
import threading
import datetime
from gi.repository import Gtk

from dbus.mainloop.glib import DBusGMainLoop, threads_init
threads_init()
DBusGMainLoop(set_as_default=True)
sysbus = dbus.SystemBus()

class MainLoop(threading.Thread):
  def __init__(self):
    threading.Thread.__init__(self)
    self.daemon = True
    self.start()

  def run(self):
    Gtk.main()

MainLoop()

from gnunet import _Key, GNUNetDaemonError
import gnunet.strings as strings

def pythonize(arg, argtype):
  if argtype is datetime.datetime:
    if isinstance(arg, str):
      return strings.string_to_absolute_time(arg)
    if isinstance(arg. dbus.UInt64):
      return datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=arg)
    return datatime.datetime(arg)

def dbusize(arg, pretty):
  if isinstance(arg, _Key):
    if pretty:
      return dbus.String(arg, variant_level=1)
    else:
      return dbus.Array(arg._data[:], variant_level=1, signature="y")

  #if type(arg) is gnsrecord.Data:
    #return dbus.Struct([arg._recordtype, 

  if isinstance(arg, datetime.datetime):
    if pretty:
      return dbus.String(strings.absolute_time_to_string(arg), variant_level=1)
    else:
      return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1)
      
def handle_exception(e, daemon, daemon_address):
  name = e.get_dbus_name()
  message = e.get_dbus_message()
  if not name.startswith("org.freedesktop.DBus.Error."):
    raise e
  name = name[len("org.freedesktop.DBus.Error."):]

  if name == "Failed" or name == "InvalidArgs":
    raise GNUNetDaemonError(message)
  if name == "NoMemory":
    raise MemoryError(message)
  if name == "ServiceUnknown" or name == "NameHasNoOwner":
    raise GNUNetDaemonError("Failed to contact " + daemon + " daemon at " + daemon_address)
  if name == "NoReply" or name == "Timeout":
    raise GNUNetDaemonError("Did not receive reply from " + daemon + " daemon at " + daemon_address + ". Daemon might of crashed")
  raise e