cliutil: Add is_main_script function.

This commit is contained in:
Brett Smith 2020-05-29 22:03:56 -04:00
parent 2b550a2037
commit 3fbd05d553
2 changed files with 9 additions and 0 deletions

View file

@ -18,6 +18,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>."""
import argparse
import enum
import inspect
import logging
import operator
import os
@ -133,6 +134,11 @@ def add_version_argument(parser: argparse.ArgumentParser) -> argparse.Action:
help="Show program version and license information",
)
def is_main_script() -> bool:
"""Return true if the caller is the "main" program."""
stack = inspect.stack(context=False)
return len(stack) <= 3 and stack[-1].function.startswith('<')
def setup_logger(logger: Union[str, logging.Logger]='',
loglevel: int=logging.INFO,
stream: TextIO=sys.stderr,

View file

@ -91,6 +91,9 @@ def test_excepthook_traceback(caplog):
assert caplog.records
assert caplog.records[-1].message == ''.join(traceback.format_exception(*args))
def test_is_main_script():
assert not cliutil.is_main_script()
@pytest.mark.parametrize('arg,expected', [
('debug', logging.DEBUG),
('info', logging.INFO),