summaryrefslogtreecommitdiffstats
path: root/py-bin/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'py-bin/utils.py')
-rw-r--r--py-bin/utils.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/py-bin/utils.py b/py-bin/utils.py
new file mode 100644
index 0000000..b13949e
--- /dev/null
+++ b/py-bin/utils.py
@@ -0,0 +1,71 @@
+#helper functions/classes
+
+import string, logging, re, os, urllib
+import lib.jon.cgi as cgi, lib.jon.session as session
+import lib.em as em
+import config
+
+def xmap(function, data):
+ if isinstance(data, str):
+ return function(data)
+ if isinstance(data, int) or isinstance(data, long) or isinstance(data, float):
+ return function(str(data))
+ if isinstance(data, list):
+ return map(lambda x: xmap(function, x), data)
+ if isinstance(data, tuple):
+ return tuple(xmap(function, list(data)))
+
+def html_encode_struct(data):
+ return xmap(cgi.html_encode, data)
+
+def __empy_render_step(interpreter, filename, context_raw):
+ path = os.path.join(config.template_dir, filename)
+ out = interpreter.expand(file(path).read(), context_raw)
+ if "em_inherit" in context_raw:
+ filename = context_raw["em_inherit"]
+ del(context_raw["em_inherit"])
+ context_raw["em_child_content"] = out
+ out = __empy_render_step(interpreter, filename, context_raw)
+ return out
+
+def empy_render(filename, context={}, context_raw={}):
+ for k in context.keys():
+ context[k] = html_encode_struct(context[k])
+ context_raw.update(context)
+ interpreter = em.Interpreter()
+ out = __empy_render_step(interpreter, filename, context_raw)
+ interpreter.shutdown()
+ return out
+
+if config.debugmode:
+ HandlerBaseClass = cgi.DebugHandler
+else:
+ HandlerBaseClass = cgi.Handler
+class BasicHandler(HandlerBaseClass):
+ def process(self, req):
+ self.session = session.FileSession(req, config.the_secret, config.session_dir)
+ req.set_header("Content-Type", "text/html; charset=utf-8")
+ self.do_process(req)
+ self.session.save()
+
+ def render_template(self, req, filename, context={}, context_raw={}):
+ req.write(empy_render(filename, context, context_raw))
+
+ def error_page(self, req, message):
+ self.render_template(req, "error.em", dict(message=message))
+
+ def make_url(self, params):
+ return config.script_url + "?" + urllib.urlencode(params)
+
+ def redirect_to(self, req, url):
+ req.set_header("Status", "302 Temporarily moved")
+ req.set_header("Location", url)
+
+def process_request(handler):
+ cgi.CGIRequest(handler).process()
+
+def set_logging_defaults():
+ logging.basicConfig(level=logging.DEBUG,
+ format='%(asctime)s %(levelname)-8s %(message)s',
+ datefmt='%d.%m.%Y %H:%M:%S',
+ filename=config.logfile_path) \ No newline at end of file