commit 4969fef209856f416219fd8ef58fbeb1d717a174
parent 72fd43c87c5bc56b8173c56f95d91408119f3d40
Author: Louis Holbrook <accounts-gitlab@holbrook.no>
Date: Fri, 3 Jul 2020 22:10:26 +0000
Merge branch 'test' into 'master'
Add reasonable couchdb adapter and first unit test for file and couchdb
See merge request nolash/feedwarrior!1
Diffstat:
5 files changed, 145 insertions(+), 3 deletions(-)
diff --git a/feedwarrior/adapters/__init__.py b/feedwarrior/adapters/__init__.py
@@ -0,0 +1 @@
+from .fileadapter import fileadapter
diff --git a/feedwarrior/adapters/couchdbadapter.py b/feedwarrior/adapters/couchdbadapter.py
@@ -0,0 +1,33 @@
+# standard imports
+import copy
+import json
+import uuid
+
+# third party imports
+import pycouchdb
+
+class couchdbadapter:
+
+ def __init__(self, username, password, database='feedwarrior', host='localhost', port=5984, ssl=False):
+ scheme = 'http'
+ if ssl:
+ scheme += 's'
+ dsn = '{}://{}:{}@{}:{}'.format(scheme, username, password, host, port)
+ self.server = pycouchdb.Server(dsn)
+ self.database = self.server.database(database)
+ self.dsn = dsn
+ self.username = username
+ self.password = password
+
+
+ def put(self, uu, entry, **kwargs):
+ e = entry.serialize()
+ e.pop('uuid')
+ e['_id'] =uu.hex
+ self.database.save(e)
+
+
+ def get(self, uu, **kwargs):
+ e = self.database.get(uu.hex)
+ # reverse parsing this is going to take a bit of effort
+ print('entry ZZZZZZZZZZZZZZZZZZZZZZZZz', e)
diff --git a/feedwarrior/adapters/fileadapter.py b/feedwarrior/adapters/fileadapter.py
@@ -2,9 +2,11 @@
import os
import gzip
import logging
+import json
logg = logging.getLogger()
+
class fileadapter:
def __init__(self, db_directory, uu):
@@ -49,12 +51,13 @@ class fileadapter:
entry_serialized = entry.serialize()
entry_json = json.dumps(entry_serialized)
+ contents_bytes = entry_json.encode('utf-8')
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 kwargs['compress']:
+ if kwargs.get('compress') != None:
entry_path += '.gz'
f = gzip.open(entry_path, 'xb')
else:
@@ -63,7 +66,7 @@ class fileadapter:
f.write(contents_bytes)
f.close()
- feeds_entry_path = os.path.join(self.src, str(self.feeds_uuid), 'entries', str(uu))
- if kwargs['compress']:
+ feeds_entry_path = os.path.join(self.src, 'feeds', str(self.feeds_uuid), 'entries', str(uu))
+ if kwargs.get('compress') != None:
feeds_entry_path += '.gz'
os.symlink(entry_path, feeds_entry_path)
diff --git a/test/test_couchdb.py b/test/test_couchdb.py
@@ -0,0 +1,59 @@
+# standard imports
+import os
+import unittest
+import uuid
+from email.message import EmailMessage
+
+# third party imports
+import pycouchdb
+
+# local imports
+from feedwarrior.adapters.couchdbadapter import couchdbadapter
+from feedwarrior.entry import entry
+
+DBHOST = os.environ.get('FEEDWARRIOR_HOST', 'localhost')
+DBPORT = os.environ.get('FEEDWARRIOR_PORT', 5984)
+DBPASS = os.environ.get('FEEDWARRIOR_PASS')
+DBUSER = os.environ.get('FEEDWARRIOR_USER')
+DBSSL = os.environ.get('FEEDWARRIOR_SSL')
+
+uu = 'x' + uuid.uuid4().hex
+
+class TestCouchdbadapter(unittest.TestCase):
+
+ def setUp(self):
+ ssl = DBSSL != None and DBSSL != ''
+ scheme = 'http'
+ if ssl:
+ scheme += 's'
+ self.srv = pycouchdb.Server('{}://{}:{}@{}:{}'.format(scheme, DBUSER, DBPASS, DBHOST, DBPORT))
+ self.db_name = str(uu)
+ self.db = self.srv.create(self.db_name)
+
+
+ def tearDown(self):
+ self.db.cleanup()
+ self.srv.delete(self.db_name)
+
+
+ def test_init(self):
+ a = couchdbadapter(DBUSER, DBPASS, self.db_name)
+
+
+ def test_put(self):
+ a = couchdbadapter(DBUSER, DBPASS, self.db_name)
+ uu = uuid.uuid4()
+
+ msg = EmailMessage()
+ msg.add_header('Date', 'Thu, 2 Jul 2020 12:00:58 +0200')
+ msg.add_header('Content-Type', 'multipart/mixed')
+ msg.add_attachment('foo')
+
+ e = entry(uu, msg)
+ a.put(uu, e)
+
+ a.get(uu)
+
+
+if __name__ == '__main__':
+ unittest.main()
diff --git a/test/test_file.py b/test/test_file.py
@@ -0,0 +1,46 @@
+# standard imports
+import os
+import unittest
+import tempfile
+import logging
+import uuid
+import shutil
+from email.message import EmailMessage
+
+# local imports
+from feedwarrior.adapters.fileadapter import fileadapter
+from feedwarrior.entry import entry
+
+logging.basicConfig(level=logging.DEBUG)
+logg = logging.getLogger()
+
+
+class TestFileadapter(unittest.TestCase):
+
+ def setUp(self):
+ uu = uuid.uuid4()
+ self.feed_uuid = uu
+ self.path = tempfile.mkdtemp()
+
+
+ def tearDown(self):
+ shutil.rmtree(self.path)
+
+
+ def test_put(self):
+ a = fileadapter(self.path, self.feed_uuid)
+ uu = uuid.uuid4()
+
+ msg = EmailMessage()
+ msg.add_header('Date', 'Thu, 2 Jul 2020 12:00:58 +0200')
+ msg.add_header('Content-Type', 'multipart/mixed')
+ msg.add_attachment('foo')
+
+ e = entry(uu, msg)
+ a.put(uu, e)
+
+ print('ieeeee')
+ logg.debug(a.get(uu))
+
+if __name__ == '__main__':
+ unittest.main()