comparison makeitso/python.py @ 201:65684aae6bfe

make unittest template smarter
author Jeff Hammel <k0scist@gmail.com>
date Wed, 09 Jul 2014 15:48:41 -0700
parents 5a2edca13b36
children a1a615aae3d1
comparison
equal deleted inserted replaced
200:5a2edca13b36 201:65684aae6bfe
26 from makeitso import ContentTemplate 26 from makeitso import ContentTemplate
27 from optparse import OptionParser 27 from optparse import OptionParser
28 from template import MakeItSoTemplate 28 from template import MakeItSoTemplate
29 from template import Variable 29 from template import Variable
30 30
31
31 class SetupPy(MakeItSoTemplate): 32 class SetupPy(MakeItSoTemplate):
32 """template for setup.py""" 33 """template for setup.py"""
33 templates = [('python_package', 'setup.py')] 34 templates = [('python_package', 'setup.py')]
34 35
36
35 class Unittest(MakeItSoTemplate): 37 class Unittest(MakeItSoTemplate):
36 """template for python unittest""" 38 """template for python unittest"""
37 templates = [('python_package', 'tests', 'test_{{package}}.py')] 39 templates = [('python_package', 'tests', 'test_{{package}}.py')]
40 def pre(self, variables, output):
41 if 'package' not in variables:
42 package = os.path.splitext(os.path.basename(output.rstrip(os.path.sep)))[0]
43 indicator = 'test_'
44 if package.startswith(indicator):
45 package = package[len(indicator):]
46 variables['package'] = package
47
38 48
39 class PythonTemplate(MakeItSoTemplate): 49 class PythonTemplate(MakeItSoTemplate):
40 """abstract base class for python-type templates""" 50 """abstract base class for python-type templates"""
41 vars = [Variable('description'), 51 vars = [Variable('description'),
42 Variable('author', 'author of the package'), 52 Variable('author', 'author of the package'),
43 Variable('email', "author's email"), 53 Variable('email', "author's email"),
44 Variable('url', 'project url'), 54 Variable('url', 'project url'),
45 Variable('repo', 'project repository'), 55 Variable('repo', 'project repository'),
46 ] 56 ]
47 57
48 def output2name(self, path): 58 def output2name(self, path):
49 return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0] 59 return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0]
50 60
51 61
52 class PythonScriptTemplate(PythonTemplate): 62 class PythonScriptTemplate(PythonTemplate):
53 """template for a single python script""" 63 """template for a single python script"""
54 templates = [('python_package', '{{package}}', '{{main}}.py')] 64 templates = [('python_package', '{{package}}', '{{main}}.py')]
55 vars = [Variable('description')] 65 vars = [Variable('description')]
56 66
57 67
58 class PythonModuleTemplate(PythonTemplate): 68 class PythonModuleTemplate(PythonTemplate):
59 """single module python package""" 69 """single module python package"""
60 # TODO: this should use the same files as PythonPackageTemplate
61 templates = ['python_module',
62 ('python_package', '{{package}}', '{{main}}.py')]
63 vars = [Variable('description')]
64 look = False
65 70
66 def pre(self, variables, output): 71 templates = ['python_module',
67 variables['project'] = variables['module'] = variables['main'] = self.output2name(output) 72 ('python_package', '{{package}}', '{{main}}.py')]
73 vars = [Variable('description')]
74 look = False
68 75
69 def post(self, variables, output): 76 def pre(self, variables, output):
70 shutil.move(os.path.join(output, '{{main.py}}'), 77 variables['project'] = variables['module'] = variables['main'] = self.output2name(output)
71 os.path.join(output, variables['main'])) 78
79 def post(self, variables, output):
80 shutil.move(os.path.join(output, '{{main.py}}'),
81 os.path.join(output, variables['main']))
72 82
73 class PythonPackageTemplate(PythonTemplate): 83 class PythonPackageTemplate(PythonTemplate):
74 """ 84 """
75 python package template 85 python package template
76 """ 86 """
77 name = 'python-package'
78 templates = ['python_package']
79 vars = [Variable('description'),
80 Variable('author', 'author of the package'),
81 Variable('email', "author's email"),
82 Variable('url', 'project url'),
83 Variable('repo', 'project repository'),
84 ]
85 look = False
86
87 # things that go in setup.py
88 dependencies = {'web.py': ['webob'],
89 'template.py': ['MakeItSo']}
90 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main',
91 'template.py': '{{package}}-template = {{package}}.template:main'
92 }
93
94 def __init__(self, **kw):
95 MakeItSoTemplate.__init__(self, **kw)
96
97 # TODO: get the templates you actually care about [maybe from the CLI?] 87 # TODO: get the templates you actually care about [maybe from the CLI?]
98 88
99 def pre(self, variables, output): 89 name = 'python-package'
100 """ 90 templates = ['python_package']
101 sanitize some variables 91 vars = [Variable('description'),
102 """ 92 Variable('author', 'author of the package'),
93 Variable('email', "author's email"),
94 Variable('url', 'project url'),
95 Variable('repo', 'project repository'),
96 ]
97 look = False
103 98
104 # get project from output directory 99 # things that go in setup.py
105 variables['project'] = self.output2name(output) 100 dependencies = {'web.py': ['webob'],
101 'template.py': ['MakeItSo']}
102 console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main',
103 'template.py': '{{package}}-template = {{package}}.template:main'
104 }
106 105
107 # get package name from project 106 def pre(self, variables, output):
108 allowable = set(string.letters + string.digits + '_') 107 """
109 if not set(variables['project']).issubset(allowable): 108 sanitize some variables
110 raise AssertionError("Illegal fields in package name") 109 """
111 variables['package'] = variables['project'].lower()
112 110
113 # name of CLI main file 111 # get project from output directory
114 variables['main'] = 'main' 112 variables['project'] = self.output2name(output)
115 113
116 # dependencies 114 # get package name from project
117 dependencies = set([]) 115 allowable = set(string.letters + string.digits + '_')
118 for template, dependency in self.dependencies.items(): 116 if not set(variables['project']).issubset(allowable):
119 dependencies.update(dependency) 117 raise AssertionError("Illegal fields in package name")
120 dependencies = list(dependencies) 118 variables['package'] = variables['project'].lower()
121 variables['dependencies'] = dependencies
122 119
123 # console_scripts 120 # name of CLI main file
124 console_scripts = [] 121 variables['main'] = 'main'
125 for template, console_script in self.console_scripts.items(): 122
126 console_scripts.append(console_script) 123 # package dependencies
127 if console_scripts: 124 dependencies = set([])
128 s = 'setup(' # placeholder string 125 for template, dependency in self.dependencies.items():
129 script_strings = ['[console_scripts]'] 126 dependencies.update(dependency)
130 for console_script in console_scripts: 127 variables['dependencies'] = list(dependencies)
131 template = ContentTemplate(console_script) 128
132 output = template.substitute(project=variables['project'], 129 # console_scripts
133 package=variables['package'], 130 console_scripts = []
134 main=variables['main']) 131 for template, console_script in self.console_scripts.items():
135 script_strings.append(output) 132 console_scripts.append(console_script)
136 variables['console_scripts'] = '\n'.join([' ' * len(s) + i 133 if console_scripts:
137 for i in script_strings]) 134 s = 'setup(' # placeholder string
138 else: 135 script_strings = ['[console_scripts]']
139 variables['console_scripts'] = '' 136 for console_script in console_scripts:
137 template = ContentTemplate(console_script)
138 output = template.substitute(project=variables['project'],
139 package=variables['package'],
140 main=variables['main'])
141 script_strings.append(output)
142 variables['console_scripts'] = '\n'.join([' ' * len(s) + i
143 for i in script_strings])
144 else:
145 variables['console_scripts'] = ''
140 146
141 147
142 class PythonPackageCLI(MakeItSoCLI): 148 class PythonPackageCLI(MakeItSoCLI):
143 """ 149 """
144 CLI front end for the python package template 150 CLI front end for the python package template
145 """ 151 """
146 usage = '%prog [options] project' 152 usage = '%prog [options] project'
153
147 154
148 def main(args=sys.argv[1:]): 155 def main(args=sys.argv[1:]):
149 cli = PythonPackageCLI(PythonPackageTemplate) 156 """CLI"""
150 cli(*args) 157 cli = PythonPackageCLI(PythonPackageTemplate)
158 cli(*args)
151 159
152 if __name__ == '__main__': 160 if __name__ == '__main__':
153 main() 161 main()