summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Merickel <michael@merickel.org>2024-01-27 18:23:23 -0700
committerGitHub <noreply@github.com>2024-01-27 18:23:23 -0700
commit55f9eb0bb0b72fe60ecde7529edbc27aceade187 (patch)
tree1e34614ff9be1c59ad6a2ebb1f4e8bc2a17c6c1d
parent19ae96b97ea055c2e36c8fb86819c426b37ab6af (diff)
parentfd42b0c8d08f69f88e3728b1c1ff3f50071f4b30 (diff)
downloadpyramid-55f9eb0bb0b72fe60ecde7529edbc27aceade187.tar.gz
pyramid-55f9eb0bb0b72fe60ecde7529edbc27aceade187.tar.bz2
pyramid-55f9eb0bb0b72fe60ecde7529edbc27aceade187.zip
Merge pull request #3735 from adroullier/script_info
Adding script name to 'global_config' during application setup
-rw-r--r--CHANGES.rst11
-rw-r--r--docs/narr/startup.rst4
-rw-r--r--src/pyramid/paster.py2
-rw-r--r--src/pyramid/scripts/prequest.py2
-rw-r--r--src/pyramid/scripts/proutes.py2
-rw-r--r--src/pyramid/scripts/pserve.py2
-rw-r--r--src/pyramid/scripts/pshell.py2
-rw-r--r--src/pyramid/scripts/ptweens.py2
-rw-r--r--src/pyramid/scripts/pviews.py2
-rw-r--r--tests/test_paster.py1
-rw-r--r--tests/test_scripts/test_prequest.py9
-rw-r--r--tests/test_scripts/test_pserve.py4
12 files changed, 40 insertions, 3 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index cb9628827..b4a62f94e 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -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
---------
diff --git a/docs/narr/startup.rst b/docs/narr/startup.rst
index 37825d2da..27970ed88 100644
--- a/docs/narr/startup.rst
+++ b/docs/narr/startup.rst
@@ -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
diff --git a/src/pyramid/paster.py b/src/pyramid/paster.py
index f14acacf7..30e4f3e27 100644
--- a/src/pyramid/paster.py
+++ b/src/pyramid/paster.py
@@ -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
diff --git a/src/pyramid/scripts/prequest.py b/src/pyramid/scripts/prequest.py
index ab5e81dfc..c537ae677 100644
--- a/src/pyramid/scripts/prequest.py
+++ b/src/pyramid/scripts/prequest.py
@@ -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),
@@ -136,6 +137,7 @@ class PRequestCommand:
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)
diff --git a/src/pyramid/scripts/proutes.py b/src/pyramid/scripts/proutes.py
index 9c54c06cc..9d419bd8b 100644
--- a/src/pyramid/scripts/proutes.py
+++ b/src/pyramid/scripts/proutes.py
@@ -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
@@ -316,6 +317,7 @@ class PRoutesCommand:
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)
diff --git a/src/pyramid/scripts/pserve.py b/src/pyramid/scripts/pserve.py
index 8eb0ca52f..7504d9a1f 100644
--- a/src/pyramid/scripts/pserve.py
+++ b/src/pyramid/scripts/pserve.py
@@ -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(
@@ -182,6 +183,7 @@ class PServeCommand:
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
diff --git a/src/pyramid/scripts/pshell.py b/src/pyramid/scripts/pshell.py
index f80509fa8..63600abaf 100644
--- a/src/pyramid/scripts/pshell.py
+++ b/src/pyramid/scripts/pshell.py
@@ -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
@@ -130,6 +131,7 @@ class PShellCommand:
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)
diff --git a/src/pyramid/scripts/ptweens.py b/src/pyramid/scripts/ptweens.py
index 494575ff7..cb19eec12 100644
--- a/src/pyramid/scripts/ptweens.py
+++ b/src/pyramid/scripts/ptweens.py
@@ -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,
@@ -81,6 +82,7 @@ class PTweensCommand:
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']
diff --git a/src/pyramid/scripts/pviews.py b/src/pyramid/scripts/pviews.py
index f35daad45..be19e48bc 100644
--- a/src/pyramid/scripts/pviews.py
+++ b/src/pyramid/scripts/pviews.py
@@ -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(
@@ -248,6 +249,7 @@ class PViewsCommand:
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)
diff --git a/tests/test_paster.py b/tests/test_paster.py
index 09e3bb868..e787e6973 100644
--- a/tests/test_paster.py
+++ b/tests/test_paster.py
@@ -159,6 +159,7 @@ class Test_bootstrap(unittest.TestCase):
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)
diff --git a/tests/test_scripts/test_prequest.py b/tests/test_scripts/test_prequest.py
index 2214f6350..4ff6cb34c 100644
--- a/tests/test_scripts/test_prequest.py
+++ b/tests/test_scripts/test_prequest.py
@@ -234,10 +234,17 @@ class TestPRequestCommand(unittest.TestCase):
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):
diff --git a/tests/test_scripts/test_pserve.py b/tests/test_scripts/test_pserve.py
index 5e5c28c7b..a56f25597 100644
--- a/tests/test_scripts/test_pserve.py
+++ b/tests/test_scripts/test_pserve.py
@@ -47,7 +47,9 @@ class TestPServeCommand(unittest.TestCase):
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'