qutebrowser/qutebrowser/completion/models/configmodel.py

96 lines
3.4 KiB
Python
Raw Normal View History

2014-06-21 22:41:04 +02:00
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
2014-06-19 09:04:37 +02:00
2017-05-09 21:37:03 +02:00
# Copyright 2014-2017 Florian Bruhin (The Compiler) <mail@qutebrowser.org>
2014-02-06 14:01:23 +01:00
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
2014-01-29 15:30:19 +01:00
"""Functions that return config-related completion models."""
2014-04-17 17:44:27 +02:00
from qutebrowser.config import configdata, configexc
from qutebrowser.completion.models import completionmodel
from qutebrowser.utils import objreg
2014-01-27 21:42:00 +01:00
def section():
2014-04-25 12:29:00 +02:00
"""A CompletionModel filled with settings sections."""
model = completionmodel.CompletionModel(column_widths=(20, 70, 10))
sections = ((name, configdata.SECTION_DESC[name].splitlines()[0].strip())
for name in configdata.DATA)
model.add_list("Sections", sections)
return model
2014-02-07 20:21:50 +01:00
2014-02-19 10:58:32 +01:00
def option(sectname):
2014-05-02 16:23:11 +02:00
"""A CompletionModel filled with settings and their descriptions.
Args:
sectname: The name of the config section this model shows.
2014-05-02 16:23:11 +02:00
"""
model = completionmodel.CompletionModel(column_widths=(20, 70, 10))
try:
sectdata = configdata.DATA[sectname]
except KeyError:
return None
options = []
for name in sectdata:
2014-05-02 16:23:51 +02:00
try:
desc = sectdata.descriptions[name]
except (KeyError, AttributeError):
# Some stuff (especially ValueList items) don't have a
# description.
desc = ""
else:
desc = desc.splitlines()[0]
config = objreg.get('config')
val = config.get(sectname, name, raw=True)
options.append((name, desc, val))
model.add_list(sectname, options)
return model
2014-02-19 10:58:32 +01:00
def value(sectname, optname):
"""A CompletionModel filled with setting values.
Args:
sectname: The name of the config section this model shows.
optname: The name of the config option this model shows.
"""
model = completionmodel.CompletionModel(column_widths=(20, 70, 10))
config = objreg.get('config')
try:
current = config.get(sectname, optname, raw=True) or '""'
except (configexc.NoSectionError, configexc.NoOptionError):
return None
default = configdata.DATA[sectname][optname].default() or '""'
if hasattr(configdata.DATA[sectname], 'valtype'):
# Same type for all values (ValueList)
vals = configdata.DATA[sectname].valtype.complete()
else:
if optname is None:
raise ValueError("optname may only be None for ValueList "
"sections, but {} is not!".format(sectname))
# Different type for each value (KeyValue)
vals = configdata.DATA[sectname][optname].typ.complete()
model.add_list("Current/Default", [(current, "Current value"),
(default, "Default value")])
if vals is not None:
model.add_list("Completions", vals)
return model