From a5466c940eccc9bedda9ca9da5022c8bf812f27f Mon Sep 17 00:00:00 2001 From: Olivier Mangin Date: Fri, 12 May 2017 18:10:47 -0400 Subject: [PATCH] Prepare configuration loading for autocomplete. In particular, tries to load configuration when no argument is given. Also removes unused check option from load_conf. --- pubs/commands/conf_cmd.py | 2 +- pubs/config/__init__.py | 3 ++- pubs/config/conf.py | 18 +++++++++++++----- pubs/pubs_cmd.py | 15 +++++++++------ tests/test_usecase.py | 9 ++++++++- 5 files changed, 33 insertions(+), 14 deletions(-) diff --git a/pubs/commands/conf_cmd.py b/pubs/commands/conf_cmd.py index 1f5ba44..75eed5a 100644 --- a/pubs/commands/conf_cmd.py +++ b/pubs/commands/conf_cmd.py @@ -17,7 +17,7 @@ def command(conf, args): # get modif from user ui.edit_file(config.get_confpath()) - new_conf = config.load_conf(check=False) + new_conf = config.load_conf() try: config.check_conf(new_conf) ui.message('The configuration file was updated.') diff --git a/pubs/config/__init__.py b/pubs/config/__init__.py index 0f5a133..3e8ec08 100644 --- a/pubs/config/__init__.py +++ b/pubs/config/__init__.py @@ -1,2 +1,3 @@ -from .conf import get_confpath, load_default_conf, load_conf, save_conf, check_conf +from .conf import (get_confpath, load_default_conf, load_conf, save_conf, + check_conf, ConfigurationNotFound) from .conf import default_open_cmd, post_process_conf diff --git a/pubs/config/conf.py b/pubs/config/conf.py index af2f3ce..2086601 100644 --- a/pubs/config/conf.py +++ b/pubs/config/conf.py @@ -1,7 +1,5 @@ import os import platform -import shutil - import configobj import validate @@ -11,6 +9,16 @@ from .spec import configspec DFT_CONFIG_PATH = os.path.expanduser('~/.pubsrc') + +class ConfigurationNotFound(IOError): + + def __init__(self, path): + super(ConfigurationNotFound, self).__init__( + "No configuration found at path {}. Maybe you need to initialize " + "your repository with `pubs init` or specify a --config argument." + "".format(path)) + + def post_process_conf(conf): """Do some post processing on the configuration""" if conf['main']['docsdir'] == 'docsdir://': @@ -50,14 +58,14 @@ def check_conf(conf): assert results == True, '{}'.format(results) # TODO: precise error dialog when parsing error -def load_conf(check=True, path=None): +def load_conf(path=None): """Load the configuration""" if path is None: path = get_confpath(verify=True) + if not os.path.exists(path): + raise ConfigurationNotFound(path) with open(path, 'rb') as f: conf = configobj.ConfigObj(f.readlines(), configspec=configspec) - if check: - check_conf(conf) conf.filename = path conf = post_process_conf(conf) return conf diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 62a58b8..976be3e 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -53,15 +53,18 @@ def execute(raw_args=sys.argv): conf_path = config.get_confpath(verify=False) # will be checked on load # Loading config - if len(remaining_args) > 0 and remaining_args[0] != 'init': - conf = config.load_conf(path=conf_path, check=False) + try: + conf = config.load_conf(path=conf_path) if update.update_check(conf, path=conf.filename): # an update happened, reload conf. - conf = config.load_conf(path=conf_path, check=False) + conf = config.load_conf(path=conf_path) config.check_conf(conf) - else: - conf = config.load_default_conf() - conf.filename = conf_path + except config.ConfigurationNotFound: + if len(remaining_args) == 0 or remaining_args[0] == 'init': + conf = config.load_default_conf() + conf.filename = conf_path + else: + raise uis.init_ui(conf, force_colors=top_args.force_colors) ui = uis.get_ui() diff --git a/tests/test_usecase.py b/tests/test_usecase.py index 1120141..4c275d5 100644 --- a/tests/test_usecase.py +++ b/tests/test_usecase.py @@ -33,7 +33,7 @@ class FakeSystemExit(Exception): SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds() function, so they can be catched by ExpectedFailure tests in Python 2.x. - If a code is accepted to raise SystemExit, catch FakeSystemExit instead. + If a code is expected to raise SystemExit, catch FakeSystemExit instead. """ pass @@ -151,6 +151,13 @@ class DataCommandTestCase(CommandTestCase): # Actual tests +class TestAlone(CommandTestCase): + + def test_alone_misses_command(self): + with self.assertRaises(FakeSystemExit): + self.execute_cmds(['pubs']) + + class TestInit(CommandTestCase): def test_init(self):