commit e89faa25da50b566488fb126e10b1efbd2cde082
parent b819ea3bd47716a7a0408e162b502409f8efb6fc
Author: nolash <dev@holbrook.no>
Date: Sun, 28 Jun 2020 16:19:37 +0200
Add template for entry subcommand
Diffstat:
6 files changed, 78 insertions(+), 14 deletions(-)
diff --git a/src/feedwarrior/__init__.py b/src/feedwarrior/__init__.py
@@ -1 +1,2 @@
from .feed import feed
+from .config import load_config
diff --git a/src/feedwarrior/cmd/entry.py b/src/feedwarrior/cmd/entry.py
@@ -0,0 +1,9 @@
+def parse_args(argparser):
+ argparser.add_argument('-l', required=True, help='log to add entry to')
+ return True
+
+def check_args(args):
+ pass
+
+def execute(config, feed, args):
+ pass
diff --git a/src/feedwarrior/cmd/log.py b/src/feedwarrior/cmd/log.py
@@ -0,0 +1,28 @@
+# standard imports
+import os
+import sys
+import json
+import logging
+
+logg = logging.getLogger(__name__)
+
+# TODO: move to submodule asap
+def parse_args(argparser):
+ pass
+
+
+def check_args(argparser):
+ pass
+
+
+def execute(config, feed, args):
+ if args.command == None:
+ uu = str(feed.uuid)
+ logg.debug('new log {}'.format(uu))
+ log_path = os.path.join(config.data_dir, str(uu))
+ os.mkdir(log_path)
+
+ log_meta_path = os.path.join(log_path, '.log')
+ f = open(log_meta_path, 'x')
+ json.dump(feed.serialize(), f)
+ f.close()
diff --git a/src/feedwarrior/config.py b/src/feedwarrior/config.py
@@ -0,0 +1,12 @@
+# standard imports
+import configparser
+
+class config:
+
+ def __init__(self, filename):
+ cp = configparser.ConfigParser()
+ cp.read(filename)
+ self.data_dir = cp['FEEDWARRIOR']['datadir']
+
+def load_config(filename):
+ return config(filename)
diff --git a/src/feedwarrior/feed.py b/src/feedwarrior/feed.py
@@ -2,7 +2,7 @@
import uuid
import copy
import time
-from .feedwarrior import parse_uuid
+from .common import parse_uuid
class feed:
diff --git a/src/main.py b/src/main.py
@@ -14,11 +14,12 @@ import logging
# local imports
import feedwarrior
+from feedwarrior.cmd import log as cmd_log
+from feedwarrior.cmd import entry as cmd_entry
logging.basicConfig(level=logging.ERROR)
logg = logging.getLogger()
-DATA_DIR='/dev/null'
argparser = argparse.ArgumentParser(description='create and manipulate feedwarrior logs')
@@ -26,30 +27,43 @@ argparser.add_argument('-p', type=str, help='parent log uuid')
argparser.add_argument('-c', required=True, type=str, help='configuration file')
argparser.add_argument('-v', action='store_true', help='be verbose')
sub = argparser.add_subparsers()
+# TODO: add subparser to same level flags as main parser
sub.dest = 'command'
+sub_entry = sub.add_parser('entry', help='add entry to log')
+cmd_entry.parse_args(sub_entry)
args = argparser.parse_args(sys.argv[1:])
if args.v:
logging.getLogger().setLevel(logging.DEBUG)
logg.debug('loading config {}'.format(args.c))
-cp = configparser.ConfigParser()
-cp.read(args.c)
-DATA_DIR = cp['FEEDWARRIOR']['datadir']
+config = feedwarrior.load_config(args.c)
-cmd = 'log'
-if args.command != None:
- log.error('invalid command {}'.format(args.command))
- sys.exit(1)
+
+feed_current = None
+feed_parent = None
+if args.p != None:
+ try:
+ feed_parent = feedwarrior.feed(args.p)
+ except ValueError as e:
+ logg.error('invalid parent {}: {}'.format(args.p, e))
+ sys.exit(1)
cmd_mod = None
-if cmd == 'log':
- from feedwarrior.cmd import log as cmd_mod
+if args.command == None:
+ feed_current = feedwarrior.feed(parent=feed_parent)
+ cmd_mod = cmd_log
+elif args.command == 'entry':
+ cmd_mod = cmd_entry
+else:
+ log.error('invalid command {}'.format(args.command))
+ sys.exit(1)
try:
- os.mkdir(DATA_DIR)
- logg.debug('creating datadir {}'.format(DATA_DIR))
+ os.mkdir(config.data_dir)
+ logg.debug('creating datadir {}'.format(config.data_dir))
except FileExistsError as e:
- logg.debug('using datadir {}'.format(DATA_DIR))
+ logg.debug('using datadir {}'.format(config.data_dir))
if __name__ == '__main__':
+ cmd_mod.execute(config, feed_current, args)