From 5371e702786fe6c9415cfee4aa5aee35897895bc Mon Sep 17 00:00:00 2001 From: allfro Date: Wed, 10 Oct 2012 12:07:36 -0400 Subject: [PATCH] updated a few canari commands --- src/canari/commands/common.py | 38 ++++++++++++++- src/canari/commands/create_package.py | 64 ++++++++++++++++++++----- src/canari/commands/create_transform.py | 9 ++-- src/canari/commands/help.py | 2 + src/scripts/canari | 3 +- 5 files changed, 96 insertions(+), 20 deletions(-) diff --git a/src/canari/commands/common.py b/src/canari/commands/common.py index 94c358a..7e19c3d 100644 --- a/src/canari/commands/common.py +++ b/src/canari/commands/common.py @@ -1,10 +1,14 @@ #!/usr/bin/env python -from os import path, listdir, sep, environ, mkdir, pathsep +from canari.config import CanariConfigParser + +from os import path, listdir, sep, environ, mkdir, pathsep, getcwd from pkg_resources import resource_filename from sys import path as pypath, platform +from datetime import datetime from string import Template + __author__ = 'Nadeem Douba' __copyright__ = 'Copyright 2012, Canari Project' __credits__ = [] @@ -143,4 +147,34 @@ def console_message(msg, tab=-1): tab += 1 console_message(sc, tab) tab -= 1 - tab -= 1 \ No newline at end of file + tab -= 1 + + +def init_pkg(): + + conf = '.canari' + + for i in range(0, 5): + if path.exists(conf): + c = CanariConfigParser() + c.read(conf) + return { + 'author' : c['metadata/author'], + 'email' : c['metadata/email'], + 'maintainer' : c['metadata/maintainer'], + 'project' : c['metadata/project'], + 'year' : datetime.now().year, + 'dir' : getcwd() + } + conf = '..%s%s' % (sep, conf) + + return { + 'author' : '', + 'email' : '', + 'maintainer' : '', + 'project' : '', + 'year' : datetime.now().year + } + + + diff --git a/src/canari/commands/create_package.py b/src/canari/commands/create_package.py index a2f2f21..101ba7b 100644 --- a/src/canari/commands/create_package.py +++ b/src/canari/commands/create_package.py @@ -3,6 +3,7 @@ from common import read_template, write_template, generate_all, build_skeleton, cmd_name from argparse import ArgumentParser +from datetime import datetime from getpass import getuser from os import path, sep @@ -31,6 +32,7 @@ parser.add_argument( def write_setup(package_name, values): + write_template(sep.join([package_name, '.canari']), read_template('_canari', values)) write_template(sep.join([package_name, 'setup.py']), read_template('setup', values)) write_template(sep.join([package_name, 'README.md']), read_template('README', values)) @@ -65,15 +67,22 @@ def write_resources(package_name, resources, init, values): def write_common(transforms, init, values): - write_template( - sep.join([transforms, '__init__.py']), - init + generate_all('common', 'helloworld') - ) - write_template( - sep.join([transforms, 'helloworld.py']), - read_template('transform', values) - ) + if values['example']: + write_template( + sep.join([transforms, '__init__.py']), + init + generate_all('common', 'helloworld') + ) + + write_template( + sep.join([transforms, 'helloworld.py']), + read_template('transform', values) + ) + else: + write_template( + sep.join([transforms, '__init__.py']), + init + generate_all('common') + ) write_template( sep.join([transforms, 'common', '__init__.py']), @@ -94,6 +103,33 @@ def description(): return parser.description +def parse_bool(ans, default='y'): + + while True: + ans = raw_input(ans).lower() or default + if ans.startswith('y'): + return True + elif ans.startswith('n'): + return False + + +def ask_user(defaults): + + print('Welcome to the Canari transform package wizard.') + + if not parse_bool('Would you like to specify authorship information? [Y/n]: '): + return + + defaults['description'] = raw_input('Project description [%s]: ' % defaults['description']) or defaults['description'] + defaults['example'] = parse_bool('Generate an example transform? [Y/n]: ') + defaults['author'] = raw_input('Author name [%s]: ' % defaults['author']) or defaults['author'] + defaults['email'] = raw_input('Author email []: ') or '' + defaults['maintainer'] = raw_input('Maintainer name [%s]: ' % defaults['author']) or defaults['author'] + + if not parse_bool('Are you satisfied with this information? [Y/n]: '): + return ask_user(defaults) + + def run(args): opts = parser.parse_args(args) @@ -105,12 +141,18 @@ def run(args): 'package' : package_name, 'entity' : 'My%sEntity' % capitalized_package_name, 'base_entity' : '%sEntity' % capitalized_package_name, - 'author' : getuser(), - 'year' : 2012, 'project' : capitalized_package_name, - 'namespace' : package_name + 'author' : getuser(), + 'year' : datetime.now().year, + 'namespace' : package_name, + 'email' : '', + 'maintainer' : getuser(), + 'example' : True, + 'description' : '' } + ask_user(values) + base = sep.join([package_name, 'src', package_name]) transforms = sep.join([base, 'transforms']) resources = sep.join([base, 'resources']) diff --git a/src/canari/commands/create_transform.py b/src/canari/commands/create_transform.py index f620be3..e1a5b84 100644 --- a/src/canari/commands/create_transform.py +++ b/src/canari/commands/create_transform.py @@ -1,10 +1,10 @@ #!/usr/bin/env python -from common import write_template, read_template, cmd_name +from common import write_template, read_template, cmd_name, init_pkg + from argparse import ArgumentParser from os import path, sep, getcwd -from getpass import getuser from re import sub @@ -68,10 +68,7 @@ def run(args): print ('Transform %s already exists... quitting' % repr(transformf)) exit(-1) - values = { - 'author' : getuser(), - 'year' : 2012 - } + values = init_pkg() write_template( transformf, diff --git a/src/canari/commands/help.py b/src/canari/commands/help.py index 273738b..416b232 100644 --- a/src/canari/commands/help.py +++ b/src/canari/commands/help.py @@ -27,6 +27,8 @@ parser.add_argument( 'command', metavar='', choices=cmds, + default='help', + nargs='?', help='The canari command you want help for (%s)' % ', '.join(cmds) ) diff --git a/src/scripts/canari b/src/scripts/canari index 72affe9..8083f93 100644 --- a/src/scripts/canari +++ b/src/scripts/canari @@ -20,7 +20,8 @@ cmds = get_commands() def parse_args(): parser = ArgumentParser( - description='Centralized Canari Management System' + description='Centralized Canari Management System', + add_help=False ) parser.add_argument( 'command', -- 2.45.1