Add a none argument to every config type and cleanup.

- Rename none to none_ok (because of "None")
- Fix File class.
This commit is contained in:
Florian Bruhin 2014-06-20 20:18:41 +02:00
parent 92379533cd
commit 7de637baea
3 changed files with 63 additions and 68 deletions

View File

@ -85,7 +85,6 @@ hints
style
=====
- Add a global none attribute/setting to every ConfigType
- Clean up the package checking mess in earlyinit.py
dwb keybindings to possibly implement

View File

@ -228,7 +228,7 @@ DATA = OrderedDict([
"This setting enables WebKit's workaround for broken sites."),
('default-encoding',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Default encoding to use for websites."),
)),
@ -281,11 +281,11 @@ DATA = OrderedDict([
"User stylesheet to set."),
('css-media-type',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Set the CSS media type."),
('css-media-type',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Set the CSS media type."),
#('accelerated-compositing',
@ -312,11 +312,11 @@ DATA = OrderedDict([
"Value to send in the DNT header."),
('accept-language',
SettingValue(types.String(none=True), 'en-US,en'),
SettingValue(types.String(none_ok=True), 'en-US,en'),
"Value to send in the accept-language header."),
('user-agent',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"User agent to send. Empty to send the default."),
('proxy',
@ -441,12 +441,12 @@ DATA = OrderedDict([
('storage', sect.KeyValue(
('download-directory',
SettingValue(types.Directory(none=True), ''),
SettingValue(types.Directory(none_ok=True), ''),
"The directory to save downloads to. An empty value selects a "
"sensible os-specific default."),
('maximum-pages-in-cache',
SettingValue(types.Int(none=True, minval=0, maxval=MAXVALS['int']),
SettingValue(types.Int(none_ok=True, minval=0, maxval=MAXVALS['int']),
''),
"Sets the maximum number of pages to hold in the memory page cache."),
@ -942,46 +942,46 @@ DATA = OrderedDict([
"Font used for the hints."),
('web-family-standard',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for standard fonts."),
('web-family-fixed',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for fixed fonts."),
('web-family-serif',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for serif fonts."),
('web-family-sans-serif',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for sans-serif fonts."),
('web-family-cursive',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for cursive fonts."),
('web-family-fantasy',
SettingValue(types.String(none=True), ''),
SettingValue(types.String(none_ok=True), ''),
"Font family for fantasy fonts."),
('web-size-minimum',
SettingValue(types.Int(none=True, minval=1, maxval=MAXVALS['int']),
SettingValue(types.Int(none_ok=True, minval=1, maxval=MAXVALS['int']),
''),
"The hard minimum font size."),
('web-size-minimum-logical',
SettingValue(types.Int(none=True, minval=1, maxval=MAXVALS['int']),
SettingValue(types.Int(none_ok=True, minval=1, maxval=MAXVALS['int']),
''),
"The minimum logical font size that is applied when zooming out."),
('web-size-default',
SettingValue(types.Int(none=True, minval=1, maxval=MAXVALS['int']),
SettingValue(types.Int(none_ok=True, minval=1, maxval=MAXVALS['int']),
''),
"The default font size for regular text."),
('web-size-default-fixed',
SettingValue(types.Int(none=True, minval=1, maxval=MAXVALS['int']),
SettingValue(types.Int(none_ok=True, minval=1, maxval=MAXVALS['int']),
''),
"The default font size for fixed-pitch text."),
)),

View File

@ -81,6 +81,9 @@ class BaseType:
"""A type used for a setting value.
Attributes:
none_ok: Whether to convert to None for an empty string.
Class attributes:
valid_values: Possible values if they can be expressed as a fixed
string. ValidValues instance.
@ -90,6 +93,9 @@ class BaseType:
typestr = None
valid_values = None
def __init__(self, none_ok=False):
self.none_ok = none_ok
def transform(self, value):
"""Transform the setting value.
@ -103,6 +109,8 @@ class BaseType:
Return:
The transformed value.
"""
if self.none_ok and not value:
return None
return value
def validate(self, value):
@ -119,6 +127,8 @@ class BaseType:
NotImplementedError if self.valid_values is not defined and this
method should be overridden.
"""
if not value and self.none_ok:
return
if self.valid_values is not None:
if value not in self.valid_values:
raise ValidationError(value, "valid values: {}".format(
@ -159,24 +169,20 @@ class String(BaseType):
minlen: Minimum length (inclusive).
maxlen: Maximum length (inclusive).
forbidden: Forbidden chars in the string.
none: Whether to convert to None for an empty string.
"""
typestr = 'string'
def __init__(self, minlen=None, maxlen=None, forbidden=None, none=False):
super().__init__()
def __init__(self, minlen=None, maxlen=None, forbidden=None,
none_ok=False):
super().__init__(none_ok)
self.minlen = minlen
self.maxlen = maxlen
self.forbidden = forbidden
self.none = none
def transform(self, value):
if self.none and not value:
return None
return value
def validate(self, value):
if self.none_ok and not value:
return
if self.forbidden is not None and any(c in value
for c in self.forbidden):
raise ValidationError(value, "may not contain the chars "
@ -234,24 +240,22 @@ class Int(BaseType):
Attributes:
minval: Minimum value (inclusive).
maxval: Maximum value (inclusive).
none: Whether to accept empty values as None.
"""
typestr = 'int'
def __init__(self, minval=None, maxval=None, none=False):
super().__init__()
def __init__(self, minval=None, maxval=None, none_ok=False):
super().__init__(none_ok)
self.minval = minval
self.maxval = maxval
self.none = none
def transform(self, value):
if self.none and not value:
if self.none_ok and not value:
return None
return int(value)
def validate(self, value):
if self.none and not value:
if self.none_ok and not value:
return
try:
intval = int(value)
@ -293,8 +297,8 @@ class Float(BaseType):
typestr = 'float'
def __init__(self, minval=None, maxval=None):
super().__init__()
def __init__(self, minval=None, maxval=None, none_ok=False):
super().__init__(none_ok)
self.minval = minval
self.maxval = maxval
@ -325,8 +329,8 @@ class Perc(BaseType):
typestr = 'percentage'
def __init__(self, minval=None, maxval=None):
super().__init__()
def __init__(self, minval=None, maxval=None, none_ok=False):
super().__init__(none_ok)
self.minval = minval
self.maxval = maxval
@ -359,8 +363,8 @@ class PercList(List):
typestr = 'perc-list'
def __init__(self, minval=None, maxval=None):
super().__init__()
def __init__(self, minval=None, maxval=None, none_ok=False):
super().__init__(none_ok)
self.minval = minval
self.maxval = maxval
@ -391,8 +395,9 @@ class PercOrInt(BaseType):
typestr = 'percentage-or-int'
def __init__(self, minperc=None, maxperc=None, minint=None, maxint=None):
super().__init__()
def __init__(self, minperc=None, maxperc=None, minint=None, maxint=None,
none_ok=False):
super().__init__(none_ok)
self.minperc = minperc
self.maxperc = maxperc
self.minint = minint
@ -530,8 +535,8 @@ class Regex(BaseType):
typestr = 'regex'
def __init__(self, flags=0):
super().__init__()
def __init__(self, flags=0, none_ok=False):
super().__init__(none_ok)
self.flags = flags
def validate(self, value):
@ -550,8 +555,8 @@ class RegexList(List):
typestr = 'regex-list'
def __init__(self, flags=0):
super().__init__()
def __init__(self, flags=0, none_ok=False):
super().__init__(none_ok)
self.flags = flags
def transform(self, value):
@ -573,26 +578,20 @@ class File(BaseType):
typestr = 'file'
def validate(self, value):
if self.none_ok and not value:
return
if not os.path.isfile(value):
raise ValidationError(value, "must be a valid file!")
class Directory(BaseType):
"""A directory on the local filesystem.
Attributes:
none: Whether to accept empty values as None.
"""
"""A directory on the local filesystem."""
typestr = 'directory'
def __init__(self, none=False):
super().__init__()
self.none = none
def validate(self, value):
if self.none and not value:
if self.none_ok and not value:
return
if not os.path.isdir(value):
raise ValidationError(value, "must be a valid directory!")
@ -627,8 +626,8 @@ class WebKitBytes(BaseType):
typestr = 'bytes'
def __init__(self, maxsize=None):
super().__init__()
def __init__(self, maxsize=None, none_ok=False):
super().__init__(none_ok)
self.maxsize = maxsize
def validate(self, value):
@ -669,8 +668,8 @@ class WebKitBytesList(List):
typestr = 'bytes-list'
def __init__(self, maxsize=None, length=None):
super().__init__()
def __init__(self, maxsize=None, length=None, none_ok=False):
super().__init__(none_ok)
self.length = length
self.bytestype = WebKitBytes(maxsize)
@ -703,9 +702,9 @@ class ShellCommand(String):
typestr = 'shell-command'
def __init__(self, placeholder=False):
def __init__(self, placeholder=False, none_ok=False):
super().__init__(none_ok=none_ok)
self.placeholder = placeholder
super().__init__()
def validate(self, value):
super().validate(value)
@ -824,18 +823,15 @@ class KeyBinding(Command):
class WebSettingsFile(File):
"""QWebSettings file which also can be none."""
"""QWebSettings file."""
typestr = 'file'
def validate(self, value):
if value == '':
# empty values are okay
return
super().validate(value)
def __init__(self):
super().__init__(none_ok=True)
def transform(self, value):
if value == '':
if not value:
return None
else:
return QUrl.fromLocalFile(value)