This class uses a separate Python module for each page in the application. This scales very well at runtime because at most two modules will be loaded for each request; one to process the browser request, and possibly another to display the response. Page modules are cached for further efficiency. The class is designed to be used where the application controls the sequence of pages seen in a browser session so the start page is also specified in the constructor.
Application page modules are loaded from a root directory which is specified in the constructor. The current application page is identified by the path to the page module relative to the module root directory. Page identifiers consist of an optional path component followed by a module name without extension. For example "login", "user/list", "home-page/default".
To load a page module the class combines the root directory passed to
the constructor with the page identifier using os.path.join()
.
It then checks the page module cache to see if the module needs to be
imported or reloaded. If the module needs to be imported, the full
module path is split into directory and filename components using
os.path.split()
. The standard module importer in the
imp module is then used load the page module using the
filename part of the full path as the module name and the directory
component as the module search path. The simplified code looks like
this:
path = os.path.join(self.module_base_dir, name) dirname, name = os.path.split(path) file, filepath, desc = imp.find_module(name, [dirname]) page = imp.load_module(name, file, filepath, desc)
Due to this load method page module identifiers are not relative to the current page, they are always relative to the module root directory specified in the base_dir argument to the PageModuleMixin constructor.
Page modules handled by this mixin have the following interface:
ctx [, ...]) |
The ctx argument contains the execution context. Any extra arguments which are passed to the set_page() method are passed as optional extra arguments to this function.
ctx) |
The ctx argument contains the execution context.
ctx) |
Refer to page
ctx) |
Refer to page
The PageModuleMixin class has the following interface.
base_dir, start_page) |
The base_dir argument specifies the path of the root directory where page modules are loaded from. When deploying your application as a CGI program you can specify a relative path from the location of the application mainline. Apache sets the current directory to root so when using mod_python deployment you will need to specify a path relative to the root.
The start_page argument is a page identifier which specifies the first page that new browser session will see.
) |
) |
ctx) |
If no current page is defined in ctx then the method will invoke ctx.set_page() passing the page specified as the start_page argument to the application constructor.
The actual page module load is performed via the load_page_module() method.
Refer to page
ctx, name) |
ctx, args) |
The page module page_enter() function is called by this method.
ctx) |
The page module page_leave() function is called by this method.
ctx) |
The page module page_process() function is called by this method.
Refer to page
ctx) |
The page module page_display() function is called by this method.
Refer to page