"""path_info.py

In many places, by default we exclude the files from this package,
thus restricting tracebacks to code written by the users.

If Friendly-traceback is used by some other program,
it might be desirable to exclude additional files.
"""
import os

EXCLUDED_FILE_PATH = set([])


def exclude_file_from_traceback(full_path):
    """Exclude a file from appearing in a traceback generated by
       Friendly-traceback.  Note that verbosity ``level=0`` is the
       traceback generated by Python and is unaffected by this.
    """
    EXCLUDED_FILE_PATH.add(full_path)


dirname = os.path.dirname(__file__)
for file in os.listdir(os.path.dirname(__file__)):
    if file.endswith(".py"):
        exclude_file_from_traceback(os.path.join(dirname, file))


def is_excluded_file(full_path):
    """Determines if the file belongs to the group that is excluded from tracebacks.
    """
    return full_path in EXCLUDED_FILE_PATH


def include_file_in_traceback(full_path):
    """Reverses the effect of ``exclude_file_from_traceback()`` so that
       the file can potentially appear in later tracebacks generated
       by Friendly-traceback.

       A typical pattern might be something like::

            import some_module

            revert = not is_excluded_file(some_module.__file__)
            if revert:
                exclude_file_from_traceback(some_module.__file__)

            try:
                some_module.do_something(...)
            except Exception:
                friendly_traceback.explain()
            finally:
                if revert:
                    include_file_in_traceback(some_module.__file__)

    """
    EXCLUDED_FILE_PATH.discard(full_path)
