Prechádzať zdrojové kódy

Switch to yaml for storage backend

theenglishway (time) 6 rokov pred
rodič
commit
1cb7b3e642
4 zmenil súbory, kde vykonal 53 pridanie a 17 odobranie
  1. 2 1
      .gitignore
  2. 1 10
      planner/__init__.py
  3. 7 6
      planner/cli.py
  4. 43 0
      planner/database.py

+ 2 - 1
.gitignore

@@ -2,4 +2,5 @@
 *.egg-info
 venv/
 *.pyc
-planning.json
+planning.json
+planning.yaml

+ 1 - 10
planner/__init__.py

@@ -1,8 +1,7 @@
-import json
-from datetime import datetime
 from tinydb import TinyDB
 from cerberus import schema_registry
 
+from .database import db
 from .validator import DbValidator
 
 __author__ = """theenglishway"""
@@ -10,14 +9,6 @@ __email__ = 'me@theenglishway.eu'
 __version__ = '0.1.0'
 
 
-class Encoder(json.JSONEncoder):
-    def default(self, o):
-        if isinstance(o, datetime):
-            return o.strftime("%c %z")
-
-        return o.__dict__
-
-db = TinyDB('planning.json', indent=4, cls=Encoder)
 validator = DbValidator(db)
 
 from planner.models import *

+ 7 - 6
planner/cli.py

@@ -1,5 +1,6 @@
 import click
 import json
+import yaml
 from tinydb import Query
 from planner import db
 from planner.models import *
@@ -154,12 +155,12 @@ def edit(ctx, short_uuid):
 
     if result_list:
         result = result_list[0]
-        editable = json.dumps(result_list[0], indent=4)
-        edited_json = click.edit(editable)
-        if edited_json:
-            updated_instance = json.loads(edited_json)
-            db.table(model.class_name()).update(updated_instance, doc_ids=[result.doc_id])
-            click.echo(Term.success(f"Successfully updated {updated_instance}"))
+        item = yaml.dump(result_list[0], allow_unicode=True)
+        edited = click.edit(item)
+        if edited:
+            updated_item = yaml.safe_load(edited)
+            db.table(model.class_name()).update(updated_item, doc_ids=[result.doc_id])
+            click.echo(Term.success(f"Successfully updated {updated_item}"))
 
     else:
         click.echo(Term.failure(f"No matching result found"))

+ 43 - 0
planner/database.py

@@ -0,0 +1,43 @@
+import yaml
+import json
+from datetime import datetime
+from tinydb import TinyDB
+from tinydb.database import Document
+from tinydb.storages import Storage
+
+def represent_doc(dumper, data):
+    # Represent `Document` objects as their dict's string representation
+    # which PyYAML understands
+    return dumper.represent_data(dict(data))
+
+yaml.add_representer(Document, represent_doc)
+
+class YAMLStorage(Storage):
+    def __init__(self, filename):
+        self.filename = filename
+
+    def read(self):
+        with open(self.filename) as handle:
+            try:
+                data = yaml.safe_load(handle.read())
+                return data
+            except yaml.YAMLError:
+                return None
+
+    def write(self, data):
+        with open(self.filename, 'w') as handle:
+            yaml.dump(data, handle, default_flow_style=False, allow_unicode=True)
+
+    def close(self):
+        pass
+
+
+class Encoder(json.JSONEncoder):
+    def default(self, o):
+        if isinstance(o, datetime):
+            return o.strftime("%c %z")
+
+        return o.__dict__
+
+#db = TinyDB('planning.json', indent=4, cls=Encoder)
+db = TinyDB('planning.yaml', storage=YAMLStorage)