|
@@ -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)
|