From d7e4575c156ff4dda08871d57b6a7a51b79996f0 Mon Sep 17 00:00:00 2001 From: bbangert Date: Mon, 2 Jul 2007 17:22:44 -0700 Subject: [PATCH] [svn] * Updated default project template files for new configuration layout. Options to handle config now just in environment.py, and middleware.py handling just middleware. * Removing mako tests, as its now the default. Default test changed from Myghty to Mako. * Changing default templating to mako. --HG-- branch : trunk --- CHANGELOG | 6 ++++ pylons/configuration.py | 13 ++++--- pylons/database.py | 35 +++++-------------- .../+package+/config/environment.py_tmpl | 32 +++++++++++------ .../+package+/config/middleware.py_tmpl | 22 +++++------- .../filestotest/controller_sample.py | 5 +-- .../functional_sample_controller_mako.py | 2 +- .../filestotest/middleware_cheetah_engine.py | 18 ++++------ .../filestotest/middleware_def_engine.py | 21 ++++------- .../filestotest/middleware_mako.py | 20 ++++------- .../filestotest/middleware_two_engines.py | 18 ++++------ tests/test_webapps/filestotest/test_mako.html | 9 ++++- .../test_webapps/filestotest/test_myghty.myt | 9 ----- tests/test_webapps/test_make_project.py | 18 +--------- 14 files changed, 88 insertions(+), 140 deletions(-) delete mode 100644 tests/test_webapps/filestotest/test_myghty.myt diff --git a/CHANGELOG b/CHANGELOG index f47b25c0..135fd12c 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,12 @@ Pylons Changelog ================= 0.9.6 (**svn**) +* Updated default project template files for new configuration layout. Options + to handle config now just in environment.py, and middleware.py handling just + middleware. +* Removing mako tests, as its now the default. Default test changed from + Myghty to Mako. +* Changing default templating to mako. * Added the https decorator. It requires an action to be loaded via https. Patch by ido. Fixes #241. * Added upgrade instructions, and posted a copy on the wiki. Fixes #230. diff --git a/pylons/configuration.py b/pylons/configuration.py index ab4b00c2..bdce3bc7 100644 --- a/pylons/configuration.py +++ b/pylons/configuration.py @@ -208,7 +208,7 @@ def add_template_engine(self, engine, root, options=None, alias=None): self['buffet.template_engines'].append(config) def init_app(self, global_conf, app_conf, package=None, - template_engine=default_template_engine): + template_engine=default_template_engine, paths=None): """Initialize configuration for the application ``global_config`` @@ -240,14 +240,17 @@ def init_app(self, global_conf, app_conf, package=None, conf.update(app_conf) conf.update(dict(app_conf=app_conf, global_conf=global_conf)) conf.update(self.pop('environment_load', {})) - + + if paths: + conf['pylons.paths'] = paths + # XXX Legacy: More backwards compatibility locations for the package # name conf['pylons.package'] = conf['package'] = conf['app_conf']['package'] = package - + self.push_process_config(conf) self.set_defaults(template_engine) - + def set_defaults(self, template_engine): conf = self._current_obj() # Ensure all the keys from defaults are present, load them if not @@ -358,7 +361,7 @@ def set_defaults(self, template_engine): self.add_template_engine(template_engine, conf['pylons.package'] + '.templates') # Save our errorware values - conf['errorware'] = errorware + conf['pylons.errorware'] = errorware config = PylonsConfig() diff --git a/pylons/database.py b/pylons/database.py index c5a4d1e6..cd932bef 100644 --- a/pylons/database.py +++ b/pylons/database.py @@ -13,12 +13,9 @@ from paste.deploy.converters import asbool import pylons -import pylons.util __all__ = ["PackageHub", "AutoConnectHub"] -# Provide support for sqlalchemy -_db_engines = {} try: import sqlalchemy from sqlalchemy.ext import sessioncontext @@ -31,7 +28,7 @@ def app_scope(): application's Globals object at pylons.g (if one is registered). """ try: - app_scope_id = str(id(pylons.g._current_obj())) + app_scope_id = str(id(pylons.config._current_obj())) except TypeError: app_scope_id = '' return '%s|%i' % (app_scope_id, thread.get_ident()) @@ -45,7 +42,7 @@ def create_engine(uri=None, echo=None, **kwargs): uri, echo = get_engine_conf(uri, echo) kwargs['echo'] = echo engine_key = '%s|%s' % (uri, str(kwargs)) - db_engines = get_engines() + db_engines = pylons.config['pylons.db_engines'] if engine_key in db_engines: engine = db_engines[engine_key] else: @@ -58,31 +55,15 @@ def get_engine_conf(uri=None, echo=None): echo) from the Pylons config file values ``sqlalchemy.dburi`` and ``sqlalchemy.echo`` when none are specified.""" if uri is None: - uri = pylons.util.config_get('sqlalchemy.dburi') + uri = pylons.config.get('sqlalchemy.dburi') if not uri: raise KeyError('No SQLAlchemy database config found!') if echo is None: - echo = pylons.util.config_get('sqlalchemy.echo', False) + echo = pylons.config.get('sqlalchemy.echo', False) if echo != 'debug': echo = asbool(echo) return uri, echo - - def get_engines(): - """Return a dict of cached SQLAlchemy engines. - - Engines are cached to an application's ``Globals`` (``g``) object. If - for whatever reason the ``g`` object has not been registered (outside - of a web request), engines are cached to the global ``engines`` dict - instead.""" - try: - if not hasattr(pylons.g, '_db_engines'): - db_engines = pylons.g._db_engines = {} - else: - db_engines = pylons.g._db_engines - return db_engines - except TypeError: - return _db_engines - + def make_session(uri=None, echo=None, session_kwargs=None, **kwargs): """Returns a SQLAlchemy session for the specified database uri from the the engine cache (returned from ``get_engines``)``. Uses the @@ -127,7 +108,7 @@ class AutoConnectHub(ConnectionHub): def __init__(self, uri=None, pool_connections=True): if not uri: - uri = pylons.util.config_get('sqlobject.dburi') + uri = pylons.config.get('sqlobject.dburi') self.uri = uri self.pool_connections = pool_connections ConnectionHub.__init__(self) @@ -240,13 +221,13 @@ def set_hub(self): dburi = self.dburi if not dburi: try: - dburi = pylons.util.config_get("%s.dburi" % \ + dburi = pylons.config.get("%s.dburi" % \ self.packagename) except TypeError, e: # No configuration is registered raise UnconfiguredConnectionError(str(e)) if not dburi: - dburi = pylons.util.config_get("sqlobject.dburi") + dburi = pylons.config.get("sqlobject.dburi") if not dburi: raise UnconfiguredConnectionError( "No database configuration found!") diff --git a/pylons/templates/default_project/+package+/config/environment.py_tmpl b/pylons/templates/default_project/+package+/config/environment.py_tmpl index 3d539af7..4ccac464 100644 --- a/pylons/templates/default_project/+package+/config/environment.py_tmpl +++ b/pylons/templates/default_project/+package+/config/environment.py_tmpl @@ -1,14 +1,14 @@ import os -import pylons.config +from paste.deploy.config import CONFIG + +from pylons import config import webhelpers from ${package}.config.routing import make_map -def load_environment(conf={}): - - map = make_map(conf) - # Setup our paths +def load_environment(global_conf, app_conf): + # Create our paths root_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) paths = {'root_path': root_path, 'controllers': os.path.join(root_path, 'controllers'), @@ -16,14 +16,24 @@ def load_environment(conf={}): ('components', 'templates')], 'static_files': os.path.join(root_path, 'public') } + + # Initialize the other basic options + config.init_app(global_conf, app_conf, package='${package}', + template_engine='${template_engine}', paths=paths) + + map = make_map(config) + config['pylons.map'] = map - # The following template options are passed to your template engines - tmpl_options = {} - tmpl_options['myghty.log_errors'] = True - tmpl_options['myghty.escapes'] = dict(l=webhelpers.auto_link, s=webhelpers.simple_format) # Add your own template options config options here, note that all config options will override # any Pylons config options - # Return our loaded config object - return pylons.config.Config(tmpl_options, map, paths) + # The following template options are passed to your template engines + tmpl_options = {} + + # Load-up the template options + config['buffet.template_options'] = tmpl_options + + # Setup the Paste CONFIG object for legacy code + CONFIG.push_process_config(config._current_obj()) + \ No newline at end of file diff --git a/pylons/templates/default_project/+package+/config/middleware.py_tmpl b/pylons/templates/default_project/+package+/config/middleware.py_tmpl index a6fcd08f..f5f506b2 100644 --- a/pylons/templates/default_project/+package+/config/middleware.py_tmpl +++ b/pylons/templates/default_project/+package+/config/middleware.py_tmpl @@ -1,10 +1,11 @@ from paste.cascade import Cascade from paste.urlparser import StaticURLParser from paste.registry import RegistryManager -from paste.deploy.config import ConfigMiddleware, CONFIG +from paste.deploy.config import ConfigMiddleware from paste.deploy.converters import asbool from pylons.error import error_template +from pylons import config from pylons.middleware import ErrorHandler, ErrorDocuments, StaticJavascripts, error_mapper import pylons.wsgiapp @@ -19,21 +20,14 @@ def make_app(global_conf, full_stack=True, **app_conf): paste.deploy.converters should be used when parsing Paste config options to ensure they're treated properly. """ - # Setup the Paste CONFIG object, adding app_conf/global_conf for legacy code - conf = global_conf.copy() - conf.update(app_conf) - conf.update(dict(app_conf=app_conf, global_conf=global_conf)) - CONFIG.push_process_config(conf) - # Load our Pylons configuration defaults - config = load_environment(conf) - config.init_app(global_conf, app_conf, package='${package}', template_engine='${template_engine}') + load_environment(global_conf, app_conf) # Load our default Pylons WSGI app and make g available - app = pylons.wsgiapp.PylonsApp(config, helpers=${package}.lib.helpers, + app = pylons.wsgiapp.PylonsApp(helpers=${package}.lib.helpers, g=app_globals.Globals) - g = app.globals - app = ConfigMiddleware(app, conf) + + app = ConfigMiddleware(app, config._current_obj()) # YOUR MIDDLEWARE # Put your own middleware here, so that any problems are caught by the error @@ -44,7 +38,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # catch the problems. if asbool(full_stack): # Error Handling - app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware) + app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (if debug is disabled also # intercepts 500) @@ -53,7 +47,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # Establish the Registry for this application app = RegistryManager(app) - static_app = StaticURLParser(config.paths['static_files']) + static_app = StaticURLParser(config['pylons.paths']['static_files']) javascripts_app = StaticJavascripts() app = Cascade([static_app, javascripts_app, app]) return app diff --git a/tests/test_webapps/filestotest/controller_sample.py b/tests/test_webapps/filestotest/controller_sample.py index 16f332f4..9a53ab83 100644 --- a/tests/test_webapps/filestotest/controller_sample.py +++ b/tests/test_webapps/filestotest/controller_sample.py @@ -30,14 +30,11 @@ def myparams(self): def testdefault(self): return render_response('testkid') - def testmako(self): - return render_response('/test_mako.html') - def test_extra_engine(self): return render_response('kid', 'testkid') def test_template_caching(self): - return render_response('/test_myghty.myt', cache_expire='never') + return render_response('/test_mako.html', cache_expire='never') def test_only_post(self): return Response('It was a post!') diff --git a/tests/test_webapps/filestotest/functional_sample_controller_mako.py b/tests/test_webapps/filestotest/functional_sample_controller_mako.py index 00c04b43..a2a04837 100644 --- a/tests/test_webapps/filestotest/functional_sample_controller_mako.py +++ b/tests/test_webapps/filestotest/functional_sample_controller_mako.py @@ -1,6 +1,6 @@ from projectname.tests import * -class TestCheetahController(TestController): +class TestMakoController(TestController): def test_mako(self): response = self.app.get(url_for(controller='/sample', action='testmako')) assert 'Hello, 5+5 is 10' in response diff --git a/tests/test_webapps/filestotest/middleware_cheetah_engine.py b/tests/test_webapps/filestotest/middleware_cheetah_engine.py index d872eb42..4eab2a02 100755 --- a/tests/test_webapps/filestotest/middleware_cheetah_engine.py +++ b/tests/test_webapps/filestotest/middleware_cheetah_engine.py @@ -6,6 +6,7 @@ from paste.deploy.converters import asbool from pylons.error import error_template +from pylons import config from pylons.middleware import ErrorHandler, ErrorDocuments, StaticJavascripts, error_mapper import pylons.wsgiapp @@ -21,24 +22,17 @@ def make_app(global_conf, full_stack=True, **app_conf): to ensure they're treated properly. """ - conf = global_conf.copy() - conf.update(app_conf) - conf.update(dict(app_conf=app_conf, global_conf=global_conf)) - CONFIG.push_process_config(conf) - # Load our Pylons configuration defaults - config = load_environment(conf) - config.init_app(global_conf, app_conf, package='projectname') + load_environment(global_conf, app_conf) # Pull the other engine and put a new one up first config.template_engines.pop() config.add_template_engine('cheetah', 'projectname.cheetah', {}) # Load our default Pylons WSGI app and make g available - app = pylons.wsgiapp.PylonsApp(config, helpers=projectname.lib.helpers, + app = pylons.wsgiapp.PylonsApp(helpers=projectname.lib.helpers, g=app_globals.Globals) - g = app.globals - app = ConfigMiddleware(app, conf) + app = ConfigMiddleware(app, config._current_obj()) # If errror handling and exception catching will be handled by middleware # for multiple apps, you will want to set full_stack = False in your config @@ -48,7 +42,7 @@ def make_app(global_conf, full_stack=True, **app_conf): app = httpexceptions.make_middleware(app, global_conf) # Error Handling - app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware) + app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (if debug is disabled also # intercepts 500) @@ -57,7 +51,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # Establish the Registry for this application app = RegistryManager(app) - static_app = StaticURLParser(config.paths['static_files']) + static_app = StaticURLParser(config['pylons.paths']['static_files']) javascripts_app = StaticJavascripts() app = Cascade([static_app, javascripts_app, app]) return app diff --git a/tests/test_webapps/filestotest/middleware_def_engine.py b/tests/test_webapps/filestotest/middleware_def_engine.py index 2754fa36..924eb4b1 100644 --- a/tests/test_webapps/filestotest/middleware_def_engine.py +++ b/tests/test_webapps/filestotest/middleware_def_engine.py @@ -6,6 +6,7 @@ from paste.deploy.converters import asbool from pylons.error import error_template +from pylons import config from pylons.middleware import ErrorHandler, ErrorDocuments, StaticJavascripts, error_mapper import pylons.wsgiapp @@ -21,25 +22,17 @@ def make_app(global_conf, full_stack=True, **app_conf): to ensure they're treated properly. """ - conf = global_conf.copy() - conf.update(app_conf) - conf.update(dict(app_conf=app_conf, global_conf=global_conf)) - CONFIG.push_process_config(conf) - - # Load our Pylons configuration defaults - config = load_environment(conf) - config.init_app(global_conf, app_conf, package='projectname') + load_environment(global_conf, app_conf) # Pull the other engine and put a new one up first config.template_engines.pop() kidopts = {'kid.assume_encoding':'utf-8', 'kid.encoding':'utf-8'} - config.add_template_engine('kid', 'projectname.kidtemplates', kidopts) + pylons.config.add_template_engine('kid', 'projectname.kidtemplates', kidopts) # Load our default Pylons WSGI app and make g available - app = pylons.wsgiapp.PylonsApp(config, helpers=projectname.lib.helpers, + app = pylons.wsgiapp.PylonsApp(helpers=projectname.lib.helpers, g=app_globals.Globals) - g = app.globals - app = ConfigMiddleware(app, conf) + app = ConfigMiddleware(app, config._current_obj()) # If errror handling and exception catching will be handled by middleware # for multiple apps, you will want to set full_stack = False in your config @@ -49,7 +42,7 @@ def make_app(global_conf, full_stack=True, **app_conf): app = httpexceptions.make_middleware(app, global_conf) # Error Handling - app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware) + app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (if debug is disabled also # intercepts 500) @@ -58,7 +51,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # Establish the Registry for this application app = RegistryManager(app) - static_app = StaticURLParser(config.paths['static_files']) + static_app = StaticURLParser(config['pylons.paths']['static_files']) javascripts_app = StaticJavascripts() app = Cascade([static_app, javascripts_app, app]) return app diff --git a/tests/test_webapps/filestotest/middleware_mako.py b/tests/test_webapps/filestotest/middleware_mako.py index bc4cbc66..20673e74 100644 --- a/tests/test_webapps/filestotest/middleware_mako.py +++ b/tests/test_webapps/filestotest/middleware_mako.py @@ -6,6 +6,7 @@ from paste.deploy.converters import asbool from pylons.error import error_template +from pylons import config from pylons.middleware import ErrorHandler, ErrorDocuments, StaticJavascripts, error_mapper import pylons.wsgiapp @@ -21,20 +22,13 @@ def make_app(global_conf, full_stack=True, **app_conf): to ensure they're treated properly. """ - conf = global_conf.copy() - conf.update(app_conf) - conf.update(dict(app_conf=app_conf, global_conf=global_conf)) - CONFIG.push_process_config(conf) - # Load our Pylons configuration defaults - config = load_environment(conf) - config.init_app(global_conf, app_conf, package='projectname', template_engine='mako') - + load_environment(global_conf, app_conf) + # Load our default Pylons WSGI app and make g available - app = pylons.wsgiapp.PylonsApp(config, helpers=projectname.lib.helpers, + app = pylons.wsgiapp.PylonsApp(helpers=projectname.lib.helpers, g=app_globals.Globals) - g = app.globals - app = ConfigMiddleware(app, conf) + app = ConfigMiddleware(app, config._current_obj()) # If errror handling and exception catching will be handled by middleware # for multiple apps, you will want to set full_stack = False in your config @@ -44,7 +38,7 @@ def make_app(global_conf, full_stack=True, **app_conf): app = httpexceptions.make_middleware(app, global_conf) # Error Handling - app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware) + app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (if debug is disabled also # intercepts 500) @@ -53,7 +47,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # Establish the Registry for this application app = RegistryManager(app) - static_app = StaticURLParser(config.paths['static_files']) + static_app = StaticURLParser(config['pylons.paths']['static_files']) javascripts_app = StaticJavascripts() app = Cascade([static_app, javascripts_app, app]) return app diff --git a/tests/test_webapps/filestotest/middleware_two_engines.py b/tests/test_webapps/filestotest/middleware_two_engines.py index 84775e7e..82d23e5f 100644 --- a/tests/test_webapps/filestotest/middleware_two_engines.py +++ b/tests/test_webapps/filestotest/middleware_two_engines.py @@ -6,6 +6,7 @@ from paste.deploy.converters import asbool from pylons.error import error_template +from pylons import config from pylons.middleware import ErrorHandler, ErrorDocuments, StaticJavascripts, error_mapper import pylons.wsgiapp @@ -21,24 +22,17 @@ def make_app(global_conf, full_stack=True, **app_conf): to ensure they're treated properly. """ - conf = global_conf.copy() - conf.update(app_conf) - conf.update(dict(app_conf=app_conf, global_conf=global_conf)) - CONFIG.push_process_config(conf) - # Load our Pylons configuration defaults - config = load_environment(conf) - config.init_app(global_conf, app_conf, package='projectname') + load_environment(global_conf, app_conf) # Add the second engine kidopts = {'kid.assume_encoding':'utf-8', 'kid.encoding':'utf-8'} config.add_template_engine('kid', 'projectname.kidtemplates', kidopts) # Load our default Pylons WSGI app and make g available - app = pylons.wsgiapp.PylonsApp(config, helpers=projectname.lib.helpers, + app = pylons.wsgiapp.PylonsApp(helpers=projectname.lib.helpers, g=app_globals.Globals) - g = app.globals - app = ConfigMiddleware(app, conf) + app = ConfigMiddleware(app, config._current_obj()) # If errror handling and exception catching will be handled by middleware # for multiple apps, you will want to set full_stack = False in your config @@ -48,7 +42,7 @@ def make_app(global_conf, full_stack=True, **app_conf): app = httpexceptions.make_middleware(app, global_conf) # Error Handling - app = ErrorHandler(app, global_conf, error_template=error_template, **config.errorware) + app = ErrorHandler(app, global_conf, error_template=error_template, **config['pylons.errorware']) # Display error documents for 401, 403, 404 status codes (if debug is disabled also # intercepts 500) @@ -57,7 +51,7 @@ def make_app(global_conf, full_stack=True, **app_conf): # Establish the Registry for this application app = RegistryManager(app) - static_app = StaticURLParser(config.paths['static_files']) + static_app = StaticURLParser(config['pylons.paths']['static_files']) javascripts_app = StaticJavascripts() app = Cascade([static_app, javascripts_app, app]) return app diff --git a/tests/test_webapps/filestotest/test_mako.html b/tests/test_webapps/filestotest/test_mako.html index 388492bf..336e948d 100644 --- a/tests/test_webapps/filestotest/test_mako.html +++ b/tests/test_webapps/filestotest/test_mako.html @@ -1 +1,8 @@ -Hello, 5+5 is ${5+5} +<% +from datetime import datetime +mytime = datetime.now() +%> + +Hi everyone! + +The time is ${mytime} diff --git a/tests/test_webapps/filestotest/test_myghty.myt b/tests/test_webapps/filestotest/test_myghty.myt deleted file mode 100644 index 4b3cf24d..00000000 --- a/tests/test_webapps/filestotest/test_myghty.myt +++ /dev/null @@ -1,9 +0,0 @@ -Hi everyone! - -The time is <% mytime %> - - -<%init> -from datetime import datetime -mytime = datetime.now() - \ No newline at end of file diff --git a/tests/test_webapps/test_make_project.py b/tests/test_webapps/test_make_project.py index fece85b4..e9ec26bc 100644 --- a/tests/test_webapps/test_make_project.py +++ b/tests/test_webapps/test_make_project.py @@ -127,7 +127,7 @@ def do_kid_default(): def do_two_engines(): copydict = { 'middleware_two_engines.py':'projectname/config/middleware.py', - 'test_myghty.myt':'projectname/templates/test_myghty.myt', + 'test_mako.html':'projectname/templates/test_mako.html', 'functional_sample_controller_sample3.py':'projectname/tests/functional/test_sample2.py', } _do_proj_test(copydict) @@ -150,21 +150,6 @@ def do_cheetah(): ] _do_proj_test(copydict, empty) -def do_mako(): - copydict = { - 'controller_sample.py':'projectname/controllers/mako.py', - 'test_mako.html':'projectname/templates/test_mako.html', - 'middleware_mako.py':'projectname/config/middleware.py', - 'functional_sample_controller_mako.py':'projectname/tests/functional/test_mako.py', - } - empty = [ - 'projectname/tests/functional/test_cheetah.py', - 'projectname/tests/functional/test_sample.py', - 'projectname/tests/functional/test_sample2.py', - 'projectname/tests/functional/test_sample3.py' - ] - _do_proj_test(copydict, empty) - def do_cache_decorator(): copydict = { 'middleware_def_engine.py':'projectname/config/middleware.py', @@ -230,7 +215,6 @@ def test_project(): yield (do_kid_default,) yield (do_two_engines,) yield (do_cheetah,) - yield (do_mako,) yield (do_crazy_decorators,) yield (do_cache_decorator,) yield (do_xmlrpc,)