From e35dc1a58a4e91977dc9819c80259f09800a0b58 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Mon, 18 Aug 2008 04:06:35 +0000 Subject: - Generated application differences: ``make_app`` entry point renamed to ``app`` in order to have a different name than the bfg function of the same name, to prevent confusion. - Add "options" processing to bfg's ``make_app`` to support runtime options. A new API function named ``get_options`` was added to the registry module. This function is typically used in an application's ``app`` entry point. The Paste config file section for the app can now supply the ``reload_templates`` option, which, if true, will prevent the need to restart the appserver in order for ``z3c.pt`` or XSLT template changes to be detected. - Use only the module name in generated project's "test_suite" (run all tests found in the package). - Default port for generated apps changed from 5432 to 6543 (Postgres default port is 6543). --- CHANGES.txt | 25 +++++-- docs/narr/myproject/myproject.ini | 3 +- docs/narr/myproject/myproject/configure.zcml | 4 - docs/narr/myproject/myproject/run.py | 12 +-- docs/narr/myproject/setup.py | 4 +- docs/narr/project.rst | 95 +++++++++++++----------- repoze/bfg/__init__.py | 1 + repoze/bfg/meta.zcml | 6 -- repoze/bfg/paster_template/+package+.ini_tmpl | 5 +- repoze/bfg/paster_template/+package+/run.py_tmpl | 12 +-- repoze/bfg/paster_template/setup.py_tmpl | 4 +- repoze/bfg/registry.py | 24 +++++- repoze/bfg/router.py | 19 +++-- repoze/bfg/sampleapp/configure.zcml | 3 - repoze/bfg/sampleapp/run.py | 2 +- repoze/bfg/tests/test_registry.py | 22 ++++++ repoze/bfg/tests/test_zcml.py | 31 -------- repoze/bfg/zcml.py | 27 ------- 18 files changed, 147 insertions(+), 152 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 4717cfce2..6325e6c32 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,13 +1,22 @@ Next release - - Add ```` directive. This directive currently allows - only one attribute: ``reload_templates``. If e.g.:: - - - - is in your application's ZCML, you will not need to restart the - appserver in order for ``z3c.pt`` or XSLT template changes to be - detected and displayed. + - Generated application differences: ``make_app`` entry point + renamed to ``app`` in order to have a different name than the bfg + function of the same name, to prevent confusion. + + - Add "options" processing to bfg's ``make_app`` to support runtime + options. A new API function named ``get_options`` was added to + the registry module. This function is typically used in an + application's ``app`` entry point. The Paste config file section + for the app can now supply the ``reload_templates`` option, which, + if true, will prevent the need to restart the appserver in order + for ``z3c.pt`` or XSLT template changes to be detected. + + - Use only the module name in generated project's "test_suite" (run + all tests found in the package). + + - Default port for generated apps changed from 5432 to 6543 + (Postgres default port is 6543). 0.3.0 diff --git a/docs/narr/myproject/myproject.ini b/docs/narr/myproject/myproject.ini index 21292d14c..318c2214a 100644 --- a/docs/narr/myproject/myproject.ini +++ b/docs/narr/myproject/myproject.ini @@ -2,7 +2,8 @@ debug = true [app:main] -use = egg:myproject#make_app +use = egg:myproject#app +reload_templates = true [server:main] use = egg:PasteScript#cherrypy diff --git a/docs/narr/myproject/myproject/configure.zcml b/docs/narr/myproject/myproject/configure.zcml index 6cd692784..fdae69cb3 100644 --- a/docs/narr/myproject/myproject/configure.zcml +++ b/docs/narr/myproject/myproject/configure.zcml @@ -5,10 +5,6 @@ - - - - - diff --git a/repoze/bfg/paster_template/+package+.ini_tmpl b/repoze/bfg/paster_template/+package+.ini_tmpl index 9bb03718c..2d6c4d0a2 100644 --- a/repoze/bfg/paster_template/+package+.ini_tmpl +++ b/repoze/bfg/paster_template/+package+.ini_tmpl @@ -2,10 +2,11 @@ debug = true [app:main] -use = egg:{{project}}#make_app +use = egg:{{project}}#app +reload_templates = true [server:main] use = egg:PasteScript#cherrypy host = 0.0.0.0 -port = 5432 +port = 6543 numthreads = 4 diff --git a/repoze/bfg/paster_template/+package+/run.py_tmpl b/repoze/bfg/paster_template/+package+/run.py_tmpl index f8228d30a..b3c3d1ed7 100644 --- a/repoze/bfg/paster_template/+package+/run.py_tmpl +++ b/repoze/bfg/paster_template/+package+/run.py_tmpl @@ -1,13 +1,13 @@ -def make_app(global_config, **kw): +from repoze.bfg import make_app +from repoze.bfg import get_options + +def app(global_config, **kw): # paster app config callback - from repoze.bfg import make_app from {{project}}.models import get_root import {{ project}} - app = make_app(get_root, {{project}}) - return app + return make_app(get_root, {{project}}, options=get_options(kw)) if __name__ == '__main__': from paste import httpserver - app = make_app(None) - httpserver.serve(app, host='0.0.0.0', port='5432') + httpserver.serve(app(None), host='0.0.0.0', port='6543') diff --git a/repoze/bfg/paster_template/setup.py_tmpl b/repoze/bfg/paster_template/setup.py_tmpl index 278153de8..d2b4d575b 100644 --- a/repoze/bfg/paster_template/setup.py_tmpl +++ b/repoze/bfg/paster_template/setup.py_tmpl @@ -35,10 +35,10 @@ setup(name='{{project}}', tests_require=[ 'repoze.bfg', ], - test_suite="{{project}}.tests", + test_suite="{{project}}", entry_points = """\ [paste.app_factory] - make_app = {{project}}.run:make_app + app = {{project}}.run:app """ ) diff --git a/repoze/bfg/registry.py b/repoze/bfg/registry.py index e1887bbdd..e02e5124e 100644 --- a/repoze/bfg/registry.py +++ b/repoze/bfg/registry.py @@ -6,9 +6,12 @@ from zope.component.interfaces import ComponentLookupError from zope.component.interfaces import IComponentLookup from zope.component.registry import Components from zope.component import getSiteManager as original_getSiteManager - from zope.configuration import xmlconfig +from zope.interface import implements + +from repoze.bfg.interfaces import ISettings + class ThreadLocalRegistryManager(threading.local): registry = getGlobalSiteManager() def set(self, registry): @@ -28,7 +31,7 @@ def setRegistryManager(manager): # for unit tests registry_manager = manager return old_registry_manager -def makeRegistry(filename, package, lock=threading.Lock()): +def makeRegistry(filename, package, options=None, lock=threading.Lock()): # This is absurd and probably not worth it. We want to try to # push our ZCML-defined configuration into an app-local component # registry in order to allow more than one bfg app to live in the @@ -50,12 +53,21 @@ def makeRegistry(filename, package, lock=threading.Lock()): original_getSiteManager.sethook(getSiteManager) zope.component.getGlobalSiteManager = registry_manager.get xmlconfig.file(filename, package=package) + if options is None: + options = {} + settings = Settings(options) + registry.registerUtility(settings, ISettings) return registry finally: zope.component.getGlobalSiteManager = getGlobalSiteManager lock.release() registry_manager.clear() +class Settings(object): + implements(ISettings) + def __init__(self, options): + self.reload_templates = options.get('reload_templates', False) + def getSiteManager(context=None): if context is None: return registry_manager.get() @@ -65,6 +77,14 @@ def getSiteManager(context=None): except TypeError, error: raise ComponentLookupError(*error.args) +def asbool(s): + s = str(s).strip() + return s.lower() in ('t', 'true', 'y', 'yes', 'on', '1') + +def get_options(kw): + reload_templates = asbool(kw.get('reload_templates')) + return {'reload_templates':reload_templates} + from zope.testing.cleanup import addCleanUp try: addCleanUp(original_getSiteManager.reset) diff --git a/repoze/bfg/router.py b/repoze/bfg/router.py index d6b60f5ec..49ccd1ad6 100644 --- a/repoze/bfg/router.py +++ b/repoze/bfg/router.py @@ -74,16 +74,21 @@ def isResponse(ob): isinstance(ob.status, basestring) ) : return True -def make_app(root_policy, package=None, filename='configure.zcml'): +def make_app(root_policy, package=None, filename='configure.zcml', + options=None): """ Create a view registry based on the application's ZCML. and return a Router object, representing a ``repoze.bfg`` WSGI - application. 'root_policy' must be a callable that accepts a WSGI - environment and returns a graph root object. 'package' is the - dotted-Python-path packagename of the application, 'filename' is - the filesystem path to a ZCML file (optionally relative to the - package path) that should be parsed to create the view registry.""" + application. ``root_policy`` must be a callable that accepts a + WSGI environment and returns a graph root object. ``package`` is + a Python module representing the application's package, + ``filename`` is the filesystem path to a ZCML file (optionally + relative to the package path) that should be parsed to create the + view registry. ``options``, if used, should be a dictionary + containing bfg-specific runtime options, with each key + representing the option and the key's value representing the + specific option value, e.g. ``{'reload_templates':True}``""" from repoze.bfg.registry import makeRegistry - registry = makeRegistry(filename, package) + registry = makeRegistry(filename, package, options) return Router(root_policy, registry) diff --git a/repoze/bfg/sampleapp/configure.zcml b/repoze/bfg/sampleapp/configure.zcml index d20d7d8f9..a5f27595e 100644 --- a/repoze/bfg/sampleapp/configure.zcml +++ b/repoze/bfg/sampleapp/configure.zcml @@ -4,9 +4,6 @@ - -