I want to follow this structure for a WEB "WSGI pep 3333" API (educational purposes):
/home/
`--app.py
`--API_module/
`--__init__.py
`--api.py
`--exceptions.py
`--modules/
`--default/
`--__init__.py
`--default.py
app.py
calls API_module
with something like:
app = API_module.api()
the api.py
based on "HTTP GET requests" will load modules stored on directory named modules
, for now, I am just loading a module named default
.
api.py
looks like:
import os
import imp
from exceptions import HTTPError, HTTPException
class API(object):
def __call__(self, env, start_response):
self.env = env
self.method = env['REQUEST_METHOD']
try:
body = self.router()
body.dispatch()
except HTTPError, e:
print 'HTTP method not valid %s' % e
except, Exception e:
print 'something went wrong'
start_response(status, headers)
yield body
def router():
module_path = '/home/modules/default/default.py'
if not os.access(module_path, os.R_OK):
raise HTTPException()
else:
py_mod = imp.load_source('default', '/home/modules/default/default.py'
return py_mod.Resource(self)
and default.py
contains something like:
class Resoure(object):
def __init__(self, app):
self.app = app
def dispatch(self):
raise HTTPException()
So far I can import dynamically the modules but if I want to raise an exception from the default.py
module I get an:
global name 'HTTPException' is not defined
Therefore I would like to know, how to take advantage of the API_module/exceptions
and use them in all the dynamically modules.
Any ideas, suggestions comments are welcome.