diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index f0cc1be0e..a54cb4dee 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -52,8 +52,7 @@ def init(confdir): class NewConfig: - """Contains the structure of the config file.""" - + """New configuration manager.""" def __init__(self): self.config = configdata.configdata() @@ -63,7 +62,6 @@ class NewConfig: 'break_long_words': False, 'break_on_hyphens': False, } - print(str(self)) def __getitem__(self, key): """Get a section from the config.""" @@ -80,6 +78,7 @@ class NewConfig: return '\n'.join(lines) def _str_section_desc(self, secname): + """Get the section description string for secname.""" wrapper = textwrap.TextWrapper(initial_indent='# ', subsequent_indent='# ', **self._wrapper_args) @@ -93,6 +92,7 @@ class NewConfig: return lines def _str_option_desc(self, secname, section): + """Get the option description strings for section/secname.""" wrapper = textwrap.TextWrapper(initial_indent='#' + ' ' * 5, subsequent_indent='#' + ' ' * 5, **self._wrapper_args) @@ -126,9 +126,10 @@ class NewConfig: return lines def _str_items(self, section): + """Get the option items as string for section.""" keyval_wrapper = textwrap.TextWrapper(initial_indent='', subsequent_indent=' ' * 4, - drop_whitespace = False, + drop_whitespace=False, **self._wrapper_args) lines = [] for optname, option in section.items(): diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index e4c620f20..6167d3e83 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -94,213 +94,206 @@ SECTION_DESC = { def configdata(): + """Get the config structure as an OrderedDict.""" MONOSPACE = ('Monospace, "DejaVu Sans Mono", Consolas, Monaco, ' '"Bitstream Vera Sans Mono", "Andale Mono", ' '"Liberation Mono", "Courier New", Courier, monospace, ' 'Fixed, Terminal') return OrderedDict([ - ('general', sect.KeyValue( - ('show_completion', - SettingValue(types.Bool, "true"), - "Whether to show the autocompletion window or not."), + ('general', sect.KeyValue( + ('show_completion', + SettingValue(types.Bool, "true"), + "Whether to show the autocompletion window or not."), - ('completion_height', - SettingValue(types.PercOrInt, "50%"), - "The height of the completion, in px or as percentage of the " - "window."), + ('completion_height', + SettingValue(types.PercOrInt, "50%"), + "The height of the completion, in px or as percentage of the " + "window."), - ('ignorecase', - SettingValue(types.Bool, "true"), - "Whether to do case-insensitive searching."), + ('ignorecase', + SettingValue(types.Bool, "true"), + "Whether to do case-insensitive searching."), - ('wrapsearch', - SettingValue(types.Bool, "true"), - "Whether to wrap search to the top when arriving at the " - "end."), + ('wrapsearch', + SettingValue(types.Bool, "true"), + "Whether to wrap search to the top when arriving at the end."), - ('startpage', - SettingValue(types.List, "http://www.duckduckgo.com"), - "The default page(s) to open at the start, separated with " - "commas."), + ('startpage', + SettingValue(types.List, "http://www.duckduckgo.com"), + "The default page(s) to open at the start, separated with " + "commas."), - ('auto_search', - SettingValue(types.AutoSearch, "naive"), - "Whether to start a search when something else than an URL " - "is entered."), + ('auto_search', + SettingValue(types.AutoSearch, "naive"), + "Whether to start a search when something else than an URL is " + "entered."), - ('zoomlevels', - SettingValue(types.IntList, "25,33,50,67,75,90,100,110,125," - "150,175,200,250,300,400,500"), - "The available zoom levels, separated by commas."), + ('zoomlevels', + SettingValue(types.IntList, "25,33,50,67,75,90,100,110,125,150," + "175,200,250,300,400,500"), + "The available zoom levels, separated by commas."), - ('defaultzoom', - SettingValue(types.Int, "100"), - "The default zoom level."), - )), + ('defaultzoom', + SettingValue(types.Int, "100"), + "The default zoom level."), + )), - ('tabbar', sect.KeyValue( - ('movable', - SettingValue(types.Bool, "true"), - "Whether tabs should be movable."), + ('tabbar', sect.KeyValue( + ('movable', + SettingValue(types.Bool, "true"), + "Whether tabs should be movable."), - ('closebuttons', - SettingValue(types.Bool, "false"), - "Whether tabs should have close-buttons."), + ('closebuttons', + SettingValue(types.Bool, "false"), + "Whether tabs should have close-buttons."), - ('scrollbuttons', - SettingValue(types.Bool, "true"), - "Whether there should be scroll buttons if there are too " - "many tabs."), + ('scrollbuttons', + SettingValue(types.Bool, "true"), + "Whether there should be scroll buttons if there are too many " + "tabs."), - ('position', - SettingValue(types.Position, "north"), - "The position of the tab bar."), + ('position', + SettingValue(types.Position, "north"), + "The position of the tab bar."), - ('select_on_remove', - SettingValue(types.SelectOnRemove, "previous"), - "Which tab to select when the focused tab is removed."), + ('select_on_remove', + SettingValue(types.SelectOnRemove, "previous"), + "Which tab to select when the focused tab is removed."), - ('last_close', - SettingValue(types.LastClose, "ignore"), - "Behaviour when the last tab is closed."), - )), + ('last_close', + SettingValue(types.LastClose, "ignore"), + "Behaviour when the last tab is closed."), + )), - ('searchengines', sect.SearchEngines()), + ('searchengines', sect.SearchEngines()), - ('keybind', sect.KeyBindings()), + ('keybind', sect.KeyBindings()), - ('aliases', sect.Aliases()), + ('aliases', sect.Aliases()), - ('colors', sect.KeyValue( - ('completion.fg', - SettingValue(types.Color, "#333333"), - "Text color of the completion widget."), + ('colors', sect.KeyValue( + ('completion.fg', + SettingValue(types.Color, "#333333"), + "Text color of the completion widget."), - ('completion.item.bg', - SettingValue(types.Color, "white"), - "Background color of completion widget items."), + ('completion.item.bg', + SettingValue(types.Color, "white"), + "Background color of completion widget items."), - ('completion.category.bg', - SettingValue( - types.Color, - "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e4e4e4, " - "stop:1 #dbdbdb)"), - "Background color of the completion widget category " - "headers."), + ('completion.category.bg', + SettingValue( + types.Color, + "qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #e4e4e4, " + "stop:1 #dbdbdb)"), + "Background color of the completion widget category headers."), - ('completion.category.border.top', - SettingValue(types.Color, "#808080"), - "Top border color of the completion widget category " - "headers."), + ('completion.category.border.top', + SettingValue(types.Color, "#808080"), + "Top border color of the completion widget category headers."), - ('completion.category.border.bottom', - SettingValue(types.Color, "#bbbbbb"), - "Bottom border color of the completion widget category " - "headers."), + ('completion.category.border.bottom', + SettingValue(types.Color, "#bbbbbb"), + "Bottom border color of the completion widget category headers."), - ('completion.item.selected.fg', - SettingValue(types.Color, "#333333"), - "Foreground color of the selected completion item."), + ('completion.item.selected.fg', + SettingValue(types.Color, "#333333"), + "Foreground color of the selected completion item."), - ('completion.item.selected.bg', - SettingValue(types.Color, "#ffec8b"), - "Background color of the selected completion item."), + ('completion.item.selected.bg', + SettingValue(types.Color, "#ffec8b"), + "Background color of the selected completion item."), - ('completion.item.selected.border.top', - SettingValue(types.Color, "#f2f2c0"), - "Top border color of the completion widget category " - "headers."), + ('completion.item.selected.border.top', + SettingValue(types.Color, "#f2f2c0"), + "Top border color of the completion widget category headers."), - ('completion.item.selected.border.bottom', - SettingValue(types.Color, "#ffec8b"), - "Bottom border color of the selected completion item."), + ('completion.item.selected.border.bottom', + SettingValue(types.Color, "#ffec8b"), + "Bottom border color of the selected completion item."), - ('completion.match.fg', - SettingValue(types.Color, "red"), - "Foreground color of the matched text in the completion."), + ('completion.match.fg', + SettingValue(types.Color, "red"), + "Foreground color of the matched text in the completion."), - ('statusbar.bg', - SettingValue(types.Color, "black"), - "Foreground color of the statusbar."), + ('statusbar.bg', + SettingValue(types.Color, "black"), + "Foreground color of the statusbar."), - ('statusbar.fg', - SettingValue(types.Color, "white"), - "Foreground color of the statusbar."), + ('statusbar.fg', + SettingValue(types.Color, "white"), + "Foreground color of the statusbar."), - ('statusbar.bg.error', - SettingValue(types.Color, "red"), - "Background color of the statusbar if there was an error."), + ('statusbar.bg.error', + SettingValue(types.Color, "red"), + "Background color of the statusbar if there was an error."), - ('statusbar.fg.error', - SettingValue(types.Color, "white", "${statusbar.fg}"), - "Foreground color of the statusbar if there was an error."), + ('statusbar.fg.error', + SettingValue(types.Color, "white", "${statusbar.fg}"), + "Foreground color of the statusbar if there was an error."), - ('statusbar.progress.bg', - SettingValue(types.Color, "white"), - "Background color of the progress bar."), + ('statusbar.progress.bg', + SettingValue(types.Color, "white"), + "Background color of the progress bar."), - ('statusbar.url.fg', - SettingValue(types.Color, "white", "${statusbar.fg}"), - "Default foreground color of the URL in the statusbar."), + ('statusbar.url.fg', + SettingValue(types.Color, "white", "${statusbar.fg}"), + "Default foreground color of the URL in the statusbar."), - ('statusbar.url.fg.success', - SettingValue(types.Color, "lime"), - "Foreground color of the URL in the statusbar on successful " - "load."), + ('statusbar.url.fg.success', + SettingValue(types.Color, "lime"), + "Foreground color of the URL in the statusbar on successful " + "load."), - ('statusbar.url.fg.error', - SettingValue(types.Color, "orange"), - "Foreground color of the URL in the statusbar on error."), + ('statusbar.url.fg.error', + SettingValue(types.Color, "orange"), + "Foreground color of the URL in the statusbar on error."), - ('statusbar.url.fg.warn', - SettingValue(types.Color, "yellow"), - "Foreground color of the URL in the statusbar when there's a " - "warning."), + ('statusbar.url.fg.warn', + SettingValue(types.Color, "yellow"), + "Foreground color of the URL in the statusbar when there's a " + "warning."), - ('statusbar.url.fg.hover', - SettingValue(types.Color, "aqua"), - "Foreground color of the URL in the statusbar for hovered " - "links."), + ('statusbar.url.fg.hover', + SettingValue(types.Color, "aqua"), + "Foreground color of the URL in the statusbar for hovered " + "links."), - ('tab.fg', - SettingValue(types.Color, "white"), - "Foreground color of the tabbar."), + ('tab.fg', + SettingValue(types.Color, "white"), + "Foreground color of the tabbar."), - ('tab.bg', - SettingValue(types.Color, "grey"), - "Background color of the tabbar."), + ('tab.bg', + SettingValue(types.Color, "grey"), + "Background color of the tabbar."), - ('tab.bg.selected', - SettingValue(types.Color, "black"), - "Background color of the tabbar for the selected tab."), + ('tab.bg.selected', + SettingValue(types.Color, "black"), + "Background color of the tabbar for the selected tab."), - ('tab.seperator', - SettingValue(types.Color, "white"), - "Color for the tab seperator."), - )), + ('tab.seperator', + SettingValue(types.Color, "white"), + "Color for the tab seperator."), + )), - ('fonts', sect.KeyValue( - ('_monospace', - SettingValue(types.Font, MONOSPACE), - "Default monospace fonts."), + ('fonts', sect.KeyValue( + ('_monospace', + SettingValue(types.Font, MONOSPACE), + "Default monospace fonts."), - ('completion', - SettingValue(types.Font, "8pt " + MONOSPACE, - "8pt ${_monospace}"), - "Font used in the completion widget."), + ('completion', + SettingValue(types.Font, "8pt " + MONOSPACE, "8pt ${_monospace}"), + "Font used in the completion widget."), - ('tabbar', - SettingValue(types.Font, "8pt " + MONOSPACE, - "8pt ${_monospace}"), - "Font used in the tabbar."), + ('tabbar', + SettingValue(types.Font, "8pt " + MONOSPACE, "8pt ${_monospace}"), + "Font used in the tabbar."), - ('statusbar', - SettingValue(types.Font, "8pt " + MONOSPACE, - "8pt ${_monospace}"), - "Font used in the statusbar."), + ('statusbar', + SettingValue(types.Font, "8pt " + MONOSPACE, "8pt ${_monospace}"), + "Font used in the statusbar."), - )), - ]) + )), + ]) class SettingValue: diff --git a/qutebrowser/config/conftypes.py b/qutebrowser/config/conftypes.py index 09dcd2553..048e38331 100644 --- a/qutebrowser/config/conftypes.py +++ b/qutebrowser/config/conftypes.py @@ -29,7 +29,7 @@ class BaseType: string. Either a list of strings, or a list of (value, desc) tuples. # FIXME actually handle tuples and stuff when validating - show_valid_values; Whether to show valid values in config or not. + show_valid_values" Whether to show valid values in config or not. typestr: The name of the type to appear in the config. """ @@ -45,18 +45,24 @@ class BaseType: The default implementation returns the original value. + Args: + value: The original string value. + Return: The transformed value. """ return value - def validate(self): + def validate(self, value): """Validate value against possible values. The default implementation checks the value against self.valid_values if it was defined. + Args: + value: The value to validate. + Return: Ture if validation succeeded, False otherwise. @@ -66,10 +72,11 @@ class BaseType: """ if self.valid_values is not None: - return self.value in self.valid_values + return value in self.valid_values else: raise NotImplementedError + class String(BaseType): """Base class for a string setting (case-insensitive).""" @@ -95,8 +102,8 @@ class Bool(BaseType): def transform(self, value): return self._BOOLEAN_STATES[value.lower()] - def validate(self): - return self.value.lower() in self._BOOLEAN_STATES + def validate(self, value): + return value.lower() in self._BOOLEAN_STATES class Int(BaseType): @@ -108,9 +115,9 @@ class Int(BaseType): def transform(self, value): return int(value) - def validate(self): + def validate(self, value): try: - int(self.value) + int(value) except ValueError: return False else: @@ -126,7 +133,7 @@ class List(BaseType): def transform(self, value): return value.split(',') - def validate(self): + def validate(self, value): return True @@ -140,9 +147,9 @@ class IntList(List): vals = super().transform(value) return map(int, vals) - def validate(self): + def validate(self, value): try: - self.transform(self.value) + self.transform(value) except ValueError: return False else: @@ -153,17 +160,17 @@ class PercOrInt(BaseType): """Percentage or integer.""" - def validate(self): - if self.value.endswith('%'): + def validate(self, value): + if value.endswith('%'): try: - intval = int(self.value.rstrip('%')) + intval = int(value.rstrip('%')) except ValueError: return False else: return 0 <= intval <= 100 else: try: - intval = int(self.value) + intval = int(value) except ValueError: return False else: @@ -178,13 +185,13 @@ class Command(BaseType): valid_values = cmdutils.cmd_dict.items() - def validate(self): + def validate(self, value): # We need to import this here to avoid circular dependencies from qutebrowser.commands.parsers import (CommandParser, NoSuchCommandError) cp = CommandParser() try: - cp.parse(self.value) + cp.parse(value) except NoSuchCommandError: return False else: @@ -197,7 +204,7 @@ class Color(BaseType): typestr = 'color' - def validate(self): + def validate(self, value): # FIXME validate colors return True @@ -208,7 +215,7 @@ class Font(BaseType): typestr = 'font' - def validate(self): + def validate(self, value): # FIXME validate fonts return True @@ -217,7 +224,7 @@ class SearchEngineName(BaseType): """A search engine name.""" - def validate(self): + def validate(self, value): return True @@ -225,20 +232,19 @@ class SearchEngineUrl(BaseType): """A search engine URL.""" - def validate(self): - return "{}" in self.value + def validate(self, value): + return "{}" in value class KeyBindingName(BaseType): """The name (keys) of a keybinding.""" - def validate(self): + def validate(self, value): # FIXME can we validate anything here? return True - class AutoSearch(BaseType): """Whether to start a search when something else than an URL is entered.""" @@ -247,11 +253,11 @@ class AutoSearch(BaseType): ("dns", "Use DNS requests (might be slow!)."), ("false", "Never search automatically.")] - def validate(self): - if self.value.lower() in ["naive", "dns"]: + def validate(self, value): + if value.lower() in ["naive", "dns"]: return True else: - return Bool.validate(self, self.value) + return Bool.validate(self, value) def transform(self, value): if value.lower() in ["naive", "dns"]: @@ -293,5 +299,3 @@ class KeyBinding(Command): """The command of a keybinding.""" pass - - diff --git a/qutebrowser/config/sections.py b/qutebrowser/config/sections.py index 031d7327e..df66883d9 100644 --- a/qutebrowser/config/sections.py +++ b/qutebrowser/config/sections.py @@ -77,7 +77,7 @@ class KeyValue: def __iter__(self): """Iterate over all set values.""" - # FIXME using a custon iterator this could be done more efficiently + # FIXME using a custom iterator this could be done more efficiently. return self.values.__iter__() def __bool__(self): @@ -124,9 +124,11 @@ class ValueList: self.default = OrderedDict( [(keytype.transform(key), valtype.transform(value)) for key, value in self.default.items()]) + self.valdict = OrderedDict() def update_valdict(self): - self.valdict = OrderedDict() + """Update the global "true" value dict.""" + self.valdict.clear() self.valdict.update(self.default) if self.values is not None: self.valdict.update(self.values)