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.
master
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
1 changed files with 16 additions and 5 deletions

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 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 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. 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 # This serves the purpose of normalizing the argument, but also creating a
# duplicate list so we are not altering sys.argv. # 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] argv = ['--operatornotify-level' if arg == '--operatornotify_level' else arg for arg in argv]
level = None 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: try:
level = int(level) level = int(level)
except ValueError: except ValueError:
level = vlogging.get_level_by_name(level) 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: if level is None:
level = vlogging.WARNING level = vlogging.WARNING
argv.remove('--operatornotify') argv.pop(index)
return (argv, level) return (argv, level)