Mercurial > hg > MakeItSo
changeset 201:65684aae6bfe
make unittest template smarter
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Wed, 09 Jul 2014 15:48:41 -0700 |
parents | 5a2edca13b36 |
children | 2d009a183d53 |
files | makeitso/python.py setup.py |
diffstat | 2 files changed, 99 insertions(+), 91 deletions(-) [+] |
line wrap: on
line diff
--- a/makeitso/python.py Tue Jun 24 11:44:43 2014 -0700 +++ b/makeitso/python.py Wed Jul 09 15:48:41 2014 -0700 @@ -28,126 +28,134 @@ from template import MakeItSoTemplate from template import Variable + class SetupPy(MakeItSoTemplate): """template for setup.py""" templates = [('python_package', 'setup.py')] + class Unittest(MakeItSoTemplate): """template for python unittest""" templates = [('python_package', 'tests', 'test_{{package}}.py')] + def pre(self, variables, output): + if 'package' not in variables: + package = os.path.splitext(os.path.basename(output.rstrip(os.path.sep)))[0] + indicator = 'test_' + if package.startswith(indicator): + package = package[len(indicator):] + variables['package'] = package + class PythonTemplate(MakeItSoTemplate): - """abstract base class for python-type templates""" - vars = [Variable('description'), - Variable('author', 'author of the package'), - Variable('email', "author's email"), - Variable('url', 'project url'), - Variable('repo', 'project repository'), - ] + """abstract base class for python-type templates""" + vars = [Variable('description'), + Variable('author', 'author of the package'), + Variable('email', "author's email"), + Variable('url', 'project url'), + Variable('repo', 'project repository'), + ] - def output2name(self, path): - return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0] + def output2name(self, path): + return os.path.splitext(os.path.basename(path.rstrip(os.path.sep)))[0] class PythonScriptTemplate(PythonTemplate): - """template for a single python script""" - templates = [('python_package', '{{package}}', '{{main}}.py')] - vars = [Variable('description')] + """template for a single python script""" + templates = [('python_package', '{{package}}', '{{main}}.py')] + vars = [Variable('description')] class PythonModuleTemplate(PythonTemplate): - """single module python package""" - # TODO: this should use the same files as PythonPackageTemplate - templates = ['python_module', - ('python_package', '{{package}}', '{{main}}.py')] - vars = [Variable('description')] - look = False + """single module python package""" + + templates = ['python_module', + ('python_package', '{{package}}', '{{main}}.py')] + vars = [Variable('description')] + look = False - def pre(self, variables, output): - variables['project'] = variables['module'] = variables['main'] = self.output2name(output) + def pre(self, variables, output): + variables['project'] = variables['module'] = variables['main'] = self.output2name(output) - def post(self, variables, output): - shutil.move(os.path.join(output, '{{main.py}}'), - os.path.join(output, variables['main'])) + def post(self, variables, output): + shutil.move(os.path.join(output, '{{main.py}}'), + os.path.join(output, variables['main'])) class PythonPackageTemplate(PythonTemplate): - """ - python package template - """ - name = 'python-package' - templates = ['python_package'] - vars = [Variable('description'), - Variable('author', 'author of the package'), - Variable('email', "author's email"), - Variable('url', 'project url'), - Variable('repo', 'project repository'), - ] - look = False - - # things that go in setup.py - dependencies = {'web.py': ['webob'], - 'template.py': ['MakeItSo']} - console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main', - 'template.py': '{{package}}-template = {{package}}.template:main' - } - - def __init__(self, **kw): - MakeItSoTemplate.__init__(self, **kw) - + """ + python package template + """ # TODO: get the templates you actually care about [maybe from the CLI?] - def pre(self, variables, output): - """ - sanitize some variables - """ - - # get project from output directory - variables['project'] = self.output2name(output) + name = 'python-package' + templates = ['python_package'] + vars = [Variable('description'), + Variable('author', 'author of the package'), + Variable('email', "author's email"), + Variable('url', 'project url'), + Variable('repo', 'project repository'), + ] + look = False - # get package name from project - allowable = set(string.letters + string.digits + '_') - if not set(variables['project']).issubset(allowable): - raise AssertionError("Illegal fields in package name") - variables['package'] = variables['project'].lower() + # things that go in setup.py + dependencies = {'web.py': ['webob'], + 'template.py': ['MakeItSo']} + console_scripts = {'main.py': '{{project}} = {{package}}.{{main}}:main', + 'template.py': '{{package}}-template = {{package}}.template:main' + } - # name of CLI main file - variables['main'] = 'main' + def pre(self, variables, output): + """ + sanitize some variables + """ + + # get project from output directory + variables['project'] = self.output2name(output) - # dependencies - dependencies = set([]) - for template, dependency in self.dependencies.items(): - dependencies.update(dependency) - dependencies = list(dependencies) - variables['dependencies'] = dependencies + # get package name from project + allowable = set(string.letters + string.digits + '_') + if not set(variables['project']).issubset(allowable): + raise AssertionError("Illegal fields in package name") + variables['package'] = variables['project'].lower() + + # name of CLI main file + variables['main'] = 'main' + + # package dependencies + dependencies = set([]) + for template, dependency in self.dependencies.items(): + dependencies.update(dependency) + variables['dependencies'] = list(dependencies) - # console_scripts - console_scripts = [] - for template, console_script in self.console_scripts.items(): - console_scripts.append(console_script) - if console_scripts: - s = 'setup(' # placeholder string - script_strings = ['[console_scripts]'] - for console_script in console_scripts: - template = ContentTemplate(console_script) - output = template.substitute(project=variables['project'], - package=variables['package'], - main=variables['main']) - script_strings.append(output) - variables['console_scripts'] = '\n'.join([' ' * len(s) + i - for i in script_strings]) - else: - variables['console_scripts'] = '' + # console_scripts + console_scripts = [] + for template, console_script in self.console_scripts.items(): + console_scripts.append(console_script) + if console_scripts: + s = 'setup(' # placeholder string + script_strings = ['[console_scripts]'] + for console_script in console_scripts: + template = ContentTemplate(console_script) + output = template.substitute(project=variables['project'], + package=variables['package'], + main=variables['main']) + script_strings.append(output) + variables['console_scripts'] = '\n'.join([' ' * len(s) + i + for i in script_strings]) + else: + variables['console_scripts'] = '' class PythonPackageCLI(MakeItSoCLI): - """ - CLI front end for the python package template - """ - usage = '%prog [options] project' + """ + CLI front end for the python package template + """ + usage = '%prog [options] project' + def main(args=sys.argv[1:]): - cli = PythonPackageCLI(PythonPackageTemplate) - cli(*args) + """CLI""" + cli = PythonPackageCLI(PythonPackageTemplate) + cli(*args) if __name__ == '__main__': - main() + main()