Configuration functionality#
- Option Types
- Core Section
COMMAND_DEFAULT_HELP_PREFIX
COMMAND_DEFAULT_PREFIX
CoreSection
CoreSection.admin_accounts
CoreSection.admins
CoreSection.alias_nicks
CoreSection.antiloop_repeat_text
CoreSection.antiloop_silent_after
CoreSection.antiloop_threshold
CoreSection.antiloop_window
CoreSection.auth_method
CoreSection.auth_password
CoreSection.auth_target
CoreSection.auth_username
CoreSection.auto_url_schemes
CoreSection.bind_host
CoreSection.ca_certs
CoreSection.channels
CoreSection.client_cert_file
CoreSection.commands_on_connect
CoreSection.db_driver
CoreSection.db_filename
CoreSection.db_host
CoreSection.db_name
CoreSection.db_pass
CoreSection.db_port
CoreSection.db_type
CoreSection.db_url
CoreSection.db_user
CoreSection.default_time_format
CoreSection.default_timezone
CoreSection.enable
CoreSection.exclude
CoreSection.extra
CoreSection.flood_burst_lines
CoreSection.flood_empty_wait
CoreSection.flood_max_wait
CoreSection.flood_penalty_ratio
CoreSection.flood_refill_rate
CoreSection.flood_text_length
CoreSection.help_prefix
CoreSection.homedir
CoreSection.host
CoreSection.host_blocks
CoreSection.log_raw
CoreSection.logdir
CoreSection.logging_channel
CoreSection.logging_channel_datefmt
CoreSection.logging_channel_format
CoreSection.logging_channel_level
CoreSection.logging_datefmt
CoreSection.logging_format
CoreSection.logging_level
CoreSection.modes
CoreSection.name
CoreSection.nick
CoreSection.nick_auth_method
CoreSection.nick_auth_password
CoreSection.nick_auth_target
CoreSection.nick_auth_username
CoreSection.nick_blocks
CoreSection.not_configured
CoreSection.owner
CoreSection.owner_account
CoreSection.pid_dir
CoreSection.port
CoreSection.prefix
CoreSection.reply_errors
CoreSection.server_auth_method
CoreSection.server_auth_password
CoreSection.server_auth_sasl_mech
CoreSection.server_auth_username
CoreSection.ssl_ciphers
CoreSection.ssl_minimum_version
CoreSection.throttle_join
CoreSection.throttle_wait
CoreSection.timeout
CoreSection.timeout_ping_interval
CoreSection.use_ssl
CoreSection.user
CoreSection.verify_ssl
URL_DEFAULT_SCHEMES
configure()
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.