Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli.config: implement set and unset subcommands #1986

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions sopel/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ def build_parser():
get_parser.add_argument('option')
utils.add_common_arguments(get_parser)

# sopel-config set <section> <key> <value>
set_parser = subparsers.add_parser(
'set',
help="Set a configuration option's value",
description="Set a configuration option's value",
)
set_parser.add_argument('section')
set_parser.add_argument('option')
set_parser.add_argument('value')
utils.add_common_arguments(set_parser)

# sopel-config unset <section> <key>
unset_parser = subparsers.add_parser(
'unset',
help="Unset a configuration option",
description="Unset a configuration option",
)
unset_parser.add_argument('section')
unset_parser.add_argument('option')
utils.add_common_arguments(unset_parser)

return parser


Expand Down Expand Up @@ -167,6 +188,78 @@ def handle_get(options):
return 0 # successful operation


def handle_set(options):
"""Set the ``<section> <key>`` setting to ``<value>``.

:param options: parsed arguments
:type options: :class:`argparse.Namespace`
:return: 0 if everything went fine;
1 if the section and/or key does not exist;
2 if the settings can't be loaded
"""
try:
settings = utils.load_settings(options)
except Exception as error:
tools.stderr(error)
return 2

section = options.section
option = options.option
value = options.value

# Make sure the section exists
if not settings.parser.has_section(section):
try:
settings.parser.add_section(section)
except (TypeError, ValueError):
tools.stderr('Invalid section name "%s"' % section)
return 1

# Update the option & save config file
settings.parser.set(section, option, value)
dgw marked this conversation as resolved.
Show resolved Hide resolved
settings.save()
print('Updated option "{}.{}"'.format(section, option))
return 0 # successful operation


def handle_unset(options):
"""Unset the setting value of ``<section> <key>``.

:param options: parsed arguments
:type options: :class:`argparse.Namespace`
:return: 0 if everything went fine;
1 if the section and/or key does not exist;
2 if the settings can't be loaded
"""
try:
settings = utils.load_settings(options)
except Exception as error:
tools.stderr(error)
return 2

section = options.section
option = options.option

# Make sure the section exists
if not settings.parser.has_section(section):
tools.stderr('Section "%s" does not exist' % section)
return 1

# Unset the option
removed = settings.parser.remove_option(section, option)
# Remove section completely if empty
if len(settings.parser.options(section)) == 0:
settings.parser.remove_section(section)
# Save config file
settings.save()
# Report result
if removed:
print('Unset option "{}.{}"'.format(section, option))
else:
print('Option "{}.{}" was not set'.format(section, option))
return 0 # successful operation


def main():
"""Console entry point for ``sopel-config``."""
parser = build_parser()
Expand All @@ -184,3 +277,7 @@ def main():
return handle_init(options)
elif action == 'get':
return handle_get(options)
elif action == 'set':
return handle_set(options)
elif action == 'unset':
return handle_unset(options)