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

Class OpagCGI

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

This class represents a running instance of a CGI on the OPAG website. It provides methods to give output from a CGI to a user's browser while maintaining the site's look and feel. It does this via template parsing of a standard template, permitting parsing of other templates as well.

Source from the content-addressed store, hash-verified

35 return ''
36
37class OpagCGI:
38 """This class represents a running instance of a CGI on the OPAG website.
39 It provides methods to give output from a CGI to a user's browser while
40 maintaining the site's look and feel. It does this via template parsing of
41 a standard template, permitting parsing of other templates as well."""
42
43 def __init__(self, template=site_template):
44 """OpagCGI(template) -> OpagCGI object
45 The class constructor, taking the path to the template to use, using
46 the site template as default.
47 """
48 self.template = template
49 self.template_file = None
50 if not os.path.exists(self.template):
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 1

recipe-146460.pyFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected