allow config files to be optional

This commit is contained in:
Felix Van der Jeugt 2016-01-05 20:42:32 +01:00
parent 32de5b76a9
commit e28c1bf9b8
3 changed files with 17 additions and 11 deletions

View File

@ -51,6 +51,7 @@ Target = usertypes.enum('Target', ['normal', 'tab', 'tab_fg', 'tab_bg',
class WordHintingError(Exception): class WordHintingError(Exception):
"""Exception raised on errors during word hinting.""" """Exception raised on errors during word hinting."""
@ -985,6 +986,7 @@ class WordHinter:
Attributes: Attributes:
""" """
FIRST_ALPHABETIC = re.compile('[A-Za-z]{3,}') FIRST_ALPHABETIC = re.compile('[A-Za-z]{3,}')
def __init__(self): def __init__(self):

View File

@ -891,7 +891,7 @@ def data(readonly=False):
"Make chars in hint strings uppercase."), "Make chars in hint strings uppercase."),
('dictionary', ('dictionary',
SettingValue(typ.File(), '/usr/share/dict/words'), SettingValue(typ.File(required=False), '/usr/share/dict/words'),
"The dictionary file to be used by the word hints."), "The dictionary file to be used by the word hints."),
('auto-follow', ('auto-follow',

View File

@ -891,6 +891,10 @@ class File(BaseType):
"""A file on the local filesystem.""" """A file on the local filesystem."""
def __init__(self, required=True, **kwargs):
super().__init__(**kwargs)
self.required = required
def transform(self, value): def transform(self, value):
if not value: if not value:
return None return None
@ -899,7 +903,9 @@ class File(BaseType):
if not os.path.isabs(value): if not os.path.isabs(value):
cfgdir = standarddir.config() cfgdir = standarddir.config()
assert cfgdir is not None assert cfgdir is not None
return os.path.join(cfgdir, value) value = os.path.join(cfgdir, value)
#if not self.required and not os.access(value, os.F_OK | os.R_OK):
# return None
return value return value
def validate(self, value): def validate(self, value):
@ -915,15 +921,13 @@ class File(BaseType):
raise configexc.ValidationError( raise configexc.ValidationError(
value, "must be an absolute path when not using a " value, "must be an absolute path when not using a "
"config directory!") "config directory!")
elif not os.path.isfile(os.path.join(cfgdir, value)): value = os.path.join(cfgdir, value)
raise configexc.ValidationError( not_isfile_message = ("must be a valid path relative to the "
value, "must be a valid path relative to the config " "config directory!")
"directory!") else:
else: not_isfile_message = "must be a valid file!"
return if self.required and not os.path.isfile(value):
elif not os.path.isfile(value): raise configexc.ValidationError(value, not_isfile_message)
raise configexc.ValidationError(
value, "must be a valid file!")
except UnicodeEncodeError as e: except UnicodeEncodeError as e:
raise configexc.ValidationError(value, e) raise configexc.ValidationError(value, e)