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

leif at geodynamics.org leif at geodynamics.org
Wed Mar 7 14:00:40 PST 2007


Author: leif
Date: 2007-03-07 14:00:39 -0800 (Wed, 07 Mar 2007)
New Revision: 6184

Modified:
   cs/pythia/trunk/cig/cs/petsc.py
   cs/pythia/trunk/pyre/applications/CommandlineParser.py
   cs/pythia/trunk/pyre/applications/Executive.py
   cs/pythia/trunk/pyre/applications/Shell.py
   cs/pythia/trunk/pyre/components/Component.py
   cs/pythia/trunk/pyre/inventory/Inventory.py
Log:
Added PetscApplication, which calls Petsc{Initialize,Finalize} on
behalf of the application (yeah, big deal); but also -- in conjunction
with the new PetscProperty -- allows PyLith v0.8 to declare its
"interesting" PETSc options in a very terse fashion.

Made sure that PetscCommandlineParser (used by PyLith v0.8) works with
Bash tab-completion (r6121).  Allow the app to override the default
CommandlineParser by simply declaring a class attribute.

Fixed a bug where "--help" wouldn't work for the top-level component
(I'm almost certain that I introduced this bug in r5988).  By default,
"app -h"/"app -?" now produces the same behavior as "--help" (instead
of just printing the message "please consider writing a help screen
for this application").  The framework once again calls usage() when
bogus options are specified (I unintentionally orphaned this method).
Finally, I restructured the "-h/--help" machinery a little... I think
I could make it still cleaner if I formalized the notion of a
"Command" or an "Action"...


Modified: cs/pythia/trunk/cig/cs/petsc.py
===================================================================
--- cs/pythia/trunk/cig/cs/petsc.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/cig/cs/petsc.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -43,6 +43,8 @@
 
 
 from pyre.applications.CommandlineParser import CommandlineParser
+from pyre.inventory.properties.String import String
+from mpi.Application import Application as MPIApplication
 
 
 class PetscCommandlineParser(CommandlineParser):
@@ -68,6 +70,12 @@
             if iname is None:
                 continue
             
+            iname = self._filterAction(iname)
+            if iname is None:
+                continue
+
+            iname = self._mapAlias(iname)
+
             if iname.lower() == "options_file":
                 # NYI
                 if self.argv:
@@ -172,4 +180,54 @@
 
 
 
+class PetscProperty(String):
+
+    def __init__(self, name=None, default="", meta=None, validator=None, petscOptionName=None):
+        String.__init__(self, name, default, meta, validator)
+        self._petscOptionName = petscOptionName
+        return
+
+    def _getPetscOptionName(self): return self._petscOptionName or self.name
+    petscOptionName = property(_getPetscOptionName)
+
+
+class PetscApplication(MPIApplication):
+
+
+    class Inventory(MPIApplication.Inventory):
+
+        # a dummy facility for passing arbitrary options to PETSc
+        petsc = PetscFacility("petsc")
+
+
+    def _configure(self):
+
+        super(PetscApplication, self)._configure()
+        
+        import sys
+        
+        self.petscArgs = [sys.executable]
+
+        for prop in self.properties():
+            if isinstance(prop, PetscProperty):
+                descriptor = self.inventory.getTraitDescriptor(prop.name)
+                value = descriptor.value
+                if value:
+                    self.petscArgs.append("-" + prop.petscOptionName)
+                    if value != "true":
+                        self.petscArgs.append(value)
+
+        self.petscArgs.extend(self.inventory.petsc.getArgs())
+
+        return
+
+
+    def onComputeNodes(self, *args, **kwds):
+        petsc = self.petsc
+        petsc.PetscInitialize(self.petscArgs)
+        super(PetscApplication, self).onComputeNodes(*args, **kwds)
+        petsc.PetscFinalize()
+
+
+
 # end of file 

Modified: cs/pythia/trunk/pyre/applications/CommandlineParser.py
===================================================================
--- cs/pythia/trunk/pyre/applications/CommandlineParser.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/pyre/applications/CommandlineParser.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -43,9 +43,11 @@
 
     def __init__(self):
         self.actions = {
-            'help': ['?', 'h'],
             'complete': ['c'],
             }
+        self.aliases = {
+            'help': ['?', 'h'],
+            }
         self.assignment = '='
         self.prefixes = ['--', '-']
         self.separator = '.'
@@ -84,6 +86,8 @@
             if candidate is None:
                 continue
 
+            candidate = self._mapAlias(candidate)
+
             lhs, rhs = self._parseArgument(candidate)
             
             # store this option
@@ -140,6 +144,13 @@
         return candidate
 
 
+    def _mapAlias(self, candidate):
+        for realName, args in self.aliases.iteritems():
+            if candidate in args:
+                return realName
+        return candidate
+
+
     def _processArgument(self, key, value, root):
         separator = self.separator
         fields = key.split(separator)
@@ -147,6 +158,8 @@
 
         children = []
         for level, field in enumerate(fields):
+            if not field:
+                raise CommandlineParser.CommandlineException("bad name: '%s'" % key)
             if field[0] == '[' and field[-1] == ']':
                 candidates = field[1:-1].split(',')
             else:

Modified: cs/pythia/trunk/pyre/applications/Executive.py
===================================================================
--- cs/pythia/trunk/pyre/applications/Executive.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/pyre/applications/Executive.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -16,13 +16,15 @@
 
 
     # factories
+
+    from CommandlineParser import CommandlineParser
+    
     def createCommandlineParser(self):
         """create a command line parser"""
-        
-        import pyre.applications
-        return pyre.applications.commandlineParser()
 
+        return self.CommandlineParser()
 
+
     def createRegistry(self, name=None):
         """create a registry instance to store my configuration"""
 
@@ -88,7 +90,7 @@
 
     # user assistance
     def help(self):
-        print 'Please consider writing a help screen for this application'
+        self.showHelp()
         return
 
 
@@ -160,7 +162,9 @@
 
 
     def usage(self):
-        print 'Please consider writing a usage screen for this application'
+        from os.path import basename
+        print 'usage: %s [--<property>=<value>] [--<facility>.<property>=<value>] [FILE.cfg] ...' % basename(self.arg0)
+        self.showUsage()
         return
 
 

Modified: cs/pythia/trunk/pyre/applications/Shell.py
===================================================================
--- cs/pythia/trunk/pyre/applications/Shell.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/pyre/applications/Shell.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -11,7 +11,7 @@
 #
 
 
-from pyre.components import Component
+from pyre.inventory.Configurable import Configurable
 
 
 try:
@@ -21,7 +21,7 @@
     defaultExceptHook = "current"
 
 
-class Shell(Component):
+class Shell(Configurable):
 
 
     import pyre.hooks
@@ -34,7 +34,7 @@
 
 
     def __init__(self, app):
-        Component.__init__(self, app.name)
+        Configurable.__init__(self, app.name)
 
         self.app = app
         self.registry = None
@@ -100,6 +100,7 @@
 
         # verify that the application input did not contain any typos
         if not app.verifyConfiguration(context, app.inventory.typos):
+            app.usage()
             import sys
             sys.exit("%s: configuration error(s)" % app.name)
 
@@ -112,8 +113,8 @@
         action = action and getattr(app, action)
         if action:
             action()
-        elif app._showHelpOnly:
-            pass
+        elif app._helpRequested:
+            app.help()
         else:
             message = kwds.get('message', 'execute')
             method = getattr(app, message)

Modified: cs/pythia/trunk/pyre/components/Component.py
===================================================================
--- cs/pythia/trunk/pyre/components/Component.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/pyre/components/Component.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -51,36 +51,45 @@
         Configurable.__init__(self, name)
         #self.facility = facility # not used
 
-        self._showHelpOnly = False
-        
+        self._helpRequested = False
+
         return
 
 
-    def _init(self):
-        Configurable._init(self)
+    def _configure(self):
+        Configurable._configure(self)
+        if (self.inventory.usage or
+            self.inventory.showProperties or
+            self.inventory.showComponents or
+            self.inventory.showCurator):
+            self._helpRequested = True
+        else:
+            for component in self.components():
+                if component._helpRequested:
+                    self._helpRequested = True
+                    break
+        return
 
+
+    def showHelp(self):
+        self.inventory.showHelp()
+        self._showHelp()
+        return
+
+
+    def _showHelp(self):
         if self.inventory.usage:
             self.showUsage()
-            self._showHelpOnly = True
 
         if self.inventory.showProperties:
             self.showProperties()
-            self._showHelpOnly = True
 
         if self.inventory.showComponents:
             self.showComponents()
-            self._showHelpOnly = True
 
         if self.inventory.showCurator:
             self.showCurator()
-            self._showHelpOnly = True
 
-        if not self._showHelpOnly:
-            for component in self.components():
-                if component._showHelpOnly:
-                    self._showHelpOnly = True
-                    break
-
         return
 
 

Modified: cs/pythia/trunk/pyre/inventory/Inventory.py
===================================================================
--- cs/pythia/trunk/pyre/inventory/Inventory.py	2007-03-06 21:14:48 UTC (rev 6183)
+++ cs/pythia/trunk/pyre/inventory/Inventory.py	2007-03-07 22:00:39 UTC (rev 6184)
@@ -245,6 +245,12 @@
         return
 
 
+    def showHelp(self):
+        for component in self.components():
+            component.showHelp()
+        return
+
+
     # lower level interface
     def getVault(self):
         """return the address of my vault"""



More information about the cig-commits mailing list