From 175eabdc80d31a1613d8ddac2b28ba86aca7a0f2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 10 Apr 2014 14:40:02 +0200 Subject: [PATCH] Remove fallback values from config --- qutebrowser/commands/parsers.py | 4 ++-- qutebrowser/config/config.py | 25 +++++++++---------------- qutebrowser/utils/url.py | 23 +++++++++++++++-------- qutebrowser/widgets/browsertab.py | 7 ++++++- 4 files changed, 32 insertions(+), 27 deletions(-) diff --git a/qutebrowser/commands/parsers.py b/qutebrowser/commands/parsers.py index 2392fe052..5a8488e2e 100644 --- a/qutebrowser/commands/parsers.py +++ b/qutebrowser/commands/parsers.py @@ -64,9 +64,9 @@ class SearchParser(QObject): self.do_search.emit('', 0) self._text = text self._flags = 0 - if config.config.get('general', 'ignorecase', fallback=True): + if config.config.get('general', 'ignorecase'): self._flags |= QWebPage.FindCaseSensitively - if config.config.get('general', 'wrapsearch', fallback=True): + if config.config.get('general', 'wrapsearch'): self._flags |= QWebPage.FindWrapsAroundDocument if rev: self._flags |= QWebPage.FindBackward diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 158996319..b48eb4930 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -39,9 +39,6 @@ from qutebrowser.config.conftypes import ValidationError config = None state = None -# Special value for an unset fallback, so None can be passed as fallback. -_UNSET = object() - class NoSectionError(configparser.NoSectionError): @@ -225,30 +222,23 @@ class Config: val = self.get(section, option) message.info("{} {} = {}".format(section, option, val)) - def get(self, section, option, fallback=_UNSET, raw=False): + def get(self, section, option, raw=False): """Get the value from a section/option. Arguments: section: The section to get the option from. option: The option name - fallback: A fallback value. raw: Whether to get the uninterpolated, untransformed value. """ logging.debug("getting {} -> {}".format(section, option)) try: sect = self.config[section] except KeyError: - if fallback is _UNSET: - raise NoSectionError(section) - else: - return fallback + raise NoSectionError(section) try: val = sect[option] except KeyError: - if fallback is _UNSET: - raise NoOptionError(option, section) - else: - return fallback + raise NoOptionError(option, section) if raw: return val.value mapping = {key: val.value for key, val in sect.values.items()} @@ -409,15 +399,18 @@ class SectionProxy(MutableMapping): """Get the option keys from this section.""" return self._conf.config[self._name].values.keys() - def get(self, option, fallback=_UNSET, *, raw=False): + def get(self, option, *, raw=False): """Get a value from this section. + We deliberately don't support the default argument here, but have a raw + argument instead. + Arguments: option: The option name to get. - fallback: A fallback value. raw: Whether to get a raw value or not. """ - return self._conf.get(self._name, option, raw=raw, fallback=fallback) + # pylint: disable=arguments-differ + return self._conf.get(self._name, option, raw=raw) @property def conf(self): diff --git a/qutebrowser/utils/url.py b/qutebrowser/utils/url.py index a3f9b391d..0d249e453 100644 --- a/qutebrowser/utils/url.py +++ b/qutebrowser/utils/url.py @@ -27,6 +27,13 @@ from PyQt5.QtCore import QUrl import qutebrowser.config.config as config +class SearchEngineError(Exception): + + """Exception raised when a search engine wasn't found.""" + + pass + + def _get_search_url(txt): """Get a search engine URL for a text. @@ -37,26 +44,26 @@ def _get_search_url(txt): The search URL as a QUrl. Raise: - ValueError if there is no template or no search term was found. + SearchEngineError if there is no template or no search term was found. """ logging.debug('Finding search engine for "{}"'.format(txt)) r = re.compile(r'(^|\s+)!(\w+)($|\s+)') m = r.search(txt) if m: engine = m.group(2) - # FIXME why doesn't fallback work?! - template = config.config.get('searchengines', engine, fallback=None) + try: + template = config.config.get('searchengines', engine) + except config.NoOptionError: + raise SearchEngineError("Search engine {} not found!".format( + engine)) term = r.sub('', txt) logging.debug('engine {}, term "{}"'.format(engine, term)) else: - template = config.config.get('searchengines', 'DEFAULT', - fallback=None) + template = config.config.get('searchengines', 'DEFAULT') term = txt logging.debug('engine: default, term "{}"'.format(txt)) - if template is None: - raise ValueError("Search template is None") if not term: - raise ValueError("No search term given") + raise SearchEngineError("No search term given") return QUrl.fromUserInput(template.format(urllib.parse.quote(term))) diff --git a/qutebrowser/widgets/browsertab.py b/qutebrowser/widgets/browsertab.py index 9a371ae29..251bdd647 100644 --- a/qutebrowser/widgets/browsertab.py +++ b/qutebrowser/widgets/browsertab.py @@ -26,6 +26,7 @@ from PyQt5.QtWebKitWidgets import QWebView, QWebPage import qutebrowser.utils.url as urlutils import qutebrowser.config.config as config +import qutebrowser.utils.message as message from qutebrowser.widgets.browserpage import BrowserPage from qutebrowser.utils.signals import SignalCache from qutebrowser.utils.usertypes import NeighborList @@ -93,7 +94,11 @@ class BrowserTab(QWebView): Emit: titleChanged and urlChanged """ - u = urlutils.fuzzy_url(url) + try: + u = urlutils.fuzzy_url(url) + except urlutils.SearchEngineError as e: + message.error(str(e)) + return logging.debug('New title: {}'.format(urlutils.urlstring(u))) self.titleChanged.emit(urlutils.urlstring(u)) self.urlChanged.emit(urlutils.qurl(u))