Add several missing files

This commit is contained in:
nolash 2021-02-18 09:29:52 +01:00
parent 51e09e5287
commit d417bbc938
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
3 changed files with 76 additions and 0 deletions

42
chainlib/chain.py Normal file
View File

@ -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

12
tests/base.py Normal file
View File

@ -0,0 +1,12 @@
import unittest
class TestBase(unittest.TestCase):
def setUp(self):
pass
def tearDown(self):
pass

22
tests/test_chain.py Normal file
View File

@ -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()