[Mumps2Py:] [75] added support for a Mumps routines-collection in text format, and m2p files.

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 75
Author:   pgallot
Date:     2008-01-31 21:40:56 +0000 (Thu, 31 Jan 2008)

Log Message:
-----------
added support for a Mumps routines-collection in text format, and m2p files.

Modified Paths:
--------------
    trunk/mumps2py/mumps_module.py


Modified: trunk/mumps2py/mumps_module.py
===================================================================
--- trunk/mumps2py/mumps_module.py	2008-01-31 21:39:41 UTC (rev 74)
+++ trunk/mumps2py/mumps_module.py	2008-01-31 21:40:56 UTC (rev 75)
@@ -16,6 +16,7 @@
 ##    along with Mumps2Py.  If not, see <http://www.gnu.org/licenses/>.
 """ code relating to a Mumps module, primarily via the Module_info class."""
 import re, fileinput, os.path
+import mumps2py_config
 
 class ModuleInfo:
     """ A ModuleInfo object represents one complete unit of Mumps source code."""
@@ -40,7 +41,8 @@
 
     def empty_tokenlist(self):
         """ clear out the Token List."""
-        self.TokenList = []
+        while len(self.TokenList):
+            self.TokenList.pop()
         
     def add_token(self, newtoken):
         """ add a new token to the token list."""
@@ -62,8 +64,39 @@
     def last_token(self):
         """returns the last Token added to this module's TokenList"""
         return self.TokenList[-1]
-        
-def parseForModules(inputfile, outputdir):
+
+class RoutineFilter:
+    """creates an object that will filter which routines to process,
+based on the criteria provided"""
+    def __init__(self):
+        """initializes the object with no in or out filters"""
+        self.filter_in_patterns = []
+        self.filter_out_patterns = []
+
+    def accept(self, routine_name):
+        """returns True if the routine is to be processed"""
+        filter_in = False
+        filter_out = False
+
+        for in_filter in self.filter_in_patterns:
+            filter_in = filter_in or in_filter.match(routine_name)
+
+        for out_filter in self.filter_out_patterns:
+            filter_out = filter_out or out_filter.match(routine_name)
+
+        return filter_in or not filter_out
+
+    def add_out_filter(self, out_re_expr):
+        """ adds a filter for routines to exclude """
+        self.filter_out_patterns.append ( re.compile(out_re_expr))
+
+    def add_in_filter(self, in_re_expr):
+        """ adds a filter for routines to include that might otherwise be
+excluded"""
+        self.filter_in_patterns.append ( re.compile(in_re_expr))
+
+    
+def parse_cache_routines(inputfile, outputdir, routine_filter = None):
     """parseForModules returns a list of ModuleInfo objects from a file
 in Cache Format, assuming that each Mumps module begins
 with a line containing modulename^INT^"""
@@ -82,8 +115,10 @@
         if modpattern.search(line):
             tmp = modsplit.split(line, 1)
             if oldmodname:
-                modules.append(ModuleInfo(inputfile, outputdir, oldmodname, \
-                                          lastmodline + 1, lineno - 1))
+                if not routine_filter or routine_filter.accept(oldmodname):
+                    modules.append( \
+                        ModuleInfo(inputfile, outputdir, \
+                                   oldmodname, lastmodline + 1, lineno - 1))
                 if __debug__:
                     print oldmodname, lastmodline + 1, lineno - 1, "\t", \
                           lineno - lastmodline
@@ -92,3 +127,85 @@
 
     fileinput.close()
     return modules                  
+
+def parse_astextroutines(inputfile, outputdir, routine_filter = None):
+    """returns a list of ModuleInfo object from a file which has
+each routine separated by a blank line and the routine name given
+on its own line before the routine body."""
+
+    modules = []
+    lastmodline = 2
+    oldmodname = None
+    modpattern = re.compile(r"(?P<name>[%A-Za-z0-9][A-Za-z0-9]*)\s*$")
+    print "parse_astextroutines"
+    emptypattern = re.compile(r"\s*$")
+    endpattern = re.compile("#########")
+    last_line = " "
+    
+    for line in fileinput.input(inputfile):
+        lineno = fileinput.lineno()
+        if lineno < 3:
+            continue
+
+        if endpattern.match(line):
+            break
+        
+        modmatch = modpattern.match(line)
+        if modmatch and emptypattern.match(last_line):
+            tmp = modmatch.group("name")
+            if oldmodname:
+                if not routine_filter or routine_filter.accept(oldmodname):
+                    modules.append( \
+                        ModuleInfo(inputfile, outputdir, \
+                                   oldmodname, lastmodline + 1, lineno - 1))
+                if __debug__:
+                    print oldmodname, lastmodline + 1, lineno - 1, "\t", \
+                          lineno - lastmodline
+            oldmodname = tmp
+            lastmodline = lineno
+        
+        last_line = line
+
+    fileinput.close()
+    return modules
+
+def parse_from_m2p_cfg(mp2_fname):
+    """reads an m2p configuration file which specifies a file to parse,
+but also which routines to skip and not skip, using Python R.E.s"""
+    info = mumps2py_config.get_m2p_info(mp2_fname)
+    inputfile = info['inputfile']
+    outputdir = info['outputdir']
+
+    routine_filter = RoutineFilter()
+    for filter_re in info['filter_out']:
+        routine_filter.add_out_filter(filter_re)
+    for filter_re in info['filter_in']:
+        routine_filter.add_in_filter(filter_re)
+
+    mfile = open(inputfile)
+    headerline = mfile.readline()
+    mfile.close()
+
+    if re.match(r"VistA FOIA Routines", headerline):
+        return parse_astextroutines(inputfile, outputdir, routine_filter)
+    elif re.search(r"CACHE FORMAT\^~Format=Cache.S~", headerline):
+        return parse_cache_routines(inputfile, outputdir, routine_filter)
+    else:
+        return None
+            
+def parse_for_routines(inputfile, outputdir):
+    """returns a list of ModuleInfo objects"""
+    mfile = open(inputfile)
+    headerline = mfile.readline()
+    mfile.close()
+
+    if re.search(r"CACHE FORMAT\^~Format=Cache.S~", headerline):
+        return parse_cache_routines(inputfile, outputdir)
+    elif re.match(r"VistA FOIA Routines", headerline):
+        return parse_astextroutines(inputfile, outputdir)
+    else:
+        outputname, extension = os.path.splitext(inputfile)
+        if extension and extension == ".m2p":
+            return parse_from_m2p_cfg(inputfile)
+        else:
+            return [ModuleInfo(inputfile, outputdir, outputname), ]


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/