commit 226309295bb25d3ce7168666e6a16f1dad472339
parent 3b4f8956edaad90f152ceaa33c89d882d85a7e95
Author: nolash <dev@holbrook.no>
Date: Sun, 28 Jun 2020 15:49:36 +0200
Move feed object to package
Diffstat:
6 files changed, 123 insertions(+), 114 deletions(-)
diff --git a/config.ini b/config_example.ini
diff --git a/feedwarrior.py b/feedwarrior.py
@@ -1,114 +0,0 @@
-#!/usr/bin/python
-
-# Author: Louis Holbrook <dev@holbrook.no> (https://holbrook.no)
-# License: GPLv3
-# Description: Work log tool
-
-# standard imports
-import copy
-import os
-import sys
-import argparse
-import configparser
-import json
-import logging
-import uuid
-import time
-
-logging.basicConfig(level=logging.ERROR)
-logg = logging.getLogger()
-
-DATA_DIR='/dev/null'
-
-
-argparser = argparse.ArgumentParser(description='create and manipulate feedwarrior logs')
-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()
-sub.dest = 'command'
-
-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']
-
-try:
- os.mkdir(DATA_DIR)
- logg.debug('creating datadir {}'.format(DATA_DIR))
-except FileExistsError as e:
- logg.debug('using datadir {}'.format(DATA_DIR))
-
-
-# TODO: move to submodule asap
-
-def parse_uuid(uu):
- if type(uu).__name__ == 'str':
- return uuid.UUID('urn:uuid:' + uu)
- elif type(uu).__name__ == 'UUID':
- return uu
- raise ValueError('invalid uuid')
-
-class Feed:
-
- def __init__(self, uu=None, parent=None, created=None, updated=None):
- if uu == None:
- self.uuid = uuid.uuid4()
- else:
- self.uuid = parse_uuid(uu)
-
- self.parent = None
- if parent != None:
- if type(parent).__name__ != 'Feed':
- raise ValueError('wrong type for parent: {}'.format(type(parent).__name__))
- self.parent = parent
-
- if created != None:
- self.created = created
- if updated == None:
- self.updated = copy.copy(created)
- else:
- self.created = int(time.time())
- self.updated = copy.copy(self.created)
-
- if self.updated == None:
- self.updated = updated
-
- self.entries = []
-
-
- def serialize(self):
- o = {
- 'uuid': str(self.uuid),
- 'created': self.created,
- 'updated': self.updated,
- }
- if self.parent != None:
- o['parent_uuid'] = str(self.parent.uuid)
-
- return o
-
-feed_parent = None
-if args.p != None:
- try:
- feed_parent = Feed(args.p)
- except ValueError as e:
- logg.error('invalid parent {}: {}'.format(args.p, e))
- sys.exit(1)
-
-if args.command == None:
- feed_current = Feed(parent=feed_parent)
- uu = str(feed_current.uuid)
- logg.debug('new log {}'.format(uu))
- log_path = os.path.join(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_current.serialize(), f)
- f.close()
- sys.exit(0)
diff --git a/src/feedwarrior/__init__.py b/src/feedwarrior/__init__.py
@@ -0,0 +1 @@
+from .feed import feed
diff --git a/src/feedwarrior/common.py b/src/feedwarrior/common.py
@@ -0,0 +1,9 @@
+# standard imports
+import uuid
+
+def parse_uuid(uu):
+ if type(uu).__name__ == 'str':
+ return uuid.UUID('urn:uuid:' + uu)
+ elif type(uu).__name__ == 'UUID':
+ return uu
+ raise ValueError('invalid uuid')
diff --git a/src/feedwarrior/feed.py b/src/feedwarrior/feed.py
@@ -0,0 +1,45 @@
+# standard imports
+import uuid
+import copy
+import time
+from .feedwarrior import parse_uuid
+
+
+class feed:
+
+ def __init__(self, uu=None, parent=None, created=None, updated=None):
+ if uu == None:
+ self.uuid = uuid.uuid4()
+ else:
+ self.uuid = parse_uuid(uu)
+
+ self.parent = None
+ if parent != None:
+ if type(parent).__name__ != 'feed':
+ raise ValueError('wrong type for parent: {}'.format(type(parent).__name__))
+ self.parent = parent
+
+ if created != None:
+ self.created = created
+ if updated == None:
+ self.updated = copy.copy(created)
+ else:
+ self.created = int(time.time())
+ self.updated = copy.copy(self.created)
+
+ if self.updated == None:
+ self.updated = updated
+
+ self.entries = []
+
+
+ def serialize(self):
+ o = {
+ 'uuid': str(self.uuid),
+ 'created': self.created,
+ 'updated': self.updated,
+ }
+ if self.parent != None:
+ o['parent_uuid'] = str(self.parent.uuid)
+
+ return o
diff --git a/src/main.py b/src/main.py
@@ -0,0 +1,68 @@
+#!/usr/bin/python
+
+# Author: Louis Holbrook <dev@holbrook.no> (https://holbrook.no)
+# License: GPLv3
+# Description: Work log tool
+
+# standard imports
+import os
+import sys
+import argparse
+import configparser
+import json
+import logging
+
+# local imports
+import feedwarrior
+
+logging.basicConfig(level=logging.ERROR)
+logg = logging.getLogger()
+
+DATA_DIR='/dev/null'
+
+
+argparser = argparse.ArgumentParser(description='create and manipulate feedwarrior logs')
+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()
+sub.dest = 'command'
+
+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']
+
+try:
+ os.mkdir(DATA_DIR)
+ logg.debug('creating datadir {}'.format(DATA_DIR))
+except FileExistsError as e:
+ logg.debug('using datadir {}'.format(DATA_DIR))
+
+
+# TODO: move to submodule asap
+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)
+
+if __name__ == '__main__':
+ if args.command == None:
+ feed_current = feedwarrior.feed(parent=feed_parent)
+ uu = str(feed_current.uuid)
+ logg.debug('new log {}'.format(uu))
+ log_path = os.path.join(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_current.serialize(), f)
+ f.close()
+ sys.exit(0)