#!/usr/bin/env python
# Copyright (c) 2021 elParaguayo
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# A short script which identifies any leftover print statements
import argparse
import ast
import os
import sys
from pathlib import Path
from typing import List


def folder(value):
    value = os.path.abspath(value)
    if not os.path.isdir(value):
        raise argparse.ArgumentTypeError("Invalid root folder.")
    return value


class CommandChecker:
    """
    Object that takes path to libqtile module and loops over modules to
    identify undocumented commands.
    """
    def __init__(self, paths: List[str]):
        self.paths = paths
        self.errors = 0
        print("Checking for print statements...")

    def check(self) -> None:
        """
        Method to scan python files for surplus print statements.
        """
        for path in self.paths:
            all_files = [f.as_posix() for f in Path(path).rglob("*.py")]

            for f in all_files:
                # print(f)
                self._check_file(f)

    def _check_file(self, fname: str) -> None:
        """
        Check an individual file for print statements
        """
        with open(fname, "r") as f:
            raw = ast.parse(f.read(), type_comments=True)

        calls = [x for x in ast.walk(raw) if isinstance(x, ast.Call)]
        prints = [x for x in calls if getattr(x.func, "id", "") == "print"]
        # print(f"{calls=} {prints=}")

        ignores = [r.lineno for r in raw.type_ignores]

        errors = []

        for p in prints:
            if p.lineno in ignores:
                continue
            self.errors += 1
            errors.append((f"{self.errors:>3} {fname}:{p.lineno}", p.lineno))
        
        errors.sort(key=lambda x: x[1])

        for error in errors:
            print(error[0])


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Check code doesn't include debugging print statements.")
    parser.add_argument(
        "-f",
        "--folder",
        dest="folders",
        type=folder,
        action="append",
        help="Folder to search.",
        required=True
    )
    parser.add_argument(
        "-w",
        "--warn-only",
        dest="warn",
        action="store_true",
        help="Don't return error code on failure."
    )

    args = parser.parse_args()

    cc = CommandChecker(args.folders)
    cc.check()

    if cc.errors and not args.warn:
        sys.exit(
            "ERROR: Code has undocumented commands. "
            "Note: You can use '# type: ignore' to suppress error checking."
        )
    elif not cc.errors:
        print("No print statements found.")
