# This file is offered as an unsupported instructional example
# of how to write plugins using the Python API.
#
# Add the following to the project configuration file to enable this
# plugin
#
# PLUGINS += $GTHOME/codesonar/plugins/python_debug_console.py
#
# A plugin that does not define a warning class or a metric. This
# plugin opens a debugging console in python that allows interactive
# Python API.
#
# Import the module cs to have access to the Python API.
#
# Import sys and io to support communicating with the python console

import cs, sys, os, io

# Disable output bufferring in case codesonar is running under
# something for which isatty returns false.
sys.stdout = io.TextIOWrapper(open(sys.stdout.fileno(), 'wb', 0), write_through=True)
sys.stderr = io.TextIOWrapper(open(sys.stderr.fileno(), 'wb', 0), write_through=True)

@ cs.project_visitor
def make_console(proj):
    # import the python code module (see http://docs.python.org/3/library/code.html)
    import code
    console = code.InteractiveConsole()
    try:
        console.interact()
    except SystemExit:
        # If a user types "quit()" at the console, the entire
        # process will try to exit.  We want the analysis to
        # complete normally, so swallow the exception.
        pass
