feedwarrior

Slim, portable tooling for creating and distributing decentralized append logs
git clone git://git.defalsify.org/logwarrior.git
Log | Files | Refs | README | LICENSE

commit fc6b397c57e59bb721fe0d014a6de505353f44d6
parent 24ed2be5033acac9b4c80f5da7a670c0e813fdff
Author: nolash <dev@holbrook.no>
Date:   Sun, 28 Jun 2020 18:38:37 +0200

Add taskwarrior extension to entry

Diffstat:
Msrc/feedwarrior/__init__.py | 2++
Msrc/feedwarrior/cmd/entry.py | 16++++++++++------
Msrc/feedwarrior/entry.py | 38++++++++++++++++++++++++++++++++++----
Msrc/main.py | 2+-
4 files changed, 47 insertions(+), 11 deletions(-)

diff --git a/src/feedwarrior/__init__.py b/src/feedwarrior/__init__.py @@ -1,3 +1,5 @@ from .feed import feed from . import entry from .config import load_config + +extension = entry.extension diff --git a/src/feedwarrior/cmd/entry.py b/src/feedwarrior/cmd/entry.py @@ -1,4 +1,5 @@ # standard imports +import os import email import logging import uuid @@ -6,12 +7,14 @@ import json # local imports import feedwarrior +from feedwarrior import entry as feedentry logg = logging.getLogger() def parse_args(argparser): argparser.add_argument('-l', required=True, help='log to add entry to') + argparser.add_argument('--task', action='append', help='add taskwarrior task uuid relation') argparser.add_argument('path', help='multipart file to use for content') return True @@ -20,14 +23,15 @@ def check_args(args): pass - return feedwarrior.entry(uu, m.get_payload()) - - def execute(config, feed, args): - entry = feedwarrior.entry.from_multipart_file(args.path) + entry = feedentry.from_multipart_file(args.path) + for t in args.task: + uu = feedwarrior.common.parse_uuid(t) + entry.add_extension(feedwarrior.extension.TASKWARRIOR, uu) + entry_serialized = entry.serialize() uu = str(entry.uuid) logg.debug('adding entry {}'.format(uu)) - f = open(os.path.join(config.entries_dir, uu) - json.dump(f, entry_serialized) + f = open(os.path.join(config.entries_dir, uu), 'x') + json.dump(entry_serialized, f) f.close() diff --git a/src/feedwarrior/entry.py b/src/feedwarrior/entry.py @@ -3,24 +3,54 @@ import email import uuid import logging import base64 +import enum # local imports from .common import defaulthasher logg = logging.getLogger() +extensiontype = { + 'TASKWARRIOR': uuid.UUID +} + +class extension(enum.Enum): + TASKWARRIOR = 'TASKWARRIOR' + pass class entry: - def __init__(self, uu, payload): + def __init__(self, uu, message): self.uuid = uu - self.payload = payload + self.message = message + self.extensions = {} + + + def add_extension(self, k, v): + if not isinstance(k, extension): + raise ValueError('extension type {} invalid'.format(type(k))) + requiredtyp = extensiontype[k.value] + if not isinstance(v, requiredtyp): + raise ValueError('extension value is {}, but {} is required'.format(type(v).__name__, requiredtyp)) + if self.extensions.get(k.value) == None: + self.extensions[k.value] = [] + + self.extensions[k.value].append(str(v)) + return True + def serialize(self): + + for x in self.extensions.keys(): + logg.debug('adding extension header {}'.format(x)) + v = ','.join(self.extensions[x]) + self.message.add_header('X-FEEDWARRIOR-{}'.format(x), v) + + logg.debug('complete message {}'.format(self.message)) return { 'uuid': str(self.uuid), - 'payload': self.payload, + 'payload': self.message.as_string(), } @@ -66,5 +96,5 @@ def from_multipart_file(filename, hasher=defaulthasher): subject = str(uu) logg.info('subject not specified, using uuid {}'.format(subject)) - return entry(uu, m.as_bytes()) + return entry(uu, m) diff --git a/src/main.py b/src/main.py @@ -60,8 +60,8 @@ else: sys.exit(1) try: - os.makedirs(config.feeds_dir, mode=0o777, exist_ok=False) os.makedirs(config.entries_dir, mode=0o777, exist_ok=False) + os.makedirs(config.feeds_dir, mode=0o777, exist_ok=False) logg.debug('creating datadir {}'.format(config.data_dir)) except FileExistsError as e: logg.debug('using datadir {}'.format(config.data_dir))