Michael Merickel
2018-10-15 0c29cf2df41600d3906d521c72991c7686018b71
commit | author | age
bb69a0 1 import binascii
810942 2 import os
4e2f1f 3 from textwrap import dedent
810942 4
fe219c 5 from pyramid.compat import native_
5e21d5 6
24362a 7 from pyramid.scaffolds.template import Template  # API
810942 8
0c29cf 9
810942 10 class PyramidTemplate(Template):
bfd4b3 11     """
CM 12      A class that can be used as a base class for Pyramid scaffolding
13      templates.
14     """
0c29cf 15
810942 16     def pre(self, command, output_dir, vars):
aaedf5 17         """ Overrides :meth:`pyramid.scaffolds.template.Template.pre`, adding
bfd4b3 18         several variables to the default variables list (including
CM 19         ``random_string``, and ``package_logger``).  It also prevents common
20         misnamings (such as naming a package "site" or naming a package
21         logger "root".
22         """
bb69a0 23         vars['random_string'] = native_(binascii.hexlify(os.urandom(20)))
810942 24         package_logger = vars['package']
CM 25         if package_logger == 'root':
26             # Rename the app logger in the rare case a project is named 'root'
27             package_logger = 'app'
28         vars['package_logger'] = package_logger
29         return Template.pre(self, command, output_dir, vars)
30
0c29cf 31     def post(self, command, output_dir, vars):  # pragma: no cover
aaedf5 32         """ Overrides :meth:`pyramid.scaffolds.template.Template.post`, to
bfd4b3 33         print "Welcome to Pyramid.  Sorry for the convenience." after a
CM 34         successful scaffolding rendering."""
4e2f1f 35
GC 36         separator = "=" * 79
37         msg = dedent(
38             """
39             %(separator)s
19d341 40             Tutorials:     https://docs.pylonsproject.org/projects/pyramid_tutorials/en/latest/
SP 41             Documentation: https://docs.pylonsproject.org/projects/pyramid/en/latest/
6c38f2 42             Twitter:       https://twitter.com/PylonsProject
a95408 43             Mailing List:  https://groups.google.com/forum/#!forum/pylons-discuss
4e2f1f 44
GC 45             Welcome to Pyramid.  Sorry for the convenience.
46             %(separator)s
0c29cf 47         """
MM 48             % {'separator': separator}
49         )
4e2f1f 50
GC 51         self.out(msg)
810942 52         return Template.post(self, command, output_dir, vars)
CM 53
0c29cf 54     def out(self, msg):  # pragma: no cover (replaceable testing hook)
fe219c 55         print(msg)
0c29cf 56
810942 57
CM 58 class StarterProjectTemplate(PyramidTemplate):
59     _template_dir = 'starter'
00a16d 60     summary = 'Pyramid starter project using URL dispatch and Jinja2'
810942 61
0c29cf 62
810942 63 class ZODBProjectTemplate(PyramidTemplate):
CM 64     _template_dir = 'zodb'
a95408 65     summary = 'Pyramid project using ZODB, traversal, and Chameleon'
810942 66
0c29cf 67
810942 68 class AlchemyProjectTemplate(PyramidTemplate):
CM 69     _template_dir = 'alchemy'
24362a 70     summary = (
0c29cf 71         'Pyramid project using SQLAlchemy, SQLite, URL dispatch, and ' 'Jinja2'
MM 72     )