diff --git a/pubs/commands/__init__.py b/pubs/commands/__init__.py index 60f514f..a9e0abe 100644 --- a/pubs/commands/__init__.py +++ b/pubs/commands/__init__.py @@ -1,5 +1,7 @@ # core from . import init_cmd +from . import conf_cmd + from . import add_cmd from . import rename_cmd from . import remove_cmd @@ -16,4 +18,3 @@ from . import import_cmd from . import websearch_cmd from . import edit_cmd -# from . import update_cmd diff --git a/pubs/commands/conf_cmd.py b/pubs/commands/conf_cmd.py new file mode 100644 index 0000000..d09d61c --- /dev/null +++ b/pubs/commands/conf_cmd.py @@ -0,0 +1,36 @@ +from .. import uis +from .. import config +from .. import content + + +def parser(subparsers): + parser = subparsers.add_parser('conf', + help='open the configuration in an editor') + return parser + + +def command(conf, args): + uis.init_ui(conf) + ui = uis.get_ui() + + while True: + # get modif from user + content.edit_file(conf['main']['edit_cmd'], config.get_confpath()) + + new_conf = config.load_conf(check=False) + try: + config.check_conf(new_conf) + ui.message('The configuration file was updated.') + break + except AssertionError: # TODO better error message + ui.error('Error reading the modified configuration file.') + options = ['edit_again', 'abort'] + choice = options[ui.input_choice( + options, ['e', 'a'], + question=('Edit again or abort? If you abort, the changes will be reverted.') + )] + + if choice == 'abort': + config.save_conf(conf) + ui.message('The changes have been reverted.') + break diff --git a/pubs/config/conf.py b/pubs/config/conf.py index 7867955..259e7eb 100644 --- a/pubs/config/conf.py +++ b/pubs/config/conf.py @@ -9,16 +9,15 @@ from .spec import configspec DFT_CONFIG_PATH = os.path.expanduser('~/.pubsrc') def load_default_conf(): - """Loads the default configuration""" + """Load the default configuration""" default_conf = configobj.ConfigObj(configspec=configspec) validator = validate.Validator() default_conf.validate(validator, copy=True) return default_conf def get_confpath(verify=True): - """Returns the pubs path. - If verify is True, verify that pubs.conf exist in the directory, - and exit with an error if not. + """Return the configuration filepath + If verify is True, verify that the file exists and exit with an error if not. """ confpath = DFT_CONFIG_PATH if 'PUBSCONF' in os.environ: @@ -32,13 +31,13 @@ def get_confpath(verify=True): return confpath def check_conf(conf): - """Type checks a configuration""" + """Type check a configuration""" validator = validate.Validator() results = conf.validate(validator, copy=True) assert results == True, '{}'.format(results) # TODO: precise error dialog when parsing error def load_conf(check=True, path=None): - """Load the user config""" + """Load the configuration""" if path is None: path = get_confpath(verify=True) with open(path, 'rb') as f: @@ -50,6 +49,7 @@ def load_conf(check=True, path=None): return conf def save_conf(conf, path=None): + """Save the configuration.""" if path is None: path = get_confpath(verify=False) with open(path, 'wb') as f: diff --git a/pubs/pubs_cmd.py b/pubs/pubs_cmd.py index 512dc79..3b2fc53 100644 --- a/pubs/pubs_cmd.py +++ b/pubs/pubs_cmd.py @@ -12,6 +12,8 @@ from . import plugins CORE_CMDS = collections.OrderedDict([ ('init', commands.init_cmd), + ('conf', commands.conf_cmd), + ('add', commands.add_cmd), ('rename', commands.rename_cmd), ('remove', commands.remove_cmd), diff --git a/setup.py b/setup.py index 145a58d..462d453 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from setuptools import setup, find_packages -VERSION = '0.5.0' +VERSION = '0.6.0' setup( name = 'pubs',