diff --git a/bonobo/commands/__init__.py b/bonobo/commands/__init__.py index cd9559c..2f1c7cd 100644 --- a/bonobo/commands/__init__.py +++ b/bonobo/commands/__init__.py @@ -1,7 +1,9 @@ import argparse +import traceback from bonobo import settings, logging from bonobo.commands.base import BaseCommand, BaseGraphCommand +from bonobo.util.errors import print_error logger = logging.get_logger() @@ -52,4 +54,9 @@ def entrypoint(args=None): # Get command handler, execute, rince. command = commands[parsed_args.pop('command')] - command(**parsed_args) + + try: + command(**parsed_args) + except Exception as exc: + print_error(exc, traceback.format_exc()) + return 255 diff --git a/bonobo/commands/init.py b/bonobo/commands/init.py index 6c6c2ff..e5d11d2 100644 --- a/bonobo/commands/init.py +++ b/bonobo/commands/init.py @@ -12,9 +12,12 @@ class InitCommand(BaseCommand): def add_arguments(self, parser): parser.add_argument('filename') parser.add_argument('--force', '-f', default=False, action='store_true') - parser.add_argument('--template', '-t', choices=self.TEMPLATES, default='default') - def handle(self, *, template, filename, force=False): + target_group = parser.add_mutually_exclusive_group(required=True) + target_group.add_argument('--template', '-t', choices=self.TEMPLATES, default='default') + target_group.add_argument('--package', '-p', action='store_true', default=False) + + def create_file_from_template(self, *, template, filename): template_name = template name, ext = os.path.splitext(filename) if ext != '.py': @@ -24,10 +27,46 @@ class InitCommand(BaseCommand): env = Environment(loader=loader) template = env.get_template(template_name + '.py-tpl') - if os.path.exists(filename) and not force: - raise FileExistsError('Target filename already exists, use --force to override.') - with open(filename, 'w+') as f: f.write(template.render(name=name)) self.logger.info('Generated {} using template {!r}.'.format(filename, template_name)) + + def create_package(self, *, filename): + name, ext = os.path.splitext(filename) + if ext != '': + raise ValueError('Package names should not have an extension.') + + try: + import medikit.commands + except ImportError as exc: + raise ImportError( + 'To initialize a package, you need to install medikit (pip install --upgrade medikit).') from exc + + package_name = os.path.basename(filename) + medikit.commands.handle_init(os.path.join(os.getcwd(), filename, 'Projectfile'), name=package_name, + requirements=['bonobo']) + + self.logger.info('Generated "{}" package with medikit.'.format(package_name)) + self.create_file_from_template(template='default', filename=os.path.join(filename, package_name, '__main__.py')) + + print('Your "{}" package has been created.'.format(package_name)) + print() + print('Install it...') + print() + print(' pip install --editable {}'.format(filename)) + print() + print('Then maybe run the example...') + print() + print(' python -m {}'.format(package_name)) + print() + print('Enjoy!') + + def handle(self, *, template, filename, package=False, force=False): + if os.path.exists(filename) and not force: + raise FileExistsError('Target filename already exists, use --force to override.') + + if package: + self.create_package(filename=filename) + else: + self.create_file_from_template(template=template, filename=filename)