Skip to content

Commit

Permalink
Merge branch 'master' of madjar/pyramid into pull.911
Browse files Browse the repository at this point in the history
  • Loading branch information
mmerickel committed Mar 20, 2013
2 parents 411acbb + 9695701 commit ea1f057
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/api/paster.rst
Expand Up @@ -9,6 +9,6 @@


.. autofunction:: get_app(config_uri, name=None, options=None) .. autofunction:: get_app(config_uri, name=None, options=None)


.. autofunction:: get_appsettings(config_uri, name=None) .. autofunction:: get_appsettings(config_uri, name=None, options=None)


.. autofunction:: setup_logging(config_uri) .. autofunction:: setup_logging(config_uri)
14 changes: 7 additions & 7 deletions pyramid/paster.py
Expand Up @@ -23,26 +23,26 @@ def get_app(config_uri, name=None, options=None, loadapp=loadapp):
path, section = _getpathsec(config_uri, name) path, section = _getpathsec(config_uri, name)
config_name = 'config:%s' % path config_name = 'config:%s' % path
here_dir = os.getcwd() here_dir = os.getcwd()
if options:
kw = {'global_conf': options}
else:
kw = {}


app = loadapp(config_name, name=section, relative_to=here_dir, **kw) app = loadapp(config_name, name=section, relative_to=here_dir, global_conf=options)


return app return app


def get_appsettings(config_uri, name=None, appconfig=appconfig): def get_appsettings(config_uri, name=None, options=None, appconfig=appconfig):
""" Return a dictionary representing the key/value pairs in an ``app`` """ Return a dictionary representing the key/value pairs in an ``app``
section within the file represented by ``config_uri``. section within the file represented by ``config_uri``.
``options``, if passed, should be a dictionary used as variable assignments
like ``{'http_port': 8080}``. This is useful if e.g. ``%(http_port)s`` is
used in the config file.
If the ``name`` is None, this will attempt to parse the name from If the ``name`` is None, this will attempt to parse the name from
the ``config_uri`` string expecting the format ``inifile#name``. the ``config_uri`` string expecting the format ``inifile#name``.
If no name is found, the name will default to "main".""" If no name is found, the name will default to "main"."""
path, section = _getpathsec(config_uri, name) path, section = _getpathsec(config_uri, name)
config_name = 'config:%s' % path config_name = 'config:%s' % path
here_dir = os.getcwd() here_dir = os.getcwd()
return appconfig(config_name, name=section, relative_to=here_dir) return appconfig(config_name, name=section, relative_to=here_dir, global_conf=options)


def setup_logging(config_uri, fileConfig=fileConfig, def setup_logging(config_uri, fileConfig=fileConfig,
configparser=configparser): configparser=configparser):
Expand Down
9 changes: 6 additions & 3 deletions pyramid/scaffolds/alchemy/+package+/scripts/initializedb.py
Expand Up @@ -9,6 +9,8 @@
setup_logging, setup_logging,
) )


from pyramid.scripts.common import parse_vars

from ..models import ( from ..models import (
DBSession, DBSession,
MyModel, MyModel,
Expand All @@ -18,17 +20,18 @@


def usage(argv): def usage(argv):
cmd = os.path.basename(argv[0]) cmd = os.path.basename(argv[0])
print('usage: %s <config_uri>\n' print('usage: %s <config_uri> [var=value]\n'
'(example: "%s development.ini")' % (cmd, cmd)) '(example: "%s development.ini")' % (cmd, cmd))
sys.exit(1) sys.exit(1)




def main(argv=sys.argv): def main(argv=sys.argv):
if len(argv) != 2: if len(argv) < 2:
usage(argv) usage(argv)
config_uri = argv[1] config_uri = argv[1]
options = parse_vars(argv[2:])
setup_logging(config_uri) setup_logging(config_uri)
settings = get_appsettings(config_uri) settings = get_appsettings(config_uri, options=options)
engine = engine_from_config(settings, 'sqlalchemy.') engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine) DBSession.configure(bind=engine)
Base.metadata.create_all(engine) Base.metadata.create_all(engine)
Expand Down
16 changes: 8 additions & 8 deletions pyramid/tests/test_paster.py
Expand Up @@ -56,14 +56,15 @@ def test_it_with_options(self):
self.assertEqual(result, app) self.assertEqual(result, app)


class Test_get_appsettings(unittest.TestCase): class Test_get_appsettings(unittest.TestCase):
def _callFUT(self, config_file, section_name, appconfig): def _callFUT(self, config_file, section_name, options=None, appconfig=None):
from pyramid.paster import get_appsettings from pyramid.paster import get_appsettings
return get_appsettings(config_file, section_name, appconfig) return get_appsettings(config_file, section_name, options, appconfig)


def test_it(self): def test_it(self):
values = {'a':1} values = {'a':1}
appconfig = DummyLoadWSGI(values) appconfig = DummyLoadWSGI(values)
result = self._callFUT('/foo/bar/myapp.ini', 'myapp', appconfig) result = self._callFUT('/foo/bar/myapp.ini', 'myapp',
appconfig=appconfig)
self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini')
self.assertEqual(appconfig.section_name, 'myapp') self.assertEqual(appconfig.section_name, 'myapp')
self.assertEqual(appconfig.relative_to, os.getcwd()) self.assertEqual(appconfig.relative_to, os.getcwd())
Expand All @@ -72,7 +73,8 @@ def test_it(self):
def test_it_with_hash(self): def test_it_with_hash(self):
values = {'a':1} values = {'a':1}
appconfig = DummyLoadWSGI(values) appconfig = DummyLoadWSGI(values)
result = self._callFUT('/foo/bar/myapp.ini#myapp', None, appconfig) result = self._callFUT('/foo/bar/myapp.ini#myapp', None,
appconfig=appconfig)
self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini')
self.assertEqual(appconfig.section_name, 'myapp') self.assertEqual(appconfig.section_name, 'myapp')
self.assertEqual(appconfig.relative_to, os.getcwd()) self.assertEqual(appconfig.relative_to, os.getcwd())
Expand All @@ -81,7 +83,8 @@ def test_it_with_hash(self):
def test_it_with_hash_and_name_override(self): def test_it_with_hash_and_name_override(self):
values = {'a':1} values = {'a':1}
appconfig = DummyLoadWSGI(values) appconfig = DummyLoadWSGI(values)
result = self._callFUT('/foo/bar/myapp.ini#myapp', 'yourapp', appconfig) result = self._callFUT('/foo/bar/myapp.ini#myapp', 'yourapp',
appconfig=appconfig)
self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini') self.assertEqual(appconfig.config_name, 'config:/foo/bar/myapp.ini')
self.assertEqual(appconfig.section_name, 'yourapp') self.assertEqual(appconfig.section_name, 'yourapp')
self.assertEqual(appconfig.relative_to, os.getcwd()) self.assertEqual(appconfig.relative_to, os.getcwd())
Expand Down Expand Up @@ -181,6 +184,3 @@ def has_section(self, name):


class DummyConfigParserModule(object): class DummyConfigParserModule(object):
ConfigParser = DummyConfigParser ConfigParser = DummyConfigParser



0 comments on commit ea1f057

Please sign in to comment.