<commit_before># -*- coding: utf-8 -*-
from __future__ import print_function

import os
import sys
from pprint import pprint
import configargparse
from colorama import Fore, init as init_colors

from . import api
from . import converters

indention = " " * 4


def init_config():
    p = configargparse.ArgParser(default_config_files=["~/.config/pyLanguagetool.conf"])
    p.add_argument("-v", "--verbose", env_var="VERBOSE", default=False, action='store_true')
    p.add_argument("-a", "--api-url", env_var="API_URL", default="https://languagetool.org/api/v2/")
    p.add_argument("--no-color", env_var="NO_COLOR", action='store_true', default=False, help="don't color output")
    p.add_argument("-c", "--clipboard", env_var="CLIPBOARD", action='store_true', default=False,
                   help="get text from system clipboard")
    p.add_argument("-t", "--input-type", env_var="CLIPBOARD", default="txt",
                   choices=["txt", "html", "md", "rst", "ipynb"],
                   help="if not plaintext")
    p.add_argument('input file', help='input file', nargs='?')

    p.add_argument('-l', '--lang', env_var='TEXTLANG', default="auto",
                   help="A language code like en or en-US, or auto to guess the language automatically (see preferredVariants below). For languages with variants (English, German, Portuguese) spell checking will only be activated when you specify the variant, e.g. en-GB instead of just en."
                   )
    p.add_argument("-m", "--mother-tongue", env_var="MOTHER__TONGUE",
                   help="A language code of the user's native language, enabling false friends checks for some language pairs."
                   )
    p.add_argument("-p", "--preferred-variants", env_var="PREFERRED_VARIANTS",
                   help="Comma-separated list of preferred language variants. The language detector used with language=auto can detect e.g. English, but it cannot decide whether British English or American English is used. Thus this parameter can be used to specify the preferred variants like en-GB and de-AT. Only available with language=auto."
                   )
    p.add_argument('-e', '--enabled-rules', env_var='ENABLED_RULES', help='IDs of rules to be enabled, comma-separated')
    p.add_argument('-d', '--disabled-rules', env_var='DISABLED_RULES',
                   help='IDs of rules to be disabled, comma-separated')
    p.add_argument('--enabled-categories', env_var='ENABLED_CATEGORIES',
                   help='IDs of categories to be enabled, comma-separated')
    p.add_argument('--disabled-categories', env_var='DISABLED_CATEGORIES',
                   help='IDs of categories to be disabled, comma-separated')
    p.add_argument("--enabled-only", action='store_true', default=False,
                   help="enable only the rules and categories whose IDs are specified with --enabled-rules or --enabled-categories"
                   )

    c = vars(p.parse_args())
    if c["enabled_only"] and (c["disabled_categories"] or c["disabled_rules"]):
        print("disabled not allowed")  # TODO: ?
    if c["preferred_variants"] and c["lang"] != "auto":
        # print("You specified --preferred_variants but you didn't specify --language=auto")
        # sys.exit(2)
        print('ignoring --preferred-variants as --lang is not set to "auto"')
        c["preferred_variants"] = None
    if c["verbose"]:
        pprint(c)
    return c


def get_clipboard():
    """
    http://stackoverflow.com/a/16189232
    :rtype string
    """
    try:
        import Tkinter as tk  # Python2
    except ImportError:
        import tkinter as tk  # Python3

    root = tk.Tk()
    root.withdraw()  # keep the window from showing
    clipboard = root.clipboard_get()  # read the clipboard
    root.destroy()
    return clipboard


def get_input_text(config):
    """
    get text from stdin, clipboard or file
    :rtype: (string,string)
    """
    if not sys.stdin.isatty():  # if piped into script
        lines = [line.rstrip() for line in sys.stdin.readlines() if line.rstrip()]
        return "\n".join(lines), None  # read text from pipe and remove empty lines
    elif config["clipboard"]:
        return get_clipboard(), None
    else:
        if config["input file"]:
            extension = os.path.splitext(config["input file"])[1][1:]  # get file extention without .
            try:
                with open(config["input file"], 'r') as myfile:
                    return myfile.read(), extension
            except UnicodeDecodeError:
                print("can't read text")
                sys.exit(1)
        print("input file required")
        sys.exit(2)


