commit d1e2fd226809735f59baff7bad4353f617588773
parent 1e5813ee6e4ef3211ff0259a108f71827bcda5d1
Author: nolash <dev@holbrook.no>
Date: Fri, 3 Jul 2020 21:35:54 +0200
Move both put and get to adapter object
Diffstat:
3 files changed, 66 insertions(+), 40 deletions(-)
diff --git a/feedwarrior/adapters/fileadapter.py b/feedwarrior/adapters/fileadapter.py
@@ -0,0 +1,58 @@
+# standard imports
+import os
+import gzip
+import logging
+
+logg = logging.getLogger()
+
+class fileadapter:
+
+ def __init__(self, db_directory, uu):
+ try:
+ os.mkdir(os.path.join(db_directory, 'feeds'))
+ except FileExistsError:
+ pass
+ try:
+ os.mkdir(os.path.join(db_directory, 'feeds', str(uu)))
+ except FileExistsError:
+ pass
+
+ try:
+ os.mkdir(os.path.join(db_directory, 'entries'))
+ except FileExistsError:
+ pass
+ self.src = db_directory
+ self.feeds_uuid = uu
+
+
+ def get(self, uu):
+ entry_path = os.path.join(self.src, str(uu))
+ 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
+
+
+ def put(self, uu, contents_bytes, compress=False):
+ entry_path = os.path.join(self.src, 'entries', str(uu))
+ if os.path.exists(entry_path) or os.path.exists(entry_path + '.gz'):
+ raise FileExistsError('record {} already exists'.format(str(uu)))
+ f = None
+ if compress:
+ entry_path += '.gz'
+ f = gzip.open(entry_path, 'xb')
+ else:
+ f = open(entry_path, 'xb')
+
+ f.write(contents_bytes)
+ f.close()
+
+ feeds_entry_path = os.path.join(self.src, str(self.feeds_uuid), 'entries', str(uu))
+ if compress:
+ feeds_entry_path += '.gz'
+ os.symlink(entry_path, feeds_entry_path)
diff --git a/feedwarrior/cmd/entry.py b/feedwarrior/cmd/entry.py
@@ -9,6 +9,7 @@ import gzip
# local imports
import feedwarrior
from feedwarrior import entry as feedentry
+from feedwarrior.adapters import fileadapter
logg = logging.getLogger()
@@ -35,25 +36,10 @@ def execute(config, feed, args):
entry_serialized = entry.serialize()
uu = str(entry.uuid)
logg.debug('adding entry {}'.format(uu))
- entry_path = os.path.join(config.entries_dir, uu)
- 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
- 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)
- if args.z:
- feeds_entry_path += '.gz'
- os.symlink(entry_path, feeds_entry_path)
+
+ fa = fileadapter(config.feeds_dir, feed.uuid)
+ entry_json = json.dumps(entry_serialized)
+ fa.put(entry.uuid, entry_json.encode('utf-8'), args.z)
+
feed.add(entry)
diff --git a/feedwarrior/feed.py b/feedwarrior/feed.py
@@ -10,29 +10,11 @@ import gzip
# local imports
from feedwarrior.common import parse_uuid
+from feedwarrior.adapters import fileadapter
logg = logging.getLogger()
-class filegetter:
-
- def __init__(self, source_directory):
- self.src = source_directory
-
-
- def get(self, uu):
- entry_path = os.path.join(self.src, str(uu))
- 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
-
-
class feed:
@@ -140,7 +122,7 @@ def load(path):
for entry in os.listdir(feed_entries_path):
feed_loaded.entries.append(entry)
- fg = filegetter(os.path.join(path, 'entries'))
+ fg = fileadapter(os.path.join(path, 'entries'), uu)
feed_loaded.set_getter(fg)
return feed_loaded