[Synopsis-changes] Synopsis/Synopsis/Synopsis/Core Processor.py,1.1,1.2

Stefan Seefeld stefan at synopsis.fresco.org
Wed Nov 5 17:17:32 UTC 2003


Update of /cvs/synopsis/Synopsis/Synopsis/Core
In directory frida:/tmp/cvs-serv32101/Synopsis/Core

Modified Files:
	Processor.py 
Log Message:
refined the 'process()' handling of arguments; more convenience Processors

Index: Processor.py
===================================================================
RCS file: /cvs/synopsis/Synopsis/Synopsis/Core/Processor.py,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -p -d -r1.1 -r1.2
--- Processor.py	30 Oct 2003 05:54:36 -0000	1.1
+++ Processor.py	5 Nov 2003 17:17:29 -0000	1.2
@@ -2,11 +2,12 @@
 #
 # Copyright (C) 2003 Stefan Seefeld
 # All rights reserved.
-# Licensed to the public under the terms of the GNU GPL (>= 2),
+# Licensed to the public under the terms of the GNU LGPL (>= 2),
 # see the file COPYING for details.
 #
 
 import sys
+import AST
 
 class Processor(object):
    """Processor overrides the __new__ operator
@@ -70,9 +71,68 @@ class Processor(object):
       """Use named values to override default attributes."""
       self.__dict__.update(kwds);
 
-   def process(self, **kwds):
-      """provide the processor specific code here"""
-      self.__dict__.update(kwds);
+   def process(self, ast, **kwds):
+      """The process method provides the interface to be implemented by subclasses.
+      
+      Commonly used arguments are 'input' and 'output'. If 'input' is defined,
+      it is interpreted as one or more input file names. Of 'output' is defined, it
+      is interpreted as an output file (or directory) name."""
+      return ast
+
+class Composite(Processor):
+   """A Composite processor."""
+
+   processors=[]
+
+   def __init__(self, *processors, **kwds):
+      """This __init__ is a convenience constructor that takes a var list
+      to list the desired processors. If the named values contain 'processors',
+      they override the var list."""
+      self.processors = processors
+      self.__dict__.update(kwds)
+
+   def process(self, ast, **kwds):
+      """apply a list of processors. The 'input' value is passed to the first
+      processor only, the 'output' to the last."""
+
+      if not self.processors:
+         return ast
+
+      elif len(self.processors) == 1:
+         return self.processors(ast, **kwds)
+      
+      # first_kwds is a copy of kwds, but without 'output' defined
+      first_kwds = kwds.copy()
+      if first_kwds.has_key('output'):
+         del first_kwds['output']
+      # last_kwds is a copy of kwds, but without 'input' defined
+      last_kwds = kwds.copy()
+      if last_kwds.has_key('input'):
+         del last_kwds['input']
+      # remove 'input' and 'output' from kwds for all other processors
+      # in the pipeline
+      if kwds.has_key('input'):
+         del kwds['input']
+      if kwds.has_key('output'):
+         del kwds['output']
+
+      ast = self.processors[0].process(ast, **first_kwds)
+
+      if len(self.processors) > 2:
+         for p in self.processors[1:-1]:
+            ast = p.process(ast, **kwds)
+
+      return self.processors[-1].process(ast, **last_kwds)
+
+class Store(Processor):
+
+   output=''
+
+   def process(self, ast, **kwds):
+      """Simply store the current ast in the 'output' file."""
+      self.__dict__.update(kwds)
+      AST.save(self.output, ast)
+      return ast
 
 def error(msg):
    """Write an error message and exit."""
@@ -94,19 +154,30 @@ def process(**kwds):
    command = sys.argv[1]
    props = {}
    args = sys.argv[2:]
-   while args and args[0].find('=') != -1:
-      l = args[0].split('=')
-      if len(l) < 2:
-         raise Error, 'argument "%s" not propname=value'%args[0]
-      key, value = l[0], '='.join(l[1:])
-      if value:
-         try:
-            props[key] = eval(value)
-         except:
-            error("""an error occured trying to evaluate \'%s\'
-to pass this as a string, please use %s="'%s'" """%(value, key, value))
+   # process all option arguments (i.e. those containing a '='
+   while args:
+      arg = args[0]
+      if arg.find('=') == -1 and not arg.startswith('--'):
+         break
+      attribute = arg.split('=', 1)
+      if len(attribute) == 2:
+         name, value = attribute
+         if name.startswith('--'):
+            props[name[2:]] = value # it's a string
+         else:
+            try:
+               props[name] = eval(value) # it's a python expression
+            except:
+               error("""an error occured trying to evaluate the value of \'%s\' (\'%s\')
+to pass this as a string, please use %s="'%s'" """%(name, value, name, value))
       else:
-         props[key] = None
+         name = attribute[0]
+         if name.startswith('--'):
+            props[name[2:]] = True # flag the attribute as 'set'
+         else:
+            # the nearest thing to 'no python expression'
+            # is None...
+            props[name] = None
       args = args[1:]
 
    # remaining arguments are mapped to the 'input' value,
@@ -115,15 +186,10 @@ to pass this as a string, please use %s=
       props['input'] = args
 
    if command in kwds:
+      ast = AST.AST()
       try:
-         kwds[command].process(**props)
+         kwds[command].process(ast, **props)
       except KeyError, e:
          error('missing argument "%s"'%e)
    else:
       error('no command "%s"'%command)
-
-#
-# $Log$
-# Revision 1.1  2003/10/30 05:54:36  stefan
-# add Processor base class
-#





More information about the Synopsis-changes mailing list