[Mumps2Py:] [78] creating an m2p file now creates an idx file for the input file, which is now used instead of parsing through the entire input file every time . |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
- To: discuss@xxxxxxxxxxxxxxxxxx
- Subject: [Mumps2Py:] [78] creating an m2p file now creates an idx file for the input file, which is now used instead of parsing through the entire input file every time .
- From: subversion@xxxxxxxxxxxxx
- Date: Fri, 01 Feb 2008 21:20:02 +0100
Revision: 78
Author: pgallot
Date: 2008-02-01 20:20:02 +0000 (Fri, 01 Feb 2008)
Log Message:
-----------
creating an m2p file now creates an idx file for the input file, which is now used instead of parsing through the entire input file every time.
Modified Paths:
--------------
trunk/mumps2py/mumps2py_config.py
trunk/mumps2py/mumps_module.py
Modified: trunk/mumps2py/mumps2py_config.py
===================================================================
--- trunk/mumps2py/mumps2py_config.py 2008-02-01 20:15:49 UTC (rev 77)
+++ trunk/mumps2py/mumps2py_config.py 2008-02-01 20:20:02 UTC (rev 78)
@@ -17,9 +17,9 @@
"""mumps2py_config contains all code for reading and writing any configuration
settings related to mumps2py's translation of Mumps code to Python."""
-import ConfigParser
+import ConfigParser, os.path
-def create_m2p_file(m2p_fname, input_fname, output_dir = None):
+def create_m2p_file(m2p_fname, input_fname, output_dir = "./out"):
""" creates a new m2p file."""
config = ConfigParser.ConfigParser()
config.set("DEFAULT", "in_filter_id", 0)
@@ -29,14 +29,32 @@
config.add_section("filter_in")
config.add_section("filter_out")
config.set("input", "file", input_fname)
- if output_dir:
- config.set("output", "dir", output_dir)
- else:
- config.set("output", "dir", "./out")
+ config.set("output", "dir", output_dir)
+
+ idx_fname = create_idx_file(m2p_fname, input_fname, output_dir)
+ config.set("input", "index_file", idx_fname)
+
m2p_file = open(m2p_fname, 'w+')
config.write(m2p_file)
m2p_file.close()
+def create_idx_file(m2p_fname, input_fname, output_dir):
+ """creates an index file for a given m2p file"""
+ from mumps_module import parse_for_routines
+
+ idx_fname = os.path.splitext(m2p_fname)[0] + ".idx"
+ idx = ConfigParser.ConfigParser()
+ for num, routine in enumerate(parse_for_routines(input_fname, output_dir)):
+ section = "routine_%06d" % (num + 1)
+ idx.add_section(section)
+ idx.set(section, "name", routine.mod_name)
+ idx.set(section, "start", routine.start)
+ idx.set(section, "end", routine.end)
+ idx_file = open(idx_fname, 'w+')
+ idx.write(idx_file)
+ idx_file.close()
+ return idx_fname
+
def add_m2p_filter_out_rule(m2p_fname, rule):
""" adds a new exclusion filter rule to an m2p filter"""
config = ConfigParser.ConfigParser()
@@ -71,10 +89,12 @@
"""extracts the info from an m2p file into a dictionary"""
config = ConfigParser.ConfigParser()
config.read(m2p_fname)
+ idx_fname = config.get("input", "index_file")
info = {}
info['inputfile'] = config.get("input", "file")
info['outputdir'] = config.get("output", "dir")
+ info['index'] = []
info['filter_out'] = []
info['filter_in'] = []
@@ -86,4 +106,11 @@
for rule in config.options("filter_in"):
info['filter_in'].append(config.get("filter_in", rule))
+ idx = ConfigParser.ConfigParser()
+ idx.read(idx_fname)
+ for routine in idx.sections():
+ info['index'].append((idx.get(routine, "name"),
+ idx.getint(routine, "start"),
+ idx.getint(routine, "end")))
+ info['index'].sort()
return info
Modified: trunk/mumps2py/mumps_module.py
===================================================================
--- trunk/mumps2py/mumps_module.py 2008-02-01 20:15:49 UTC (rev 77)
+++ trunk/mumps2py/mumps_module.py 2008-02-01 20:20:02 UTC (rev 78)
@@ -96,7 +96,7 @@
self.filter_in_patterns.append ( re.compile(in_re_expr))
-def parse_cache_routines(inputfile, outputdir, routine_filter = None):
+def parse_cache_routines(inputfile, outputdir):
"""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^"""
@@ -115,20 +115,19 @@
if modpattern.search(line):
tmp = modsplit.split(line, 1)
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
+ modules.append( \
+ ModuleInfo(inputfile, outputdir,
+ oldmodname, lastmodline + 1, lineno - 1))
+ if __debug__:
+ print oldmodname, lastmodline + 1, lineno - 1, "\t", \
+ lineno - lastmodline
oldmodname = tmp[0]
lastmodline = lineno
fileinput.close()
return modules
-def parse_astextroutines(inputfile, outputdir, routine_filter = None):
+def parse_astextroutines(inputfile, outputdir):
"""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."""
@@ -154,13 +153,11 @@
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
+ 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
@@ -182,16 +179,15 @@
for filter_re in info['filter_in']:
routine_filter.add_in_filter(filter_re)
- mfile = open(inputfile)
- headerline = mfile.readline()
- mfile.close()
+ modules=[]
+ for routine, start, end in info['index']:
+ if routine_filter.accept(routine):
+ modules.append( ModuleInfo(inputfile, outputdir,
+ routine, start, end))
+ if __debug__:
+ print routine, start, end, "\t", end - start +1
- 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
+ return modules
def parse_for_routines(inputfile, outputdir):
"""returns a list of ModuleInfo objects"""