diff options
author | Ian Bicking <ian@ianbicking.org> | 2005-04-26 02:08:10 +0000 |
---|---|---|
committer | Ian Bicking <ian@ianbicking.org> | 2005-04-26 02:08:10 +0000 |
commit | f583c4dea38db829805484bf88bab754252f61e8 (patch) | |
tree | 6155edeb6f6fbfc040ce6f771be6cd4decdf5fe8 | |
parent | 56830e37f4c93ef6eff634dbbe456f15fdfb22ba (diff) | |
download | paste-git-f583c4dea38db829805484bf88bab754252f61e8.tar.gz |
Added publish_application
-rwxr-xr-x | paste/server.py | 7 | ||||
-rw-r--r-- | paste/tests/test_import_string.py | 16 | ||||
-rw-r--r-- | paste/util/import_string.py | 44 |
3 files changed, 66 insertions, 1 deletions
diff --git a/paste/server.py b/paste/server.py index 24a9b38..c2297c2 100755 --- a/paste/server.py +++ b/paste/server.py @@ -247,8 +247,13 @@ servers['console'] = console_server def make_app(conf): if conf.get('publish_dir'): app = wsgiwebkit.webkit(conf['publish_dir'], use_lint=conf.get('lint')) + elif conf.get('publish_application'): + app = conf['publish_application'] + if isinstance(app, (str, unicode)): + from paste.util import import_string + app = import_string.eval_import(app) else: - print "You must provide --webkit-dir" + print "You must provide publish_dir or publish_application" sys.exit(2) return config_middleware(app, conf) diff --git a/paste/tests/test_import_string.py b/paste/tests/test_import_string.py new file mode 100644 index 0000000..96526ac --- /dev/null +++ b/paste/tests/test_import_string.py @@ -0,0 +1,16 @@ +from paste.util.import_string import * +import sys +import os + +def test_simple(): + for func in eval_import, simple_import: + assert func('sys') is sys + assert func('sys.version') is sys.version + assert func('os.path.join') is os.path.join + +def test_complex(): + assert eval_import('sys:version') is sys.version + assert eval_import('os:getcwd()') == os.getcwd() + assert (eval_import('sys:version.split()[0]') == + sys.version.split()[0]) + diff --git a/paste/util/import_string.py b/paste/util/import_string.py new file mode 100644 index 0000000..af32b24 --- /dev/null +++ b/paste/util/import_string.py @@ -0,0 +1,44 @@ +""" +'imports' a string -- converts a string to a Python object, importing +any necessary modules and evaluating the expression. Everything +before the : in an import expression is the module path; everything +after is an expression to be evaluated in the namespace of that +module. + +Alternately, if no : is present, then import the modules and get the +attributes as necessary. Arbitrary expressions are not allowed in +that case. +""" + +def eval_import(s): + if ':' not in s: + return simple_import(s) + module_name, expr = s.split(':', 1) + module = import_module(module_name) + obj = eval(expr, module.__dict__) + return obj + +def simple_import(s): + parts = s.split('.') + module = import_module(parts[0]) + name = parts[0] + parts = parts[1:] + while parts: + name += '.' + parts[0] + try: + module = import_module(name) + parts = parts[1:] + except ImportError: + break + obj = module + while parts: + obj = getattr(module, parts[0]) + parts = parts[1:] + return obj + +def import_module(s): + mod = __import__(s) + parts = s.split('.') + for part in parts[1:]: + mod = getattr(mod, part) + return mod |