Add network module

This commit is contained in:
nolash 2021-10-09 20:37:54 +02:00
parent fad433cd55
commit 546a4d912a
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
10 changed files with 129 additions and 36 deletions

View File

@ -44,3 +44,11 @@ class Attachment(Data):
super(Attachment, self).start()
os.makedirs(self.attachment_path)
def __str__(self):
s = ''
for i in range(len(self.contents)):
s += '{}\t{}\n'.format(self.digests[i].hex(), self.contents[i])
return s

View File

@ -31,3 +31,8 @@ class Data:
def version(self):
return self.__version
def set_version(self, version):
self.__version = version

View File

@ -1,15 +0,0 @@
# local imports
from cic.attachment import Attachment
def process_args(argparser):
argparser.add_argument('-f', '--file', type=str, help='add file')
argparser.add_argument('-d', '--directory', type=str, dest='directory', default='.', help='directory')
def validate_args(args):
pass
def execute(config, eargs):
ca = Attachment(eargs.directory)
ca.load()

View File

@ -6,6 +6,7 @@ import os
from cic import Proof
from cic.meta import Meta
from cic.attachment import Attachment
from cic.network import Network
logg = logging.getLogger(__name__)
@ -24,7 +25,9 @@ def execute(config, eargs):
cp = Proof(eargs.directory)
cm = Meta(eargs.directory)
ca = Attachment(eargs.directory)
cn = Network(eargs.directory)
cp.start()
cm.start()
ca.start()
cn.start()

32
cic/cmd/show.py Normal file
View File

@ -0,0 +1,32 @@
# local imports
from cic import Proof
from cic.meta import Meta
from cic.attachment import Attachment
from cic.network import Network
def process_args(argparser):
argparser.add_argument('-f', '--file', type=str, help='add file')
argparser.add_argument('-d', '--directory', type=str, dest='directory', default='.', help='directory')
def validate_args(args):
pass
def execute(config, eargs):
cp = Proof(path=eargs.directory)
cm = Meta(path=eargs.directory)
ca = Attachment(path=eargs.directory)
cn = Network(eargs.directory)
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))

View File

@ -1,6 +1,7 @@
{
"name": "",
"phone": "",
"location": "",
"country_code": ""
"country_code": "",
"contact": {
}
}

View File

@ -11,7 +11,7 @@ class Meta(Data):
def __init__(self, path='.'):
super(Meta, self).__init__()
self.name = None
self.phone = None
self.contact = {}
self.path = path
self.meta_path = os.path.join(self.path, 'meta.json')
@ -19,13 +19,12 @@ class Meta(Data):
def load(self):
super(Meta, self).load()
f = open(self.proof_path, 'w')
o = json.read(f)
f = open(self.meta_path, 'r')
o = json.load(f)
f.close()
self.description = o['description']
self.version = o['version']
self.namespace = o['namespace']
self.name = o['name']
self.contact = o['contact']
self.inited = True
@ -42,3 +41,14 @@ class Meta(Data):
f = open(self.meta_path, 'w')
json.dump(o, f)
f.close()
def __str__(self):
s = ''
for k in self.contact.keys():
if self.contact[k] == '':
continue
s += "{}: {}\n".format(k.lower(), self.contact[k])
return s

43
cic/network.py Normal file
View File

@ -0,0 +1,43 @@
# standard imports
import os
import json
# local imports
from .base import *
class Network(Data):
def __init__(self, path='.'):
super(Network, self).__init__()
self.references = None
self.path = path
self.network_path = os.path.join(self.path, 'network.json')
def load(self):
super(Network, self).load()
f = open(self.network_path, 'r')
o = json.load(f)
f.close()
self.references = o['references']
self.inited = True
def start(self):
super(Network, self).load()
f = open(self.network_path, 'w')
json.dump({'references': {}}, f)
f.close()
def __str__(self):
s = ''
for k in self.references.keys():
s += '[ref] {}: {}'.format(k, self.references[k])
return s

View File

@ -1,10 +1,13 @@
# standard imports
import os
import json
import logging
# local imports
from .base import *
logg = logging.getLogger(__name__)
class Proof(Data):
@ -17,14 +20,14 @@ class Proof(Data):
def load(self):
super(Meta, self).load()
super(Proof, self).load()
f = open(self.proof_path, 'w')
o = json.read(f)
f = open(self.proof_path, 'r')
o = json.load(f)
f.close()
self.set_version(o['version'])
self.description = o['description']
self.version = o['version']
self.namespace = o['namespace']
self.inited = True
@ -43,3 +46,7 @@ class Proof(Data):
json.dump(o, f)
f.close()
def __str__(self):
return """description: {}
""".format(self.description)

View File

@ -7,7 +7,7 @@ import importlib
# external imports
import cic.cmd.init as cmd_init
import cic.cmd.add as cmd_add
import cic.cmd.show as cmd_show
logging.basicConfig(level=logging.WARNING)
logg = logging.getLogger()
@ -24,9 +24,8 @@ sub = argparser.add_subparsers()
sub.dest = 'command'
sub_init = sub.add_parser('init', help='initialize new cic data directory')
cmd_init.process_args(sub_init)
sub_add = sub.add_parser('add', help='add attachments to cic proof')
cmd_add.process_args(sub_add)
sub_show = sub.add_parser('show', help='display summary of current state of cic data directory')
cmd_show.process_args(sub_show)
args = argparser.parse_args(sys.argv[1:])
@ -46,11 +45,11 @@ cmd_mod = importlib.import_module(modname)
config = None
def main():
try:
cmd_mod.execute(config, args)
except ValueError as e:
logg.error('{}'.format(e))
sys.exit(1)
#try:
cmd_mod.execute(config, args)
#except ValueError as e:
# logg.error('{}'.format(e))
# sys.exit(1)
if __name__ == '__main__':
main()