MCPcopy Create free account
hub / github.com/ActiveState/code / parse

Method parse

recipes/Python/146460_HTML_Templates/recipe-146460.py:53–76  ·  view source on GitHub ↗

parse(dict) -> string This method parses the open file object passed, replacing any keys found using the replacement dictionary passed.

(self, dict, header=TRUE)

Source from the content-addressed store, hash-verified

51 raise OpagMissingPrecondition, "%s does not exist" % self.template
52
53 def parse(self, dict, header=TRUE):
54 """parse(dict) -> string
55 This method parses the open file object passed, replacing any keys
56 found using the replacement dictionary passed."""
57 if type(dict) != types.DictType:
58 raise TypeError, "Second argument must be a dictionary"
59 if not self.template:
60 raise OpagMissingPrecondition, "template path is not set"
61 # Open the file if its not already open. If it is, seek to the
62 # beginning of the file.
63 if not self.template_file:
64 self.template_file = open(self.template, "r")
65 else:
66 self.template_file.seek(0)
67 # Instantiate a new bound method to do the replacement.
68 replacer = Replacer(dict).replace
69 # Read in the entire template into memory. I guess we'd better keep
70 # the templates a reasonable size if we're going to keep doing this.
71 buffer = self.template_file.read()
72 replaced = ""
73 if header:
74 replaced = "Content-Type: text/html\n\n"
75 replaced = replaced + re.sub("%%(\w+)%%", replacer, buffer)
76 return replaced
77
78class OpagRuntimeError(RuntimeError):
79 """The purpose of this class is to act as the base class for all runtime

Callers 4

_getMethod · 0.45
recipe-146460.pyFile · 0.45
runFunction · 0.45
filter_rdfFunction · 0.45

Calls 5

ReplacerClass · 0.85
openFunction · 0.50
seekMethod · 0.45
readMethod · 0.45
subMethod · 0.45

Tested by

no test coverage detected