Skip to content

Commit

Permalink
updated config command and made settings more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
Jochem Berends committed Dec 6, 2017
1 parent 9fdc48d commit 3968b91
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
19 changes: 17 additions & 2 deletions kecpkg/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,37 @@
import click

from kecpkg.commands.utils import CONTEXT_SETTINGS, echo_info, echo_success
from kecpkg.settings import load_settings
from kecpkg.settings import load_settings, copy_default_settings, save_settings
from kecpkg.utils import get_package_dir


@click.command(context_settings=CONTEXT_SETTINGS,
short_help="Finds and updated the configuration of the kecpkg")
@click.argument('package', required=False)
@click.option('--interactive', '-i', is_flag=True, help="interactive mode; guide me through the upload")
@click.option('--init', is_flag=True, help="will init a settingsfile if not found")
@click.option('--interactive', '-i', is_flag=True, help="interactive mode; guide me through the settings")
@click.option('--verbose', '-v', is_flag=True, help="be more verbose (print settings)")
def config(package, **options):
"""Manage the configuration (or settings) of the package."""
echo_info('Locating package ``'.format(package))
package_dir = get_package_dir(package_name=package)
package_name = os.path.basename(package_dir)
echo_info('Package `{}` has been selected'.format(package_name))

if options.get('init'):
settings = copy_default_settings()
settings['package_name'] = package_name
save_settings(settings, package_dir=package_dir)

settings = load_settings(package_dir=package_dir)
if options.get('interactive'):
settings['version'] = click.prompt('Version', default=settings.get('version', '0.0.1'))
settings['description'] = click.prompt('Description', default=settings.get('description', ''))
settings['name'] = click.prompt('Author', default=settings.get('name', os.environ.get('USER', '')))
settings['email'] = click.prompt('Author\'s email', default=settings.get('email', ''))
settings['python_version'] = click.prompt('Python version (choose from: {})'.format(settings.get('pyversions')),
default='3.5')
save_settings(settings, package_dir=package_dir)

if options.get('verbose'):
for k, v in settings.items():
Expand Down
2 changes: 1 addition & 1 deletion kecpkg/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def new(package=None, **options):
if not package:
settings['version'] = click.prompt('Version', default=settings.get('version', '0.0.1'))
settings['description'] = click.prompt('Description', default='')
settings['name'] = click.prompt('Author', default=settings.get('name', ''))
settings['name'] = click.prompt('Author', default=settings.get('name', os.environ.get('USER', '')))
settings['email'] = click.prompt('Author\'s email', default=settings.get('email', ''))
settings['python_version'] = click.prompt('Python version (choose from: {})'.format(settings.get('pyversions')),
default='3.5')
Expand Down
3 changes: 3 additions & 0 deletions kecpkg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from atomicwrites import atomic_write

from kecpkg.commands.utils import echo_failure
from kecpkg.utils import ensure_dir_exists, create_file, get_package_dir

SETTINGS_FILENAME = '.kecpkg_settings.json'
Expand Down Expand Up @@ -51,6 +52,8 @@ def load_settings(lazy=False, package_dir=None):
settings_filepath = get_settings_filepath(package_dir)
if lazy and not os.path.exists(settings_filepath):
return {}
elif not os.path.exists(settings_filepath):
echo_failure('Could not find a settingsfile in path: {}'.format(settings_filepath))
with open(settings_filepath, 'r') as f:
return json.loads(f.read(), object_pairs_hook=OrderedDict)

Expand Down

0 comments on commit 3968b91

Please sign in to comment.