Replace in+remove with index+pop.

The code turns out to be longer with these duplicate try-except,
but they are different enough that I can't really condense it
at the moment.
This commit is contained in:
voussoir 2021-09-16 21:19:46 -07:00
parent b561fd24b5
commit d0d7cf2145
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -190,6 +190,8 @@ def get_level_by_argv(argv):
and level is either an integer log level, or None if the user did not
opt in. Even if you are not attaching operatornotify to your logger, you
can still use this value to make decisions about when/what to notify.
Raises ValueError if --operatornotify-level X is not a recognized level.
'''
# This serves the purpose of normalizing the argument, but also creating a
# duplicate list so we are not altering sys.argv.
@ -197,18 +199,27 @@ def get_level_by_argv(argv):
argv = ['--operatornotify-level' if arg == '--operatornotify_level' else arg for arg in argv]
level = None
if '--operatornotify-level' in argv:
level = argv.pop(argv.index('--operatornotify-level') + 1)
try:
index = argv.index('--operatornotify-level')
except ValueError:
pass
else:
argv.pop(index)
level = argv.pop(index)
try:
level = int(level)
except ValueError:
level = vlogging.get_level_by_name(level)
argv.remove('--operatornotify-level')
if '--operatornotify' in argv:
try:
index = argv.index('--operatornotify')
except ValueError:
pass
else:
if level is None:
level = vlogging.WARNING
argv.remove('--operatornotify')
argv.pop(index)
return (argv, level)