33import re
44import requests
55import subprocess
6- import sys
76import tempfile
87import tqdm
98from typing import IO , Dict , Optional , Tuple
3938 re .MULTILINE )
4039
4140def parse_java_version (version_text : str ) -> Tuple [int , int ]:
42- """Return Java version (major1, major2).
43-
44- >>> parse_java_version('''java version "1.6.0_65"
45- ... Java(TM) SE Runtime Environment (build 1.6.0_65-b14-462-11M4609)
46- ... Java HotSpot(TM) 64-Bit Server VM (build 20.65-b04-462, mixed mode))
47- ... ''')
48- (1, 6)
41+ """
42+ Parse the Java version from a given version text.
4943
50- >>> parse_java_version('''
51- ... openjdk version "1.8.0_60"
52- ... OpenJDK Runtime Environment (build 1.8.0_60-b27)
53- ... OpenJDK 64-Bit Server VM (build 25.60-b23, mixed mode))
54- ... ''')
55- (1, 8)
44+ This function attempts to extract the major version numbers from the provided
45+ Java version string using regular expressions. It supports two different
46+ version formats defined by JAVA_VERSION_REGEX and JAVA_VERSION_REGEX_UPDATED.
5647
48+ :param version_text: The Java version string to parse.
49+ :type version_text: str
50+ :return: A tuple containing the major version numbers.
51+ :rtype: Tuple[int, int]
52+ :raises SystemExit: If the version string cannot be parsed.
5753 """
5854 match = (
5955 re .search (JAVA_VERSION_REGEX , version_text )
@@ -67,7 +63,17 @@ def parse_java_version(version_text: str) -> Tuple[int, int]:
6763
6864
6965def confirm_java_compatibility () -> bool :
70- """ Confirms Java major version >= 8. """
66+ """
67+ Confirms if the installed Java version is compatible with language-tool-python.
68+ This function checks if Java is installed and verifies that the major version is at least 8.
69+ It raises an error if Java is not installed or if the version is incompatible.
70+
71+ :raises ModuleNotFoundError: If no Java installation is detected.
72+ :raises SystemError: If the detected Java version is less than 8.
73+ :return: True if the Java version is compatible.
74+ :rtype: bool
75+ """
76+
7177 java_path = which ('java' )
7278 if not java_path :
7379 raise ModuleNotFoundError (
@@ -94,15 +100,32 @@ def confirm_java_compatibility() -> bool:
94100
95101
96102def get_common_prefix (z : zipfile .ZipFile ) -> Optional [str ]:
97- """Get common directory in a zip file if any."""
103+ """
104+ Determine the common prefix of all file names in a zip archive.
105+
106+ :param z: A ZipFile object representing the zip archive.
107+ :type z: zipfile.ZipFile
108+ :return: The common prefix of all file names in the zip archive, or None if there is no common prefix.
109+ :rtype: Optional[str]
110+ """
111+
98112 name_list = z .namelist ()
99113 if name_list and all (n .startswith (name_list [0 ]) for n in name_list [1 :]):
100114 return name_list [0 ]
101115 return None
102116
103117
104118def http_get (url : str , out_file : IO [bytes ], proxies : Optional [Dict [str , str ]] = None ) -> None :
105- """ Get contents of a URL and save to a file.
119+ """
120+ Downloads a file from a given URL and writes it to the specified output file.
121+
122+ :param url: The URL to download the file from.
123+ :type url: str
124+ :param out_file: The file object to write the downloaded content to.
125+ :type out_file: IO[bytes]
126+ :param proxies: Optional dictionary of proxies to use for the request.
127+ :type proxies: Optional[Dict[str, str]]
128+ :raises Exception: If the file could not be found at the given URL (HTTP 403).
106129 """
107130 req = requests .get (url , stream = True , proxies = proxies )
108131 content_length = req .headers .get ('Content-Length' )
@@ -120,14 +143,29 @@ def http_get(url: str, out_file: IO[bytes], proxies: Optional[Dict[str, str]] =
120143
121144
122145def unzip_file (temp_file : str , directory_to_extract_to : str ) -> None :
123- """ Unzips a .zip file to folder path. """
146+ """
147+ Unzips a zip file to a specified directory.
148+
149+ :param temp_file: A temporary file object representing the zip file to be extracted.
150+ :type temp_file: str
151+ :param directory_to_extract_to: The directory where the contents of the zip file will be extracted.
152+ :type directory_to_extract_to: str
153+ """
154+
124155 logger .info (f'Unzipping { temp_file .name } to { directory_to_extract_to } .' )
125156 with zipfile .ZipFile (temp_file .name , 'r' ) as zip_ref :
126157 zip_ref .extractall (directory_to_extract_to )
127158
128159
129160def download_zip (url : str , directory : str ) -> None :
130- """ Downloads and unzips zip file from `url` to `directory`. """
161+ """
162+ Downloads a ZIP file from the given URL and extracts it to the specified directory.
163+
164+ :param url: The URL of the ZIP file to download.
165+ :type url: str
166+ :param directory: The directory where the ZIP file should be extracted.
167+ :type directory: str
168+ """
131169 # Download file.
132170 downloaded_file = tempfile .NamedTemporaryFile (suffix = '.zip' , delete = False )
133171 http_get (url , downloaded_file )
@@ -142,6 +180,19 @@ def download_zip(url: str, directory: str) -> None:
142180
143181
144182def download_lt (language_tool_version : Optional [str ] = LTP_DOWNLOAD_VERSION ) -> None :
183+ """
184+ Downloads and extracts the specified version of LanguageTool.
185+ This function checks for Java compatibility, creates the necessary download
186+ directory if it does not exist, and downloads the specified version of
187+ LanguageTool if it is not already present.
188+
189+ :param language_tool_version: The version of LanguageTool to download. If not
190+ specified, the default version defined by
191+ LTP_DOWNLOAD_VERSION is used.
192+ :type language_tool_version: Optional[str]
193+ :raises AssertionError: If the download folder is not a directory.
194+ """
195+
145196 confirm_java_compatibility ()
146197
147198 download_folder = get_language_tool_download_path ()
0 commit comments