[cig-commits] r6686 - in cs/pythia/trunk/pyre: applications inventory

leif at geodynamics.org leif at geodynamics.org
Wed Apr 25 13:20:56 PDT 2007


Author: leif
Date: 2007-04-25 13:20:55 -0700 (Wed, 25 Apr 2007)
New Revision: 6686

Modified:
   cs/pythia/trunk/pyre/applications/Preprocessor.py
   cs/pythia/trunk/pyre/applications/Shell.py
   cs/pythia/trunk/pyre/inventory/ConfigContext.py
   cs/pythia/trunk/pyre/inventory/Inventory.py
   cs/pythia/trunk/pyre/inventory/Property.py
Log:
Expand macros in defaults:

    foo = pyre.inventory.str("foo", default="${bar}")

This change has the side-effect of making file property defaults more
consistent:

    input = pyre.inventory.inputFile("input", default="default.txt")

This will now fail with a pretty error message during the
configuration phase if "default.txt" does not exist.  Before, it would
die with an exception when (and if) the 'input' inventory item was
accessed; and only print a pretty error message for a missing file if
the user explicitly set the "input" property (overriding the default).

Defaults take a different path through the framework than
user-specified values do.  In most cases, a bogus default value is a
bug in the application, so handling defaults in a consistent fashion
isn't a big deal.  InputFile and OutputFile, however, are different --
consequently, their behavior has always been slightly broken.
Fortunately, no one (other than me) has been using them (although this
will change).


Modified: cs/pythia/trunk/pyre/applications/Preprocessor.py
===================================================================
--- cs/pythia/trunk/pyre/applications/Preprocessor.py	2007-04-25 20:17:19 UTC (rev 6685)
+++ cs/pythia/trunk/pyre/applications/Preprocessor.py	2007-04-25 20:20:55 UTC (rev 6686)
@@ -25,13 +25,21 @@
         return []
 
 
-    def expandMacros(self, registry):
-        macros = self._prepareMacros()
-        self._expandMacros(macros, registry)
+    def expandMacros(self, value):
+        return expandMacros(value, self.rp)
+
+
+    def defineMacro(self, name, value):
+        self.macros[name] = value
         return
 
 
-    def _prepareMacros(self):
+    def updateMacros(self, dct):
+        self.macros.update(dct)
+        return
+
+
+    def _init(self):
         
         class RecursionProtector(object):
             def __init__(self, macros):
@@ -55,21 +63,16 @@
             key = '.'.join(path[1:])
             macros[key] = value
 
-        return RecursionProtector(macros)
+        self.macros = macros
+        self.rp = RecursionProtector(macros)
 
-
-    def _expandMacros(self, macros, registry):
-        for name, descriptor in registry.properties.items():
-            newValue = expandMacros(descriptor.value, macros)
-            descriptor.value = newValue
-        for node in registry.facilities.values():
-            self._expandMacros(macros, node)
         return
 
 
     def __init__(self, name):
         Component.__init__(self, "macros", name)
         self.macros = Registry(self.name)
+        self.rp = None
         return
 
 

Modified: cs/pythia/trunk/pyre/applications/Shell.py
===================================================================
--- cs/pythia/trunk/pyre/applications/Shell.py	2007-04-25 20:17:19 UTC (rev 6685)
+++ cs/pythia/trunk/pyre/applications/Shell.py	2007-04-25 20:20:55 UTC (rev 6686)
@@ -105,16 +105,16 @@
         if self.excepthook:
             sys.excepthook = self.excepthook.excepthook
 
+        # ~~ configure the application ~~~
+        
         # start fresh
         context = app.newConfigContext()
 
-        # ~~ configure the application ~~~
-        
         # update user options from the command line
         app.updateConfiguration(app.registry)
         
-        # expand macros
-        self.pp.expandMacros(app.inventory._priv_registry)
+        # enable macro expansion
+        context.pp = self.pp
 
         # transfer user input to the app's inventory
         app.applyConfiguration(context)

Modified: cs/pythia/trunk/pyre/inventory/ConfigContext.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/ConfigContext.py	2007-04-25 20:17:19 UTC (rev 6685)
+++ cs/pythia/trunk/pyre/inventory/ConfigContext.py	2007-04-25 20:20:55 UTC (rev 6686)
@@ -75,6 +75,13 @@
         return
 
 
+    def setProperty(self, prop, instance, value, locator):
+        if self.pp:
+            value = self.pp.expandMacros(value)
+        prop._set(instance, value, locator)
+        return
+
+
     def verifyConfiguration(self, component, modeName):
         """verify that the user input did not contain any typos"""
 
@@ -155,6 +162,8 @@
 
         self.errors = []
 
+        self.pp = None
+
         return
 
 

Modified: cs/pythia/trunk/pyre/inventory/Inventory.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Inventory.py	2007-04-25 20:17:19 UTC (rev 6685)
+++ cs/pythia/trunk/pyre/inventory/Inventory.py	2007-04-25 20:20:55 UTC (rev 6686)
@@ -45,13 +45,21 @@
     def configureProperties(self, context):
         """configure my properties using user settings in my registry"""
 
+        # Merge defaults, so that they will be subject to macro
+        # expansion.  This also forces the initialization of all
+        # properties, which is significant if there are bogus defaults
+        # (a prime example being a nonexistant pathname for an
+        # InputFile).
+        registry = self.collectPropertyDefaults()
+        registry.update(self._priv_registry)
+
         # loop over the registry property entries and
         # attempt to set the value of the corresponding inventory item
-        for name, descriptor in self._priv_registry.properties.items():
+        for name, descriptor in registry.properties.items():
             prop = self._traitRegistry.get(name, None)
             if prop:
                 try:
-                    prop._set(self, descriptor.value, descriptor.locator)
+                    context.setProperty(prop, self, descriptor.value, descriptor.locator)
                 except SystemExit:
                     raise
                 except Exception, error:
@@ -153,6 +161,35 @@
         return registry
 
 
+    def collectPropertyDefaults(self, registry=None):
+        """place my default values in the given registry"""
+
+        from Facility import Facility
+        from pyre.inventory.odb.Registry import Registry
+        import pyre.parsing.locators
+
+        if registry is None:
+            registry = Registry(self._priv_name)
+
+        locator = pyre.parsing.locators.default()
+
+        for prop in self._traitRegistry.itervalues():
+
+            # We intentionally don't call _getDefaultValue() -- at
+            # this stage, we don't want anything to happen (files to
+            # be opened, components to be instantiated, ...)
+            value = prop.default
+            
+            # The 'isinstance' is a limitation of the framework: e.g.,
+            # files and dimensionals to not stringify cleanly.
+            # Fortunately, we are only interested in string defaults
+            # at present (for macro expansion).
+            if isinstance(value, basestring):
+                registry.setProperty(prop.name, value, locator)
+
+        return registry
+
+
     def configureComponent(self, component, context, registry=None):
         """configure <component> using options from the given registry"""
 

Modified: cs/pythia/trunk/pyre/inventory/Property.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Property.py	2007-04-25 20:17:19 UTC (rev 6685)
+++ cs/pythia/trunk/pyre/inventory/Property.py	2007-04-25 20:20:55 UTC (rev 6686)
@@ -53,7 +53,7 @@
                 value = self.validator(value)
         
         import pyre.parsing.locators
-        locator = pyre.parsing.locators.simple('default')
+        locator = pyre.parsing.locators.default()
 
         return value, locator
 



More information about the cig-commits mailing list