Skip to content

Commit 95f140f

Browse files
Implemented changes suggested
- Replaced: '\' with os.sep - Renamed: Installer._download_zip() -> Installer._download_zips() - Removed: New Line at start of Installer._download_zips() - Modified: Installer.base_link is modified in _download_zips() - Modified: Used 'with' while opening zip_file in _download_zips() - Raise ValueError when setup isn't run on Windows Platform - Modified: Send logs to stdout
1 parent 402bee9 commit 95f140f

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

installer.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,53 @@
88
from zipfile import ZipFile
99

1010
class Installer:
11-
base_link = "http://apertium.projectjj.com/{}{}"
11+
base_link = "http://apertium.projectjj.com/{}"
1212

1313
def __init__(self, languages): # type: (Installer, list) -> None
1414
self._install_path = os.getenv("LOCALAPPDATA")
1515
self._apertium_path = os.path.join(self._install_path, "apertium-all-dev")
1616
self._download_path = tempfile.mkdtemp()
1717
self._languages = languages
18-
logging.basicConfig(filename="installer.log", format="%(asctime)s %(message)s",
19-
filemode="w", level=logging.DEBUG)
18+
logging.basicConfig(format="%(asctime)s %(message)s", level=logging.DEBUG)
2019
self._logger = logging.getLogger()
2120
self._logger.setLevel(logging.DEBUG)
2221

23-
def _download_zip(self, download_files, extract_path): # type: (Installer, dict, str) -> None
24-
22+
def _download_zips(self, download_files, extract_path): # type: (Installer, dict, str) -> None
2523
for zip_name, zip_link in download_files.items():
2624
zip_download_path = os.path.join(self._download_path, zip_name)
27-
urlretrieve(zip_link, filename=zip_download_path)
25+
urlretrieve(Installer.base_link.format(zip_link), filename=zip_download_path)
2826
self._logger.info("%s download completed", zip_name)
2927

3028
# Extract the zip
31-
zip_file = ZipFile(zip_download_path)
32-
zip_file.extractall(path=extract_path)
33-
zip_file.close()
34-
self._logger.info("%s Extraction completed", zip_name)
35-
os.remove(zip_download_path)
36-
self._logger.info("%s removed", zip_name)
29+
with ZipFile(zip_download_path) as zip_file:
30+
zip_file.extractall(path=extract_path)
31+
zip_file.close()
32+
self._logger.info("%s Extraction completed", zip_name)
33+
os.remove(zip_download_path)
34+
self._logger.info("%s removed", zip_name)
3735

3836
def _download_apertium_windows(self): # type: (Installer) -> None
3937
"""Installs Apertium-all-dev to %localappdata%"""
4038

4139
apertium_windows = {
4240
"apertium-all-dev.zip":
43-
Installer.base_link.format("/win64/nightly/apertium-all-dev.zip", "")
41+
"/win64/nightly/apertium-all-dev.zip"
4442
}
4543

46-
self._download_zip(apertium_windows, self._install_path)
44+
self._download_zips(apertium_windows, self._install_path)
4745

4846
def _download_package(self): # type: (Installer) -> None
4947
"""Installs Language Data to Apertium"""
5048

51-
zip_path = ""
5249
if platform.system() == "Windows":
5350
zip_path = "win32/nightly/data.php?zip="
51+
else:
52+
raise ValueError("Installation for {} is not supported".format(platform.system()))
5453
language_zip = {}
5554
for curr_lang in self._languages:
56-
language_link = Installer.base_link.format(zip_path, curr_lang)
57-
language_zip[curr_lang] = language_link
55+
language_zip[curr_lang] = zip_path + curr_lang
5856

59-
self._download_zip(language_zip, self._download_path)
57+
self._download_zips(language_zip, self._download_path)
6058

6159
# move the extracted files to desired location
6260
lang_data_path = os.path.join(self._download_path, "usr", "share", "apertium")
@@ -89,7 +87,7 @@ def _edit_modes(self): # type: (Installer) -> None
8987
# Editing mode file to be compatible with windows platform
9088
for i, t in enumerate(contents):
9189
if len(t) > 2 and t[0] == "'" and t[1] == "/":
92-
t = t.replace("/", "\\")
90+
t = t.replace("/", os.sep)
9391
t = t.replace(r"\usr", self._apertium_path)
9492
contents[i] = t
9593
line = " ".join(contents)

0 commit comments

Comments
 (0)