Use ConfigParser-like exceptions for config
This commit is contained in:
parent
2442cf11ab
commit
6175f5c489
@ -144,7 +144,7 @@ class CommandParser:
|
|||||||
if aliases:
|
if aliases:
|
||||||
try:
|
try:
|
||||||
alias = config.config.get('aliases', cmdstr)
|
alias = config.config.get('aliases', cmdstr)
|
||||||
except KeyError:
|
except (config.NoOptionError, config.NoSectionError):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
return self.parse(alias, aliases=False)
|
return self.parse(alias, aliases=False)
|
||||||
|
@ -28,7 +28,7 @@ import os.path
|
|||||||
import logging
|
import logging
|
||||||
import textwrap
|
import textwrap
|
||||||
import configparser
|
import configparser
|
||||||
from configparser import ConfigParser, ExtendedInterpolation, NoSectionError
|
from configparser import ConfigParser, ExtendedInterpolation
|
||||||
|
|
||||||
#from qutebrowser.utils.misc import read_file
|
#from qutebrowser.utils.misc import read_file
|
||||||
import qutebrowser.config.configdata as configdata
|
import qutebrowser.config.configdata as configdata
|
||||||
@ -42,6 +42,20 @@ state = None
|
|||||||
_UNSET = object()
|
_UNSET = object()
|
||||||
|
|
||||||
|
|
||||||
|
class NoSectionError(configparser.NoSectionError):
|
||||||
|
|
||||||
|
"""Exception raised when a section was not found."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NoOptionError(configparser.NoOptionError):
|
||||||
|
|
||||||
|
"""Exception raised when an option was not found."""
|
||||||
|
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
def init(configdir):
|
def init(configdir):
|
||||||
"""Initialize the global objects based on the config in configdir.
|
"""Initialize the global objects based on the config in configdir.
|
||||||
|
|
||||||
@ -162,7 +176,11 @@ class Config:
|
|||||||
return lines
|
return lines
|
||||||
|
|
||||||
def has_option(self, section, option):
|
def has_option(self, section, option):
|
||||||
"""Return True if option is in section."""
|
"""Return True if option is in section.
|
||||||
|
|
||||||
|
Return False if section does not exist."""
|
||||||
|
if section not in self.config:
|
||||||
|
return False
|
||||||
return option in self.config[section]
|
return option in self.config[section]
|
||||||
|
|
||||||
@cmdutils.register(name='get', instance='config',
|
@cmdutils.register(name='get', instance='config',
|
||||||
@ -195,17 +213,22 @@ class Config:
|
|||||||
"""
|
"""
|
||||||
logging.debug("getting {} -> {}".format(section, option))
|
logging.debug("getting {} -> {}".format(section, option))
|
||||||
try:
|
try:
|
||||||
val = self.config[section][option]
|
sect = self.config[section]
|
||||||
except KeyError:
|
except KeyError:
|
||||||
if fallback is _UNSET:
|
if fallback is _UNSET:
|
||||||
raise
|
raise NoSectionError(section)
|
||||||
else:
|
else:
|
||||||
return fallback
|
return fallback
|
||||||
|
try:
|
||||||
|
val = sect[option]
|
||||||
|
except KeyError:
|
||||||
|
if fallback is _UNSET:
|
||||||
|
raise NoOptionError(option, section)
|
||||||
else:
|
else:
|
||||||
|
return fallback
|
||||||
if raw:
|
if raw:
|
||||||
return val.value
|
return val.value
|
||||||
mapping = {key: val.value
|
mapping = {key: val.value for key, val in sect.values.items()}
|
||||||
for key, val in self.config[section].values.items()}
|
|
||||||
newval = self._interpolation.before_get(self, section, option,
|
newval = self._interpolation.before_get(self, section, option,
|
||||||
val.value, mapping)
|
val.value, mapping)
|
||||||
logging.debug("interpolated val: {}".format(newval))
|
logging.debug("interpolated val: {}".format(newval))
|
||||||
|
Loading…
Reference in New Issue
Block a user