diff options
Diffstat (limited to 'makewrappers')
-rwxr-xr-x | makewrappers | 106 |
1 files changed, 4 insertions, 102 deletions
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) |