commit 6263dabe74b3df3e78aa4c1093080bc10588d2c6
parent 10abb4bf1144082dc8eaa63b07af053b58f8eb46
Author: lash <dev@holbrook.no>
Date: Fri, 18 Nov 2022 07:52:16 +0000
Reverse message ordering
Diffstat:
3 files changed, 58 insertions(+), 29 deletions(-)
diff --git a/piknik/basket.py b/piknik/basket.py
@@ -141,12 +141,12 @@ class Basket:
return shep.state.split_elements(r)
- def __get_msg(self, issue_id, envelope_callback=None, message_callback=None):
+ def __get_msg(self, issue_id, envelope_callback=None, message_callback=None, post_callback=None):
r = self.state.get(issue_id)
o = Issue.from_str(r)
try:
v = self.__msg.get(issue_id)
- m = IssueMessage.parse(o, v.decode('utf-8'), envelope_callback=envelope_callback, message_callback=message_callback)
+ m = IssueMessage.parse(o, v.decode('utf-8'), envelope_callback=envelope_callback, message_callback=message_callback, post_callback=post_callback)
return m
except FileNotFoundError as e:
logg.debug('instantiating new message log for {} {}'.format(issue_id, e))
@@ -154,9 +154,9 @@ class Basket:
return IssueMessage(o)
- def get_msg(self, issue_id, envelope_callback=None, message_callback=None):
- return self.__get_msg(issue_id, envelope_callback=envelope_callback, message_callback=message_callback)
-
+ def get_msg(self, issue_id, envelope_callback=None, message_callback=None, post_callback=None):
+ return self.__get_msg(issue_id, envelope_callback=envelope_callback, message_callback=message_callback, post_callback=post_callback)
+
def assign(self, issue_id, identity):
r = self.state.get(issue_id)
@@ -187,7 +187,6 @@ class Basket:
def msg(self, issue_id, *args):
m = self.__get_msg(issue_id)
- print('slllslsll')
m.add(*args, wrapper=self.__msg_wrap)
ms = m.as_bytes()
self.__msg.put(issue_id, ms)
diff --git a/piknik/msg.py b/piknik/msg.py
@@ -42,10 +42,11 @@ class IssueMessage:
self.__m.set_boundary(str(uuid.uuid4()))
- def __unwrap(self, msg, envelope_callback=rubber_stamp_envelope, message_callback=None):
+ def __unwrap(self, msg, envelope_callback=rubber_stamp_envelope, message_callback=None, post_callback=None):
message_ids = []
message_id = None
envelope = None
+
for m in msg.walk():
env_header = m.get('X-Piknik-Envelope')
if env_header != None:
@@ -63,14 +64,18 @@ class IssueMessage:
message_ids.append(message_id)
message_callback(envelope, m, message_id)
+
+ if post_callback != None:
+ post_callback(message_ids)
+
return message_ids
@classmethod
- def parse(cls, issue, v, envelope_callback=None, message_callback=None):
+ def parse(cls, issue, v, envelope_callback=None, message_callback=None, post_callback=None):
o = cls(issue)
m = message_from_string(v)
- o.__unwrap(m, envelope_callback=envelope_callback, message_callback=message_callback)
+ o.__unwrap(m, envelope_callback=envelope_callback, message_callback=message_callback, post_callback=post_callback)
o.__m = m
return o
@@ -112,7 +117,6 @@ class IssueMessage:
def add(self, *args, related_id=None, wrapper=None, message_id=None):
- print('aaargs {}'.format(args))
m_id = None
try:
m_id = uuid.UUID(message_id)
diff --git a/piknik/runnable/show.py b/piknik/runnable/show.py
@@ -24,6 +24,7 @@ argp.add_argument('-d', type=str, help='Data directory')
argp.add_argument('-f', '--files', dest='f', action='store_true', help='Save attachments to filesystem')
argp.add_argument('-o', '--files-dir', dest='files_dir', type=str, default='.', help='Directory to output saved files to')
argp.add_argument('-r', '--renderer', type=str, default='default', help='Renderer module for output')
+argp.add_argument('--reverse', action='store_true', help='Sort comments by oldest first')
argp.add_argument('issue_id', type=str, help='Issue id to show')
arg = argp.parse_args(sys.argv[1:])
@@ -51,27 +52,30 @@ class PGPWrapper(PGPSigner):
def __init__(self, home_dir=None):
super(PGPWrapper, self).__init__(home_dir=home_dir)
self.message_date = None
- self.message = []
+ self.messages = []
+ self.part = []
self.message_id = None
self.sender = None
self.valid = False
- def render_message(self, envelope, messages, message_id, w=sys.stdout, dump_dir=None):
+ def render_message(self, envelope, messages, message_date, message_id, dump_dir=None, w=sys.stdout):
r = ''
for message in messages:
m = parse_mime_type(message.get_content_type())
+ filename = message.get_filename()
v = ''
- if m[0] == 'text':
- if m[1] == 'plain':
- v = message.get_payload()
- if message.get('Content-Transfer-Encoding') == 'BASE64':
- v = b64decode(v).decode()
- else:
- v = '[rich text]'
+ if filename == None:
+ if m[0] == 'text':
+ if m[1] == 'plain':
+ v = message.get_payload()
+ if message.get('Content-Transfer-Encoding') == 'BASE64':
+ v = b64decode(v).decode()
+ else:
+ v = '[rich text]'
else:
- filename = message.get_filename()
+ #filename = message.get_filename()
if dump_dir != None:
v = message.get_payload()
if message.get('Content-Transfer-Encoding') == 'BASE64':
@@ -80,13 +84,14 @@ class PGPWrapper(PGPSigner):
sz = message.get('Content-Length')
if sz == None:
sz = 'unknown'
- v = '[file: ' + filename + ', size: ' + sz + ']'
+ #v = '[file: ' + filename + ', type: ' + m[0] + m[1] + ', size: ' + sz + ']'
+ v = '[file: {}, type {}/{}, size: {}]'.format(filename, m[0], m[1], sz)
valid = '[++]'
if not self.valid:
valid = '[!!]'
r += '\n\t' + v + '\n'
- w.write('\nmessage {} from {} {} - {}\n\t{}\n'.format(self.message_date, self.sender, valid, message_id, r))
+ w.write('\nmessage {} from {} {} - {}\n\t{}\n'.format(message_date, self.sender, valid, message_id, r))
def envelope_callback(self, envelope, envelope_type):
@@ -106,21 +111,37 @@ class PGPWrapper(PGPSigner):
if message_id == None:
return
+ messages = []
if message.get('X-Piknik-Msg-Id') == None:
if message.get('Content-Type') == 'application/pgp-signature':
- dump_dir = None
- if arg.f:
- dump_dir = arg.files_dir
- self.render_message(envelope, self.message, self.message_id, dump_dir=dump_dir)
- self.message = []
+
+ #self.render_message(envelope, self.message, self.message_id, dump_dir=dump_dir)
+ self.messages.append((envelope, self.part, self.message_date, self.message_id,))
+ self.part = []
self.message_id = None
+ self.message_date = None
else:
- self.message.append(message)
+ self.part.append(message)
else:
d = message.get('Date')
self.message_date = parsedate_to_datetime(d)
self.message_id = message_id
+
+ def post_callback(self, messages_id):
+ dump_dir = None
+ if arg.f:
+ dump_dir = arg.files_dir
+ rg = None
+ if arg.reverse:
+ rg = range(0, len(self.messages))
+ else:
+ rg = range(len(self.messages)-1, -1, -1)
+ for i in rg:
+ v = self.messages[i]
+ self.render_message(v[0], v[1], v[2], v[3], dump_dir=dump_dir)
+
+
gpg_home = os.environ.get('GPGHOME')
verifier = PGPWrapper(home_dir=gpg_home)
@@ -151,7 +172,12 @@ tags: {}
s += ' (owner)'
print('\t' + str(s))
- m = basket.get_msg(arg.issue_id, envelope_callback=verifier.envelope_callback, message_callback=verifier.message_callback)
+ m = basket.get_msg(
+ arg.issue_id,
+ envelope_callback=verifier.envelope_callback,
+ message_callback=verifier.message_callback,
+ post_callback=verifier.post_callback,
+ )
def main():