- f = open(path)
- try:
- text = f.read()
- finally:
- f.close()
- code = compile(text, path, "exec")
- mod = types.ModuleType(mangle(path))
- mod.__file__ = path
- exec(code, mod.__dict__)
- entry = cachedmod(mod, sb.st_mtime)
- modcache[path] = entry
- return entry
- finally:
- cachelock.release()
+ def addext(self, ext, handler):
+ self.exts[ext] = self.resolve(handler)
+
+ def handle(self, env, startreq):
+ if not "SCRIPT_FILENAME" in env:
+ return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.")
+ path = env["SCRIPT_FILENAME"]
+ if not os.access(path, os.R_OK):
+ return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.")
+ if "HTTP_X_ASH_PYTHON_HANDLER" in env:
+ handler = self.resolve(env["HTTP_X_ASH_PYTHON_HANDLER"])
+ else:
+ base = os.path.basename(path)
+ p = base.rfind('.')
+ if p < 0:
+ return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.")
+ ext = base[p + 1:]
+ if not ext in self.exts:
+ return wsgiutil.simpleerror(env, startreq, 500, "Internal Error", "The server is erroneously configured.")
+ handler = self.exts[ext]
+ return handler(env, startreq)
+
+def wmain(*argv):
+ """Main function for ashd(7)-compatible WSGI handlers
+
+ Returns the `application' function. If any arguments are given,
+ they are parsed according to the module documentation.
+ """
+ ret = handler()
+ for arg in argv:
+ if arg[0] == '.':
+ p = arg.index('=')
+ ret.addext(arg[1:p], arg[p + 1:])
+ return ret.handle