From b2b23e032a6a56cc383be32d40177cfedaf248d2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 26 Feb 2014 22:36:06 +0100 Subject: [PATCH] Implement __str__ for Config, delete unneeded __str__'s --- qutebrowser/config/config.py | 10 ++++++++++ qutebrowser/config/sections.py | 11 ++++------- qutebrowser/config/templates.py | 11 +++++------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 40e26473c..330d6ccf9 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -123,6 +123,16 @@ class NewConfig: """Get a section from the config.""" return self.config[key] + def __str__(self): + """Get the whole config as a string.""" + # FIXME this should also generate nice comments + lines = [] + for secname, section in self.config.items(): + lines.append('[{}]'.format(secname)) + for optname, option in section.items(): + lines.append('{} = {}'.format(optname, option)) + return '\n'.join(lines) + def get(self, section, option, fallback=_UNSET): """Get the real (transformed) value from a section/option.""" try: diff --git a/qutebrowser/config/sections.py b/qutebrowser/config/sections.py index 10c4662a5..4b0f207a4 100644 --- a/qutebrowser/config/sections.py +++ b/qutebrowser/config/sections.py @@ -49,13 +49,6 @@ class KeyValue: if args: self.values = OrderedDict(args) - def __str__(self): - """Get the key = value pairs as a string.""" - # FIXME implement that in some toini() method - #return '\n'.join('{} = {}'.format(key, val.rawvalue) - # for key, val in self.values.items()) - return str(self.values) - def __getitem__(self, key): """Get the value for key. @@ -83,6 +76,10 @@ class KeyValue: # FIXME using a custon iterator this could be done more efficiently return self.values.__iter__() + def items(self): + """Get dict item tuples.""" + return self.values.items() + class SearchEngines(template.ValueListSection): diff --git a/qutebrowser/config/templates.py b/qutebrowser/config/templates.py index feadd57f1..5bbc99ed6 100644 --- a/qutebrowser/config/templates.py +++ b/qutebrowser/config/templates.py @@ -61,7 +61,11 @@ class SettingValue: def __str__(self): """Get raw string value.""" - return self.rawvalue if self.rawvalue is not None else '' + if self.rawvalue is not None: + val = self.rawvalue + else: + val = self.default + return val @property def value(self): @@ -254,11 +258,6 @@ class ValueListSection: self.default = {self.types[0](key): self.types[1](value) for key, value in self.default.items()} - def __str__(self): - """Get the key = value pairs as a string.""" - return '\n'.join('{} = {}'.format(key.rawvalue, val.rawvalue) - for key, val in self.values) - def __getitem__(self, key): """Get the value for key.