def print_errors(matches, api_url, print_color=True):
    def colored(text, color):
        if print_color:
            init_colors()
            return color + text + Fore.RESET
        else:
            return text

    tick = colored(u"\u2713", Fore.LIGHTGREEN_EX) + " "
    cross = colored(u"\u2717", Fore.LIGHTRED_EX) + " "

    for error in matches:
        context_object = error["context"]
        context = context_object["text"]
        length = context_object["length"]
        offset = context_object["offset"]

        endpostion = offset + length
        print(error["message"])

        print(
            indention[:2] +
            cross +
            colored(context[:offset], Fore.LIGHTBLACK_EX) +
            colored(context[offset:endpostion], Fore.LIGHTRED_EX) +
            colored(context[endpostion:], Fore.LIGHTBLACK_EX)
        )
        print(
            indention +
            offset * " " +
            colored(length * "^", Fore.LIGHTRED_EX)
        )

        if error["replacements"]:
            # only print first 5 replacements
            for replacement in error["replacements"][:5]:
                print(
                    indention[:2] +
                    tick +
                    colored(context[:offset], Fore.LIGHTBLACK_EX) +
                    colored(replacement["value"], Fore.LIGHTGREEN_EX) +
                    colored(context[endpostion:], Fore.LIGHTBLACK_EX)
                )
        print()
    print(colored("Text checked by {url}".format(url=api_url), Fore.LIGHTBLACK_EX))


def main():
    config = init_config()

    if config["verbose"]:
        print(sys.version)

    input_text, inputtype = get_input_text(config)
    if not inputtype:
        inputtype = config["input_type"]
    check_text = converters.convert(input_text, inputtype)
    response = api.check(check_text, **config)

    print_errors(response["matches"], config["api_url"], not config["no_color"])

    if len(response["matches"]) > 0:
        sys.exit(1)<commit_msg>add LanguageTool version to output<commit_after># -*- coding: utf-8 -*-
from __future__ import print_function

import os
import sys
from pprint import pprint
import configargparse
from colorama import Fore, init as init_colors

from . import api
from . import converters

indention = " " * 4


def init_config():
    p = configargparse.ArgParser(default_config_files=["~/.config/pyLanguagetool.conf"])
    p.add_argument("-v", "--verbose", env_var="VERBOSE", default=False, action='store_true')
    p.add_argument("-a", "--api-url", env_var="API_URL", default="https://languagetool.org/api/v2/")
    p.add_argument("--no-color", env_var="NO_COLOR", action='store_true', default=False, help="don't color output")
    p.add_argument("-c", "--clipboard", env_var="CLIPBOARD", action='store_true', default=False,
                   help="get text from system clipboard")
    p.add_argument("-t", "--input-type", env_var="CLIPBOARD", default="txt",
                   choices=["txt", "html", "md", "rst", "ipynb"],
                   help="if not plaintext")
    p.add_argument('input file', help='input file', nargs='?')

    p.add_argument('-l', '--lang', env_var='TEXTLANG', default="auto",
                   help="A language code like en or en-US, or auto to guess the language automatically (see preferredVariants below). For languages with variants (English, German, Portuguese) spell checking will only be activated when you specify the variant, e.g. en-GB instead of just en."
                   )
    p.add_argument("-m", "--mother-tongue", env_var="MOTHER__TONGUE",
                   help="A language code of the user's native language, enabling false friends checks for some language pairs."
                   )
    p.add_argument("-p", "--preferred-variants", env_var="PREFERRED_VARIANTS",
                   help="Comma-separated list of preferred language variants. The language detector used with language=auto can detect e.g. English, but it cannot decide whether British English or American English is used. Thus this parameter can be used to specify the preferred variants like en-GB and de-AT. Only available with language=auto."
                   )
    p.add_argument('-e', '--enabled-rules', env_var='ENABLED_RULES', help='IDs of rules to be enabled, comma-separated')
    p.add_argument('-d', '--disabled-rules', env_var='DISABLED_RULES',
                   help='IDs of rules to be disabled, comma-separated')
    p.add_argument('--enabled-categories', env_var='ENABLED_CATEGORIES',
                   help='IDs of categories to be enabled, comma-separated')
    p.add_argument('--disabled-categories', env_var='DISABLED_CATEGORIES',
                   help='IDs of categories to be disabled, comma-separated')
    p.add_argument("--enabled-only", action='store_true', default=False,
                   help="enable only the rules and categories whose IDs are specified with --enabled-rules or --enabled-categories"
                   )

    c = vars(p.parse_args())
    if c["enabled_only"] and (c["disabled_categories"] or c["disabled_rules"]):
        print("disabled not allowed")  # TODO: ?
    if c["preferred_variants"] and c["lang"] != "auto":
        # print("You specified --preferred_variants but you didn't specify --language=auto")
        # sys.exit(2)
        print('ignoring --preferred-variants as --lang is not set to "auto"')
        c["preferred_variants"] = None
    if c["verbose"]:
        pprint(c)
    return c


