aboutsummaryrefslogtreecommitdiff
path: root/gnunet
diff options
context:
space:
mode:
Diffstat (limited to 'gnunet')
-rw-r--r--gnunet/_dbus_utils.py24
-rw-r--r--gnunet/dht.py19
-rw-r--r--gnunet/gns.py7
-rw-r--r--gnunet/gnsrecord.py10
4 files changed, 40 insertions, 20 deletions
diff --git a/gnunet/_dbus_utils.py b/gnunet/_dbus_utils.py
index 2eb317f..26cdadd 100644
--- a/gnunet/_dbus_utils.py
+++ b/gnunet/_dbus_utils.py
@@ -32,7 +32,8 @@ def pythonize(arg, argtype):
32 if isinstance(arg, str): 32 if isinstance(arg, str):
33 return strings.string_to_absolute_time(arg) 33 return strings.string_to_absolute_time(arg)
34 if isinstance(arg. dbus.UInt64): 34 if isinstance(arg. dbus.UInt64):
35 return datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=arg) 35 return (datetime.datetime(1970, 1, 1)
36 + datetime.timedelta(microseconds=arg))
36 return datetime.datetime(arg) 37 return datetime.datetime(arg)
37 38
38 39
@@ -48,7 +49,8 @@ def dbusize(arg, pretty):
48 49
49 if isinstance(arg, datetime.datetime): 50 if isinstance(arg, datetime.datetime):
50 if pretty: 51 if pretty:
51 return dbus.String(strings.absolute_time_to_string(arg), variant_level=1) 52 return dbus.String(strings.absolute_time_to_string(arg),
53 variant_level=1)
52 else: 54 else:
53 return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1) 55 return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1)
54 56
@@ -60,12 +62,16 @@ def handle_exception(e, daemon, daemon_address):
60 raise e 62 raise e
61 name = name[len("org.freedesktop.DBus.Error."):] 63 name = name[len("org.freedesktop.DBus.Error."):]
62 64
63 if name == "Failed" or name == "InvalidArgs": 65 if name in ("Failed", "InvalidArgs"):
64 raise GNUNetDaemonError(message) 66 raise GNUNetDaemonError(message)
65 if name == "NoMemory": 67 elif name == "NoMemory":
66 raise MemoryError(message) 68 raise MemoryError(message)
67 if name == "ServiceUnknown" or name == "NameHasNoOwner": 69 elif name in ("ServiceUnknown", "NameHasNoOwner"):
68 raise GNUNetDaemonError("Failed to contact " + daemon + " daemon at " + daemon_address) 70 raise GNUNetDaemonError("Failed to contact %s daemon at %s" %
69 if name == "NoReply" or name == "Timeout": 71 (daemon, daemon_address))
70 raise GNUNetDaemonError("Did not receive reply from " + daemon + " daemon at " + daemon_address + ". Daemon might of crashed") 72 elif name in ("NoReply", "Timeout"):
71 raise e 73 raise GNUNetDaemonError("Did not receive reply from %s daemon at %s. "
74 "Daemon might of crashed."
75 % (daemon, daemon_address))
76 else:
77 raise e
diff --git a/gnunet/dht.py b/gnunet/dht.py
index b97c7a1..e9aa525 100644
--- a/gnunet/dht.py
+++ b/gnunet/dht.py
@@ -30,9 +30,12 @@ class GetResult(threading.Thread):
30 30
31 if request: 31 if request:
32 if request.record_route: 32 if request.record_route:
33 request.callback(self.block_type, self.key, self.data, self.expiry, get_path=self.get_path, put_path=self.put_path) 33 request.callback(self.block_type, self.key, self.data,
34 self.expiry, get_path=self.get_path,
35 put_path=self.put_path)
34 else: 36 else:
35 request.callback(self.block_type, self.key, self.data, self.expiry) 37 request.callback(self.block_type, self.key, self.data,
38 self.expiry)
36 39
37 40
38def _result(expiry, key, get_path, put_path, block_type, data, path): 41def _result(expiry, key, get_path, put_path, block_type, data, path):
@@ -44,7 +47,8 @@ def _result(expiry, key, get_path, put_path, block_type, data, path):
44 data = bytearray(data) 47 data = bytearray(data)
45 GetResult(expiry, key, get_path, put_path, block_type, data, path) 48 GetResult(expiry, key, get_path, put_path, block_type, data, path)
46 49
47sysbus.add_signal_receiver(_result, "result", "gnu.gnunet.dht.get", "gnu.gnunet.dht", path_keyword="path") 50sysbus.add_signal_receiver(_result, "result", "gnu.gnunet.dht.get",
51 "gnu.gnunet.dht", path_keyword="path")
48 52
49 53
50class GetRequest: 54class GetRequest:
@@ -54,7 +58,8 @@ class GetRequest:
54 self.record_route = record_route 58 self.record_route = record_route
55 59
56 def filter_known_results(self, keys): 60 def filter_known_results(self, keys):
57 keys = dbus.Array([dbusize(HashCode(key)) for key in list(keys)], signature="v") 61 keys = dbus.Array([dbusize(HashCode(key))
62 for key in list(keys)], signature="v")
58 try: 63 try:
59 sysbus.get_object("gnu.gnunet.dht", self._path).filter_known_results(keys, dbus_interface="gnu.gnunet.dht.get") 64 sysbus.get_object("gnu.gnunet.dht", self._path).filter_known_results(keys, dbus_interface="gnu.gnunet.dht.get")
60 except dbus.DBusException as e: 65 except dbus.DBusException as e:
@@ -67,7 +72,8 @@ class GetRequest:
67 handle_exception(e, "dht", "gnu.gnunet.dht") 72 handle_exception(e, "dht", "gnu.gnunet.dht")
68 73
69 74
70def put(key, desired_replication_level, block_type, data, expiry=None, demultiplex_everywhere=False, record_route=False, bart=False): 75def put(key, desired_replication_level, block_type, data, expiry=None,
76 demultiplex_everywhere=False, record_route=False, bart=False):
71 key = dbusize(HashCode(key), True) 77 key = dbusize(HashCode(key), True)
72 desired_replication_level = dbus.UInt32(desired_replication_level) 78 desired_replication_level = dbus.UInt32(desired_replication_level)
73 if block_type not in block.TYPES: 79 if block_type not in block.TYPES:
@@ -94,7 +100,8 @@ def put(key, desired_replication_level, block_type, data, expiry=None, demultipl
94 handle_exception(e, "dht", "gnu.gnunet.dht") 100 handle_exception(e, "dht", "gnu.gnunet.dht")
95 101
96 102
97def get_start(callback, block_type, key, desired_replication_level, demultiplex_everywhere=False, record_route=False, bart=False): 103def get_start(callback, block_type, key, desired_replication_level,
104 demultiplex_everywhere=False, record_route=False, bart=False):
98 if block_type not in block.TYPES: 105 if block_type not in block.TYPES:
99 raise ValueError("'block_type' must be one of %s" % block.TYPES) 106 raise ValueError("'block_type' must be one of %s" % block.TYPES)
100 block_type = dbus.String(block_type, variant_level=1) 107 block_type = dbus.String(block_type, variant_level=1)
diff --git a/gnunet/gns.py b/gnunet/gns.py
index 5f33566..b46b681 100644
--- a/gnunet/gns.py
+++ b/gnunet/gns.py
@@ -17,7 +17,9 @@ def lookup(name, zone, record_type, only_cached):
17 only_cached = dbus.Boolean(only_cached) 17 only_cached = dbus.Boolean(only_cached)
18 18
19 try: 19 try:
20 results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone, record_type, only_cached) 20 results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone,
21 record_type,
22 only_cached)
21 except dbus.DBusException as e: 23 except dbus.DBusException as e:
22 handle_exception(e, "gns", "gnu.gnunet.gns") 24 handle_exception(e, "gns", "gnu.gnunet.gns")
23 25
@@ -43,6 +45,7 @@ def lookup(name, zone, record_type, only_cached):
43 expiration_time = pythonize(r[3], datetime.timedelta) 45 expiration_time = pythonize(r[3], datetime.timedelta)
44 else: 46 else:
45 expiration_time = pythonize(r[3], datetime.datetime) 47 expiration_time = pythonize(r[3], datetime.datetime)
46 ret.append(gnsrecord.Data(record_type, data, expiration_time, private, pending, shadow)) 48 ret.append(gnsrecord.Data(record_type, data, expiration_time,
49 private, pending, shadow))
47 50
48 return ret 51 return ret
diff --git a/gnunet/gnsrecord.py b/gnunet/gnsrecord.py
index e9774c4..d869d05 100644
--- a/gnunet/gnsrecord.py
+++ b/gnunet/gnsrecord.py
@@ -26,14 +26,18 @@ TYPES.update(GNS_TYPES.items())
26 26
27 27
28class Data: 28class Data:
29 def __init__(self, record_type, data, expiration_time=None, private=None, pending=None, shadow=None): 29 def __init__(self, record_type, data, expiration_time=None, private=None,
30 pending=None, shadow=None):
30 self.record_type = str(record_type) 31 self.record_type = str(record_type)
31 if record_type not in TYPES: 32 if record_type not in TYPES:
32 raise ValueError("'record_type' must be one of %s" % TYPES) 33 raise ValueError("'record_type' must be one of %s" % TYPES)
33 # self.data = bytearray(data) 34 # self.data = bytearray(data)
34 self.data = str(data) 35 self.data = str(data)
35 if expiration_time is not None and not isinstance(expiration_time, datetime.datetime) or isinstance(expiration_time, datetime.timedelta): 36 if (expiration_time is not None and
36 raise TypeError("'expiration_time' must be a datetime.datetime or a datetime.timedelta") 37 not isinstance(expiration_time,
38 (datetime.datetime, datetime.timedelta))):
39 raise TypeError("'expiration_time' must be a datetime.datetime "
40 "or a datetime.timedelta")
37 self.expiration_time = expiration_time 41 self.expiration_time = expiration_time
38 self.private = private 42 self.private = private
39 self.pending = pending 43 self.pending = pending