Add token part

This commit is contained in:
nolash 2021-10-10 14:49:22 +02:00
parent 7f18b6d6d2
commit 538b352cf7
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 109 additions and 18 deletions

View File

@ -55,6 +55,6 @@ class Attachment(Data):
def __str__(self):
s = ''
for i in range(len(self.contents)):
s += '{}\t{}\n'.format(self.digests[i].hex(), self.contents[i])
s += '{} = {}\n'.format(self.digests[i].hex(), self.contents[i])
return s

View File

@ -29,6 +29,10 @@ class Data:
raise RuntimeError('Object already initialized')
def verify(self):
return True
def version(self):
return self.__version

View File

@ -7,6 +7,7 @@ from cic import Proof
from cic.meta import Meta
from cic.attachment import Attachment
from cic.network import Network
from cic.token import Token
logg = logging.getLogger(__name__)
@ -25,11 +26,13 @@ def execute(config, eargs):
modname = 'cic.ext.{}'.format(eargs.target)
cmd_mod = importlib.import_module(modname)
ct = Token(path=eargs.directory)
cp = Proof(path=eargs.directory)
cm = Meta(path=eargs.directory)
ca = Attachment(path=eargs.directory)
cn = Network(eargs.directory)
cn = Network(path=eargs.directory)
ct.load()
cp.load()
cm.load()
ca.load()

View File

@ -7,11 +7,16 @@ from cic import Proof
from cic.meta import Meta
from cic.attachment import Attachment
from cic.network import Network
from cic.token import Token
logg = logging.getLogger(__name__)
def process_args(argparser):
argparser.add_argument('--target', action='append', type=str, help='initialize network specification file with target')
argparser.add_argument('--name', type=str, help='token name')
argparser.add_argument('--symbol', type=str, help='token symbol')
argparser.add_argument('--precision', type=str, help='token unit precision')
argparser.add_argument('directory', help='directory to initialize')
@ -22,11 +27,13 @@ def validate_args(args):
def execute(config, eargs):
os.makedirs(eargs.directory)
ct = Token(eargs.directory, name=eargs.name, symbol=eargs.symbol, precision=eargs.precision)
cp = Proof(eargs.directory)
cm = Meta(eargs.directory)
ca = Attachment(eargs.directory)
cn = Network(eargs.directory, targets=eargs.target)
ct.start()
cp.start()
cm.start()
ca.start()

View File

@ -3,6 +3,7 @@ from cic import Proof
from cic.meta import Meta
from cic.attachment import Attachment
from cic.network import Network
from cic.token import Token
def process_args(argparser):
@ -15,18 +16,22 @@ def validate_args(args):
def execute(config, eargs):
ct = Token(path=eargs.directory)
cp = Proof(path=eargs.directory)
cm = Meta(path=eargs.directory)
ca = Attachment(path=eargs.directory)
cn = Network(eargs.directory)
ct.load()
cp.load()
cm.load()
ca.load()
cn.load()
print("Version: {}\n".format(cp.version()))
print("Proof:\n{}".format(cp))
print("Meta:\n{}".format(cm))
print("Attachments:\n{}".format(ca))
print("Network:\n{}".format(cn))
print("""[cic.header]
version = {}\n""".format(cp.version()))
print("[cic.token]\n{}".format(ct))
print("[cic.proof]\n{}".format(cp))
print("[cic.meta]\n{}".format(cm))
print("[cic.attachment]\n{}".format(ca))
print("[cic.network]\n{}".format(cn))

View File

@ -0,0 +1,8 @@
{
"name": "",
"symbol": "",
"precision": 0,
"code": null,
"extra": {}
}

View File

@ -47,11 +47,11 @@ class Meta(Data):
def __str__(self):
s = ''
s = "contact.name = {}\n".format(self.name)
for k in self.contact.keys():
if self.contact[k] == '':
continue
s += "{}: {}\n".format(k.lower(), self.contact[k])
s += "contact.{} = {}\n".format(k.lower(), self.contact[k])
return s

View File

@ -26,7 +26,7 @@ class Network(Data):
o = json.load(f)
f.close()
self.references = o['references']
self.resources = o['resources']
self.inited = True
@ -58,10 +58,11 @@ class Network(Data):
def __str__(self):
s = ''
for k in self.references.keys():
v = self.references[k]
for k in self.resources.keys():
for kk in self.resources[k].keys():
v = self.resources[k][kk]
if v == None:
v = ''
s += '{}: {}\n'.format(k, v)
s += '{}.{} = {}\n'.format(k, kk, v)
return s

View File

@ -47,7 +47,5 @@ class Proof(Data):
f.close()
def __str__(self):
return """description: {}
""".format(self.description)
return "description = {}\n".format(self.description)

65
cic/token.py Normal file
View File

@ -0,0 +1,65 @@
# standard imports
import os
import json
# local imports
from .base import (
Data,
data_dir,
)
class Token(Data):
def __init__(self, path='.', name=None, symbol=None, precision=1, code=None):
super(Token, self).__init__()
self.name = name
self.symbol = symbol
self.precision = precision
self.code = code
self.extra_args = None
self.path = path
self.token_path = os.path.join(self.path, 'token.json')
def load(self):
super(Token, self).load()
f = open(self.token_path, 'r')
o = json.load(f)
f.close()
self.name = o['name']
self.symbol = o['symbol']
self.precision = o['precision']
self.code = o['code']
self.extra_args = o['extra']
self.inited = True
def start(self):
super(Token, self).load()
token_template_file_path = os.path.join(data_dir, 'token_template_v{}.json'.format(self.version()))
f = open(token_template_file_path)
o = json.load(f)
f.close()
o['name'] = self.name
o['symbol'] = self.symbol
o['precision'] = self.precision
o['code'] = self.code
f = open(self.token_path, 'w')
json.dump(o, f)
f.close()
def __str__(self):
s = """name = {}
symbol = {}
precision = {}
""".format(self.name, self.symbol, self.precision)
return s