[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[GNUnet-SVN] [gnunet-python] 01/02: PEP
From: |
gnunet |
Subject: |
[GNUnet-SVN] [gnunet-python] 01/02: PEP |
Date: |
Fri, 01 Dec 2017 21:56:35 +0100 |
This is an automated email from the git hooks/post-receive script.
ng0 pushed a commit to branch master
in repository gnunet-python.
commit ca2344051422ee3593c95c79d16010d3b9108352
Author: ng0 <address@hidden>
AuthorDate: Fri Dec 1 20:54:48 2017 +0000
PEP
---
gnunet/__init__.py | 41 ++++++++++++------------
gnunet/_dbus_utils.py | 87 ++++++++++++++++++++++++++-------------------------
gnunet/block.py | 1 -
gnunet/crypto.py | 10 +++---
gnunet/gns.py | 72 +++++++++++++++++++++---------------------
gnunet/gnsrecord.py | 57 +++++++++++++++++----------------
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 @@
import gnunet.strings as strings
+
class GNUNetDaemonError(Exception):
- pass
+ pass
+
class _Key:
- def __init__(self, arg, subtype, bits):
- if isinstance(arg, subtype):
- self._data = arg._data
- elif isinstance(arg, str):
- self._data = strings.string_to_data(arg)
- else:
- try:
- self._data = bytearray(arg)
- except:
- raise TypeError("'arg' must be a " + type(subtype).__name__ + ", a
string or an array of bytes. Not a '" + type(arg).__name__ + "'.")
+ def __init__(self, arg, subtype, bits):
+ if isinstance(arg, subtype):
+ self._data = arg._data
+ elif isinstance(arg, str):
+ self._data = strings.string_to_data(arg)
+ else:
+ try:
+ self._data = bytearray(arg)
+ except:
+ raise TypeError("'arg' must be a " + type(subtype).__name__ +
", a string or an array of bytes. Not a '" + type(arg).__name__ + "'.")
- if len(self._data) * 8 != bits:
- raise ValueError("'arg' must be a " + bits + " bit hash. Got " +
len(self._data) + " bits.")
+ if len(self._data) * 8 != bits:
+ raise ValueError("'arg' must be a " + bits + " bit hash. Got " +
len(self._data) + " bits.")
- def __str__(self):
- return strings.data_to_string(self._data)
+ def __str__(self):
+ return strings.data_to_string(self._data)
class HashCode(_Key):
- def __init__(self, arg):
- _Key.__init__(self, arg, HashCode, 512)
-
- def __repr__(self):
- return "gnunet.HashCode('" + str(self) + "')"
+ def __init__(self, arg):
+ _Key.__init__(self, arg, HashCode, 512)
+ def __repr__(self):
+ 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()
DBusGMainLoop(set_as_default=True)
sysbus = dbus.SystemBus()
+from gnunet import _Key, GNUNetDaemonError
+import gnunet.strings as strings
+
+
class MainLoop(threading.Thread):
- def __init__(self):
- threading.Thread.__init__(self)
- self.daemon = True
- self.start()
+ def __init__(self):
+ threading.Thread.__init__(self)
+ self.daemon = True
+ self.start()
- def run(self):
- Gtk.main()
+ 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)
+ 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 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 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 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)
- 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
+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
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([
"gns_namerecord",
"regex",
"regex_accept"])
-
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 @@
from gnunet import _Key
import gnunet.strings as strings
-class EcdsaPublicKey(_Key):
- def __init__(self, arg):
- _Key.__init__(self, arg, EcdsaPublicKey, 256)
- def __repr__(self):
- return "gnunet.crypto.EcdsaPublicKey('" + str(self) + "')"
+class EcdsaPublicKey(_Key):
+ def __init__(self, arg):
+ _Key.__init__(self, arg, EcdsaPublicKey, 256)
+ def __repr__(self):
+ 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 *
import gnunet.crypto as crypto
import gnunet.gnsrecord as gnsrecord
-def lookup(name, zone, record_type, only_cached):
- name = str(name)
- zone = dbusize(crypto.EcdsaPublicKey(zone), True)
- if record_type not in gnsrecord.types:
- raise ValueError("'record_type' must be one of %s" % gnsrecord.types)
- #record_type = dbus.UInt32(gnsrecord.types[record_type], variant_level=1)
- record_type = dbus.String(record_type, variant_level=1)
- only_cached = dbus.Boolean(only_cached)
- try:
- results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone,
record_type, only_cached)
- except dbus.DBusException as e:
- handle_exception(e, "gns", "gnu.gnunet.gns")
+def lookup(name, zone, record_type, only_cached):
+ name = str(name)
+ zone = dbusize(crypto.EcdsaPublicKey(zone), True)
+ if record_type not in gnsrecord.types:
+ raise ValueError("'record_type' must be one of %s" % gnsrecord.types)
+ # record_type = dbus.UInt32(gnsrecord.types[record_type], variant_level=1)
+ record_type = dbus.String(record_type, variant_level=1)
+ only_cached = dbus.Boolean(only_cached)
- ret = []
- for r in results:
- record_type = str(r[0])
- private = False
- pending = False
- shadow = False
- relative = False
- for f in r[1]:
- if f == "private":
- private = True
- if f == "pending":
- pending = True
- if f == "shadow":
- shadow = True
- if f == "relative_expiration":
- relative = True
- data = str(r[2])
- expiration_time = None
- if relative:
- expiration_time = pythonize(r[3], datetime.timedelta)
- else:
- expiration_time = pythonize(r[3], datetime.datetime)
- ret.append(gnsrecord.Data(record_type, data, expiration_time, private,
pending, shadow))
+ try:
+ results = sysbus.get_object("gnu.gnunet.gns", "/").lookup(name, zone,
record_type, only_cached)
+ except dbus.DBusException as e:
+ handle_exception(e, "gns", "gnu.gnunet.gns")
- return ret
+ ret = []
+ for r in results:
+ record_type = str(r[0])
+ private = False
+ pending = False
+ shadow = False
+ relative = False
+ for f in r[1]:
+ if f == "private":
+ private = True
+ if f == "pending":
+ pending = True
+ if f == "shadow":
+ shadow = True
+ if f == "relative_expiration":
+ relative = True
+ data = str(r[2])
+ expiration_time = None
+ if relative:
+ expiration_time = pythonize(r[3], datetime.timedelta)
+ else:
+ expiration_time = pythonize(r[3], datetime.datetime)
+ ret.append(gnsrecord.Data(record_type, data, expiration_time, private,
pending, shadow))
+ 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 @@
import datetime
+
dns_types = {
- "A": 1,
- "NS": 2,
- "CNAME": 5,
- "SOA": 6,
- "PTR": 12,
- "MX": 15,
- "TXT": 16,
- "AAAA": 28,
- "TLSA": 52}
+ "A": 1,
+ "NS": 2,
+ "CNAME": 5,
+ "SOA": 6,
+ "PTR": 12,
+ "MX": 15,
+ "TXT": 16,
+ "AAAA": 28,
+ "TLSA": 52}
+
gns_types = {
- "PKEY": 65536,
- "NICK": 65537,
- "LEHO": 65538,
- "VPN": 65539,
- "GNS2DNS": 65540}
+ "PKEY": 65536,
+ "NICK": 65537,
+ "LEHO": 65538,
+ "VPN": 65539,
+ "GNS2DNS": 65540}
+
types = dict(list(dns_types.items()) + list(gns_types.items()))
-class Data:
- def __init__(self, record_type, data, expiration_time=None, private=None,
pending=None, shadow=None):
- self.record_type = str(record_type)
- if record_type not in types:
- raise ValueError("'record_type' must be one of %s" % types)
- #self.data = bytearray(data)
- self.data = str(data)
- if expiration_time is not None and not isinstance(expiration_time,
datetime.datetime) or isinstance(expiration_time, datetime.timedelta):
- raise TypeError("'expiration_time' must be a datetime.datetime or a
datetime.timedelta")
- self.expiration_time = expiration_time
- self.private = private
- self.pending = pending
- self.shadow = shadow
+class Data:
+ def __init__(self, record_type, data, expiration_time=None, private=None,
pending=None, shadow=None):
+ self.record_type = str(record_type)
+ if record_type not in types:
+ raise ValueError("'record_type' must be one of %s" % types)
+ # self.data = bytearray(data)
+ self.data = str(data)
+ if expiration_time is not None and not isinstance(expiration_time,
datetime.datetime) or isinstance(expiration_time, datetime.timedelta):
+ raise TypeError("'expiration_time' must be a datetime.datetime or
a datetime.timedelta")
+ self.expiration_time = expiration_time
+ self.private = private
+ self.pending = pending
+ self.shadow = shadow
--
To stop receiving notification emails like this one, please contact
address@hidden