diff --git a/cic/__init__.py b/cic/__init__.py index e69de29..2073594 100644 --- a/cic/__init__.py +++ b/cic/__init__.py @@ -0,0 +1 @@ +from .proof import Proof diff --git a/cic/attachment.py b/cic/attachment.py new file mode 100644 index 0000000..c3111fb --- /dev/null +++ b/cic/attachment.py @@ -0,0 +1,46 @@ +# standard imports +import os +import hashlib +import logging + +# local imports +from .base import * + +logg = logging.getLogger(__name__) + + +class Attachment(Data): + + def __init__(self, path='.'): + super(Attachment, self).__init__() + self.contents = [] + self.digests = [] + self.path = path + self.attachment_path = os.path.join(self.path, 'attachments') + self.__hasher = self.__basehasher + + + def __basehasher(self, v): + h = hashlib.sha256() + h.update(v) + return h.digest() + + + def load(self): + for s in os.listdir(self.attachment_path): + fp = os.path.realpath(os.path.join(self.attachment_path, s)) + f = open(fp, 'rb') + r = f.read() + f.close() + self.contents.append(fp) + + z = self.__hasher(r) + self.digests.append(z) + + logg.debug('loaded attachment file {} digest {}'.format(fp, z.hex())) + + + def start(self): + super(Attachment, self).start() + + os.makedirs(self.attachment_path) diff --git a/cic/base.py b/cic/base.py new file mode 100644 index 0000000..edee41e --- /dev/null +++ b/cic/base.py @@ -0,0 +1,33 @@ +# standard imports +import os + + +mod_dir = os.path.dirname(os.path.realpath(__file__)) +root_dir = os.path.join(mod_dir, '..') +data_dir = os.path.join(mod_dir, 'data') +schema_dir = os.path.join(mod_dir, 'schema') + + +class Data: + + + __default_version = 0 + + def __init__(self): + self.dirty = False + self.inited = False + self.__version = self.__default_version + + + def load(self): + if self.dirty: + raise RuntimeError('Object contains uncommitted changes') + + + def start(self): + if self.inited: + raise RuntimeError('Object already initialized') + + + def version(self): + return self.__version diff --git a/cic/cmd/add.py b/cic/cmd/add.py new file mode 100644 index 0000000..69b65d2 --- /dev/null +++ b/cic/cmd/add.py @@ -0,0 +1,15 @@ +# 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() diff --git a/cic/cmd/init.py b/cic/cmd/init.py index f0ca715..229ff56 100644 --- a/cic/cmd/init.py +++ b/cic/cmd/init.py @@ -1,5 +1,11 @@ # standard imports import logging +import os + +# local imports +from cic import Proof +from cic.meta import Meta +from cic.attachment import Attachment logg = logging.getLogger(__name__) @@ -13,4 +19,12 @@ def validate_args(args): def execute(config, eargs): - pass + os.makedirs(eargs.directory) + + cp = Proof(eargs.directory) + cm = Meta(eargs.directory) + ca = Attachment(eargs.directory) + + cp.start() + cm.start() + ca.start() diff --git a/cic/data/metadata_template_v0.json b/cic/data/meta_template_v0.json similarity index 100% rename from cic/data/metadata_template_v0.json rename to cic/data/meta_template_v0.json diff --git a/cic/meta.py b/cic/meta.py new file mode 100644 index 0000000..4ae7238 --- /dev/null +++ b/cic/meta.py @@ -0,0 +1,44 @@ +# standard imports +import os +import json + +# local imports +from .base import * + + +class Meta(Data): + + def __init__(self, path='.'): + super(Meta, self).__init__() + self.name = None + self.phone = None + self.path = path + self.meta_path = os.path.join(self.path, 'meta.json') + + + def load(self): + super(Meta, self).load() + + f = open(self.proof_path, 'w') + o = json.read(f) + f.close() + + self.description = o['description'] + self.version = o['version'] + self.namespace = o['namespace'] + + 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() diff --git a/cic/proof.py b/cic/proof.py new file mode 100644 index 0000000..9d12a3d --- /dev/null +++ b/cic/proof.py @@ -0,0 +1,45 @@ +# standard imports +import os +import json + +# local imports +from .base import * + + +class Proof(Data): + + def __init__(self, path='.'): + super(Proof, self).__init__() + self.namespace = 'ge' + self.description = None + self.path = path + self.proof_path = os.path.join(self.path, 'proof.json') + + + def load(self): + super(Meta, self).load() + + f = open(self.proof_path, 'w') + o = json.read(f) + f.close() + + self.description = o['description'] + self.version = o['version'] + self.namespace = o['namespace'] + + self.inited = True + + + def start(self): + super(Proof, self).start() + + proof_template_file_path = os.path.join(data_dir, 'proof_template_v{}.json'.format(self.version())) + + f = open(proof_template_file_path) + o = json.load(f) + f.close() + + f = open(self.proof_path, 'w') + json.dump(o, f) + f.close() + diff --git a/cic/runnable/cic_cmd.py b/cic/runnable/cic_cmd.py index 981930b..2914f1d 100644 --- a/cic/runnable/cic_cmd.py +++ b/cic/runnable/cic_cmd.py @@ -7,6 +7,7 @@ import importlib # external imports import cic.cmd.init as cmd_init +import cic.cmd.add as cmd_add logging.basicConfig(level=logging.WARNING) logg = logging.getLogger() @@ -23,6 +24,9 @@ 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) + args = argparser.parse_args(sys.argv[1:])