[cig-commits] r4020 - in cs/pythia/trunk/pyre: applications inventory inventory/odb odb/fs

leif at geodynamics.org leif at geodynamics.org
Fri Jul 14 16:55:40 PDT 2006


Author: leif
Date: 2006-07-14 16:55:40 -0700 (Fri, 14 Jul 2006)
New Revision: 4020

Modified:
   cs/pythia/trunk/pyre/applications/Application.py
   cs/pythia/trunk/pyre/inventory/Configurable.py
   cs/pythia/trunk/pyre/inventory/Facility.py
   cs/pythia/trunk/pyre/inventory/Inventory.py
   cs/pythia/trunk/pyre/inventory/odb/Curator.py
   cs/pythia/trunk/pyre/odb/fs/Curator.py
Log:
Added code to collect all of an application's defaults
into a Registry.  This could be used to dump an input
file template, for example.

The code finds all possible components using brute
force:  for each facility, it finds every .odb file with
a matching factory method, and calls it (!).  It would
be more graceful to use metadata, but design choices in
Pyre as well as limitations in Python interplay to make
such an approach difficult, if not impossible.


Modified: cs/pythia/trunk/pyre/applications/Application.py
===================================================================
--- cs/pythia/trunk/pyre/applications/Application.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/applications/Application.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -145,7 +145,16 @@
         return []
 
 
+    def dumpDefaults(self):
+        configuration = self.collectDefaults()
+        # save the configuation as a PML file
+        configPml = self.name + "-defaults.pml"
+        pml = open(configPml, 'w')
+        print >> pml, "\n".join(self.weaver.render(configuration))
+        pml.close()
 
+
+
 # version
 __id__ = "$Id: Application.py,v 1.6 2005/04/05 21:34:12 aivazis Exp $"
 

Modified: cs/pythia/trunk/pyre/inventory/Configurable.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Configurable.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/inventory/Configurable.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -111,6 +111,13 @@
         return up, uc
 
 
+    def collectDefaults(self, registry=None):
+        """return a registry containing my default values"""
+        if registry is None:
+            registry = self.createRegistry()
+        return self.inventory.collectDefaults(registry)
+
+
     # curator accessors
     def getCurator(self):
         """return my persistent store manager"""

Modified: cs/pythia/trunk/pyre/inventory/Facility.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Facility.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/inventory/Facility.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -114,6 +114,10 @@
         return component, locator
 
 
+    def _retrieveAllComponents(self, instance):
+        return instance.retrieveAllComponents(factory=self.family)
+
+
     def _import(self, name):
         try:
             module = __import__(name, {}, {})

Modified: cs/pythia/trunk/pyre/inventory/Inventory.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Inventory.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/inventory/Inventory.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -131,6 +131,30 @@
         return registry
 
 
+    def collectDefaults(self, registry):
+        """place my default values in the given registry"""
+
+        from Facility import Facility
+
+        node = registry.getNode(self._priv_name)
+
+        for prop in self._traitRegistry.itervalues():
+            name = prop.name
+            value, locator = prop._getDefaultValue(self)
+            if isinstance(prop, Facility):
+                # This isn't necessarily true.
+                value = value.name
+            node.setProperty(name, value, locator)
+
+        for facility in self._facilityRegistry.itervalues():
+            components = facility._retrieveAllComponents(self)
+            for component in components:
+                component.setCurator(self._priv_curator)
+                component.collectDefaults(node)
+
+        return registry
+
+
     def configureComponent(self, component, registry=None):
         """configure <component> using options from the given registry"""
 
@@ -170,6 +194,19 @@
             vault=vault, extraDepositories=self._priv_depositories)
         
 
+    def retrieveAllComponents(
+        self, factory, args=(), encoding='odb', vault=[], extraDepositories=[]):
+        """retrieve all possible components for <factory> from the persistent store"""
+
+        if extraDepositories:
+            import journal
+            journal.firewall("inventory").log("non-null extraDepositories")
+
+        return self._priv_curator.retrieveAllComponents(
+            facility=factory, args=args, encoding=encoding,
+            vault=vault, extraDepositories=self._priv_depositories)
+        
+
     def init(self):
         """initialize subcomponents"""
 

Modified: cs/pythia/trunk/pyre/inventory/odb/Curator.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/odb/Curator.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/inventory/odb/Curator.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -94,6 +94,44 @@
         return None
 
 
+    def retrieveAllComponents(
+        self, facility, args=(), encoding='odb', vault=[], extraDepositories=[]):
+        """construct all possible components by locating and invoking the component factories"""
+
+        # get the requested codec
+        codec = self.codecs[encoding]
+
+        components = []
+
+        # loop over my depositories looking for apprpriate factories
+        for factory, locator in self.loadSymbols(
+            codec=codec, address=vault, symbol=facility, extras=extraDepositories,
+            errorHandler=self._recordComponentLookup):
+
+            if not callable(factory):
+                self._recordComponentLookup(
+                    facility, locator, "factory '%s' found but not callable" % facility)
+                continue
+
+            try:
+                component = factory(*args)
+            except TypeError, message:
+                self._recordComponentLookup(
+                    facility, locator, "error invoking '%s': %s" % (facility, message))
+                continue
+
+            # set the locator
+            if component:
+                component.setLocator(locator)
+
+            # record this request
+            self._recordComponentLookup(facility, locator, "success")
+
+            components.append(component)
+                
+        return components
+
+
     def config(self, registry):
         # gain access to the installation defaults
         import prefix

Modified: cs/pythia/trunk/pyre/odb/fs/Curator.py
===================================================================
--- cs/pythia/trunk/pyre/odb/fs/Curator.py	2006-07-14 17:47:32 UTC (rev 4019)
+++ cs/pythia/trunk/pyre/odb/fs/Curator.py	2006-07-14 23:55:40 UTC (rev 4020)
@@ -87,6 +87,48 @@
         return
 
 
+    def loadSymbols(self, codec, address, symbol, extras=[], errorHandler=None):
+        """extract all symbols named <symbol> from the vault pointed to by <address>"""
+
+        import pyre.parsing.locators
+        from os.path import basename
+
+        # loop over the depositories
+        for depository in self.searchOrder(extraDepositories=extras):
+            
+            files = depository.retrieveShelves(address, codec.extension)
+            
+            # loop over the shelves
+            for file in files:
+
+                spec = depository.resolve(address + [file])
+                filename = codec.resolve(spec)
+                locator = pyre.parsing.locators.file(filename)
+
+                # open the shelf
+                try:
+                    shelf = codec.open(spec, 'r')
+                except IOError, error:
+                    # the codec failed to open the spec
+                    if callable(errorHandler):
+                        errorHandler(symbol, locator, error)
+                    continue
+                
+                # retrieve the factory method
+                try:
+                    item = shelf[symbol]
+                except KeyError:
+                    # no factory by that name exists
+                    if callable(errorHandler):
+                        errorHandler(symbol, locator, "'%s' not found" % symbol)
+                    continue
+
+                # success
+                yield item, locator
+            
+        return
+
+
     # depository management
     def createDepository(self, directory):
         """create a new depository rooted at <directory>"""



More information about the cig-commits mailing list