import os
import sys

import pydevd_pycharm


def import_pydevd(loader_path, port, suspend=False):
    # type: (str, int, bool) -> None
    if "DEBUG" in os.environ:
        dir_path = os.path.dirname(os.path.realpath(loader_path))
        filename = os.path.join(dir_path, "debug.txt")
        if os.path.isfile(filename):
            with open(filename, "r") as debug_file:
                pydev_path = debug_file.read()
            if os.path.exists(pydev_path):
                if pydev_path not in sys.path:
                    sys.path.append(pydev_path)

                pydevd_pycharm.settrace('localhost',
                                        port=port,
                                        suspend=suspend,
                                        stdoutToServer=True,
                                        stderrToServer=True)
