cic-cli/cic/meta.py
2021-10-14 16:06:07 +02:00

97 lines
2.2 KiB
Python

# standard imports
import os
import json
# external imports
from cic_types import MetadataPointer
from cic_types.processor import generate_metadata_pointer
from cic_types.ext.metadata import MetadataRequestsHandler
from hexathon import strip_0x
# local imports
from .base import (
Data,
data_dir,
)
from cic.output import OutputWriter
class Meta(Data):
def __init__(self, path='.', writer=None):
super(Meta, self).__init__()
self.name = None
self.contact = {}
self.path = path
self.writer = writer
self.meta_path = os.path.join(self.path, 'meta.json')
def load(self):
super(Meta, self).load()
f = open(self.meta_path, 'r')
o = json.load(f)
f.close()
self.name = o['name']
self.contact = o['contact']
self.inited = True
def start(self):
super(Meta, self).start()
meta_template_file_path = os.path.join(data_dir, 'meta_template_v{}.json'.format(self.version()))
f = open(meta_template_file_path)
o = json.load(f)
f.close()
f = open(self.meta_path, 'w')
json.dump(o, f)
f.close()
def reference(self, token_address):
token_address_bytes = bytes.fromhex(strip_0x(token_address))
return generate_metadata_pointer(token_address_bytes, MetadataPointer.TOKEN)
def asdict(self):
return {
'name': self.name,
'contact': self.contact,
}
def process(self, token_address=None, writer=None):
if writer == None:
writer = self.writer
k = self.reference(token_address)
v = json.dumps(self.asdict())
writer.write(k, v.encode('utf-8'))
return (k, v)
def __str__(self):
s = "contact.name = {}\n".format(self.name)
for k in self.contact.keys():
if self.contact[k] == '':
continue
s += "contact.{} = {}\n".format(k.lower(), self.contact[k])
return s
class MetadataWriter(OutputWriter):
def write(self, k, v):
rq = MetadataRequestsHandler(MetadataPointer.TOKEN, bytes.fromhex(k))
return rq.create(json.loads(v.decode('utf-8')))