Configuration functionality#
The description of config
below is a quick guide to the basic structure
of configuration settings in Sopel. Consult the config.types
module for
more specific information about the available kinds of options, and
config.core_section
for examples of how the different setting types are
practically used.
sopel.config#
Sopel’s configuration module.
The Config
object provides an interface to access Sopel’s
configuration file. It exposes the configuration’s sections through its
attributes as objects, which in turn expose their directives through their
attributes.
For example, this is how to access core.nick
on a Config
object:
>>> from sopel import config
>>> settings = config.Config('/sopel/config.cfg')
>>> settings.core.nick
'Sopel'
The configuration file being:
[core]
nick = Sopel
host = irc.libera.chat
use_ssl = true
port = 6697
owner = dgw
channels =
"#sopel"
A section can be represented by a subclass of
StaticSection
; for example a [spam]
section
with eggs
and bacon
can be defined like this:
from sopel import config
class SpamSection(config.types.StaticSection):
eggs = config.types.ListAttribute('eggs')
bacon = config.types.ValidatedAttribute('bacon')
The [core]
section itself is represented by the
CoreSection
class, which is a subclass of
StaticSection
. It is automatically added when
the Config
object is instantiated; it uses
Config.define_section()
for that purpose.
New in version 6.0.0.
- class sopel.config.Config(filename, validate=True)#
The bot’s configuration.
- Parameters:
The configuration object will load sections (see
ConfigSection
) from the file atfilename
during initialization. Callingsave()
writes any runtime changes to the loaded settings back to the same file.Only the
[core]
section (seeCoreSection
) is added and made available by default; it is the only section required for Sopel to run. All other sections must be defined later, by the code that needs them, usingdefine_section()
.- class ConfigSection(name, items, parent)#
Represents a section of the config file.
- Parameters:
Contains all keys in the section as attributes.
- add_section(name)#
Add a new, empty section to the config file.
- Parameters:
name (str) – name of the new section
- Returns:
None
if successful;False
if a section namedname
already exists
Note
Plugin authors very rarely want or need to use this method.
You will almost always want to define (and optionally validate values within) a section with specific attributes using
define_section()
and a child class ofStaticSection
.Important
The section’s
name
SHOULD follow snake_case naming rules:use only lowercase letters, digits, and underscore (
_
)SHOULD NOT start with a digit
Deviations from snake_case can break the following operations:
accessing the section from Python code using the
Config
object’s attributesoverriding the section’s values using environment variables
- basename#
The config’s base filename, i.e. the filename without the extension.
If the filename is
libera.config.cfg
, then thebasename
will belibera.config
.The config’s
basename
is useful as a component of log file names, for example.
- define_section(name, cls_, validate=True)#
Define the available settings in a section.
- Parameters:
name (str) – name of the new section
cls_ (subclass of
StaticSection
) – class defining the settings within the sectionvalidate (bool) – whether to validate the section’s values (optional; defaults to
True
)
- Raises:
ValueError – if the section
name
has been defined already with a differentcls_
If
validate
isTrue
, the section’s values will be validated, and an exception (usuallyValueError
orAttributeError
) raised if they are invalid. This is desirable in a plugin’ssetup()
function, for example, but might not be in theconfigure()
function.Important
The section’s
name
SHOULD follow snake_case naming rules:use only lowercase letters, digits, and underscore (
_
)SHOULD NOT start with a digit
Deviations from snake_case can break the following operations:
accessing the section from Python code using the
Config
object’s attributesoverriding the section’s values using environment variables
- filename#
The config object’s associated file.
- get#
Shortcut to
parser.get
.
- get_defined_sections()#
Retrieve all defined static sections of this configuration.
- Returns:
all instances of
StaticSection
defined for this configuration file- Return type:
When a plugin defines a section (using
define_section()
), it associates aStaticSection
for the section. This method allows to retrieve these instances ofStaticSection
, and only these.New in version 7.1.
- property homedir#
The config file’s home directory.
If the
core.homedir
setting is available, that value is used. Otherwise, the defaulthomedir
is the directory portion of theConfig
’sfilename
.
- option(question, default=False)#
Ask the user a “y/n” question.
- Parameters:
- Returns:
the Boolean value corresponding to the user’s choice
- Return type:
This will show a “y/n” prompt in the user’s terminal, and return
True
orFalse
based on the response.question
should be phrased as a question, but without a question mark at the end.
- parser#
The configuration parser object that does the heavy lifting.
See also
Python’s built-in
configparser
module and itsRawConfigParser
class.
- save()#
Write all changes to the config file.
Note
Saving the config file will remove any comments that might have existed, as Python’s
configparser
ignores them when parsing.This will become less and less important as we continue to improve Sopel’s tools for making automated changes to config files and eliminate most users’ need to ever manually edit the text, but it’s still worth keeping in mind.