def get_clipboard():
    """
    http://stackoverflow.com/a/16189232
    :rtype string
    """
    try:
        import Tkinter as tk  # Python2
    except ImportError:
        import tkinter as tk  # Python3

    root = tk.Tk()
    root.withdraw()  # keep the window from showing
    clipboard = root.clipboard_get()  # read the clipboard
    root.destroy()
    return clipboard


def get_input_text(config):
    """
    get text from stdin, clipboard or file
    :rtype: (string,string)
    """
    if not sys.stdin.isatty():  # if piped into script
        lines = [line.rstrip() for line in sys.stdin.readlines() if line.rstrip()]
        return "\n".join(lines), None  # read text from pipe and remove empty lines
    elif config["clipboard"]:
        return get_clipboard(), None
    else:
        if config["input file"]:
            extension = os.path.splitext(config["input file"])[1][1:]  # get file extention without .
            try:
                with open(config["input file"], 'r') as myfile:
                    return myfile.read(), extension
            except UnicodeDecodeError:
                print("can't read text")
                sys.exit(1)
        print("input file required")
        sys.exit(2)


def print_errors(matches, api_url, version, print_color=True):
    def colored(text, color):
        if print_color:
            init_colors()
            return color + text + Fore.RESET
        else:
            return text

    tick = colored(u"\u2713", Fore.LIGHTGREEN_EX) + " "
    cross = colored(u"\u2717", Fore.LIGHTRED_EX) + " "

    for error in matches:
        context_object = error["context"]
        context = context_object["text"]
        length = context_object["length"]
        offset = context_object["offset"]

        endpostion = offset + length
        print(error["message"])

        print(
            indention[:2] +
            cross +
            colored(context[:offset], Fore.LIGHTBLACK_EX) +
            colored(context[offset:endpostion], Fore.LIGHTRED_EX) +
            colored(context[endpostion:], Fore.LIGHTBLACK_EX)
        )
        print(
            indention +
            offset * " " +
            colored(length * "^", Fore.LIGHTRED_EX)
        )

        if error["replacements"]:
            # only print first 5 replacements
            for replacement in error["replacements"][:5]:
                print(
                    indention[:2] +
                    tick +
                    colored(context[:offset], Fore.LIGHTBLACK_EX) +
                    colored(replacement["value"], Fore.LIGHTGREEN_EX) +
                    colored(context[endpostion:], Fore.LIGHTBLACK_EX)
                )
        print()
    print(colored("Text checked by {url} ({version})".format(url=api_url, version=version), Fore.LIGHTBLACK_EX))


def main():
    config = init_config()

    if config["verbose"]:
        print(sys.version)

    input_text, inputtype = get_input_text(config)
    if not inputtype:
        inputtype = config["input_type"]
    check_text = converters.convert(input_text, inputtype)
    response = api.check(check_text, **config)

    print_errors(response["matches"],
                 config["api_url"],
                 response["software"]["name"] + " " + response["software"]["version"],
                 not config["no_color"]
                 )

    if len(response["matches"]) > 0:
        sys.exit(1)
{'fraction_non_alphanumeric': 0.12616476852536607, 'fraction_numerical': 0.00473302765863038, 'mean_word_length': 2.5206977349648527, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1849364', 'n_tokens_mistral': 3988, 'n_tokens_neox': 3874, 'n_words': 1020}