Changeset - 3fbd05d55303
[Not reviewed]
0 2 0
Brett Smith - 4 years ago 2020-05-30 02:05:26
brettcsmith@brettcsmith.org
cliutil: Add is_main_script function.
2 files changed with 9 insertions and 0 deletions:
0 comments (0 inline, 0 general)
conservancy_beancount/cliutil.py
Show inline comments
...
 
@@ -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,
tests/test_cliutil.py
Show inline comments
...
 
@@ -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),
0 comments (0 inline, 0 general)