Add global _did_earlybird to allow main_decorator to coexist.

This way you don't get duplicate handlers.
master
voussoir 2021-11-08 23:35:00 -08:00
parent b1849ccefa
commit 5a33c9369e
No known key found for this signature in database
GPG Key ID: 5F7554F8C26DACCB
1 changed files with 11 additions and 2 deletions

View File

@ -38,6 +38,8 @@ root.setLevel(NOTSET)
LOUD = 1 LOUD = 1
SILENT = 99999999999 SILENT = 99999999999
_did_earlybird = False
def add_loud(log): def add_loud(log):
''' '''
Add the `loud` method to the given logger. Add the `loud` method to the given logger.
@ -89,7 +91,9 @@ def earlybird_config():
However, this might have the downside of making your program more difficult However, this might have the downside of making your program more difficult
to debug because sys.argv is permanently altered. to debug because sys.argv is permanently altered.
''' '''
global _did_earlybird
sys.argv = basic_config_by_argv(sys.argv) sys.argv = basic_config_by_argv(sys.argv)
_did_earlybird = True
def get_level_by_argv(argv): def get_level_by_argv(argv):
''' '''
@ -199,7 +203,12 @@ def main_decorator(main):
betterhelp.HELPTEXT_EPILOGUES.add(BETTERHELP_EPILOGUE) betterhelp.HELPTEXT_EPILOGUES.add(BETTERHELP_EPILOGUE)
@functools.wraps(main) @functools.wraps(main)
def wrapped(argv, *args, **kwargs): def wrapped(argv, *args, **kwargs):
(level, argv) = get_level_by_argv(argv) # The reason we don't call basic_config is that another module may have
add_root_handler(level) # attached a root handler for another purpose.
# However we do check _did_earlybird so that we don't get double
# handlers from this module.
if not _did_earlybird:
(level, argv) = get_level_by_argv(argv)
add_root_handler(level)
return main(argv, *args, **kwargs) return main(argv, *args, **kwargs)
return wrapped return wrapped