From c4275e90c554cd64667a9dfcf30cc19cd3fdcc84 Mon Sep 17 00:00:00 2001
From: Philipp Schiffmann <philipp.schiffmann@hs-hannover.de>
Date: Wed, 26 Mar 2014 13:29:38 +0100
Subject: [PATCH] [TASK] Started writing a JSON dumper for the YAML editor.

---
 hshetl/editor.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)
 create mode 100644 hshetl/editor.py

diff --git a/hshetl/editor.py b/hshetl/editor.py
new file mode 100644
index 0000000..ec93a1b
--- /dev/null
+++ b/hshetl/editor.py
@@ -0,0 +1,56 @@
+from __future__ import print_function
+import hshetl
+import yaml
+import json
+
+def find_subclasses(cls):
+    result = []
+    for subcls in cls.__subclasses__():
+        if not subcls.__module__.startswith("hshetl"):
+            continue
+        result.append(subcls)
+        result += find_subclasses(subcls)
+    return result
+
+def json_encode_class(cls):
+    arg_resolver = hshetl.ConfigurationArgumentMatcher()
+    props, required = arg_resolver.get_constructor_arguments(cls)
+    cfg = {"yaml_tag": cls.yaml_tag,
+           "verbose_name": cls.__name__,
+           "properties": {}}
+    for prop in props:
+        cfg["properties"][prop] = {"type": "!!str",
+                                   "form": {"display": "textfield"}}
+        if prop in required:
+            cfg["properties"][prop]["defaults"] = "foo"
+
+def dump_schema():
+    yaml_subclasses = {}
+    for cls in find_subclasses(yaml.YAMLObject):
+        name = cls.__module__ + "." + cls.__name__
+        yaml_subclasses.setdefault(name, cls)
+    docs = {"order": ["connectors", "entities", "jobs"],
+            "children": {"connectors": [],
+                       "entities": ["!entity"],
+                       "jobs": []}}
+    classes = []
+    for cls in sorted(yaml_subclasses):
+        print(yaml_subclasses[cls])
+        continue
+    return
+    if True:
+        if "job" in cls.__name__.lower():
+            docs["children"]["jobs"].append(cls.yaml_tag)
+        elif "connector" in cls.__name__.lower():
+            docs["children"]["connectors"].append(cls.yaml_tag)
+        classes.append(json_encode_class(cls))
+    with open('editor_data.js','w') as f:
+        print("\"use strict\";\n\nvar hshetl_editor_data = " +
+                       json.dumps({"documents": docs, "classes": classes},
+                                  indent=4,
+                                  separators=(',', ': ')),
+                       file=f)
+
+
+if __name__ == '__main__':
+    dump_schema()
-- 
GitLab