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
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.')

@ -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

@ -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

@ -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()

@ -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):

Loading…
Cancel
Save