1
# Copyright (c) 2002, 2005 Allan Saddi <allan@saddi.com>
4
# Redistribution and use in source and binary forms, with or without
5
# modification, are permitted provided that the following conditions
7
# 1. Redistributions of source code must retain the above copyright
8
# notice, this list of conditions and the following disclaimer.
9
# 2. Redistributions in binary form must reproduce the above copyright
10
# notice, this list of conditions and the following disclaimer in the
11
# documentation and/or other materials provided with the distribution.
13
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16
# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25
# $Id: importingmodule.py 1837 2006-01-15 18:20:37Z asaddi $
27
__author__ = 'Allan Saddi <allan@saddi.com>'
28
__version__ = '$Revision: 1837 $'
34
from resolver import *
36
__all__ = ['ImportingModuleResolver']
38
class NoDefault(object):
41
class ImportingModuleResolver(Resolver):
43
Constructor takes a directory name or a list of directories. Interprets
44
the first two components of PATH_INFO as 'module/function'. Modules
45
are imported as needed and must reside in the directories specified.
46
Module and function names beginning with underscore are ignored.
48
If the 'module' part of PATH_INFO is missing, it is assumed to be
51
If the 'function' part of PATH_INFO is missing, it is assumed to be
54
Upon successful resolution, appends the module and function names to
55
SCRIPT_NAME and updates PATH_INFO as the remaining components of the path.
57
NB: I would recommend explicitly setting all modules' __all__ list.
58
Otherwise, be sure all the names of module-level callables that you
59
don't want exported begin with underscore.
61
# No default module by default.
65
def __init__(self, path, defaultModule=NoDefault, index=NoDefault):
67
if defaultModule is not NoDefault:
68
self.default_module = defaultModule
69
if index is not NoDefault:
70
self.index_page = index
72
def resolve(self, request, redirect=False):
73
path_info = request.pathInfo.split(';')[0]
74
path_info = path_info.split('/')
76
assert len(path_info) > 0
77
assert not path_info[0]
79
while len(path_info) < 3:
82
module_name, func_name = path_info[1:3]
85
module_name = self.default_module
88
func_name = self.index_page
91
if module_name and (module_name[0] != '_' or redirect) and \
92
not module_name.count('.'):
93
module = _import_module(module_name, path=self.path)
95
if module is not None:
96
if func_name and (func_name[0] != '_' or redirect):
97
module_all = getattr(module, '__all__', None)
98
if module_all is None or func_name in module_all or redirect:
99
func = getattr(module, func_name, None)
101
self._updatePath(request, 2)
106
def _import_module(name, path=None):
108
Imports a module. If path is None, module will be searched for in
109
sys.path. If path is given (which may be a single string or a list),
110
the module will only be searched for in those directories.
112
if path is not None and type(path) is not list:
115
module = sys.modules.get(name)
116
if module is not None:
117
module_file = getattr(module, '__file__')
118
if module_file is None or \
119
(path is not None and os.path.dirname(module_file) not in path):
124
fp, pathname, description = imp.find_module(name, path)
126
return imp.load_module(name, fp, pathname, description)