Prepare configuration loading for autocomplete.

In particular, tries to load configuration when no argument is given.
Also removes unused check option from load_conf.
main
Olivier Mangin 8 years ago
parent f6e0412306
commit a5466c940e

@ -17,7 +17,7 @@ def command(conf, args):
# get modif from user # get modif from user
ui.edit_file(config.get_confpath()) ui.edit_file(config.get_confpath())
new_conf = config.load_conf(check=False) new_conf = config.load_conf()
try: try:
config.check_conf(new_conf) config.check_conf(new_conf)
ui.message('The configuration file was updated.') ui.message('The configuration file was updated.')

@ -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 from .conf import default_open_cmd, post_process_conf

@ -1,7 +1,5 @@
import os import os
import platform import platform
import shutil
import configobj import configobj
import validate import validate
@ -11,6 +9,16 @@ from .spec import configspec
DFT_CONFIG_PATH = os.path.expanduser('~/.pubsrc') 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): def post_process_conf(conf):
"""Do some post processing on the configuration""" """Do some post processing on the configuration"""
if conf['main']['docsdir'] == 'docsdir://': 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 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""" """Load the configuration"""
if path is None: if path is None:
path = get_confpath(verify=True) path = get_confpath(verify=True)
if not os.path.exists(path):
raise ConfigurationNotFound(path)
with open(path, 'rb') as f: with open(path, 'rb') as f:
conf = configobj.ConfigObj(f.readlines(), configspec=configspec) conf = configobj.ConfigObj(f.readlines(), configspec=configspec)
if check:
check_conf(conf)
conf.filename = path conf.filename = path
conf = post_process_conf(conf) conf = post_process_conf(conf)
return conf return conf

@ -53,15 +53,18 @@ def execute(raw_args=sys.argv):
conf_path = config.get_confpath(verify=False) # will be checked on load conf_path = config.get_confpath(verify=False) # will be checked on load
# Loading config # Loading config
if len(remaining_args) > 0 and remaining_args[0] != 'init': try:
conf = config.load_conf(path=conf_path, check=False) conf = config.load_conf(path=conf_path)
if update.update_check(conf, path=conf.filename): if update.update_check(conf, path=conf.filename):
# an update happened, reload conf. # 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) config.check_conf(conf)
else: except config.ConfigurationNotFound:
if len(remaining_args) == 0 or remaining_args[0] == 'init':
conf = config.load_default_conf() conf = config.load_default_conf()
conf.filename = conf_path conf.filename = conf_path
else:
raise
uis.init_ui(conf, force_colors=top_args.force_colors) uis.init_ui(conf, force_colors=top_args.force_colors)
ui = uis.get_ui() ui = uis.get_ui()

@ -33,7 +33,7 @@ class FakeSystemExit(Exception):
SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds() SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds()
function, so they can be catched by ExpectedFailure tests in Python 2.x. 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 pass
@ -151,6 +151,13 @@ class DataCommandTestCase(CommandTestCase):
# Actual tests # Actual tests
class TestAlone(CommandTestCase):
def test_alone_misses_command(self):
with self.assertRaises(FakeSystemExit):
self.execute_cmds(['pubs'])
class TestInit(CommandTestCase): class TestInit(CommandTestCase):
def test_init(self): def test_init(self):

Loading…
Cancel
Save