[cig-commits] [commit] baagaard/feature-add-json-config: Started work on parsing registry from json files. (bcdb52b)
cig_noreply at geodynamics.org
cig_noreply at geodynamics.org
Mon Sep 23 18:10:54 PDT 2013
Repository : ssh://geoshell/pythia
On branch : baagaard/feature-add-json-config
Link : https://github.com/geodynamics/pythia/compare/0000000000000000000000000000000000000000...7b5240df77e6a48a6b245bb29be8914ce1810f4e
>---------------------------------------------------------------
commit bcdb52bdaa58104eff9dd2d8d13836301e70e1e4
Author: Brad Aagaard <baagaard at usgs.gov>
Date: Mon Sep 23 18:11:48 2013 -0700
Started work on parsing registry from json files.
>---------------------------------------------------------------
bcdb52bdaa58104eff9dd2d8d13836301e70e1e4
pyre/inventory/__init__.py | 5 +++
pyre/inventory/json/CodecJSON.py | 38 +++++++++++++++++
pyre/inventory/json/Parser.py | 89 ++++++++++++++++++++++++++++++++++++++++
pyre/inventory/json/Renderer.py | 48 ++++++++++++++++++++++
pyre/inventory/json/__init__.py | 14 +++++++
pyre/inventory/odb/Curator.py | 3 +-
6 files changed, 196 insertions(+), 1 deletion(-)
diff --git a/pyre/inventory/__init__.py b/pyre/inventory/__init__.py
index 6fda5ad..c54b704 100644
--- a/pyre/inventory/__init__.py
+++ b/pyre/inventory/__init__.py
@@ -45,6 +45,11 @@ def codecConfig():
return CodecConfig()
+def codecJSON():
+ from json.CodecJSON import CodecJSON
+ return CodecJSON()
+
+
def codecConfigSheet():
from pcs.CodecConfigSheet import CodecConfigSheet
return CodecConfigSheet()
diff --git a/pyre/inventory/json/CodecJSON.py b/pyre/inventory/json/CodecJSON.py
new file mode 100644
index 0000000..d5008f2
--- /dev/null
+++ b/pyre/inventory/json/CodecJSON.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+# Brad T. Aagaard, U.S. Geological Survey
+#
+# This code was developed as part of the Computational Infrastructure
+# for Geodynamics (http://geodynamics.org).
+#
+# Copyright (c) 2010-2013 University of California, Davis
+#
+# ----------------------------------------------------------------------
+
+from pyre.odb.fs.CodecODB import CodecODB
+from Parser import Parser
+
+
+class CodecJSON(CodecODB):
+
+ def __init__(self):
+ CodecODB.__init__(self, encoding='json')
+ return
+
+
+ def _createRenderer(self):
+ from Renderer import Renderer
+ return Renderer()
+
+
+ def _decode(self, shelf):
+ parser = Parser(shelf.name)
+ root = parser.read()
+ shelf['inventory'] = root
+ shelf._frozen = True
+ return
+
+
+# end of file
diff --git a/pyre/inventory/json/Parser.py b/pyre/inventory/json/Parser.py
new file mode 100644
index 0000000..7a82ab7
--- /dev/null
+++ b/pyre/inventory/json/Parser.py
@@ -0,0 +1,89 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+# Brad T. Aagaard, U.S. Geological Survey
+#
+# This code was developed as part of the Computational Infrastructure
+# for Geodynamics (http://geodynamics.org).
+#
+# Copyright (c) 2010-2013 University of California, Davis
+#
+# ----------------------------------------------------------------------
+
+from pyre.util import expandMacros
+import pyre.parsing.locators as locators
+
+class Parser(object):
+ """
+ Use json parser to get data and then populate Pyre Registry.
+ """
+
+ def __init__(self, name):
+ """
+ Constructor.
+ """
+ self.name = name
+ return
+
+
+ def read(self):
+ """
+ Parse json and populate registry.
+ """
+ import json
+ jdata = json.load(file(self.name))
+
+ from pyre.inventory.odb.Registry import Registry
+ root = Registry("root")
+
+ # :KLUDGE: Would have to reimplement json scanner to get line
+ # number. Set the filename correctly in the locator, but use a
+ # line number of -1.
+ self.locatorNL = locators.file(self.name, -1)
+
+ self._setNode(root, jdata)
+ return root
+
+
+ def _setNode(self, node, jdata):
+ """
+ Set registry data for node from json data.
+ """
+ if not isinstance(jdata, dict):
+ raise ValueError("Dictionary expected. Object: %s" % jdata)
+
+ for (key, value) in jdata.items():
+ if isinstance(value, dict): # facility or dimensional
+ if value.has_key("value") and value.has_key("units"): # dimensional
+ node.setProperty(key, "%s*%s" % (value['value'], value['units']), self.locatorNL)
+ else: # facility
+ module = value.pop('_module', None)
+ if module is None:
+ raise ValueError("Component missing '_module' key. Object: %s" % value)
+ node.setProperty(key, module, self.locatorNL)
+ facility = node.getNode(key)
+ self._setNode(facility, value)
+
+ elif isinstance(value, list): # facility array
+ facilityArray = node.getNode(key)
+ for item in value:
+ if not isinstance(item, dict):
+ raise ValueError("Expected dictionaries in facility array '%s'. Object: %s" % \
+ (value, item))
+ if not item.has_key('_value') or not item.has_key('_item'):
+ raise ValueError("Expected facility array dictionary to contain '_value' and '_item' keys. Object: %s" % item)
+ module = item['_value'].pop('_module', None)
+ if module is None:
+ raise ValueError("Component '%s' missing '_module' key." % item['_value'])
+ facilityArray.setProperty(item['_item'], module, self.locatorNL)
+ facility = facilityArray.getNode(item['_item'])
+ self._setNode(facility, item['_value'])
+
+ else: # property
+ node.setProperty(key, value, self.locatorNL)
+
+ return
+
+
+# End of file
diff --git a/pyre/inventory/json/Renderer.py b/pyre/inventory/json/Renderer.py
new file mode 100644
index 0000000..feff8a4
--- /dev/null
+++ b/pyre/inventory/json/Renderer.py
@@ -0,0 +1,48 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+# Brad T. Aagaard, U.S. Geological Survey
+#
+# This code was developed as part of the Computational Infrastructure
+# for Geodynamics (http://geodynamics.org).
+#
+# Copyright (c) 2010-2013 University of California, Davis
+#
+# ----------------------------------------------------------------------
+
+
+from pyre.weaver.mills.ConfigMill import ConfigMill
+
+
+class Renderer(ConfigMill):
+
+
+ def render(self, inventory):
+ raise NotImplementedError("Renering json files not implemented.")
+ retun
+
+
+ # handlers
+
+ def onInventory(self, inventory):
+ raise NotImplementedError("Renering json files not implemented.")
+ return
+
+
+ def onRegistry(self, registry):
+ raise NotImplementedError("Renering json files not implemented.")
+ return
+
+
+ def __init__(self):
+ ConfigMill.__init__(self)
+ self.path = []
+ return
+
+
+ def _renderDocument(self, document):
+ return document.identify(self)
+
+
+# end of file
diff --git a/pyre/inventory/json/__init__.py b/pyre/inventory/json/__init__.py
new file mode 100644
index 0000000..cb0f703
--- /dev/null
+++ b/pyre/inventory/json/__init__.py
@@ -0,0 +1,14 @@
+#!/usr/bin/env python
+#
+# ----------------------------------------------------------------------
+#
+# Brad T. Aagaard, U.S. Geological Survey
+#
+# This code was developed as part of the Computational Infrastructure
+# for Geodynamics (http://geodynamics.org).
+#
+# Copyright (c) 2010-2013 University of California, Davis
+#
+# ----------------------------------------------------------------------
+
+# End of file
diff --git a/pyre/inventory/odb/Curator.py b/pyre/inventory/odb/Curator.py
index 9cf80c6..d66e7f2 100644
--- a/pyre/inventory/odb/Curator.py
+++ b/pyre/inventory/odb/Curator.py
@@ -309,6 +309,7 @@ class Curator(Base):
import pyre.inventory
pml = pyre.inventory.codecPML()
cfg = pyre.inventory.codecConfig()
+ json = pyre.inventory.codecJSON()
pcs = pyre.inventory.codecConfigSheet()
import pyre.odb
@@ -317,7 +318,7 @@ class Curator(Base):
import pyre.templates
tmpl = pyre.templates.codecTmpl()
- self.registerCodecs(pml, cfg, pcs, odb, tmpl)
+ self.registerCodecs(pml, cfg, json, pcs, odb, tmpl)
return
More information about the CIG-COMMITS
mailing list