From cbdc4b9ad9bdb20a4e0ad17f9d5af26147b771f9 Mon Sep 17 00:00:00 2001 From: adroullier Date: Thu, 19 Oct 2023 17:40:31 +0200 Subject: Adding script name to 'global_config' during application setup as '__script__' --- docs/narr/startup.rst | 4 +++- src/pyramid/scripts/prequest.py | 4 ++++ src/pyramid/scripts/proutes.py | 4 ++++ src/pyramid/scripts/pserve.py | 4 ++++ src/pyramid/scripts/pshell.py | 4 ++++ src/pyramid/scripts/ptweens.py | 4 ++++ src/pyramid/scripts/pviews.py | 4 ++++ tests/test_scripts/test_prequest.py | 7 ++++++- tests/test_scripts/test_pserve.py | 2 +- 9 files changed, 34 insertions(+), 3 deletions(-) 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] - ` is present). It also accepts a + ` 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/scripts/prequest.py b/src/pyramid/scripts/prequest.py index ab5e81dfc..b7f148430 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,9 @@ class PRequestCommand: return 2 config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__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..ec3075be6 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,9 @@ class PRoutesCommand: config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__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 0c4782ce5..48048f893 100644 --- a/src/pyramid/scripts/pserve.py +++ b/src/pyramid/scripts/pserve.py @@ -39,6 +39,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( @@ -183,6 +184,9 @@ class PServeCommand: return 2 config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__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 09baf1736..c5e5c2fb2 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,9 @@ class PShellCommand: config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__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..ca8469169 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,9 @@ class PTweensCommand: return 2 config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__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..da9881434 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,9 @@ class PViewsCommand: return 2 config_uri = self.args.config_uri config_vars = parse_vars(self.args.config_vars) + # bw update 2.1 don't overwrite if set + if not '__script__' in config_vars: + config_vars['__script__'] = self.script_name url = self.args.url self.setup_logging(config_uri, global_conf=config_vars) diff --git a/tests/test_scripts/test_prequest.py b/tests/test_scripts/test_prequest.py index 2214f6350..97786ec83 100644 --- a/tests/test_scripts/test_prequest.py +++ b/tests/test_scripts/test_prequest.py @@ -234,10 +234,15 @@ 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..0b010692a 100644 --- a/tests/test_scripts/test_pserve.py +++ b/tests/test_scripts/test_pserve.py @@ -47,7 +47,7 @@ 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' -- cgit v1.2.3