Here's the code to run a Pylons app without using entry points or
Setuptools. It works from the command line, so now we're going to
test it with py2app and the GUI (wxPython).
===
"""Run the standalone on the console similar to "paster serve".
This version imports the server and application rather than loading entry
points based on the ``use`` variables in the config file. However, it does do
a string comparision on the ``use`` values and aborts if point to something
different than what it intends to load.
Make sure you've installed the application, Pylons, and any other dependencies
before running this.
"""
import logging
import logging.config
import optparse
import os
from paste.deploy.loadwsgi import NicerConfigParser
# Server class and corresponding 'use=' value in config
SERVER_USE_VALUE = "egg:Paste#http"
from paste.httpserver import server_runner as server_factory
# Application factory and corresponding 'use=' value in config
APP_USE_VALUE = "egg:cameo"
from cameo.config.middleware import make_app as app_factory
def get_config(cp, section, expected_use_value):
"""Get a section from an INI-style config file as a dict.
``cp`` -- NicerConfigParser.
``section`` -- the section to read.
``expected_use_value`` -- expected value of ``use`` option in the section.
Aborts if the value of ``use`` doesn't equal the expected value. This
indicates Paster would instantiate a different object than we're expecting.
The ``use`` key is removed from the dict before returning.
"""
defaults = cp.defaults()
ret = {}
for option in cp.options(section):
if option.startswith("set "): # Override a global option.
option = option[4:]
elif option in defaults: # Don't carry over other global options.
continue
ret[option] = cp.get(section, option)
use = ret.pop("use", "")
if use != expected_use_value:
msg = ("unexpected value for 'use=' in section '%s': "
"expected '%s', found '%s'")
msg %= (section, expected_use_value, use)
raise EnvironmentError(msg)
return ret
def go(ini_file):
ini_file = os.path.abspath(ini_file)
if not os.path.exists(ini_file):
raise OSError("File %s not found" % ini_file)
config_dir = os.path.dirname(ini_file)
logging.config.fileConfig(ini_file)
cp = NicerConfigParser(ini_file)
cp.read(ini_file)
global_conf = cp.defaults()
cp._defaults.setdefault("here", config_dir)
cp._defaults.setdefault("__file__", ini_file)
server_conf = get_config(cp, "server:main", "egg:Paste#http")
app_conf = get_config(cp, "app:main", "egg:cameo")
app = app_factory(global_conf, **app_conf)
serve = server_factory(app, global_conf, **server_conf)
serve(app) # Serves forever until ctrl-C pressed.
def main():
parser = optparse.OptionParser(usage="%prog STANDALONE.ini")
opts, args = parser.parse_args()
if len(args) != 1:
parser.error("wrong number of command-line arguments")
go(args[0])
if __name__ == "__main__":
main()
===
--
Mike Orr <[EMAIL PROTECTED]>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pylons-discuss?hl=en
-~----------~----~----~----~------~----~------~--~---