def parse(self, dict, header=TRUE):
"""parse(dict) -> string
This method parses the open file object passed, replacing any keys
found using the replacement dictionary passed."""
if type(dict) != types.DictType:
raise TypeError, "Second argument must be a dictionary"
if not self.template:
raise OpagMissingPrecondition, "template path is not set"
# Open the file if its not already open. If it is, seek to the
# beginning of the file.
if not self.template_file:
self.template_file = open(self.template, "r")
else:
self.template_file.seek(0)
# Instantiate a new bound method to do the replacement.
replacer = Replacer(dict).replace
# Read in the entire template into memory. I guess we'd better keep
# the templates a reasonable size if we're going to keep doing this.
buffer = self.template_file.read()
replaced = ""
if header:
replaced = "Content-Type: text/html\n\n"
replaced = replaced + re.sub("%%(\w+)%%", replacer, buffer)
return replaced
评论列表
文章目录