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

View File

@ -50,6 +50,13 @@ from .utils import minisix
from .i18n import PluginInternationalization, internationalizeDocstring from .i18n import PluginInternationalization, internationalizeDocstring
_ = PluginInternationalization() _ = 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 # Non-arg wrappers -- these just change the behavior of a command without
# changing the arguments given to it. # changing the arguments given to it.
@ -509,7 +516,8 @@ def getChannel(irc, msg, args, state):
elif msg.channel: elif msg.channel:
channel = msg.channel channel = msg.channel
else: else:
state.log.debug('Raising ArgumentError because there is no channel.') if LOG_CONVERTERS:
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError raise callbacks.ArgumentError
state.channel = channel state.channel = channel
state.args.append(channel) state.args.append(channel)
@ -520,7 +528,8 @@ def getChannels(irc, msg, args, state):
elif msg.channel: elif msg.channel:
channels = [msg.channel] channels = [msg.channel]
else: else:
state.log.debug('Raising ArgumentError because there is no channel.') if LOG_CONVERTERS:
state.log.debug('Raising ArgumentError because there is no channel.')
raise callbacks.ArgumentError raise callbacks.ArgumentError
state.args.append(channels) state.args.append(channels)
@ -898,9 +907,11 @@ class context(object):
self.converter = spec self.converter = spec
def __call__(self, irc, msg, args, state): def __call__(self, irc, msg, args, state):
log.debug('args before %r: %r', self, args) if LOG_CONVERTERS:
log.debug('args before %r: %r', self, args)
self.converter(irc, msg, args, state, *self.args) self.converter(irc, msg, args, state, *self.args)
log.debug('args after %r: %r', self, args) if LOG_CONVERTERS:
log.debug('args after %r: %r', self, args)
def __repr__(self): def __repr__(self):
return '<%s for %s>' % (self.__class__.__name__, self.spec) return '<%s for %s>' % (self.__class__.__name__, self.spec)
@ -929,7 +940,8 @@ class additional(context):
try: try:
self.__parent.__call__(irc, msg, args, state) self.__parent.__call__(irc, msg, args, state)
except IndexError: except IndexError:
log.debug('Got IndexError, returning default.') if LOG_CONVERTERS:
log.debug('Got IndexError, returning default.')
setDefault(state, self.default) setDefault(state, self.default)
# optional means: Look for this, but if it's not the type I'm expecting or # optional means: Look for this, but if it's not the type I'm expecting or
@ -939,7 +951,8 @@ class optional(additional):
try: try:
super(optional, self).__call__(irc, msg, args, state) super(optional, self).__call__(irc, msg, args, state)
except (callbacks.ArgumentError, callbacks.Error) as e: except (callbacks.ArgumentError, callbacks.Error) as e:
log.debug('Got %s, returning default.', utils.exnToString(e)) if LOG_CONVERTERS:
log.debug('Got %s, returning default.', utils.exnToString(e))
state.errored = False state.errored = False
setDefault(state, self.default) setDefault(state, self.default)
@ -960,7 +973,8 @@ class any(context):
if not self.continueOnError: if not self.continueOnError:
raise raise
else: else:
log.debug('Got %s, returning default.', utils.exnToString(e)) if LOG_CONVERTERS:
log.debug('Got %s, returning default.', utils.exnToString(e))
pass pass
state.args.append(st.args) state.args.append(st.args)
@ -1041,11 +1055,13 @@ class getopts(context):
self.getopts[name] = contextify(spec) self.getopts[name] = contextify(spec)
self.getoptL.append(name + '=') self.getoptL.append(name + '=')
self.getopts[name] = contextify(spec) self.getopts[name] = contextify(spec)
log.debug('getopts: %r', self.getopts) if LOG_CONVERTERS:
log.debug('getoptL: %r', self.getoptL) log.debug('getopts: %r', self.getopts)
log.debug('getoptL: %r', self.getoptL)
def __call__(self, irc, msg, args, state): def __call__(self, irc, msg, args, state):
log.debug('args before %r: %r', self, args) if LOG_CONVERTERS:
log.debug('args before %r: %r', self, args)
(optlist, rest) = getopt.getopt(args, self.getoptLs, self.getoptL) (optlist, rest) = getopt.getopt(args, self.getoptLs, self.getoptL)
getopts = [] getopts = []
for (opt, arg) in optlist: for (opt, arg) in optlist:
@ -1053,7 +1069,8 @@ class getopts(context):
opt = opt[2:] # Strip -- opt = opt[2:] # Strip --
else: else:
opt = opt[1:] opt = opt[1:]
log.debug('opt: %r, arg: %r', opt, arg) if LOG_CONVERTERS:
log.debug('opt: %r, arg: %r', opt, arg)
context = self.getopts[opt] context = self.getopts[opt]
if context is not None: if context is not None:
st = state.essence() st = state.essence()
@ -1064,7 +1081,8 @@ class getopts(context):
getopts.append((opt, True)) getopts.append((opt, True))
state.args.append(getopts) state.args.append(getopts)
args[:] = rest args[:] = rest
log.debug('args after %r: %r', self, args) if LOG_CONVERTERS:
log.debug('args after %r: %r', self, args)
### ###
# This is our state object, passed to converters along with irc, msg, and args. # This is our state object, passed to converters along with irc, msg, and args.
@ -1123,7 +1141,8 @@ class Spec(object):
except IndexError: except IndexError:
raise callbacks.ArgumentError raise callbacks.ArgumentError
if args and not state.allowExtra: if args and not state.allowExtra:
log.debug('args and not self.allowExtra: %r', args) if LOG_CONVERTERS:
log.debug('args and not self.allowExtra: %r', args)
raise callbacks.ArgumentError raise callbacks.ArgumentError
return state return state
@ -1134,9 +1153,11 @@ def _wrap(f, specList=[], name=None, checkDoc=True, **kw):
spec = Spec(specList, **kw) spec = Spec(specList, **kw)
def newf(self, irc, msg, args, **kwargs): def newf(self, irc, msg, args, **kwargs):
state = spec(irc, msg, args, stateAttrs={'cb': self, 'log': self.log}) state = spec(irc, msg, args, stateAttrs={'cb': self, 'log': self.log})
self.log.debug('State before call: %s', state) if LOG_CONVERTERS:
self.log.debug('State before call: %s', state)
if state.errored: if state.errored:
self.log.debug('Refusing to call %s due to state.errored.', f) if LOG_CONVERTERS:
self.log.debug('Refusing to call %s due to state.errored.', f)
else: else:
try: try:
f(self, irc, msg, args, *state.args, **state.kwargs) f(self, irc, msg, args, *state.args, **state.kwargs)