Skip to content

Commit

Permalink
Adding script name to 'global_config' during application setup as '__…
Browse files Browse the repository at this point in the history
…script__'
  • Loading branch information
adroullier committed Oct 24, 2023
1 parent 3739a77 commit cbdc4b9
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 3 deletions.
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
4 changes: 4 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,9 @@ def run(self):
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)
Expand Down
4 changes: 4 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,9 @@ def run(self, quiet=False):

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)
Expand Down
4 changes: 4 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,9 @@ def run(self): # pragma: no cover
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

Expand Down
4 changes: 4 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,9 @@ def run(self, shell=None):

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)
Expand Down
4 changes: 4 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,9 @@ def run(self):
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']
Expand Down
4 changes: 4 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,9 @@ def run(self):
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)
Expand Down
7 changes: 6 additions & 1 deletion tests/test_scripts/test_prequest.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,15 @@ 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
2 changes: 1 addition & 1 deletion tests/test_scripts/test_pserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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 cbdc4b9

Please sign in to comment.