|
| 1 | +import contextlib |
| 2 | +import locale |
| 3 | +import logging |
| 4 | +import math |
| 5 | +import os |
| 6 | +import urllib.parse |
| 7 | +from pathlib import Path |
| 8 | +from typing import Protocol, runtime_checkable |
| 9 | + |
| 10 | +import psutil |
| 11 | + |
| 12 | +from language_tool_python.exceptions import PathError |
| 13 | + |
| 14 | +__all__ = [ |
| 15 | + "SupportsBool", |
| 16 | + "get_env_float", |
| 17 | + "get_env_int", |
| 18 | + "get_language_tool_download_path", |
| 19 | + "get_locale_language", |
| 20 | + "kill_process_force", |
| 21 | + "parse_url", |
| 22 | + "version_tuple", |
| 23 | +] |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | +JAR_NAMES = [ |
| 28 | + "languagetool-server.jar", |
| 29 | + "LanguageTool.jar", |
| 30 | +] |
| 31 | +FAILSAFE_LANGUAGE = "en" |
| 32 | + |
| 33 | +LTP_PATH_ENV_VAR = "LTP_PATH" # LanguageTool download path |
| 34 | + |
| 35 | +# Directory containing the LanguageTool jar file: |
| 36 | +LTP_JAR_DIR_PATH_ENV_VAR = "LTP_JAR_DIR_PATH" |
| 37 | + |
| 38 | + |
| 39 | +def parse_url(url_str: str) -> str: |
| 40 | + """Parse the given URL string and ensure it has a scheme. |
| 41 | +
|
| 42 | + If the input URL string does not contain 'http', 'http://' is prepended to it. The |
| 43 | + function then parses the URL and returns its canonical form. |
| 44 | +
|
| 45 | + :param url_str: The URL string to be parsed. |
| 46 | + :type url_str: str |
| 47 | + :return: The parsed URL in its canonical form. |
| 48 | + :rtype: str |
| 49 | + """ |
| 50 | + if "http" not in url_str: |
| 51 | + url_str = "http://" + url_str |
| 52 | + |
| 53 | + return urllib.parse.urlparse(url_str).geturl() |
| 54 | + |
| 55 | + |
| 56 | +def get_env_int(env_var: str, default: int) -> int: |
| 57 | + """Read a positive integer from the environment. |
| 58 | +
|
| 59 | + :param env_var: Environment variable name. |
| 60 | + :type env_var: str |
| 61 | + :param default: Value to use when the environment variable is absent. |
| 62 | + :type default: int |
| 63 | + :return: Configured integer value, or the default. |
| 64 | + :rtype: int |
| 65 | + :raises PathError: If the configured value is invalid. |
| 66 | + """ |
| 67 | + configured = os.environ.get(env_var) |
| 68 | + |
| 69 | + if configured is None: |
| 70 | + return default |
| 71 | + |
| 72 | + try: |
| 73 | + value = int(configured) |
| 74 | + except ValueError as e: |
| 75 | + err = f"Invalid integer configured by {env_var}: {configured!r}." |
| 76 | + raise PathError(err) from e |
| 77 | + |
| 78 | + if value <= 0: |
| 79 | + err = f"Invalid integer configured by {env_var}: {configured!r}." |
| 80 | + raise PathError(err) |
| 81 | + |
| 82 | + return value |
| 83 | + |
| 84 | + |
| 85 | +def get_env_float(env_var: str, default: float) -> float: |
| 86 | + """Read a positive float from the environment. |
| 87 | +
|
| 88 | + :param env_var: Environment variable name. |
| 89 | + :type env_var: str |
| 90 | + :param default: Value to use when the environment variable is absent. |
| 91 | + :type default: float |
| 92 | + :return: Configured float value, or the default. |
| 93 | + :rtype: float |
| 94 | + :raises PathError: If the configured value is invalid. |
| 95 | + """ |
| 96 | + configured = os.environ.get(env_var) |
| 97 | + |
| 98 | + if configured is None: |
| 99 | + return default |
| 100 | + |
| 101 | + try: |
| 102 | + value = float(configured) |
| 103 | + except ValueError as e: |
| 104 | + err = f"Invalid float configured by {env_var}: {configured!r}." |
| 105 | + raise PathError(err) from e |
| 106 | + |
| 107 | + if not math.isfinite(value) or value <= 0: |
| 108 | + err = f"Invalid float configured by {env_var}: {configured!r}." |
| 109 | + raise PathError(err) |
| 110 | + |
| 111 | + return value |
| 112 | + |
| 113 | + |
| 114 | +def get_language_tool_download_path() -> Path: |
| 115 | + """Get the download path for LanguageTool. |
| 116 | +
|
| 117 | + This function retrieves the download path for LanguageTool from the environment |
| 118 | + variable specified by ``LTP_PATH_ENV_VAR``. If the environment variable is not set, |
| 119 | + it defaults to a path in the user's home directory under |
| 120 | + ``.cache/language_tool_python``. The function ensures that the directory exists |
| 121 | + before returning it. |
| 122 | +
|
| 123 | + :return: The download path for LanguageTool. |
| 124 | + :rtype: Path |
| 125 | + """ |
| 126 | + # Get download path from environment or use default. |
| 127 | + path_str = os.environ.get( |
| 128 | + LTP_PATH_ENV_VAR, |
| 129 | + str(Path.home() / ".cache" / "language_tool_python"), |
| 130 | + ) |
| 131 | + path = Path(path_str) |
| 132 | + path.mkdir(parents=True, exist_ok=True) |
| 133 | + return path |
| 134 | + |
| 135 | + |
| 136 | +def get_locale_language() -> str: |
| 137 | + """Get the current locale language. |
| 138 | +
|
| 139 | + This function retrieves the current locale language setting of the system. It first |
| 140 | + attempts to get the locale using ``locale.getlocale()``. If that fails, it falls |
| 141 | + back to using ``locale.getdefaultlocale()``. If both methods fail to provide a valid |
| 142 | + language code, it returns a default failsafe language code. |
| 143 | +
|
| 144 | + :return: The language code of the current locale. |
| 145 | + :rtype: str |
| 146 | + """ |
| 147 | + return locale.getlocale()[0] or locale.getdefaultlocale()[0] or FAILSAFE_LANGUAGE |
| 148 | + |
| 149 | + |
| 150 | +def kill_process_force( |
| 151 | + *, |
| 152 | + pid: int | None = None, |
| 153 | + proc: psutil.Process | None = None, |
| 154 | +) -> None: |
| 155 | + """Terminate a process and all of its child processes forcefully. |
| 156 | +
|
| 157 | + This function attempts to kill a process specified either by its PID or by a |
| 158 | + psutil.Process object. If the process has any child processes, they will be killed |
| 159 | + first. |
| 160 | +
|
| 161 | + :param pid: The process ID of the process to be killed. Either ``pid`` or ``proc`` |
| 162 | + must be provided. |
| 163 | + :type pid: int | None |
| 164 | + :param proc: A psutil.Process object representing the process to be killed. Either |
| 165 | + ``pid`` or ``proc`` must be provided. |
| 166 | + :type proc: psutil.Process | None |
| 167 | + :raises ValueError: If neither ``pid`` nor ``proc`` is provided. |
| 168 | + """ |
| 169 | + if not any([pid, proc]): |
| 170 | + err = "Must pass either pid or proc" |
| 171 | + raise ValueError(err) |
| 172 | + try: |
| 173 | + proc = psutil.Process(pid) if proc is None else proc |
| 174 | + except psutil.NoSuchProcess: |
| 175 | + logger.debug("Process %s does not exist, nothing to kill", pid) |
| 176 | + return |
| 177 | + logger.debug("Killing process %s and its children", proc.pid) |
| 178 | + for child in proc.children(recursive=True): |
| 179 | + with contextlib.suppress(psutil.NoSuchProcess): |
| 180 | + logger.debug("Killing child process %s", child.pid) |
| 181 | + child.kill() |
| 182 | + with contextlib.suppress(psutil.NoSuchProcess): |
| 183 | + proc.kill() |
| 184 | + |
| 185 | + |
| 186 | +@runtime_checkable |
| 187 | +class SupportsBool(Protocol): |
| 188 | + """Protocol for types that can be converted to a boolean value.""" |
| 189 | + |
| 190 | + def __bool__(self) -> bool: |
| 191 | + """Define the interface for types that can be evaluated in a boolean context.""" |
| 192 | + ... |
| 193 | + |
| 194 | + |
| 195 | +def version_tuple(v: str) -> tuple[int, int]: |
| 196 | + """Convert a version string into a tuple of integers. |
| 197 | +
|
| 198 | + This function takes a version string in the format 'X.Y' and converts it into a |
| 199 | + tuple of integers (X, Y). This can be useful for comparing version numbers. |
| 200 | +
|
| 201 | + :param v: The version string to be converted, expected in the format 'X.Y'. |
| 202 | + :type v: str |
| 203 | + :return: A tuple of integers representing the version, in the format (X, Y). |
| 204 | + :rtype: tuple[int, int] |
| 205 | + :raises ValueError: If the version string is not in the expected format or contains |
| 206 | + non-integer components. |
| 207 | + """ |
| 208 | + major, minor = v.split(".") |
| 209 | + return int(major), int(minor) |
0 commit comments