Skip to content

Commit

Permalink
The config command is now more robust. Added options --init to in…
Browse files Browse the repository at this point in the history
…itialise a new settingsfile and added option `--interactive` to walk throug the current settings file and be able to redefine settings.

 * Also the loading of the settings is now more robust and does not fail when a settings file is not found
  • Loading branch information
Jochem Berends committed Dec 6, 2017
1 parent 3968b91 commit e5dfd18
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 0.7.0 (6DEC17)
* The `config` command is now more robust. Added options `--init` to initialise a new settingsfile and added option `--interactive` to walk throug the current settings file and be able to redefine settings.
* Also the loading of the settings is now more robust and does not fail when a settings file is not found

## 0.6.1 (6DEC17)
* The `upload` command now properly checks if an build is made in the build directory and gives a proper warning

Expand Down
2 changes: 1 addition & 1 deletion kecpkg/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.6.1'
__version__ = '0.7.0'
9 changes: 7 additions & 2 deletions kecpkg/commands/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import click

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


@click.command(context_settings=CONTEXT_SETTINGS,
Expand All @@ -21,6 +21,11 @@ def config(package, **options):
echo_info('Package `{}` has been selected'.format(package_name))

if options.get('init'):
if os.path.exists(os.path.join(package_dir, SETTINGS_FILENAME)) and \
click.confirm('Are you sure you want to overwrite the current settingsfile (old settings will be a backup)?'):
copy_path(os.path.join(package_dir,SETTINGS_FILENAME),
os.path.join(package_dir, "{}-backup".format(SETTINGS_FILENAME)))
echo_info('Creating new settingsfile')
settings = copy_default_settings()
settings['package_name'] = package_name
save_settings(settings, package_dir=package_dir)
Expand Down
5 changes: 2 additions & 3 deletions kecpkg/commands/new.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,8 @@ def new(package=None, **options):
+-- .gitignore
+-- .kecpkg-settings.json
"""
try:
settings = load_settings()
except IOError:
settings = load_settings(lazy=True)
if not settings:
settings = copy_default_settings()
package_root_dir = os.getcwd()

Expand Down
7 changes: 5 additions & 2 deletions kecpkg/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from collections import OrderedDict
from copy import deepcopy

import sys
from atomicwrites import atomic_write

from kecpkg.commands.utils import echo_failure
Expand Down Expand Up @@ -54,8 +55,10 @@ def load_settings(lazy=False, package_dir=None):
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)
sys.exit(404)
else:
with open(settings_filepath, 'r') as f:
return json.loads(f.read(), object_pairs_hook=OrderedDict)


def get_settings_filepath(package_dir=None):
Expand Down
16 changes: 6 additions & 10 deletions kecpkg/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,21 @@ def get_package_dir(package_name=None, fail=True):
:param fail: (optional, default=True) fail hard with exit when no package dir found
:return: full path name to the package directory
"""
from kecpkg.settings import SETTINGS_FILENAME
def _inner(d):
from kecpkg.settings import load_settings
try:
# load settings just to test that we are inside a package dir
load_settings(package_dir=d)
if os.path.exists(os.path.join(d, SETTINGS_FILENAME)):
return d
except IOError:
if os.path.exists(os.path.join(d, 'package_info.json')):
return d
else:
return None
elif os.path.exists(os.path.join(d, 'package_info.json')):
return d
else:
return None

package_dir = _inner(os.getcwd())
if not package_dir:
package_dir = _inner(os.path.join(os.getcwd(), package_name))
if not package_dir:
package_dir = _inner(package_name)
if not package_dir:
from kecpkg.settings import SETTINGS_FILENAME
echo_failure('This does not seem to be a package in path `{}` - please check that there is a '
'`package_info.json` or a `{}`'.format(package_dir, SETTINGS_FILENAME))
if fail:
Expand Down

0 comments on commit e5dfd18

Please sign in to comment.