annotate makeitso/python.py @ 113:c3b8ce33d3ad

make variable getting logic a little less horrible
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 17 Jan 2011 14:32:07 -0800
parents bad70fb08b15
children b8d5d2041fe0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
27 class PythonPackageTemplate(MakeItSoTemplate):
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 python package template
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 """
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 name = 'python-package'
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 templates = ['python_package']
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
33 vars = [Variable('description'),
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
34 Variable('url'),
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
35 Variable('repo', 'project repository'),
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
36 ]
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
37 look = False
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 # things that go in setup.py
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40 dependencies = {'web.py': ['webob'],
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
41 'template.py': ['MakeItSo']}
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
42 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
43 'template.py': '{{project}}-template = {{project}}.template:main'
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 }
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 def __init__(self, **kw):
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 MakeItSoTemplate.__init__(self, **kw)
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
49 # 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
50
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
51 def pre(self, variables, output):
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
52 """
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
53 sanitize some variables
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
54 """
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
55
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
56 # get project from output directory
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
57 variables['project'] = os.path.basename(output)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
58
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
59 # dependencies
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
60 dependencies = set([])
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
61 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
62 dependencies.update(dependency)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
63 dependencies = list(dependencies)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
64 variables['dependencies'] = dependencies
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
65
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
66 # console_scripts
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
67 console_scripts = []
111
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
68 for template, console_script in self.console_scripts.items():
bad70fb08b15 minor syntax corrections
Jeff Hammel <jhammel@mozilla.com>
parents: 109
diff changeset
69 console_scripts.append(console_script)
109
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
70 if console_scripts:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
71 s = 'setup(' # placeholder string
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
72 script_strings = ['[console_scripts]']
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
73 for console_script in console_scripts:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
74 template = ContentTemplate(console_script)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
75 output = template.substitute(project=variables['project'])
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
76 script_strings.append(output)
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
77 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
78 for i in script_strings])
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
79 else:
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
80 variables['console_scripts'] = ''
697568ba4a22 make the python package template a little fancier
Jeff Hammel <jhammel@mozilla.com>
parents: 108
diff changeset
81
108
32893f67f85d stub out a better python package
Jeff Hammel <jhammel@mozilla.com>
parents: 102
diff changeset
82
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
83 class PythonPackageCLI(MakeItSoCLI):
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
84 """
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
85 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
86 """
95
e74baa8e6df4 fix CLI interface a bit....write a test for it
Jeff Hammel <jhammel@mozilla.com>
parents: 78
diff changeset
87
102
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
88 def main(args=sys.argv[1:]):
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
89 cli = PythonPackageCLI(PythonPackageTemplate)
ad5fd3eb6674 template fixes....not the best, but will do
Jeff Hammel <jhammel@mozilla.com>
parents: 95
diff changeset
90 cli(*args)
78
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
91
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
92 if __name__ == '__main__':
d4184945f8a8 stub out python package creation
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
93 main()