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 style
===== =====
- Add a global none attribute/setting to every ConfigType
- Clean up the package checking mess in earlyinit.py - Clean up the package checking mess in earlyinit.py
dwb keybindings to possibly implement dwb keybindings to possibly implement

View File

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

View File

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