commands: Silence noisy logging of command evaluation

This commit is contained in:
Valentin Lorentz 2022-07-29 10:29:48 +02:00
parent ee60431396
commit ef081746b1
1 changed files with 36 additions and 15 deletions

View File

@ -50,6 +50,13 @@ from .utils import minisix
from .i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization()
LOG_CONVERTERS = world.testing
"""Defines whether converters and contexts should log the argument stack
while parsing it.
Disabled by default (unless running via supybot-test) as it is very noisy
and rarely needs to be debugged.
"""
###
# Non-arg wrappers -- these just change the behavior of a command without
# changing the arguments given to it.
@ -509,6 +516,7 @@ def getChannel(irc, msg, args, state):
elif msg.channel:
channel = msg.channel
else:
if LOG_CONVERTERS:
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError
state.channel = channel
@ -520,6 +528,7 @@ def getChannels(irc, msg, args, state):
elif msg.channel:
channels = [msg.channel]
else:
if LOG_CONVERTERS:
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError
state.args.append(channels)
@ -898,8 +907,10 @@ class context(object):
self.converter = spec
def __call__(self, irc, msg, args, state):
if LOG_CONVERTERS:
log.debug('args before %r: %r', self, args)
self.converter(irc, msg, args, state, *self.args)
if LOG_CONVERTERS:
log.debug('args after %r: %r', self, args)
def __repr__(self):
@ -929,6 +940,7 @@ class additional(context):
try:
self.__parent.__call__(irc, msg, args, state)
except IndexError:
if LOG_CONVERTERS:
log.debug('Got IndexError, returning default.')
setDefault(state, self.default)
@ -939,6 +951,7 @@ class optional(additional):
try:
super(optional, self).__call__(irc, msg, args, state)
except (callbacks.ArgumentError, callbacks.Error) as e:
if LOG_CONVERTERS:
log.debug('Got %s, returning default.', utils.exnToString(e))
state.errored = False
setDefault(state, self.default)
@ -960,6 +973,7 @@ class any(context):
if not self.continueOnError:
raise
else:
if LOG_CONVERTERS:
log.debug('Got %s, returning default.', utils.exnToString(e))
pass
state.args.append(st.args)
@ -1041,10 +1055,12 @@ class getopts(context):
self.getopts[name] = contextify(spec)
self.getoptL.append(name + '=')
self.getopts[name] = contextify(spec)
if LOG_CONVERTERS:
log.debug('getopts: %r', self.getopts)
log.debug('getoptL: %r', self.getoptL)
def __call__(self, irc, msg, args, state):
if LOG_CONVERTERS:
log.debug('args before %r: %r', self, args)
(optlist, rest) = getopt.getopt(args, self.getoptLs, self.getoptL)
getopts = []
@ -1053,6 +1069,7 @@ class getopts(context):
opt = opt[2:] # Strip --
else:
opt = opt[1:]
if LOG_CONVERTERS:
log.debug('opt: %r, arg: %r', opt, arg)
context = self.getopts[opt]
if context is not None:
@ -1064,6 +1081,7 @@ class getopts(context):
getopts.append((opt, True))
state.args.append(getopts)
args[:] = rest
if LOG_CONVERTERS:
log.debug('args after %r: %r', self, args)
###
@ -1123,6 +1141,7 @@ class Spec(object):
except IndexError:
raise callbacks.ArgumentError
if args and not state.allowExtra:
if LOG_CONVERTERS:
log.debug('args and not self.allowExtra: %r', args)
raise callbacks.ArgumentError
return state
@ -1134,8 +1153,10 @@ def _wrap(f, specList=[], name=None, checkDoc=True, **kw):
spec = Spec(specList, **kw)
def newf(self, irc, msg, args, **kwargs):
state = spec(irc, msg, args, stateAttrs={'cb': self, 'log': self.log})
if LOG_CONVERTERS:
self.log.debug('State before call: %s', state)
if state.errored:
if LOG_CONVERTERS:
self.log.debug('Refusing to call %s due to state.errored.', f)
else:
try: