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 d1be2103908c11b681ce89e36995aa9f1670e366
parent e65ffcda46080c637bcebeb42526911b12f8c5c7
Author: nolash <dev@holbrook.no>
Date:   Thu,  2 Jul 2020 13:12:52 +0200

Add gzip option for entries

Diffstat:
Mfeedwarrior/cmd/entry.py | 13+++++++++++--
Mfeedwarrior/entry.py | 7++++++-
Mfeedwarrior/feed.py | 8+++++++-
3 files changed, 24 insertions(+), 4 deletions(-)

diff --git a/feedwarrior/cmd/entry.py b/feedwarrior/cmd/entry.py @@ -4,6 +4,7 @@ import email import logging import uuid import json +import gzip # local imports import feedwarrior @@ -13,6 +14,7 @@ logg = logging.getLogger() def parse_args(argparser): + argparser.add_argument('-z', action='store_true', help='compress entry with gzip') 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 @@ -22,6 +24,7 @@ def check_args(args): pass +# TODO: move logic to package to get symmetry with the show.py logic def execute(config, feed, args): entry = feedentry.from_multipart_file(args.path) if args.task != None: @@ -33,13 +36,19 @@ def execute(config, feed, args): uu = str(entry.uuid) logg.debug('adding entry {}'.format(uu)) entry_path = os.path.join(config.entries_dir, uu) - f = open(entry_path, 'x') + f = None + if args.z: + entry_path += '.gz' + f = gzip.open(entry_path, 'xb') + else: + f = open(entry_path, 'x') feeds_entries_dir = os.path.join(config.feeds_dir, str(feed.uuid), 'entries') try: os.mkdir(feeds_entries_dir) except FileExistsError: pass - json.dump(entry_serialized, f) + entry_json = json.dumps(entry_serialized) + f.write(entry_json.encode('utf-8')) f.close() feeds_entry_path = os.path.join(feeds_entries_dir, uu) diff --git a/feedwarrior/entry.py b/feedwarrior/entry.py @@ -5,6 +5,7 @@ import logging import base64 import enum import time +import gzip # local imports from .common import defaulthashers @@ -63,7 +64,11 @@ class entry: def from_multipart_file(filename, hashers=defaulthashers): #def process_as_multipart_file(config, feed, filename): - f = open(filename, 'r') + f = None + try: + f = open(filename, 'r') + except FileNotFoundError: + f = gzip.open(filename + '.gz', 'rb') m = email.message_from_file(f) f.close() if not m.is_multipart(): diff --git a/feedwarrior/feed.py b/feedwarrior/feed.py @@ -6,6 +6,7 @@ import copy import time import json import logging +import gzip # local imports from feedwarrior.common import parse_uuid @@ -21,7 +22,12 @@ class filegetter: def get(self, uu): entry_path = os.path.join(self.src, str(uu)) - f = open(entry_path, 'r') + f = None + if entry_path[len(entry_path)-3:] == '.gz': + logg.debug('uncompressing {}'.format(entry_path)) + f = gzip.open(entry_path, 'rb') + else: + f = open(entry_path, 'r') c = f.read() f.close() return c