Skip to content

Commit

Permalink
config: add Config.get_defined_sections
Browse files Browse the repository at this point in the history
  • Loading branch information
Exirel committed Oct 30, 2020
1 parent 5002183 commit 096135e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
24 changes: 24 additions & 0 deletions sopel/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,30 @@ def homedir(self):
else:
return os.path.dirname(self.filename)

def get_defined_sections(self):
"""Retrieve all defined static sections of this configuration.
:return: all instances of :class:`~sopel.config.types.StaticSection`
defined for this configuration file
:rtype: list
When a plugin defines a section (using :meth:`define_section`), it
associates a :class:`~sopel.config.types.StaticSection` for the section.
This method allows to retrieve these instances of ``StaticSection``,
and only these.
.. versionadded:: 7.1
"""
sections = (
(name, getattr(self, name))
for name in self.parser.sections()
)
return [
(name, section)
for name, section in sections
if isinstance(section, types.StaticSection)
]

def save(self):
"""Write all changes to the config file.
Expand Down
15 changes: 9 additions & 6 deletions sopel/config/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,23 @@ def __init__(self, config, section_name, validate=True):
self._parent = config
self._parser = config.parser
self._section_name = section_name

for value in dir(self):
if value in ('_parent', '_parser', '_section_name'):
# ignore internal attributes
continue

try:
getattr(self, value)
except ValueError as e:
raise ValueError(
'Invalid value for {}.{}: {}'.format(section_name, value,
str(e))
)
'Invalid value for {}.{}: {}'.format(
section_name, value, str(e)))
except AttributeError:
if validate:
raise ValueError(
'Missing required value for {}.{}'.format(section_name,
value)
)
'Missing required value for {}.{}'.format(
section_name, value))

def configure_setting(self, name, prompt, default=NO_DEFAULT):
"""Return a validated value for this attribute from the terminal.
Expand Down
21 changes: 21 additions & 0 deletions test/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@
"#startquote
&endquote"
"&quoted"
[somesection]
is_defined = no
""" # noqa (trailing whitespaces are intended)

TEST_CHANNELS = [
Expand Down Expand Up @@ -343,3 +346,21 @@ def test_save_modified_config(multi_fakeconfig):
'&endquote"',
'"&quoted"', # doesn't start with a # so it isn't escaped
]


def test_get_defined_sections(multi_fakeconfig):
assert multi_fakeconfig.parser.has_section('core')
assert multi_fakeconfig.parser.has_section('fake')
assert multi_fakeconfig.parser.has_section('spam')
assert multi_fakeconfig.parser.has_section('somesection')

results = multi_fakeconfig.get_defined_sections()

assert len(results) == 3, 'There should be 3 defined sections'

items = dict(results)
assert 'core' in items, 'core must be always defined'
assert 'fake' in items
assert 'spam' in items
assert 'somesection' not in items, (
'somesection was not defined and should not appear as such')

0 comments on commit 096135e

Please sign in to comment.