3
3
import re
4
4
import requests
5
5
import subprocess
6
- import sys
7
6
import tempfile
8
7
import tqdm
9
8
from typing import IO , Dict , Optional , Tuple
39
38
re .MULTILINE )
40
39
41
40
def 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.
49
43
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.
56
47
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.
57
53
"""
58
54
match = (
59
55
re .search (JAVA_VERSION_REGEX , version_text )
@@ -67,7 +63,17 @@ def parse_java_version(version_text: str) -> Tuple[int, int]:
67
63
68
64
69
65
def 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
+
71
77
java_path = which ('java' )
72
78
if not java_path :
73
79
raise ModuleNotFoundError (
@@ -94,15 +100,32 @@ def confirm_java_compatibility() -> bool:
94
100
95
101
96
102
def 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
+
98
112
name_list = z .namelist ()
99
113
if name_list and all (n .startswith (name_list [0 ]) for n in name_list [1 :]):
100
114
return name_list [0 ]
101
115
return None
102
116
103
117
104
118
def 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).
106
129
"""
107
130
req = requests .get (url , stream = True , proxies = proxies )
108
131
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]] =
120
143
121
144
122
145
def 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
+
124
155
logger .info (f'Unzipping { temp_file .name } to { directory_to_extract_to } .' )
125
156
with zipfile .ZipFile (temp_file .name , 'r' ) as zip_ref :
126
157
zip_ref .extractall (directory_to_extract_to )
127
158
128
159
129
160
def 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
+ """
131
169
# Download file.
132
170
downloaded_file = tempfile .NamedTemporaryFile (suffix = '.zip' , delete = False )
133
171
http_get (url , downloaded_file )
@@ -142,6 +180,19 @@ def download_zip(url: str, directory: str) -> None:
142
180
143
181
144
182
def 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
+
145
196
confirm_java_compatibility ()
146
197
147
198
download_folder = get_language_tool_download_path ()
0 commit comments