61 lines
1.3 KiB
Python
61 lines
1.3 KiB
Python
# 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)
|
|
|
|
|
|
def get(self):
|
|
def maphex(v):
|
|
return v.hex()
|
|
return list(map(maphex, self.digests))
|
|
|
|
|
|
def __str__(self):
|
|
s = ''
|
|
for i in range(len(self.contents)):
|
|
s += '{} = {}\n'.format(self.digests[i].hex(), self.contents[i])
|
|
|
|
return s
|