set valid_values in __init__ methods, except for BaseType s.t. variable is shared across derived class scopes

This commit is contained in:
Patric Schmitz 2015-11-25 14:19:14 +01:00
parent 55edd9cda7
commit 9aaf5c18c1

View File

@ -244,8 +244,9 @@ class String(BaseType):
def __init__(self, minlen=None, maxlen=None, forbidden=None, def __init__(self, minlen=None, maxlen=None, forbidden=None,
none_ok=False, completions=None, valid_values=None): none_ok=False, completions=None, valid_values=None):
super().__init__(none_ok)
self.valid_values = valid_values self.valid_values = valid_values
super().__init__(none_ok)
if minlen is not None and minlen < 1: if minlen is not None and minlen < 1:
raise ValueError("minlen ({}) needs to be >= 1!".format(minlen)) raise ValueError("minlen ({}) needs to be >= 1!".format(minlen))
elif maxlen is not None and maxlen < 1: elif maxlen is not None and maxlen < 1:
@ -352,7 +353,9 @@ class Bool(BaseType):
"""Base class for a boolean setting.""" """Base class for a boolean setting."""
valid_values = ValidValues('true', 'false') def __init__(self, none_ok=False):
self.valid_values = ValidValues('true', 'false')
super().__init__(none_ok)
def transform(self, value): def transform(self, value):
if not value: if not value:
@ -372,7 +375,9 @@ class BoolAsk(Bool):
"""A yes/no/ask question.""" """A yes/no/ask question."""
valid_values = ValidValues('true', 'false', 'ask') def __init__(self, none_ok=False):
self.valid_values = ValidValues('true', 'false', 'ask')
super().__init__(none_ok)
def transform(self, value): def transform(self, value):
if value.lower() == 'ask': if value.lower() == 'ask':
@ -652,11 +657,13 @@ class ColorSystem(MappingType):
special = True special = True
valid_values = ValidValues( def __init__(self, none_ok=False):
('rgb', "Interpolate in the RGB color system."), self.valid_values = ValidValues(
('hsv', "Interpolate in the HSV color system."), ('rgb', "Interpolate in the RGB color system."),
('hsl', "Interpolate in the HSL color system."), ('hsv', "Interpolate in the HSV color system."),
('none', "Don't show a gradient.")) ('hsl', "Interpolate in the HSL color system."),
('none', "Don't show a gradient."))
super().__init__(none_ok)
MAPPING = { MAPPING = {
'rgb': QColor.Rgb, 'rgb': QColor.Rgb,
@ -1095,9 +1102,6 @@ class Proxy(BaseType):
"""A proxy URL or special value.""" """A proxy URL or special value."""
special = True special = True
valid_values = ValidValues(
('system', "Use the system wide proxy."),
('none', "Don't use any proxy"))
PROXY_TYPES = { PROXY_TYPES = {
'http': QNetworkProxy.HttpProxy, 'http': QNetworkProxy.HttpProxy,
@ -1105,6 +1109,12 @@ class Proxy(BaseType):
'socks5': QNetworkProxy.Socks5Proxy, 'socks5': QNetworkProxy.Socks5Proxy,
} }
def __init__(self, none_ok=False):
self.valid_values = ValidValues(
('system', "Use the system wide proxy."),
('none', "Don't use any proxy"))
super().__init__(none_ok)
def validate(self, value): def validate(self, value):
self._basic_validation(value) self._basic_validation(value)
if not value: if not value:
@ -1289,13 +1299,14 @@ class AutoSearch(BaseType):
"""Whether to start a search when something else than a URL is entered.""" """Whether to start a search when something else than a URL is entered."""
special = True special = True
valid_values = ValidValues(('naive', "Use simple/naive check."),
('dns', "Use DNS requests (might be slow!)."),
('false', "Never search automatically."))
def __init__(self, none_ok=False): def __init__(self, none_ok=False):
super().__init__(none_ok)
self.booltype = Bool(none_ok=none_ok) self.booltype = Bool(none_ok=none_ok)
self.valid_values = ValidValues(
('naive', "Use simple/naive check."),
('dns', "Use DNS requests (might be slow!)."),
('false', "Never search automatically."))
super().__init__(none_ok)
def validate(self, value): def validate(self, value):
self._basic_validation(value) self._basic_validation(value)
@ -1322,8 +1333,6 @@ class Position(MappingType):
"""The position of the tab bar.""" """The position of the tab bar."""
valid_values = ValidValues('top', 'bottom', 'left', 'right')
MAPPING = { MAPPING = {
'top': QTabWidget.North, 'top': QTabWidget.North,
'bottom': QTabWidget.South, 'bottom': QTabWidget.South,
@ -1331,12 +1340,18 @@ class Position(MappingType):
'right': QTabWidget.East, 'right': QTabWidget.East,
} }
def __init__(self, none_ok=False):
self.valid_values = ValidValues('top', 'bottom', 'left', 'right')
super().__init__(none_ok)
class VerticalPosition(BaseType): class VerticalPosition(BaseType):
"""The position of the download bar.""" """The position of the download bar."""
valid_values = ValidValues('top', 'bottom') def __init__(self, none_ok=False):
self.valid_values = ValidValues('top', 'bottom')
super().__init__(none_ok)
class UrlList(List): class UrlList(List):
@ -1382,17 +1397,19 @@ class SelectOnRemove(MappingType):
special = True special = True
valid_values = ValidValues(
('left', "Select the tab on the left."),
('right', "Select the tab on the right."),
('previous', "Select the previously selected tab."))
MAPPING = { MAPPING = {
'left': QTabBar.SelectLeftTab, 'left': QTabBar.SelectLeftTab,
'right': QTabBar.SelectRightTab, 'right': QTabBar.SelectRightTab,
'previous': QTabBar.SelectPreviousTab, 'previous': QTabBar.SelectPreviousTab,
} }
def __init__(self, none_ok=False):
self.valid_values = ValidValues(
('left', "Select the tab on the left."),
('right', "Select the tab on the right."),
('previous', "Select the previously selected tab."))
super().__init__(none_ok)
class ConfirmQuit(FlagList): class ConfirmQuit(FlagList):
@ -1400,15 +1417,19 @@ class ConfirmQuit(FlagList):
special = True special = True
valid_values = ValidValues(('always', "Always show a confirmation."),
('multiple-tabs', "Show a confirmation if "
"multiple tabs are opened."),
('downloads', "Show a confirmation if "
"downloads are running"),
('never', "Never show a confirmation."))
# Values that can be combined with commas # Values that can be combined with commas
combinable_values = ('multiple-tabs', 'downloads') combinable_values = ('multiple-tabs', 'downloads')
def __init__(self, none_ok=False):
self.valid_values = ValidValues(
('always', "Always show a confirmation."),
('multiple-tabs', "Show a confirmation if "
"multiple tabs are opened."),
('downloads', "Show a confirmation if "
"downloads are running"),
('never', "Never show a confirmation."))
super().__init__(none_ok)
def validate(self, value): def validate(self, value):
super().validate(value) super().validate(value)
if not value: if not value:
@ -1431,11 +1452,13 @@ class NewTabPosition(BaseType):
special = True special = True
valid_values = ValidValues( def __init__(self, none_ok=False):
('left', "On the left of the current tab."), self.valid_values = ValidValues(
('right', "On the right of the current tab."), ('left', "On the left of the current tab."),
('first', "At the left end."), ('right', "On the right of the current tab."),
('last', "At the right end.")) ('first', "At the left end."),
('last', "At the right end."))
super().__init__(none_ok)
class IgnoreCase(Bool): class IgnoreCase(Bool):
@ -1444,11 +1467,13 @@ class IgnoreCase(Bool):
special = True special = True
valid_values = ValidValues( def __init__(self, none_ok=False):
('true', "Search case-insensitively"), self.valid_values = ValidValues(
('false', "Search case-sensitively"), ('true', "Search case-insensitively"),
('smart', "Search case-sensitively if there " ('false', "Search case-sensitively"),
"are capital chars")) ('smart', "Search case-sensitively if there "
"are capital chars"))
super().__init__(none_ok)
def transform(self, value): def transform(self, value):
if value.lower() == 'smart': if value.lower() == 'smart':