# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# Path: lib/utils/pathutils.py
# def get_expandglob_path():
#     """ Return path to expandglob.js """
#     return os.path.join(package_path, 'bin', 'expandglob.js')
# 
# def default_node_path(node_path, project=None):
#     if node_path == 'none':
#         Debug('notify', 'The setting node_path is set to "none". That is depreciated. Remove the setting.')
#         node_path = None
# 
#     if node_path == "" or node_path is None:
#         if sys.platform == "linux":
#             return "nodejs"
#         elif sys.platform == "nt":
#             return "node"
#         elif sys.platform == "darwin":
#             return "/usr/local/bin/node"
#         else:
#             return "node"
#     else:
#         return expand_variables(node_path, project)
# 
# Path: lib/utils/osutils.py
# def get_kwargs(stderr=True):
#     if os.name == 'nt':
#         startupinfo = subprocess.STARTUPINFO()
#         startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#         if stderr:
#             #errorlog = open(os.devnull, 'w')
#             return {'stderr':subprocess.DEVNULL, 'startupinfo': startupinfo}
#         return {'startupinfo': startupinfo}
#     else:
#         return {}
# 
# Path: lib/utils/disabling.py
# def set_tsglobexpansion_disabled():
#     """ Sets the disabled flag for tsglobexpansion """
#     if "*tsglob" not in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.append("*tsglob")
# 
# def set_tsglobexpansion_enabled():
#     """ Clears the disabled flag for tsglobexpansion """
#     if "*tsglob" in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.remove("*tsglob")
# 
# def is_tsglobexpansion_disabled():
#     """ Checks the disabled flag for tsglobexpansion """
#     return "*tsglob"  in plugin_disabled_for_folders


import os
import glob
import json
from subprocess import Popen, PIPE, TimeoutExpired
from .TsconfigLinter import check_tsconfig
from ..utils.debug import Debug
from ..utils.pathutils import get_expandglob_path, default_node_path
from ..utils.osutils import get_kwargs
from ..utils.disabling import set_tsglobexpansion_disabled, set_tsglobexpansion_enabled, is_tsglobexpansion_disabled
    if "filesGlob" not in linter.tsconfig:
        return True # Should reopen project, so return True here

    # Expand!
    project_dir = os.path.dirname(linter.view.file_name())
    file_list = _expand_globs_with_javascript(project_dir, linter)
    Debug("tsconfig.json", "fileGlobs expaned")

    # reload file
    linter.view.run_command("revert")

    # lint again, so the soft errors are still displayed
    check_tsconfig(linter.view)

    return True


def _expand_globs_with_javascript(project_dir, linter):
    """ use the nodejs script bin/expandglob.js to expand the glob entries in
        the already saved file tsconfig.json in project_dir.
        Returns the files list, but also CHANGES THE DISK CONTENTS.
        We use javascript here, because python shuffles the dicts keys in each
        iteration """

    try:
        node_path = None
        if "ArcticTypescript" in linter.tsconfig:
            if "node_path" in linter.tsconfig["ArcticTypescript"]:
                node_path = linter.tsconfig["ArcticTypescript"]["node_path"]


# Path: lib/tsconfiglint/TsconfigLinter.py
# @catch_CancelCommand
# def check_tsconfig(view):
#     """ Tests view for being a tsconfig.json.
#         Reads the tsconfig.json and checks for errors.
#         Show Errors using Regions.
#         Set a error map [<(a,b), msg>] into views settings. """
# 
#     if not _is_valid_and_enabled(view):
#         return
# 
#     Debug('tsconfig', 'tsconfig modified, check')
# 
#     linter = TsconfigLinter(view)
# 
#     return linter
# 
# Path: lib/utils/debug.py
# def Debug(classification, text):
#     if 'all' in print_classifications or classification in print_classifications:
#         print("ArcticTypescript: %s: %s" % (classification.ljust(8), text))
#     if classification not in possible_classifications:
#         print("ArcticTypescript: debug: got unknown debug message classification: %s. " \
#             "Consider adding this to possible_classifications" % classification)
#     sys.stdout.flush()
# 
# Path: lib/utils/pathutils.py
# def get_expandglob_path():
#     """ Return path to expandglob.js """
#     return os.path.join(package_path, 'bin', 'expandglob.js')
# 
# def default_node_path(node_path, project=None):
#     if node_path == 'none':
#         Debug('notify', 'The setting node_path is set to "none". That is depreciated. Remove the setting.')
#         node_path = None
# 
#     if node_path == "" or node_path is None:
#         if sys.platform == "linux":
#             return "nodejs"
#         elif sys.platform == "nt":
#             return "node"
#         elif sys.platform == "darwin":
#             return "/usr/local/bin/node"
#         else:
#             return "node"
#     else:
#         return expand_variables(node_path, project)
# 
# Path: lib/utils/osutils.py
# def get_kwargs(stderr=True):
#     if os.name == 'nt':
#         startupinfo = subprocess.STARTUPINFO()
#         startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
#         if stderr:
#             #errorlog = open(os.devnull, 'w')
#             return {'stderr':subprocess.DEVNULL, 'startupinfo': startupinfo}
#         return {'startupinfo': startupinfo}
#     else:
#         return {}
# 
# Path: lib/utils/disabling.py
# def set_tsglobexpansion_disabled():
#     """ Sets the disabled flag for tsglobexpansion """
#     if "*tsglob" not in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.append("*tsglob")
# 
# def set_tsglobexpansion_enabled():
#     """ Clears the disabled flag for tsglobexpansion """
#     if "*tsglob" in plugin_disabled_for_folders:
#         plugin_disabled_for_folders.remove("*tsglob")
# 
# def is_tsglobexpansion_disabled():
#     """ Checks the disabled flag for tsglobexpansion """
#     return "*tsglob"  in plugin_disabled_for_folders


# Path: lib/tsconfiglint/tsconfigglobexpand.py
import os
import glob
import json
from subprocess import Popen, PIPE, TimeoutExpired
from .TsconfigLinter import check_tsconfig
from ..utils.debug import Debug
from ..utils.pathutils import get_expandglob_path, default_node_path
from ..utils.osutils import get_kwargs
from ..utils.disabling import set_tsglobexpansion_disabled, set_tsglobexpansion_enabled, is_tsglobexpansion_disabled

    if "filesGlob" not in linter.tsconfig:
        return True # Should reopen project, so return True here

    # Expand!
    project_dir = os.path.dirname(linter.view.file_name())
    file_list = _expand_globs_with_javascript(project_dir, linter)
    Debug("tsconfig.json", "fileGlobs expaned")

    # reload file
    linter.view.run_command("revert")

    # lint again, so the soft errors are still displayed
    check_tsconfig(linter.view)

    return True


def _expand_globs_with_javascript(project_dir, linter):
    """ use the nodejs script bin/expandglob.js to expand the glob entries in
        the already saved file tsconfig.json in project_dir.
        Returns the files list, but also CHANGES THE DISK CONTENTS.
        We use javascript here, because python shuffles the dicts keys in each
        iteration """

    try:
        node_path = None
        if "ArcticTypescript" in linter.tsconfig:
            if "node_path" in linter.tsconfig["ArcticTypescript"]:
                node_path = linter.tsconfig["ArcticTypescript"]["node_path"]


        node_path = default_node_path(node_path)