Skip to content

Commit 908dcc6

Browse files
authored
Merge pull request jxmorris12#86 from pidefrem/deactivate_download_if_env_var_defined
Introduce new env var `LTP_JAR_DIR_PATH` to let the user specify a custom installation path of LT
2 parents 6246cdf + c2fcc7f commit 908dcc6

File tree

10 files changed

+435
-216
lines changed

10 files changed

+435
-216
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
![Test with PyTest](https://github.com/jxmorris12/language_tool_python/workflows/Test%20with%20PyTest/badge.svg)
66

7-
Current LanguageTool version: **5.5**
7+
Current LanguageTool version: **6.4**
88

99
This is a Python wrapper for [LanguageTool](https://languagetool.org). LanguageTool is open-source grammar tool, also known as the spellchecker for OpenOffice. This library allows you to make to detect grammar errors and spelling mistakes through a Python script or through a command-line interface.
1010

@@ -151,7 +151,7 @@ You can run LanguageTool on one host and connect to it from another. This is us
151151

152152
## Configuration
153153

154-
LanguageTool offers lots of built-in configuration options.
154+
LanguageTool offers lots of built-in configuration options.
155155

156156
### Example: Enabling caching
157157
Here's an example of using the configuration options to enable caching. Some users have reported that this helps performance a lot.
@@ -222,13 +222,20 @@ Searching for a specific rule to enable or disable? Curious the breadth of rules
222222

223223
### Customizing Download URL or Path
224224

225+
If LanguageTool is already installed on your system, you can defined the following environment variable:
226+
```bash
227+
$ export LTP_JAR_DIR_PATH = /path/to/the/language/tool/jar/files
228+
```
229+
230+
Overwise, `language_tool_python` can download LanguageTool for you automatically.
231+
225232
To overwrite the host part of URL that is used to download LanguageTool-{version}.zip:
226233

227234
```bash
228235
$ export LTP_DOWNLOAD_HOST = [alternate URL]
229236
```
230237

231-
This can be used to downgrade to an older version, for example, or to download from a mirror.
238+
This can be used to downgrade to an older version, for example, or to download from a mirror.
232239

233240
And to choose the specific folder to download the server to:
234241

@@ -252,6 +259,7 @@ into where the ``language_tool_python`` package resides.
252259

253260
As of April 2020, `language_tool_python` was forked from `language-check` and no longer supports LanguageTool versions lower than 4.0.
254261

255-
### Acknowledgements
262+
### Acknowledgements
263+
256264
This is a fork of https://github.com/myint/language-check/ that produces more easily parsable
257265
results from the command-line.

language_tool_python/__main__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from .server import LanguageTool
99
from .utils import LanguageToolError
1010

11-
import pkg_resources
11+
import pkg_resources
1212
__version__ = pkg_resources.require("language_tool_python")[0].version
1313

1414

@@ -175,4 +175,4 @@ def main():
175175
return status
176176

177177

178-
sys.exit(main())
178+
sys.exit(main())

language_tool_python/download_lt.py

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
# -*- coding: utf-8 -*-
33
"""Download latest LanguageTool distribution."""
44

5-
import glob
65
import logging
76
import os
87
import re
@@ -11,11 +10,16 @@
1110
import sys
1211
import tempfile
1312
import tqdm
13+
from typing import Optional
1414
import zipfile
1515

1616
from distutils.spawn import find_executable
1717
from urllib.parse import urljoin
18-
from .utils import get_language_tool_download_path
18+
from .utils import (
19+
find_existing_language_tool_downloads,
20+
get_language_tool_download_path,
21+
LTP_JAR_DIR_PATH_ENV_VAR
22+
)
1923

2024
# Create logger for this file.
2125
logging.basicConfig(format='%(message)s')
@@ -27,7 +31,7 @@
2731
BASE_URL = os.environ.get('LTP_DOWNLOAD_HOST', 'https://www.languagetool.org/download/')
2832
FILENAME = 'LanguageTool-{version}.zip'
2933

30-
LATEST_VERSION = '6.2'
34+
LTP_DOWNLOAD_VERSION = '6.4'
3135

3236
JAVA_VERSION_REGEX = re.compile(
3337
r'^(?:java|openjdk) version "(?P<major1>\d+)(|\.(?P<major2>\d+)\.[^"]+)"',
@@ -56,27 +60,36 @@ def parse_java_version(version_text):
5660
(1, 8)
5761
5862
"""
59-
match = re.search(JAVA_VERSION_REGEX, version_text) or re.search(JAVA_VERSION_REGEX_UPDATED, version_text)
63+
match = (
64+
re.search(JAVA_VERSION_REGEX, version_text)
65+
or re.search(JAVA_VERSION_REGEX_UPDATED, version_text)
66+
)
6067
if not match:
6168
raise SystemExit(
6269
'Could not parse Java version from """{}""".'.format(version_text))
6370
major1 = int(match.group('major1'))
6471
major2 = int(match.group('major2')) if match.group('major2') else 0
6572
return (major1, major2)
6673

74+
6775
def confirm_java_compatibility():
6876
""" Confirms Java major version >= 8. """
6977
java_path = find_executable('java')
7078
if not java_path:
71-
raise ModuleNotFoundError('No java install detected. Please install java to use language-tool-python.')
79+
raise ModuleNotFoundError(
80+
'No java install detected. '
81+
'Please install java to use language-tool-python.'
82+
)
7283

7384
output = subprocess.check_output([java_path, '-version'],
7485
stderr=subprocess.STDOUT,
7586
universal_newlines=True)
7687

7788
major_version, minor_version = parse_java_version(output)
78-
# Some installs of java show the version number like `14.0.1` and others show `1.14.0.1`
79-
# (with a leading 1). We want to support both, as long as the major version is >= 8.
89+
# Some installs of java show the version number like `14.0.1`
90+
# and others show `1.14.0.1`
91+
# (with a leading 1). We want to support both,
92+
# as long as the major version is >= 8.
8093
# (See softwareengineering.stackexchange.com/questions/175075/why-is-java-version-1-x-referred-to-as-java-x)
8194
if major_version == 1 and minor_version >= 8:
8295
return True
@@ -85,31 +98,37 @@ def confirm_java_compatibility():
8598
else:
8699
raise SystemError('Detected java {}.{}. LanguageTool requires Java >= 8.'.format(major_version, minor_version))
87100

101+
88102
def get_common_prefix(z):
89103
"""Get common directory in a zip file if any."""
90104
name_list = z.namelist()
91105
if name_list and all(n.startswith(name_list[0]) for n in name_list[1:]):
92106
return name_list[0]
93107
return None
94108

109+
95110
def http_get(url, out_file, proxies=None):
96111
""" Get contents of a URL and save to a file.
97112
"""
98113
req = requests.get(url, stream=True, proxies=proxies)
99114
content_length = req.headers.get('Content-Length')
100115
total = int(content_length) if content_length is not None else None
101-
if req.status_code == 403: # Not found on AWS
116+
if req.status_code == 403: # Not found on AWS
102117
raise Exception('Could not find at URL {}.'.format(url))
103-
progress = tqdm.tqdm(unit="B", unit_scale=True, total=total, desc=f'Downloading LanguageTool {LATEST_VERSION}')
118+
progress = tqdm.tqdm(unit="B", unit_scale=True, total=total,
119+
desc=f'Downloading LanguageTool {LTP_DOWNLOAD_VERSION}')
104120
for chunk in req.iter_content(chunk_size=1024):
105-
if chunk: # filter out keep-alive new chunks
121+
if chunk: # filter out keep-alive new chunks
106122
progress.update(len(chunk))
107123
out_file.write(chunk)
108124
progress.close()
109125

126+
110127
def unzip_file(temp_file, directory_to_extract_to):
111128
""" Unzips a .zip file to folder path. """
112-
logger.info('Unzipping {} to {}.'.format(temp_file.name, directory_to_extract_to))
129+
logger.info(
130+
'Unzipping {} to {}.'.format(temp_file.name, directory_to_extract_to)
131+
)
113132
with zipfile.ZipFile(temp_file.name, 'r') as zip_ref:
114133
zip_ref.extractall(directory_to_extract_to)
115134

@@ -128,26 +147,34 @@ def download_zip(url, directory):
128147
# Tell the user the download path.
129148
logger.info('Downloaded {} to {}.'.format(url, directory))
130149

131-
def download_lt():
132-
download_folder = get_language_tool_download_path()
133-
assert os.path.isdir(download_folder)
134-
old_path_list = [
135-
path for path in
136-
glob.glob(os.path.join(download_folder, 'LanguageTool*'))
137-
if os.path.isdir(path)
138-
]
139150

151+
def download_lt(language_tool_version: Optional[str] = LTP_DOWNLOAD_VERSION):
140152
confirm_java_compatibility()
141-
version = LATEST_VERSION
142-
filename = FILENAME.format(version=version)
143-
language_tool_download_url = urljoin(BASE_URL, filename)
144-
dirname, _ = os.path.splitext(filename)
145-
extract_path = os.path.join(download_folder, dirname)
146153

147-
if extract_path in old_path_list:
154+
download_folder = get_language_tool_download_path()
155+
156+
# Use the env var to the jar directory if it is defined
157+
# otherwise look in the download directory
158+
if os.environ.get(LTP_JAR_DIR_PATH_ENV_VAR):
148159
return
149160

150-
download_zip(language_tool_download_url, download_folder)
161+
# Make download path, if it doesn't exist.
162+
os.makedirs(download_folder, exist_ok=True)
163+
164+
assert os.path.isdir(download_folder)
165+
old_path_list = find_existing_language_tool_downloads(download_folder)
166+
167+
if language_tool_version:
168+
version = language_tool_version
169+
filename = FILENAME.format(version=version)
170+
language_tool_download_url = urljoin(BASE_URL, filename)
171+
dirname, _ = os.path.splitext(filename)
172+
extract_path = os.path.join(download_folder, dirname)
173+
174+
if extract_path in old_path_list:
175+
return
176+
download_zip(language_tool_download_url, download_folder)
177+
151178

152179
if __name__ == '__main__':
153180
sys.exit(download_lt())

0 commit comments

Comments
 (0)