Skip to content

Commit

Permalink
Merge pull request #3735 from adroullier/script_info
Browse files Browse the repository at this point in the history
Adding script name to 'global_config' during application setup
  • Loading branch information
mmerickel authored Jan 28, 2024
2 parents 19ae96b + fd42b0c commit 55f9eb0
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 3 deletions.
11 changes: 11 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ Features

- Coverage reports in tests based on Python 3.12 instead of Python 3.8.

- All scripts now pass a new option ``__script__`` when loading the WSGI app.
For example, ``pserve`` sets ``__script__ == 'pserve'``. This works for
``pserve``, ``pshell``, ``prequest``, ``proutes``, ``ptweens``, ``pviews``,
as well as when using ``pyramid.paster.bootstrap`` directly.

When using ``plaster-pastedeploy`` to load an INI file, this option will
manifest as a new value passed into the ``global_conf`` arg of your
application factory, where you can use it as part of initializing your app.

See https://github.com/Pylons/pyramid/pull/3735

Bug Fixes
---------

Expand Down
4 changes: 3 additions & 1 deletion docs/narr/startup.rst
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ Here's a high-level time-ordered overview of what happens when you press
Note that the constructor function accepts a ``global_config`` argument,
which is a dictionary of key/value pairs mentioned in the ``[DEFAULT]``
section of an ``.ini`` file (if :ref:`[DEFAULT]
<defaults_section_of_pastedeploy_file>` is present). It also accepts a
<defaults_section_of_pastedeploy_file>` is present) and the executing
script name ``__script__`` like ``pserve``, ``prequest`` or ``pshell``.
It also accepts a
``**settings`` argument, which collects another set of arbitrary key/value
pairs. The arbitrary key/value pairs received by this function in
``**settings`` will be composed of all the key/value pairs that are present
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/paster.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ def bootstrap(config_uri, request=None, options=None):
by the ``closer``.
"""
options = dict(options or {})
options.setdefault('__script__', 'bootstrap')
app = get_app(config_uri, options=options)
env = prepare(request)
env['app'] = app
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/prequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PRequestCommand:
the request's WSGI environment, so your application can distinguish these
calls from normal requests.
"""
script_name = 'prequest'

parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
Expand Down Expand Up @@ -136,6 +137,7 @@ def run(self):
return 2
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
path = self.args.path_info

loader = self._get_config_loader(config_uri)
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/proutes.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ class PRoutesCommand:
will be assumed. Example: 'proutes myapp.ini'.
"""
script_name = 'proutes'
bootstrap = staticmethod(bootstrap) # testing
get_config_loader = staticmethod(get_config_loader) # testing
stdout = sys.stdout
Expand Down Expand Up @@ -316,6 +317,7 @@ def run(self, quiet=False):

config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
loader = self.get_config_loader(config_uri)
loader.setup_logging(config_vars)
self.proutes_file_config(loader, config_vars)
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PServeCommand:
You can also include variable assignments like 'http_port=8080'
and then use %(http_port)s in your config files.
"""
script_name = 'pserve'
default_verbosity = 1

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -182,6 +183,7 @@ def run(self): # pragma: no cover
return 2
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
app_spec = self.args.config_uri
app_name = self.args.app_name

Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/pshell.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class PShellCommand:
than one Pyramid application within it, the loader will use the
last one.
"""
script_name = 'pshell'
bootstrap = staticmethod(bootstrap) # for testing
get_config_loader = staticmethod(get_config_loader) # for testing
pkg_resources = pkg_resources # for testing
Expand Down Expand Up @@ -130,6 +131,7 @@ def run(self, shell=None):

config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
loader = self.get_config_loader(config_uri)
loader.setup_logging(config_vars)
self.pshell_file_config(loader, config_vars)
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/ptweens.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PTweensCommand:
will be assumed. Example: "ptweens myapp.ini#main".
"""
script_name = 'ptweens'
parser = argparse.ArgumentParser(
description=textwrap.dedent(description),
formatter_class=argparse.RawDescriptionHelpFormatter,
Expand Down Expand Up @@ -81,6 +82,7 @@ def run(self):
return 2
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
self.setup_logging(config_uri, global_conf=config_vars)
env = self.bootstrap(config_uri, options=config_vars)
registry = env['registry']
Expand Down
2 changes: 2 additions & 0 deletions src/pyramid/scripts/pviews.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class PViewsCommand:
specifies the path info portion of a URL that will be used to find
matching views. Example: 'proutes myapp.ini#main /url'
"""
script_name = 'pviews'
stdout = sys.stdout

parser = argparse.ArgumentParser(
Expand Down Expand Up @@ -248,6 +249,7 @@ def run(self):
return 2
config_uri = self.args.config_uri
config_vars = parse_vars(self.args.config_vars)
config_vars.setdefault('__script__', self.script_name)
url = self.args.url

self.setup_logging(config_uri, global_conf=config_vars)
Expand Down
1 change: 1 addition & 0 deletions tests/test_paster.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ def test_it_request_with_registry(self):
result = self._callFUT('/foo/bar/myapp.ini', request)
self.assertEqual(result['app'], self.app)
self.assertEqual(result['root'], self.root)
self.assertEqual(self.get_app.kw['options']['__script__'], 'bootstrap')
self.assertTrue('closer' in result)


Expand Down
9 changes: 8 additions & 1 deletion tests/test_scripts/test_prequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,17 @@ def test_command_response_has_no_charset(self):
self.assertEqual(self._out, [b'abc'])

def test_command_method_configures_logging(self):
command = self._makeOne(['', 'development.ini', '/'])
command = self._makeOne(['', '--method=GET', 'development.ini', '/'])
command.run()
self.assertEqual(self.loader.calls[0]['op'], 'logging')

def test_command_script_name(self):
command = self._makeOne(['', '--method=GET', 'development.ini', '/'])
command.run()
self.assertEqual(
self.loader.calls[0]['defaults']['__script__'], 'prequest'
)


class Test_main(unittest.TestCase):
def _callFUT(self, argv):
Expand Down
4 changes: 3 additions & 1 deletion tests/test_scripts/test_pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ def get_app(name, global_conf):
self.loader.server = lambda x: x

inst.run()
self.assertEqual(app.global_conf, {'a': '1', 'b': '2'})
self.assertEqual(
app.global_conf, {'a': '1', 'b': '2', '__script__': 'pserve'}
)

def test_original_ignore_files(self):
msg = 'A change to "ignore_files" was detected'
Expand Down

0 comments on commit 55f9eb0

Please sign in to comment.