Commit 66917a8f authored by inso's avatar inso

New token class method to build Conditions

parent ae3f800f
......@@ -58,3 +58,20 @@ class Test_OutputGrammar(unittest.TestCase):
self.assertEqual(result.left.right.op.name, "AND")
self.assertEqual(result.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(result, output.Condition), condition)
def test_instanciate_condition(self):
inst = output.Condition.token(output.SIG.token("HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd"),
output.Operator.token("OR"),
output.Condition.token(
output.SIG.token("DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV"),
output.Operator.token("AND"),
output.XHX.token("309BC5E644F797F53E5A2065EAF38A173437F2E6")
))
condition = "(SIG(HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd) OR (SIG(DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV) AND XHX(309BC5E644F797F53E5A2065EAF38A173437F2E6)))"
inst = pypeg2.parse(condition, output.Condition)
self.assertEqual(inst.left.left.pubkey, "HgTTJLAQ5sqfknMq7yLPZbehtuLSsKj9CxWN7k8QvYJd")
self.assertEqual(inst.left.op.name, "OR")
self.assertEqual(inst.left.right.left.pubkey, "DNann1Lh55eZMEDXeYt59bzHbA3NJR46DeQYCS2qQdLV")
self.assertEqual(inst.left.right.op.name, "AND")
self.assertEqual(inst.left.right.right.sha_hash, "309BC5E644F797F53E5A2065EAF38A173437F2E6")
self.assertEqual(pypeg2.compose(inst, output.Condition), condition)
......@@ -14,6 +14,12 @@ class Hash(Symbol):
class SIG(str):
grammar = "SIG(", attr('pubkey', Pubkey), ")"
@classmethod
def token(cls, pubkey):
sig = cls()
sig.pubkey = pubkey
return sig
def compose(self, parser, grammar=None, attr_of=None):
return "SIG({0})".format(self.pubkey)
......@@ -21,6 +27,12 @@ class SIG(str):
class XHX(str):
grammar = "XHX(", attr('sha_hash', Hash), ")"
@classmethod
def token(cls, sha_hash):
xhx = cls()
xhx.sha_hash = sha_hash
return xhx
def compose(self, parser, grammar=None, attr_of=None):
return "XHX({0})".format(self.sha_hash)
......@@ -28,11 +40,26 @@ class XHX(str):
class Operator(Keyword):
grammar = Enum(K("AND"), K("OR"))
@classmethod
def token(cls, keyword):
op = cls(keyword)
return op
def compose(self, parser, grammar=None, attr_of=None):
return "{0}".format(self.name)
class Condition(str):
@classmethod
def token(cls, left, op=None, right=None):
condition = cls()
condition.left = left
if op:
condition.op = op
if right:
condition.right = right
return condition
def compose(self, parser, grammar=None, attr_of=None):
if type(self.left) is Condition:
left = "({0})".format(parser.compose(self.left, grammar=grammar, attr_of=attr_of))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment