Add pubs conf command
Allows to edit the configuration file using pubs directly. Will check if the modified configuration file is valid.
This commit is contained in:
parent
757a8b300e
commit
14df0ad1cb
@ -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
|
||||
|
36
pubs/commands/conf_cmd.py
Normal file
36
pubs/commands/conf_cmd.py
Normal file
@ -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
|
@ -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:
|
||||
|
@ -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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user