aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--ChangeLog.txt1
-rw-r--r--Makefile.in2
-rwxr-xr-xmakewrappers106
-rw-r--r--templatefile.py99
4 files changed, 105 insertions, 103 deletions
diff --git a/ChangeLog.txt b/ChangeLog.txt
index c26f022..8e737e4 100644
--- a/ChangeLog.txt
+++ b/ChangeLog.txt
@@ -1,5 +1,6 @@
2010-11-17:
* (seebs) add "Futures.txt" notes about future development plans
+ * (seebs) split some of the templating code out of makewrappers
2010-11-16:
* (seebs) database move functionality (first pass)
diff --git a/Makefile.in b/Makefile.in
index c867978..e931faa 100644
--- a/Makefile.in
+++ b/Makefile.in
@@ -115,7 +115,7 @@ pseudo_client.o: pseudo_client.h
pseudo_server.o: pseudo_server.h
-wrappers: wrapfuncs.in $(USE_64) makewrappers $(TEMPLATES)
+wrappers: wrapfuncs.in $(USE_64) makewrappers templatefile.py $(TEMPLATES)
./makewrappers wrapfuncs.in $(USE_64)
.SECONDARY: wrappers
diff --git a/makewrappers b/makewrappers
index 073f35b..5ec58f8 100755
--- a/makewrappers
+++ b/makewrappers
@@ -20,106 +20,8 @@
import glob
import sys
import re
-import os
import datetime
-from string import Template
-
-class SourceFile(object):
- """A template for creating a source file"""
-
- def __init__(self, path):
- # default values...
- # no name or file yet
- self.name = ''
- self.sections = {}
- self.file = None
- self.path = None
- # open a new file for each function
- self.file_per_func = False
-
- # empty footer if none specified:
- self.sections['footer'] = []
-
- # lines appended to body by default
- self.sections['body'] = []
- current = self.sections['body']
-
- self.template = open(path)
- for line in self.template:
- line = line.rstrip()
- if line.startswith('@'):
- if ' ' in line:
- leading, trailing = line.split(' ', 1)
- else:
- leading, trailing = line, None
-
- if leading == '@name':
- if not trailing:
- raise Exception("@name requires a file name.")
- self.path = trailing
- if '$' in self.path:
- self.file_per_func = True
- else:
- section = leading[1:]
- if section not in self.sections:
- self.sections[section] = []
- current = self.sections[section]
- else:
- current.append(line)
- self.template.close()
- for section, data in self.sections.items():
- self.sections[section] = Template("\n".join(data))
-
- # You need a file if this isn't a file-per-func
- if not self.file_per_func:
- self.file = open(self.path, 'w')
-
- def close(self):
- """Close the associated file."""
- if self.file:
- self.file.close()
- self.file = None
-
- def __repr__(self):
- strings = []
- if self.file_per_func:
- strings.append("path: %s (per func)" % self.path)
- else:
- strings.append("path: %s" % self.path)
- for name, data in self.sections.items():
- strings.append("%s:" % name)
- strings.append(data.safe_substitute({}))
- return "\n".join(strings)
-
- def emit(self, template, func=None):
- """Emit a template, with optional interpolation of a function."""
- if self.file_per_func:
- if not func:
- return
- path = Template(self.path).safe_substitute(func)
- if os.path.exists(path):
- # print "We don't overwrite existing files."
- return
- self.file = open(path, 'w')
- if not self.file:
- print "Couldn't open '%s' (expanded from %s), " \
- "not emitting '%s'." % \
- (path, self.path, template)
- return
-
- if template == "copyright":
- # hey, at least it's not a global variable, amirite?
- self.file.write(SourceFile.copyright)
- elif template in self.sections:
- templ = self.sections[template]
- self.file.write(templ.safe_substitute(func))
- self.file.write("\n")
- else:
- print "Warning: Unknown template '%s'." % template
-
- if self.file_per_func:
- self.file.close()
- self.file = None
+from templatefile import TemplateFile
class ArgumentList:
"""A (possibly empty) list of arguments"""
@@ -460,18 +362,18 @@ class Function:
return pretty
def main():
- """Read in function defintions, write out files based on templates."""
+ """Read in function definitions, write out files based on templates."""
funcs = []
sources = []
# error checking helpfully provided by the exception handler
copyright_file = open('guts/COPYRIGHT')
- SourceFile.copyright = copyright_file.read()
+ TemplateFile.copyright = copyright_file.read()
copyright_file.close()
for path in glob.glob('templates/*'):
try:
- source = SourceFile(path)
+ source = TemplateFile(path)
source.emit('copyright')
source.emit('header')
sources.append(source)
diff --git a/templatefile.py b/templatefile.py
new file mode 100644
index 0000000..e08bc64
--- /dev/null
+++ b/templatefile.py
@@ -0,0 +1,99 @@
+from string import Template
+import os
+
+class TemplateFile:
+ """A template for creating a source file"""
+
+ def __init__(self, path):
+ # default values...
+ # no name or file yet
+ self.name = ''
+ self.sections = {}
+ self.file = None
+ self.path = None
+ # open a new file for each item
+ self.file_per_item = False
+
+ # empty footer if none specified:
+ self.sections['footer'] = []
+
+ # lines appended to body by default
+ self.sections['body'] = []
+ current = self.sections['body']
+
+ self.template = open(path)
+ for line in self.template:
+ line = line.rstrip()
+ if line.startswith('@'):
+ if ' ' in line:
+ leading, trailing = line.split(' ', 1)
+ else:
+ leading, trailing = line, None
+
+ if leading == '@name':
+ if not trailing:
+ raise Exception("@name requires a file name.")
+ self.path = trailing
+ if '$' in self.path:
+ self.file_per_item = True
+ else:
+ section = leading[1:]
+ if section not in self.sections:
+ self.sections[section] = []
+ current = self.sections[section]
+ else:
+ current.append(line)
+ self.template.close()
+ for section, data in self.sections.items():
+ self.sections[section] = Template("\n".join(data))
+
+ # You need a file if this isn't a file-per-item
+ if not self.file_per_item:
+ self.file = open(self.path, 'w')
+
+ def close(self):
+ """Close the associated file."""
+ if self.file:
+ self.file.close()
+ self.file = None
+
+ def __repr__(self):
+ strings = []
+ if self.file_per_item:
+ strings.append("path: %s (per item)" % self.path)
+ else:
+ strings.append("path: %s" % self.path)
+ for name, data in self.sections.items():
+ strings.append("%s:" % name)
+ strings.append(data.safe_substitute({}))
+ return "\n".join(strings)
+
+ def emit(self, template, item=None):
+ """Emit a template, with optional interpolation of an item."""
+ if self.file_per_item:
+ if not item:
+ return
+ path = Template(self.path).safe_substitute(item)
+ if os.path.exists(path):
+ # print "We don't overwrite existing files."
+ return
+ self.file = open(path, 'w')
+ if not self.file:
+ print "Couldn't open '%s' (expanded from %s), " \
+ "not emitting '%s'." % \
+ (path, self.path, template)
+ return
+
+ if template == "copyright":
+ # hey, at least it's not a global variable, amirite?
+ self.file.write(TemplateFile.copyright)
+ elif template in self.sections:
+ templ = self.sections[template]
+ self.file.write(templ.safe_substitute(item))
+ self.file.write("\n")
+ else:
+ print "Warning: Unknown template '%s'." % template
+
+ if self.file_per_item:
+ self.file.close()
+ self.file = None