Mercurial > hg > MakeItSo
annotate makeitso/python.py @ 168:54f42aa651bc
fix single module interpolation
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Thu, 31 Oct 2013 17:38:39 -0700 |
parents | 0c95d7f25d9b |
children | c3f719824948 |
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 |
164
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
163
diff
changeset
|
19 # TODO: console_scripts for all of these |
6cd2894bb11c
stub: makeitso/script2package.py
Jeff Hammel <jhammel@mozilla.com>
parents:
163
diff
changeset
|
20 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
21 import os |
168
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
22 import shutil |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
23 import sys |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
24 from cli import MakeItSoCLI |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
25 from makeitso import ContentTemplate |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
26 from optparse import OptionParser |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
27 from template import MakeItSoTemplate |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
28 from template import Variable |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
29 |
163 | 30 |
151 | 31 class PythonTemplate(MakeItSoTemplate): |
32 """abstract base class for python-type templates""" | |
160 | 33 vars = [Variable('description'), |
34 Variable('author', 'author of the package'), | |
35 Variable('email', "author's email"), | |
36 Variable('url', 'project url'), | |
37 Variable('repo', 'project repository'), | |
38 ] | |
151 | 39 |
161 | 40 def output2name(self, path): |
41 return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0] | |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
42 |
162 | 43 |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
44 class PythonScriptTemplate(PythonTemplate): |
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
45 """template for a single python script""" |
159 | 46 templates = [('python_package', '{{package}}', '{{main}}.py')] |
160 | 47 vars = [Variable('description')] |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
48 |
162 | 49 |
151 | 50 class PythonModuleTemplate(PythonTemplate): |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
51 """single module python package""" |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
52 # TODO: this should use the same files as PythonPackageTemplate |
159 | 53 templates = ['python_module', |
54 ('python_package', '{{package}}', '{{main}}.py')] | |
55 vars = [Variable('description')] | |
56 look = False | |
131
4922bee3d080
add single module to registered templates
Jeff Hammel <jhammel@mozilla.com>
parents:
122
diff
changeset
|
57 |
159 | 58 def pre(self, variables, output): |
162 | 59 variables['project'] = variables['module'] = variables['main'] = self.output2name(output) |
60 | |
168
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
61 def post(self, variables, output): |
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
62 shutil.move(os.path.join(output, '{{main.py}}'), |
54f42aa651bc
fix single module interpolation
Jeff Hammel <jhammel@mozilla.com>
parents:
166
diff
changeset
|
63 os.path.join(output, variables['main'])) |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
64 |
151 | 65 class PythonPackageTemplate(PythonTemplate): |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
66 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
67 python package template |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
68 """ |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
69 name = 'python-package' |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
70 templates = ['python_package'] |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
71 vars = [Variable('description'), |
115
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
72 Variable('author', 'author of the package'), |
7dbc3cdadffe
add a test for the python-package template
Jeff Hammel <jhammel@mozilla.com>
parents:
114
diff
changeset
|
73 Variable('email', "author's email"), |
159 | 74 Variable('url', 'project url'), |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
75 Variable('repo', 'project repository'), |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
76 ] |
111 | 77 look = False |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
78 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
79 # things that go in setup.py |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
80 dependencies = {'web.py': ['webob'], |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
81 'template.py': ['MakeItSo']} |
166 | 82 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main', |
83 'template.py': '{{package}}-template = {{package}}.template:main' | |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
84 } |
151 | 85 |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
86 def __init__(self, **kw): |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
87 MakeItSoTemplate.__init__(self, **kw) |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
88 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
89 # 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
|
90 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
91 def pre(self, variables, output): |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
92 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
93 sanitize some variables |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
94 """ |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
95 |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
96 # get project from output directory |
161 | 97 variables['project'] = self.output2name(output) |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
98 |
114
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
99 # get package name from project |
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
100 # 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
|
101 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
|
102 variables['package'] = variables['project'].lower() |
b8d5d2041fe0
stub out project name -> package name
Jeff Hammel <jhammel@mozilla.com>
parents:
111
diff
changeset
|
103 |
159 | 104 # name of CLI main file |
165 | 105 variables['main'] = 'main' |
159 | 106 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
107 # dependencies |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
108 dependencies = set([]) |
111 | 109 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
|
110 dependencies.update(dependency) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
111 dependencies = list(dependencies) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
112 variables['dependencies'] = dependencies |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
113 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
114 # console_scripts |
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
115 console_scripts = [] |
111 | 116 for template, console_script in self.console_scripts.items(): |
117 console_scripts.append(console_script) | |
109
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
118 if console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
119 s = 'setup(' # placeholder string |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
120 script_strings = ['[console_scripts]'] |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
121 for console_script in console_scripts: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
122 template = ContentTemplate(console_script) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
123 output = template.substitute(project=variables['project']) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
124 script_strings.append(output) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
125 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
|
126 for i in script_strings]) |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
127 else: |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
128 variables['console_scripts'] = '' |
697568ba4a22
make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents:
108
diff
changeset
|
129 |
108
32893f67f85d
stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents:
102
diff
changeset
|
130 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
131 class PythonPackageCLI(MakeItSoCLI): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
132 """ |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
133 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
|
134 """ |
122
b2152efec89a
get the description from the docstring if applicable
Jeff Hammel <jhammel@mozilla.com>
parents:
116
diff
changeset
|
135 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
|
136 |
102
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
137 def main(args=sys.argv[1:]): |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
138 cli = PythonPackageCLI(PythonPackageTemplate) |
ad5fd3eb6674
template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents:
95
diff
changeset
|
139 cli(*args) |
78
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
140 |
d4184945f8a8
stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff
changeset
|
141 if __name__ == '__main__': |
155
386a44a52139
moving to a thing with script template
Jeff Hammel <jhammel@mozilla.com>
parents:
151
diff
changeset
|
142 main() |