piknik

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit 288af8c1aecdfae42040bf6c62514f043bb41160
parent bc7d5b3cd2def836fe3785e083ec2a8773f587bf
Author: lash <dev@holbrook.no>
Date:   Sat, 19 Nov 2022 10:00:25 +0000

Consolidate renderer hooks across list and show

Diffstat:
Mpiknik/render/html.py | 49+++++++++++++++++++++++++++++++++++++------------
Mpiknik/render/plain.py | 24++++++++++++++++++++----
Mpiknik/runnable/show.py | 24++++++++++++------------
3 files changed, 69 insertions(+), 28 deletions(-)

diff --git a/piknik/render/html.py b/piknik/render/html.py @@ -1,9 +1,13 @@ +# standard imports import sys import os +# external imports import dominate -from dominate.tags import div, p, a, meta, ul, li, h1, h2, link +from dominate.tags import div, p, a, meta, ul, ol, li, h1, h2, link +from mimeparse import parse_mime_type +# local imports from .base import Renderer as BaseRenderer @@ -13,6 +17,7 @@ class Renderer(BaseRenderer): super(Renderer, self).__init__() self.issue_buf = [] self.state_buf = [] + self.message_buf = [] self.outdir = outdir @@ -32,19 +37,17 @@ class Renderer(BaseRenderer): def apply_issue(self, state, issue, tags, w=sys.stdout): v = li(a(issue.title, href=issue.id + '.html')) + r_l = ol() + while True: + try: + v = self.message_buf.pop(0) + r_l.add(v) + except IndexError: + break + v.add(r_l) self.issue_buf.append(v) - def apply_message(self, state, issue, tags, message, w=None): - pass - - - def apply_message_part(self, state, issue, envelope, message, message_date, message_id, dump_dir=None, w=sys.stdout): - m = parse_mime_type(message.get_content_type()) - filename = message.get_filename() - - - def apply_issue_post(self, state, issue, tags, w=None): close = False if w == None: @@ -59,13 +62,35 @@ class Renderer(BaseRenderer): w.close() + def apply_message_post(self, state, issue, tags, message, message_from, message_date, message_id, message_valid, w=sys.stdout): + #r = ol() + #w.write(self.message_buf.render()) + pass + + + def apply_message_part(self, state, issue, envelope, message, message_from, message_date, message_id, message_valid, dump_dir=None, w=sys.stdout): + m = parse_mime_type(message.get_content_type()) + filename = message.get_filename() + + if filename == None: + v = message.get_payload() + if message.get('Content-Transfer-Encoding') == 'BASE64': + v = b64decode(v).decode() + self.message_buf.append(p(v)) + + def apply_end(self, w=sys.stdout): r = dominate.document(title='issues for ...') r.head.add(meta(name='generator', content='piknik')) r.head.add(link(rel='stylesheet', href='style.css')) + buf = None + if len(self.state_buf) > 0: + buf = self.state_buf + else: + buf = self.issue_buf while True: try: - v = self.state_buf.pop(0) + v = buf.pop(0) r.add(v) except IndexError: break diff --git a/piknik/render/plain.py b/piknik/render/plain.py @@ -10,6 +10,11 @@ from .base import Renderer as BaseRenderer class Renderer(BaseRenderer): + def __init__(self): + super(Renderer, self).__init__() + self.msg_buf = '' + + def apply_issue(self, state, issue, tags, w=sys.stdout): w.write("""id: {} title: {} @@ -38,11 +43,22 @@ tags: {} w.write('\t' + str(s)) - def apply_message(self, state, issue, tags, message, w=sys.stdout): - pass + def apply_message_post(self, state, issue, tags, message, message_from, message_date, message_id, message_valid, w=sys.stdout): + r = self.msg_buf + self.msg_buf = '' + w.write('\nmessage {} from {} {} - {}\n\t{}\n'.format(message_date, message_from, message_valid, message_id, r)) + #return r + + + #def apply_message(self, state, issue, tags, message, dump_dir=None, w=sys.stdout): + # return + + + #ww.seek(0) + #self.msg_buf += '\n\t' + ww.read() + '\n' - def apply_message_part(self, state, issue, envelope, message, message_date, message_id, dump_dir=None, w=sys.stdout): + def apply_message_part(self, state, issue, envelope, message, message_from, message_date, message_id, message_valid, dump_dir=None, w=sys.stdout): m = parse_mime_type(message.get_content_type()) filename = message.get_filename() @@ -66,4 +82,4 @@ tags: {} sz = 'unknown' v = '[file: {}, type {}/{}, size: {}]'.format(filename, m[0], m[1], sz) - w.write(v) + w.write('\n\t' + v + '\n') diff --git a/piknik/runnable/show.py b/piknik/runnable/show.py @@ -64,17 +64,12 @@ class PGPWrapper(PGPSigner): def render_message(self, envelope, messages, message_date, message_id, dump_dir=None, w=sys.stdout): - r = '' - w.write('\n') for message in messages: - ww = io.StringIO() - self.renderer.apply_message_part(self.state, self.issue, envelope, message, message_date, message_id, dump_dir=dump_dir, w=ww) - valid = '[++]' - if not self.valid: - valid = '[!!]' - ww.seek(0) - r += '\n\t' + ww.read() + '\n' - w.write('\nmessage {} from {} {} - {}\n\t{}\n'.format(message_date, self.sender, valid, message_id, r)) + self.renderer.apply_message_part(self.state, self.issue, envelope, message, self.sender, message_date, message_id, self.valid, dump_dir=dump_dir, w=w) + #valid = '[++]' + #if not self.valid: + # valid = '[!!]' + self.renderer.apply_message_post(self.state, self.issue, envelope, message, self.sender, message_date, message_id, self.valid, w=w) def envelope_callback(self, envelope, envelope_type): @@ -129,6 +124,7 @@ gpg_home = os.environ.get('GPGHOME') def render(renderer, basket, state, issue, tags): + renderer.apply_begin() renderer.apply_issue(state, issue, tags) verifier = PGPWrapper(renderer, state, issue, home_dir=gpg_home) m = basket.get_msg( @@ -137,6 +133,10 @@ def render(renderer, basket, state, issue, tags): message_callback=verifier.message_callback, post_callback=verifier.post_callback, ) + renderer.apply_issue_post(state, issue, tags) + renderer.apply_state_post(state) + renderer.apply_end() + def render_states(renderer, basket, states): renderer.apply_begin() @@ -185,8 +185,8 @@ def main(): renderer = piknik.render.html.Renderer() return process_states(renderer, basket) - import piknik.render.plain - renderer = piknik.render.plain.Renderer() + import piknik.render.html + renderer = piknik.render.html.Renderer() issue = basket.get(arg.issue_id) tags = basket.tags(arg.issue_id)