Nick Coghlan avatar Nick Coghlan committed 979b96a

pycall: utility script for calling into Python with JSON in, JSON out

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+import json
+import sys
+try:
+    import builtins
+except ImportError:
+    import __builtin__ as builtins
+
+def _getattr_or_import(name):
+    # Inspired by zope.dottedname, we should
+    # probably have this in importlib.util
+    mod_name, sep, rest = name.partition(".")
+    if not mod_name:
+        raise ValueError("Relative imports are not supported")
+    mod = None
+    try:
+        obj = globals()[mod_name]
+    except KeyError:
+        try:
+            obj = getattr(builtins, mod_name)
+        except AttributeError:
+            mod = obj = __import__(mod_name)
+    if rest:
+        for part in rest.split("."):
+            if not part:
+                raise ValueError("The empty string is not a valid name fragment")
+            try:
+                obj = getattr(obj, part)
+            except AttributeError:
+                if obj is not mod:
+                    raise
+                mod_name = mod_name + "." + part
+                __import__(mod_name)
+                mod = obj = getattr(mod, part)
+    return obj
+    
+
+def json_call(name, input_stream=None, output_stream=None):
+    if input_stream is None:
+        input_stream = sys.stdin
+    if output_stream is None:
+        output_stream = sys.stdout
+    f = _getattr_or_import(name)
+    input_data = json.load(input_stream)
+    if isinstance(input_data, dict):
+        output_data = f(**input_data)
+    elif isinstance(input_data, list):
+        output_data = f(*input_data)
+    else:
+        output_data = f(input_data)
+    json.dump(output_data, output_stream)
+    return output_data
+
+if __name__ == "__main__":
+    json_call(sys.argv[1])
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.