Mercurial > hg > MakeItSo
annotate makeitso/python.py @ 155:386a44a52139
moving to a thing with script template
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 05 Jun 2013 13:38:23 -0700 |
parents | e70421c50d0a |
children | cfd4f1e91090 |
rev | line source |
---|---|
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
2 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
3 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
4 python package templates for makeitso |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
5 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
6 Several components are included. |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
7 [TODO] You may use these subtemplates in any combination. |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
8 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
9 * README.txt : a README in restructured text |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
10 * examples : examples for your package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
11 * setup.py : setup utility for the full package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
12 * ./main.py : CLI handler for your webapp |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
13 * ./model.py : model of a persisted object |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
14 * ./template.py : a MakeItSo template for project creation |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
15 * ./tests : doctest suite for the package |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
16 * ./web.py : a webob web handler |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
17 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
18 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
19 import os |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
20 import sys |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
21 from cli import MakeItSoCLI |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
22 from makeitso import ContentTemplate |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 from optparse import OptionParser |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 from template import MakeItSoTemplate |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
25 from template import Variable |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 |
151 | 27 class PythonTemplate(MakeItSoTemplate): |
28 """abstract base class for python-type templates""" | |
29 | |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
30 |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
31 class PythonScriptTemplate(PythonTemplate): |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
32 """template for a single python script""" |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
33 templates = [os.path.join('python_package', '{{package}}', 'main.py')] |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
34 |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
35 |
151 | 36 class PythonModuleTemplate(PythonTemplate): |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
37 """single module python package""" |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
38 # TODO: this should use the same files as PythonPackageTemplate |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
39 templates = ['python_module'] |
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
40 |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
41 |
151 | 42 class PythonPackageTemplate(PythonTemplate): |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
43 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
44 python package template |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
45 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
46 name = 'python-package' |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
47 templates = ['python_package'] |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
48 vars = [Variable('description'), |
115
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
49 Variable('author', 'author of the package'), |
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
50 Variable('email', "author's email"), |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
51 Variable('url'), |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
52 Variable('repo', 'project repository'), |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
53 ] |
111 | 54 look = False |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
55 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
56 # things that go in setup.py |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
57 dependencies = {'web.py': ['webob'], |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
58 'template.py': ['MakeItSo']} |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
59 console_scripts = {'main.py': '{{project}} = {{project}}.main:main', |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
60 'template.py': '{{project}}-template = {{project}}.template:main' |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
61 } |
151 | 62 |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
63 def __init__(self, **kw): |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
64 MakeItSoTemplate.__init__(self, **kw) |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
65 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
66 # TODO: get the templates you actually care about [maybe from the CLI?] |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
67 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
68 def pre(self, variables, output): |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
69 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
70 sanitize some variables |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
71 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
72 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
73 # get project from output directory |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
74 variables['project'] = os.path.basename(output) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
75 |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
76 # get package name from project |
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
77 # XXX could have looser restrictions with transforms |
116
908e9a653668
fix a syntax error in the python template thanks to some friendly tests
Jeff Hammel <jhammel@mozilla.com>
parents:
115
diff
changeset
|
78 assert variables['project'].isalnum(), 'Project name must be just letters, you gave %s' % variables['project'] |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
79 variables['package'] = variables['project'].lower() |
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
80 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
81 # dependencies |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
82 dependencies = set([]) |
111 | 83 for template, dependency in self.dependencies.items(): |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
84 dependencies.update(dependency) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
85 dependencies = list(dependencies) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
86 variables['dependencies'] = dependencies |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
87 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
88 # console_scripts |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
89 console_scripts = [] |
111 | 90 for template, console_script in self.console_scripts.items(): |
91 console_scripts.append(console_script) | |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
92 if console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
93 s = 'setup(' # placeholder string |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
94 script_strings = ['[console_scripts]'] |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
95 for console_script in console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
96 template = ContentTemplate(console_script) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
97 output = template.substitute(project=variables['project']) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
98 script_strings.append(output) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
99 variables['console_scripts'] = '\n'.join([' ' * len(s) + i |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
100 for i in script_strings]) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
101 else: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
102 variables['console_scripts'] = '' |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
103 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
104 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
105 class PythonPackageCLI(MakeItSoCLI): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
106 """ |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
107 CLI front end for the python package template |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
108 """ |
122
b2152efec89a
get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents:
116
diff
changeset
|
109 usage = '%prog [options] project' |
95
e74baa8e6df4
fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents:
78
diff
changeset
|
110 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
111 def main(args=sys.argv[1:]): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
112 cli = PythonPackageCLI(PythonPackageTemplate) |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
113 cli(*args) |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
114 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
115 if __name__ == '__main__': |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
116 main() |