Skip to content

Commit

Permalink
admin: refactor; pull out set command argument parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
HumorBaby committed Apr 15, 2019
1 parent c13257d commit ebe644a
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions sopel/modules/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,61 +247,85 @@ def mode(bot, trigger):
bot.write(('MODE', bot.nick + ' ' + mode))


@sopel.module.require_privmsg("This command only works as a private message.")
@sopel.module.require_admin("This command requires admin privileges.")
@sopel.module.commands('set')
@sopel.module.example('.set core.owner Me')
def set_config(bot, trigger):
"""See and modify values of Sopel's config object.
Trigger args:
arg1 - section and option, in the form "section.option"
arg2 - value
If there is no section, section will default to "core".
If value is None, the option will be deleted.
def parse_section_option_value(bot, trigger):
"""Parse trigger for set/unset to get relevant config elements.
Returns:
- False, if invalid command format; let command handle sending usage.
- (), if option not found; reply that option is not found.
- (section, section_name, static_sec, option, value) otherwise.
- value is `None` if omitted from command.
"""
# Get section and option from first argument.
match = trigger.group(3)
if match is None:
bot.reply("Usage: .set section.option value")
return
return False # Invalid command

# Get section and option from first argument.
arg1 = match.split('.')
if len(arg1) == 1:
section_name, option = "core", arg1[0]
elif len(arg1) == 2:
section_name, option = arg1
else:
bot.reply("Usage: .set section.option value")
return
return False # Invalid command
section = getattr(bot.config, section_name)
static_sec = isinstance(section, StaticSection)

if static_sec and not hasattr(section, option):
bot.say('[{}] section has no option {}.'.format(section_name, option))
return
return () # Option not found.

if not static_sec and bot.config.parser.has_option(section, option):
bot.reply("Option %s.%s does not exist." % (section_name, option))
return ()

delim = trigger.group(2).find(' ')
# Skip preceding whitespaces, if any.
while delim > 0 and delim < len(trigger.group(2)) and trigger.group(2)[delim] == ' ':
delim = delim + 1

# Display current value if no value is given.
value = trigger.group(2)[delim:]
if delim == -1 or delim == len(trigger.group(2)):
if not static_sec and bot.config.parser.has_option(section, option):
bot.reply("Option %s.%s does not exist." % (section_name, option))
return
# Except if the option looks like a password. Censor those to stop them
# from being put on log files.
value = None

return (section, section_name, static_sec, option, value)


@sopel.module.require_privmsg("This command only works as a private message.")
@sopel.module.require_admin("This command requires admin privileges.")
@sopel.module.commands('set')
@sopel.module.example('.set core.owner Me')
def set_config(bot, trigger):
"""See and modify values of Sopel's config object.
Trigger args:
arg1 - section and option, in the form "section.option"
arg2 - value
If there is no section, section will default to "core".
If value is not provided, the current value will be displayed.
"""
# Get section and option from first argument.
parsed = parse_section_option_value(bot, trigger)
if parsed is False:
bot.reply('Usage: .set section.option [value]')
return

if not parsed: # parsed = ()
# Bot already said "Option not found..."
return

(section, section_name, static_sec, option, value) = parsed
# Display current value if no value is given
if not value:
if option.endswith("password") or option.endswith("pass"):
value = "(password censored)"
else:
value = getattr(section, option)
bot.reply("%s.%s = %s" % (section_name, option, value))
return

# Otherwise, set the value to one given as argument 2.
value = trigger.group(2)[delim:]
# Otherwise, set the value to one given
if static_sec:
descriptor = getattr(section.__class__, option)
try:
Expand Down

0 comments on commit ebe644a

Please sign in to comment.