|
|
@ -2,6 +2,7 @@ import os
|
|
|
|
import platform
|
|
|
|
import platform
|
|
|
|
import shutil
|
|
|
|
import shutil
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import configobj
|
|
|
|
import configobj
|
|
|
|
import validate
|
|
|
|
import validate
|
|
|
|
|
|
|
|
|
|
|
@ -10,6 +11,7 @@ from .spec import configspec
|
|
|
|
|
|
|
|
|
|
|
|
DFT_CONFIG_PATH = os.path.expanduser('~/.pubsrc')
|
|
|
|
DFT_CONFIG_PATH = os.path.expanduser('~/.pubsrc')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def load_default_conf():
|
|
|
|
def load_default_conf():
|
|
|
|
"""Load the default configuration"""
|
|
|
|
"""Load the default configuration"""
|
|
|
|
default_conf = configobj.ConfigObj(configspec=configspec)
|
|
|
|
default_conf = configobj.ConfigObj(configspec=configspec)
|
|
|
@ -17,6 +19,7 @@ def load_default_conf():
|
|
|
|
default_conf.validate(validator, copy=True)
|
|
|
|
default_conf.validate(validator, copy=True)
|
|
|
|
return default_conf
|
|
|
|
return default_conf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_confpath(verify=True):
|
|
|
|
def get_confpath(verify=True):
|
|
|
|
"""Return the configuration filepath
|
|
|
|
"""Return the configuration filepath
|
|
|
|
If verify is True, verify that the file exists and exit with an error if not.
|
|
|
|
If verify is True, verify that the file exists and exit with an error if not.
|
|
|
@ -32,31 +35,36 @@ def get_confpath(verify=True):
|
|
|
|
ui.exit(error_code=1)
|
|
|
|
ui.exit(error_code=1)
|
|
|
|
return confpath
|
|
|
|
return confpath
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_conf(conf):
|
|
|
|
def check_conf(conf):
|
|
|
|
"""Type check a configuration"""
|
|
|
|
"""Type check a configuration"""
|
|
|
|
validator = validate.Validator()
|
|
|
|
validator = validate.Validator()
|
|
|
|
results = conf.validate(validator, copy=True)
|
|
|
|
results = conf.validate(validator, copy=True)
|
|
|
|
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(check=True, 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)
|
|
|
|
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:
|
|
|
|
if check:
|
|
|
|
check_conf(conf)
|
|
|
|
check_conf(conf)
|
|
|
|
|
|
|
|
conf.filename = path
|
|
|
|
return conf
|
|
|
|
return conf
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def save_conf(conf, path=None):
|
|
|
|
def save_conf(conf, path=None):
|
|
|
|
"""Save the configuration."""
|
|
|
|
"""Save the configuration."""
|
|
|
|
if path is None:
|
|
|
|
if path is not None:
|
|
|
|
path = get_confpath(verify=False)
|
|
|
|
conf.filename = path
|
|
|
|
with open(path, 'wb') as f:
|
|
|
|
elif conf.filename is None:
|
|
|
|
|
|
|
|
conf.filename = get_confpath(verify=False)
|
|
|
|
|
|
|
|
with open(conf.filename, 'wb') as f:
|
|
|
|
conf.write(outfile=f)
|
|
|
|
conf.write(outfile=f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_open_cmd():
|
|
|
|
def default_open_cmd():
|
|
|
|
"""Chooses the default command to open documents"""
|
|
|
|
"""Chooses the default command to open documents"""
|
|
|
|
if platform.system() == 'Darwin':
|
|
|
|
if platform.system() == 'Darwin':
|
|
|
@ -68,6 +76,7 @@ def default_open_cmd():
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def which(cmd):
|
|
|
|
def which(cmd):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
return shutil.which(cmd) # available in python 3.3
|
|
|
|
return shutil.which(cmd) # available in python 3.3
|
|
|
@ -78,6 +87,7 @@ def which(cmd):
|
|
|
|
return filepath
|
|
|
|
return filepath
|
|
|
|
return None
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def default_edit_cmd():
|
|
|
|
def default_edit_cmd():
|
|
|
|
"""Find an available editor"""
|
|
|
|
"""Find an available editor"""
|
|
|
|
if 'EDITOR' in os.environ:
|
|
|
|
if 'EDITOR' in os.environ:
|
|
|
|