Esempio n. 1
0
def print_error(err_str: str, once: bool = False) -> None:
    """Print info about an error along with pertinent context state.

    category: General Utility Functions

    Prints all positional arguments provided along with various info about the
    current context.
    Pass the keyword 'once' as True if you want the call to only happen
    one time from an exact calling location.
    """
    import traceback
    try:
        # If we're only printing once and already have, bail.
        if once:
            if not _ba.do_once():
                return

        print('ERROR:', err_str)
        _ba.print_context()

        # Basically the output of traceback.print_stack()
        stackstr = ''.join(traceback.format_stack())
        print(stackstr, end='')
    except Exception:
        print('ERROR: exception in ba.print_error():')
        traceback.print_exc()
Esempio n. 2
0
def print_error(err_str: str, once: bool = False) -> None:
    """Print info about an error along with pertinent context state.

    category: General Utility Functions

    Prints all positional arguments provided along with various info about the
    current context.
    Pass the keyword 'once' as True if you want the call to only happen
    one time from an exact calling location.
    """
    import traceback
    try:
        # If we're only printing once and already have, bail.
        if once:
            if not _ba.do_once():
                return

        # Most tracebacks are gonna have ugly long install directories in them;
        # lets strip those out when we can.
        print('ERROR:', err_str)
        _ba.print_context()

        # Basically the output of traceback.print_stack() slightly prettified:
        stackstr = ''.join(traceback.format_stack())
        for path in sys.path:
            stackstr = stackstr.replace(path + '/', '')
        print(stackstr, end='')
    except Exception:
        print('ERROR: exception in ba.print_error():')
        traceback.print_exc()
Esempio n. 3
0
def print_exception(*args: Any, **keywds: Any) -> None:
    """Print info about an exception along with pertinent context state.

    category: General Utility Functions

    Prints all arguments provided along with various info about the
    current context and the outstanding exception.
    Pass the keyword 'once' as True if you want the call to only happen
    one time from an exact calling location.
    """
    import traceback
    if keywds:
        allowed_keywds = ['once']
        if any(keywd not in allowed_keywds for keywd in keywds):
            raise Exception("invalid keyword(s)")
    try:
        # If we're only printing once and already have, bail.
        if keywds.get('once', False):
            if not _ba.do_once():
                return

        # Most tracebacks are gonna have ugly long install directories in them;
        # lets strip those out when we can.
        err_str = ' '.join([str(a) for a in args])
        print('ERROR:', err_str)
        _ba.print_context()
        print('PRINTED-FROM:')

        # Basically the output of traceback.print_stack() slightly prettified:
        stackstr = ''.join(traceback.format_stack())
        for path in sys.path:
            stackstr = stackstr.replace(path + '/', '')
        print(stackstr, end='')
        print('EXCEPTION:')

        # Basically the output of traceback.print_exc() slightly prettified:
        excstr = traceback.format_exc()
        for path in sys.path:
            excstr = excstr.replace(path + '/', '')
        print('\n'.join('  ' + l for l in excstr.splitlines()))
    except Exception:
        # I suppose using print_exception here would be a bad idea.
        print('ERROR: exception in ba.print_exception():')
        traceback.print_exc()
Esempio n. 4
0
def print_exception(*args: Any, **keywds: Any) -> None:
    """Print info about an exception along with pertinent context state.

    Category: **General Utility Functions**

    Prints all arguments provided along with various info about the
    current context and the outstanding exception.
    Pass the keyword 'once' as True if you want the call to only happen
    one time from an exact calling location.
    """
    import traceback
    if keywds:
        allowed_keywds = ['once']
        if any(keywd not in allowed_keywds for keywd in keywds):
            raise TypeError('invalid keyword(s)')
    try:
        # If we're only printing once and already have, bail.
        if keywds.get('once', False):
            if not _ba.do_once():
                return

        err_str = ' '.join([str(a) for a in args])
        print('ERROR:', err_str)
        _ba.print_context()
        print('PRINTED-FROM:')

        # Basically the output of traceback.print_stack()
        stackstr = ''.join(traceback.format_stack())
        print(stackstr, end='')
        print('EXCEPTION:')

        # Basically the output of traceback.print_exc()
        excstr = traceback.format_exc()
        print('\n'.join('  ' + l for l in excstr.splitlines()))
    except Exception:
        # I suppose using print_exception here would be a bad idea.
        print('ERROR: exception in ba.print_exception():')
        traceback.print_exc()