Michael Merickel
2018-10-15 0c29cf2df41600d3906d521c72991c7686018b71
commit | author | age
7dd421 1 import argparse
337960 2 import sys
d58614 3 import textwrap
337960 4
CM 5 from pyramid.interfaces import ITweens
6
7 from pyramid.tweens import MAIN
8 from pyramid.tweens import INGRESS
9 from pyramid.paster import bootstrap
678790 10 from pyramid.paster import setup_logging
49fb77 11 from pyramid.scripts.common import parse_vars
337960 12
0c29cf 13
d29151 14 def main(argv=sys.argv, quiet=False):
CM 15     command = PTweensCommand(argv, quiet)
d58614 16     return command.run()
0c29cf 17
337960 18
CM 19 class PTweensCommand(object):
d58614 20     description = """\
CM 21     Print all implicit and explicit tween objects used by a Pyramid
22     application.  The handler output includes whether the system is using an
23     explicit tweens ordering (will be true when the "pyramid.tweens"
24     deployment setting is used) or an implicit tweens ordering (will be true
25     when the "pyramid.tweens" deployment setting is *not* used).
337960 26
d58614 27     This command accepts one positional argument named "config_uri" which
CM 28     specifies the PasteDeploy config file to use for the interactive
29     shell. The format is "inifile#name". If the name is left off, "main"
30     will be assumed.  Example: "ptweens myapp.ini#main".
337960 31
CM 32     """
7dd421 33     parser = argparse.ArgumentParser(
d58614 34         description=textwrap.dedent(description),
c9b2fa 35         formatter_class=argparse.RawDescriptionHelpFormatter,
0c29cf 36     )
7dd421 37
0c29cf 38     parser.add_argument(
MM 39         'config_uri',
40         nargs='?',
41         default=None,
42         help='The URI to the configuration file.',
43     )
d58614 44
ba9313 45     parser.add_argument(
SP 46         'config_vars',
47         nargs='*',
48         default=(),
6721ff 49         help="Variables required by the config file. For example, "
0c29cf 50         "`http_port=%%(http_port)s` would expect `http_port=8080` to be "
MM 51         "passed here.",
52     )
ba9313 53
337960 54     stdout = sys.stdout
0c29cf 55     bootstrap = staticmethod(bootstrap)  # testing
MM 56     setup_logging = staticmethod(setup_logging)  # testing
337960 57
d29151 58     def __init__(self, argv, quiet=False):
CM 59         self.quiet = quiet
7dd421 60         self.args = self.parser.parse_args(argv[1:])
337960 61
CM 62     def _get_tweens(self, registry):
63         from pyramid.config import Configurator
0c29cf 64
25c64c 65         config = Configurator(registry=registry)
337960 66         return config.registry.queryUtility(ITweens)
CM 67
0c29cf 68     def out(self, msg):  # pragma: no cover
d29151 69         if not self.quiet:
5cf9fc 70             print(msg)
337960 71
CM 72     def show_chain(self, chain):
73         fmt = '%-10s  %-65s'
74         self.out(fmt % ('Position', 'Name'))
25c64c 75         self.out(fmt % ('-' * len('Position'), '-' * len('Name')))
337960 76         self.out(fmt % ('-', INGRESS))
CM 77         for pos, (name, _) in enumerate(chain):
78             self.out(fmt % (pos, name))
79         self.out(fmt % ('-', MAIN))
80
81     def run(self):
ba9313 82         if not self.args.config_uri:
d29151 83             self.out('Requires a config file argument')
d58614 84             return 2
ba9313 85         config_uri = self.args.config_uri
678790 86         config_vars = parse_vars(self.args.config_vars)
MM 87         self.setup_logging(config_uri, global_conf=config_vars)
88         env = self.bootstrap(config_uri, options=config_vars)
337960 89         registry = env['registry']
CM 90         tweens = self._get_tweens(registry)
91         if tweens is not None:
92             explicit = tweens.explicit
93             if explicit:
0c29cf 94                 self.out(
MM 95                     '"pyramid.tweens" config value set '
96                     '(explicitly ordered tweens used)'
97                 )
337960 98                 self.out('')
CM 99                 self.out('Explicit Tween Chain (used)')
100                 self.out('')
101                 self.show_chain(tweens.explicit)
102                 self.out('')
103                 self.out('Implicit Tween Chain (not used)')
104                 self.out('')
105                 self.show_chain(tweens.implicit())
106             else:
0c29cf 107                 self.out(
MM 108                     '"pyramid.tweens" config value NOT set '
109                     '(implicitly ordered tweens used)'
110                 )
337960 111                 self.out('')
CM 112                 self.out('Implicit Tween Chain')
113                 self.out('')
114                 self.show_chain(tweens.implicit())
d58614 115         return 0
40d54e 116
0c29cf 117
MM 118 if __name__ == '__main__':  # pragma: no cover
40d54e 119     sys.exit(main() or 0)