aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2017-12-01 20:54:48 +0000
committerng0 <ng0@n0.is>2017-12-01 20:54:48 +0000
commitca2344051422ee3593c95c79d16010d3b9108352 (patch)
treef53156f7d5b73adf84f4228815ebcf497e9082c7
parente46b690de9baafbeaa52f178d361556298538172 (diff)
downloadgnunet-python-ca2344051422ee3593c95c79d16010d3b9108352.tar.gz
gnunet-python-ca2344051422ee3593c95c79d16010d3b9108352.zip
PEP
-rw-r--r--gnunet/__init__.py41
-rw-r--r--gnunet/_dbus_utils.py87
-rw-r--r--gnunet/block.py1
-rw-r--r--gnunet/crypto.py10
-rw-r--r--gnunet/gns.py72
-rw-r--r--gnunet/gnsrecord.py57
6 files changed, 137 insertions, 131 deletions
diff --git a/gnunet/__init__.py b/gnunet/__init__.py
index a517180..dc4e701 100644
--- a/gnunet/__init__.py
+++ b/gnunet/__init__.py
@@ -1,31 +1,32 @@
1import gnunet.strings as strings 1import gnunet.strings as strings
2 2
3
3class GNUNetDaemonError(Exception): 4class GNUNetDaemonError(Exception):
4 pass 5 pass
6
5 7
6class _Key: 8class _Key:
7 def __init__(self, arg, subtype, bits): 9 def __init__(self, arg, subtype, bits):
8 if isinstance(arg, subtype): 10 if isinstance(arg, subtype):
9 self._data = arg._data 11 self._data = arg._data
10 elif isinstance(arg, str): 12 elif isinstance(arg, str):
11 self._data = strings.string_to_data(arg) 13 self._data = strings.string_to_data(arg)
12 else: 14 else:
13 try: 15 try:
14 self._data = bytearray(arg) 16 self._data = bytearray(arg)
15 except: 17 except:
16 raise TypeError("'arg' must be a " + type(subtype).__name__ + ", a string or an array of bytes. Not a '" + type(arg).__name__ + "'.") 18 raise TypeError("'arg' must be a " + type(subtype).__name__ + ", a string or an array of bytes. Not a '" + type(arg).__name__ + "'.")
17 19
18 if len(self._data) * 8 != bits: 20 if len(self._data) * 8 != bits:
19 raise ValueError("'arg' must be a " + bits + " bit hash. Got " + len(self._data) + " bits.") 21 raise ValueError("'arg' must be a " + bits + " bit hash. Got " + len(self._data) + " bits.")
20 22
21 def __str__(self): 23 def __str__(self):
22 return strings.data_to_string(self._data) 24 return strings.data_to_string(self._data)
23 25
24 26
25class HashCode(_Key): 27class HashCode(_Key):
26 def __init__(self, arg): 28 def __init__(self, arg):
27 _Key.__init__(self, arg, HashCode, 512) 29 _Key.__init__(self, arg, HashCode, 512)
28
29 def __repr__(self):
30 return "gnunet.HashCode('" + str(self) + "')"
31 30
31 def __repr__(self):
32 return "gnunet.HashCode('" + str(self) + "')"
diff --git a/gnunet/_dbus_utils.py b/gnunet/_dbus_utils.py
index 4e08c2a..739898a 100644
--- a/gnunet/_dbus_utils.py
+++ b/gnunet/_dbus_utils.py
@@ -8,58 +8,61 @@ threads_init()
8DBusGMainLoop(set_as_default=True) 8DBusGMainLoop(set_as_default=True)
9sysbus = dbus.SystemBus() 9sysbus = dbus.SystemBus()
10 10
11from gnunet import _Key, GNUNetDaemonError
12import gnunet.strings as strings
13
14
11class MainLoop(threading.Thread): 15class MainLoop(threading.Thread):
12 def __init__(self): 16 def __init__(self):
13 threading.Thread.__init__(self) 17 threading.Thread.__init__(self)
14 self.daemon = True 18 self.daemon = True
15 self.start() 19 self.start()
16 20
17 def run(self): 21 def run(self):
18 Gtk.main() 22 Gtk.main()
19 23
20MainLoop() 24MainLoop()
21 25
22from gnunet import _Key, GNUNetDaemonError
23import gnunet.strings as strings
24 26
25def pythonize(arg, argtype): 27def pythonize(arg, argtype):
26 if argtype is datetime.datetime: 28 if argtype is datetime.datetime:
27 if isinstance(arg, str): 29 if isinstance(arg, str):
28 return strings.string_to_absolute_time(arg) 30 return strings.string_to_absolute_time(arg)
29 if isinstance(arg. dbus.UInt64): 31 if isinstance(arg. dbus.UInt64):
30 return datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=arg) 32 return datetime.datetime(1970, 1, 1) + datetime.timedelta(microseconds=arg)
31 return datatime.datetime(arg) 33 return datatime.datetime(arg)
34
32 35
33def dbusize(arg, pretty): 36def dbusize(arg, pretty):
34 if isinstance(arg, _Key): 37 if isinstance(arg, _Key):
35 if pretty: 38 if pretty:
36 return dbus.String(arg, variant_level=1) 39 return dbus.String(arg, variant_level=1)
37 else: 40 else:
38 return dbus.Array(arg._data[:], variant_level=1, signature="y") 41 return dbus.Array(arg._data[:], variant_level=1, signature="y")
39 42
40 #if type(arg) is gnsrecord.Data: 43 # if type(arg) is gnsrecord.Data:
41 #return dbus.Struct([arg._recordtype, 44 # return dbus.Struct([arg._recordtype,
42 45
43 if isinstance(arg, datetime.datetime): 46 if isinstance(arg, datetime.datetime):
44 if pretty: 47 if pretty:
45 return dbus.String(strings.absolute_time_to_string(arg), variant_level=1) 48 return dbus.String(strings.absolute_time_to_string(arg), variant_level=1)
46 else: 49 else:
47 return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1) 50 return dbus.UInt64((arg - datetime.datetime(1970, 1, 1)).total_seconds() * 1000000, variant_level=1)
48
49def 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 51
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 52
53def handle_exception(e, daemon, daemon_address):
54 name = e.get_dbus_name()
55 message = e.get_dbus_message()
56 if not name.startswith("org.freedesktop.DBus.Error."):
57 raise e
58 name = name[len("org.freedesktop.DBus.Error."):]
59
60 if name == "Failed" or name == "InvalidArgs":
61 raise GNUNetDaemonError(message)
62 if name == "NoMemory":
63 raise MemoryError(message)
64 if name == "ServiceUnknown" or name == "NameHasNoOwner":
65 raise GNUNetDaemonError("Failed to contact " + daemon + " daemon at " + daemon_address)
66 if name == "NoReply" or name == "Timeout":
67 raise GNUNetDaemonError("Did not receive reply from " + daemon + " daemon at " + daemon_address + ". Daemon might of crashed")
68 raise e
diff --git a/gnunet/block.py b/gnunet/block.py
index 2a2dfec..ec363e0 100644
--- a/gnunet/block.py
+++ b/gnunet/block.py
@@ -13,4 +13,3 @@ types = set([
13 "gns_namerecord", 13 "gns_namerecord",
14 "regex", 14 "regex",
15 "regex_accept"]) 15 "regex_accept"])
16
diff --git a/gnunet/crypto.py b/gnunet/crypto.py
index b81b49d..461b0cc 100644
--- a/gnunet/crypto.py
+++ b/gnunet/crypto.py
@@ -1,10 +1,10 @@
1from gnunet import _Key 1from gnunet import _Key
2import gnunet.strings as strings 2import gnunet.strings as strings
3 3
4class EcdsaPublicKey(_Key):
5 def __init__(self, arg):
6 _Key.__init__(self, arg, EcdsaPublicKey, 256)
7 4
8 def __repr__(self): 5class EcdsaPublicKey(_Key):
9 return "gnunet.crypto.EcdsaPublicKey('" + str(self) + "')" 6 def __init__(self, arg):
7 _Key.__init__(self, arg, EcdsaPublicKey, 256)
10 8
9 def __repr__(self):
10 return "gnunet.crypto.EcdsaPublicKey('" + str(self) + "')"
diff --git a/gnunet/gns.py b/gnunet/gns.py
index 57c1bd9..b2d5e64 100644
--- a/gnunet/gns.py
+++ b/gnunet/gns.py
@@ -6,43 +6,43 @@ from gnunet import *
6import gnunet.crypto as crypto 6import gnunet.crypto as crypto
7import gnunet.gnsrecord as gnsrecord 7import gnunet.gnsrecord as gnsrecord
8 8
9def lookup(name, zone, record_type, only_cached):
10 name = str(name)
11 zone = dbusize(crypto.EcdsaPublicKey(zone), True)
12 if record_type not in gnsrecord.types:
13 raise ValueError("'record_type' must be one of %s" % gnsrecord.types)
14 #record_type = dbus.UInt32(gnsrecord.types[record_type], variant_level=1)
15 record_type = dbus.String(record_type, variant_level=1)
16 only_cached = dbus.Boolean(only_cached)
17 9
18 try: 10def lookup(name, zone, record_type, only_cached):
19 results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone, record_type, only_cached) 11 name = str(name)
20 except dbus.DBusException as e: 12 zone = dbusize(crypto.EcdsaPublicKey(zone), True)
21 handle_exception(e, "gns", "gnu.gnunet.gns") 13 if record_type not in gnsrecord.types:
14 raise ValueError("'record_type' must be one of %s" % gnsrecord.types)
15 # record_type = dbus.UInt32(gnsrecord.types[record_type], variant_level=1)
16 record_type = dbus.String(record_type, variant_level=1)
17 only_cached = dbus.Boolean(only_cached)
22 18
23 ret = [] 19 try:
24 for r in results: 20 results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone, record_type, only_cached)
25 record_type = str(r[0]) 21 except dbus.DBusException as e:
26 private = False 22 handle_exception(e, "gns", "gnu.gnunet.gns")
27 pending = False
28 shadow = False
29 relative = False
30 for f in r[1]:
31 if f == "private":
32 private = True
33 if f == "pending":
34 pending = True
35 if f == "shadow":
36 shadow = True
37 if f == "relative_expiration":
38 relative = True
39 data = str(r[2])
40 expiration_time = None
41 if relative:
42 expiration_time = pythonize(r[3], datetime.timedelta)
43 else:
44 expiration_time = pythonize(r[3], datetime.datetime)
45 ret.append(gnsrecord.Data(record_type, data, expiration_time, private, pending, shadow))
46 23
47 return ret 24 ret = []
25 for r in results:
26 record_type = str(r[0])
27 private = False
28 pending = False
29 shadow = False
30 relative = False
31 for f in r[1]:
32 if f == "private":
33 private = True
34 if f == "pending":
35 pending = True
36 if f == "shadow":
37 shadow = True
38 if f == "relative_expiration":
39 relative = True
40 data = str(r[2])
41 expiration_time = None
42 if relative:
43 expiration_time = pythonize(r[3], datetime.timedelta)
44 else:
45 expiration_time = pythonize(r[3], datetime.datetime)
46 ret.append(gnsrecord.Data(record_type, data, expiration_time, private, pending, shadow))
48 47
48 return ret
diff --git a/gnunet/gnsrecord.py b/gnunet/gnsrecord.py
index 4d00139..1cb0d72 100644
--- a/gnunet/gnsrecord.py
+++ b/gnunet/gnsrecord.py
@@ -1,36 +1,39 @@
1import datetime 1import datetime
2 2
3
3dns_types = { 4dns_types = {
4 "A": 1, 5 "A": 1,
5 "NS": 2, 6 "NS": 2,
6 "CNAME": 5, 7 "CNAME": 5,
7 "SOA": 6, 8 "SOA": 6,
8 "PTR": 12, 9 "PTR": 12,
9 "MX": 15, 10 "MX": 15,
10 "TXT": 16, 11 "TXT": 16,
11 "AAAA": 28, 12 "AAAA": 28,
12 "TLSA": 52} 13 "TLSA": 52}
14
13 15
14gns_types = { 16gns_types = {
15 "PKEY": 65536, 17 "PKEY": 65536,
16 "NICK": 65537, 18 "NICK": 65537,
17 "LEHO": 65538, 19 "LEHO": 65538,
18 "VPN": 65539, 20 "VPN": 65539,
19 "GNS2DNS": 65540} 21 "GNS2DNS": 65540}
22
20 23
21types = dict(list(dns_types.items()) + list(gns_types.items())) 24types = dict(list(dns_types.items()) + list(gns_types.items()))
22 25
23class Data:
24 def __init__(self, record_type, data, expiration_time=None, private=None, pending=None, shadow=None):
25 self.record_type = str(record_type)
26 if record_type not in types:
27 raise ValueError("'record_type' must be one of %s" % types)
28 #self.data = bytearray(data)
29 self.data = str(data)
30 if expiration_time is not None and not isinstance(expiration_time, datetime.datetime) or isinstance(expiration_time, datetime.timedelta):
31 raise TypeError("'expiration_time' must be a datetime.datetime or a datetime.timedelta")
32 self.expiration_time = expiration_time
33 self.private = private
34 self.pending = pending
35 self.shadow = shadow
36 26
27class Data:
28 def __init__(self, record_type, data, expiration_time=None, private=None, pending=None, shadow=None):
29 self.record_type = str(record_type)
30 if record_type not in types:
31 raise ValueError("'record_type' must be one of %s" % types)
32 # self.data = bytearray(data)
33 self.data = str(data)
34 if expiration_time is not None and not isinstance(expiration_time, datetime.datetime) or isinstance(expiration_time, datetime.timedelta):
35 raise TypeError("'expiration_time' must be a datetime.datetime or a datetime.timedelta")
36 self.expiration_time = expiration_time
37 self.private = private
38 self.pending = pending
39 self.shadow = shadow