2021-10-09 19:56:29 +02:00
|
|
|
# standard imports
|
|
|
|
import os
|
|
|
|
import logging
|
|
|
|
|
|
|
|
# local imports
|
|
|
|
from .base import *
|
|
|
|
|
|
|
|
logg = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Attachment(Data):
|
|
|
|
|
2021-10-11 17:39:01 +02:00
|
|
|
def __init__(self, path='.', writer=None):
|
2021-10-09 19:56:29 +02:00
|
|
|
super(Attachment, self).__init__()
|
2021-10-10 21:30:18 +02:00
|
|
|
self.contents = {}
|
2021-10-09 19:56:29 +02:00
|
|
|
self.path = path
|
2021-10-11 17:39:01 +02:00
|
|
|
self.writer = writer
|
2021-10-09 19:56:29 +02:00
|
|
|
self.attachment_path = os.path.join(self.path, 'attachments')
|
|
|
|
|
|
|
|
|
|
|
|
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()
|
|
|
|
|
2021-10-10 21:30:18 +02:00
|
|
|
z = self.hash(r).hex()
|
|
|
|
self.contents[z] = fp
|
2021-10-09 19:56:29 +02:00
|
|
|
|
2021-10-10 21:30:18 +02:00
|
|
|
logg.debug('loaded attachment file {} digest {}'.format(fp, z))
|
2021-10-09 19:56:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
super(Attachment, self).start()
|
|
|
|
os.makedirs(self.attachment_path)
|
2021-10-09 20:37:54 +02:00
|
|
|
|
|
|
|
|
2021-10-10 21:30:18 +02:00
|
|
|
def get(self, k):
|
|
|
|
return self.contents[k]
|
|
|
|
|
|
|
|
|
|
|
|
def asdict(self):
|
|
|
|
return self.contents
|
2021-10-09 21:04:11 +02:00
|
|
|
|
|
|
|
|
2021-10-11 17:39:01 +02:00
|
|
|
def process(self, token_address=None, writer=None):
|
|
|
|
if writer == None:
|
|
|
|
writer = self.writer
|
|
|
|
|
|
|
|
for k in self.contents.keys():
|
|
|
|
f = open(self.contents[k], 'rb')
|
|
|
|
v = f.read()
|
|
|
|
f.close()
|
|
|
|
logg.debug('writing {}'.format(k))
|
|
|
|
writer.write(k, v)
|
|
|
|
|
2021-10-09 20:37:54 +02:00
|
|
|
def __str__(self):
|
|
|
|
s = ''
|
|
|
|
for i in range(len(self.contents)):
|
2021-10-10 14:49:22 +02:00
|
|
|
s += '{} = {}\n'.format(self.digests[i].hex(), self.contents[i])
|
2021-10-09 20:37:54 +02:00
|
|
|
|
|
|
|
return s
|