Add attachment loader
This commit is contained in:
parent
12c3cd84a9
commit
fad433cd55
@ -0,0 +1 @@
|
||||
from .proof import Proof
|
46
cic/attachment.py
Normal file
46
cic/attachment.py
Normal file
@ -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)
|
33
cic/base.py
Normal file
33
cic/base.py
Normal file
@ -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
|
15
cic/cmd/add.py
Normal file
15
cic/cmd/add.py
Normal file
@ -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()
|
@ -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()
|
||||
|
44
cic/meta.py
Normal file
44
cic/meta.py
Normal file
@ -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()
|
45
cic/proof.py
Normal file
45
cic/proof.py
Normal file
@ -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()
|
||||
|
@ -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:])
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user