Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding script name to 'global_config' during application setup #3735

Merged
merged 8 commits into from
Jan 28, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/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 @@ -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(
Expand Down Expand Up @@ -183,6 +184,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
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