From ce657a820f9293942663eb340ce08ebe493569c1 Mon Sep 17 00:00:00 2001 From: Inso <insomniak.fr@gmail.com> Date: Thu, 11 Dec 2014 17:38:53 +0100 Subject: [PATCH] Adding "from_raw()" factory method --- ucoinpy/documents/__init__.py | 7 +--- ucoinpy/documents/block.py | 62 +++++++++++++++++++++++++++++- ucoinpy/documents/certification.py | 23 +++++++++-- ucoinpy/documents/membership.py | 8 ++++ ucoinpy/documents/peer.py | 5 +++ ucoinpy/documents/status.py | 5 +++ ucoinpy/documents/transaction.py | 5 +++ 7 files changed, 105 insertions(+), 10 deletions(-) diff --git a/ucoinpy/documents/__init__.py b/ucoinpy/documents/__init__.py index d0057d91..0fe4cc12 100644 --- a/ucoinpy/documents/__init__.py +++ b/ucoinpy/documents/__init__.py @@ -9,11 +9,8 @@ from ..key import Base58Encoder class Document: - def __init__(self, timestamp): - self.timestamp = timestamp - - def ts(self): - return "META:TS:{0}".format(self.timestamp) + def __init__(self, version): + self.version = version def content(self): return "" diff --git a/ucoinpy/documents/block.py b/ucoinpy/documents/block.py index 33d1e043..c3fc78a8 100644 --- a/ucoinpy/documents/block.py +++ b/ucoinpy/documents/block.py @@ -75,7 +75,67 @@ BOTTOM_SIGNATURE self.excluded = excluded self.certifications = certifications self.transactions = transactions - + + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + lines = raw.splitlines(True) + + n = 0 + version_re = re.compile("Version: ([0-9]+)\n") + version = version_re.match(lines[n]) + n = 1 + currency_re = re.compile("Currency: ([0-9a-zA-Z_\-]+)\n" + currency = currency_re.match(lines[n]) + + n = 2 + noonce_re = re.compile("Nonce: ([0-9]+)\n") + noonce = nonce_re.match(lines[n]) + + n = 3 + number_re = re.compile("Number: ([0-9]+)\n") + number = number_re.match(lines[n]) + + n = 4 + powmin_re = re.compile("PoWMin: ([0-9]+)\n") + powmin = powmin.match(lines[n]) + + n = 5 + time_re = re.compile("Time: ([0-9]+)\n") + time = time.match(lines[n]) + + n = 6 + mediantime_re = re.compile("MedianTime: ([0-9]+)\n") + mediantime = mediantime_re.match(line[n]) + + n = 7 + ud_re = re.compile("UniversalDividend: ([0-9]+)\n") + ud = ud_re.match(line[n]) + + n = 8 + issuer_re = re.compile("Issuer: ([1-9A-Za-z][^OIl]{43,45})\n") + issuer = issuer_re.match(line[n]) + + n = 9 + previoushash_re = re.compile("PreviousHash: ([0-9a-fA-F]{5,40})\n") + prev_hash = previoushash_re.match(line[n]) + + n = 10 + previousissuer_re = re.compile("PreviousIssuer: ([1-9A-Za-z][^OIl]{43,45})\n") + prev_issuer = previousissuer_re.match(line[n]) + + parameters = "" + members_count = "" + identities = "" + joiners = "" + actives = "" + leavers = "" + excluded = "" + certifications = "" + transactions = "" + + return cls([]) + def content(self): doc = """ Version: {0} diff --git a/ucoinpy/documents/certification.py b/ucoinpy/documents/certification.py index c5f0f09c..2ad28da1 100644 --- a/ucoinpy/documents/certification.py +++ b/ucoinpy/documents/certification.py @@ -13,12 +13,21 @@ class SelfCertification(Document): def __init__(self, identifier): self.identifier = identifier - + self.timestamp = timestamp + + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() + + def ts(self): + return "META:TS:{0}".format(self.timestamp) + def uid(self): return "UID:{0}".format(self.identifier) def content(self): - return "{0}\n{1}".format(self.uid(), self.timestamp()) + return "{0}\n{1}".format(self.uid(), self.ts()) class Certification(Document): @@ -33,8 +42,14 @@ class Certification(Document): self.selfcert = selfcert self.blockid = blockid - def timestamp(self): + + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() + + def ts(self): return "META:TS:{0}".format(self.blockid) def content(self): - return "{0}\n{1}".format(self.selfcert.content(), self.timestamp()) + return "{0}\n{1}".format(self.selfcert.content(), self.ts()) diff --git a/ucoinpy/documents/membership.py b/ucoinpy/documents/membership.py index ae174521..05ddb7df 100644 --- a/ucoinpy/documents/membership.py +++ b/ucoinpy/documents/membership.py @@ -32,6 +32,14 @@ class Membership(object): self.userid = userid self.cert_ts = cert_ts + + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() + + + def content(self): return """ Version: {0} diff --git a/ucoinpy/documents/peer.py b/ucoinpy/documents/peer.py index 082c06ef..d696665b 100644 --- a/ucoinpy/documents/peer.py +++ b/ucoinpy/documents/peer.py @@ -29,6 +29,11 @@ class Peer(Document): self.pubkey = pubkey self.blockid = blockid self.endpoints = endpoints + + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() def content(self): doc = """ diff --git a/ucoinpy/documents/status.py b/ucoinpy/documents/status.py index dd11ce1a..3913e051 100644 --- a/ucoinpy/documents/status.py +++ b/ucoinpy/documents/status.py @@ -29,6 +29,11 @@ class Status(Document): self.sender = sender self.recipient = recipient + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() + def content(self): return ''' Version: {0} diff --git a/ucoinpy/documents/transaction.py b/ucoinpy/documents/transaction.py index 10fa5d69..beaf4764 100644 --- a/ucoinpy/documents/transaction.py +++ b/ucoinpy/documents/transaction.py @@ -37,6 +37,11 @@ SIGNATURES self.outputs = outputs self.comment = comment + @classmethod + def from_raw(cls, raw): + #TODO : Parsing + return cls() + def content(self): doc = """ Version: {0} -- GitLab