diff --git a/chainlib/chain.py b/chainlib/chain.py new file mode 100644 index 0000000..2cdbc64 --- /dev/null +++ b/chainlib/chain.py @@ -0,0 +1,42 @@ +class ChainSpec: + + def __init__(self, engine, common_name, network_id, tag=None): + self.o = { + 'engine': engine, + 'common_name': common_name, + 'network_id': network_id, + 'tag': tag, + } + + def network_id(self): + return self.o['network_id'] + + + def chain_id(self): + return self.o['network_id'] + + + def engine(self): + return self.o['engine'] + + + def common_name(self): + return self.o['common_name'] + + + @staticmethod + def from_chain_str(chain_str): + o = chain_str.split(':') + if len(o) < 3: + raise ValueError('Chain string must have three sections, got {}'.format(len(o))) + tag = None + if len(o) == 4: + tag = o[3] + return ChainSpec(o[0], o[1], int(o[2]), tag) + + + def __str__(self): + s = '{}:{}:{}'.format(self.o['engine'], self.o['common_name'], self.o['network_id']) + if self.o['tag'] != None: + s += ':' + self.o['tag'] + return s diff --git a/tests/base.py b/tests/base.py new file mode 100644 index 0000000..e8e3a04 --- /dev/null +++ b/tests/base.py @@ -0,0 +1,12 @@ +import unittest + + +class TestBase(unittest.TestCase): + + + def setUp(self): + pass + + + def tearDown(self): + pass diff --git a/tests/test_chain.py b/tests/test_chain.py new file mode 100644 index 0000000..1c2bc95 --- /dev/null +++ b/tests/test_chain.py @@ -0,0 +1,22 @@ +import unittest + +from chainlib.chain import ChainSpec + +from tests.base import TestBase + + +class TestChain(TestBase): + + def test_chain_spec(self): + + s = ChainSpec.from_chain_str('foo:bar:3') + s = ChainSpec.from_chain_str('foo:bar:3:baz') + + with self.assertRaises(ValueError): + s = ChainSpec.from_chain_str('foo:bar:a') + s = ChainSpec.from_chain_str('foo:bar') + s = ChainSpec.from_chain_str('foo') + + +if __name__ == '__main__': + unittest.main()