|
8 | 8 | from zipfile import ZipFile
|
9 | 9 |
|
10 | 10 | class Installer:
|
11 |
| - base_link = "http://apertium.projectjj.com/{}{}" |
| 11 | + base_link = "http://apertium.projectjj.com/{}" |
12 | 12 |
|
13 | 13 | def __init__(self, languages): # type: (Installer, list) -> None
|
14 | 14 | self._install_path = os.getenv("LOCALAPPDATA")
|
15 | 15 | self._apertium_path = os.path.join(self._install_path, "apertium-all-dev")
|
16 | 16 | self._download_path = tempfile.mkdtemp()
|
17 | 17 | 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) |
20 | 19 | self._logger = logging.getLogger()
|
21 | 20 | self._logger.setLevel(logging.DEBUG)
|
22 | 21 |
|
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 |
25 | 23 | for zip_name, zip_link in download_files.items():
|
26 | 24 | 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) |
28 | 26 | self._logger.info("%s download completed", zip_name)
|
29 | 27 |
|
30 | 28 | # 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) |
37 | 35 |
|
38 | 36 | def _download_apertium_windows(self): # type: (Installer) -> None
|
39 | 37 | """Installs Apertium-all-dev to %localappdata%"""
|
40 | 38 |
|
41 | 39 | apertium_windows = {
|
42 | 40 | "apertium-all-dev.zip":
|
43 |
| - Installer.base_link.format("/win64/nightly/apertium-all-dev.zip", "") |
| 41 | + "/win64/nightly/apertium-all-dev.zip" |
44 | 42 | }
|
45 | 43 |
|
46 |
| - self._download_zip(apertium_windows, self._install_path) |
| 44 | + self._download_zips(apertium_windows, self._install_path) |
47 | 45 |
|
48 | 46 | def _download_package(self): # type: (Installer) -> None
|
49 | 47 | """Installs Language Data to Apertium"""
|
50 | 48 |
|
51 |
| - zip_path = "" |
52 | 49 | if platform.system() == "Windows":
|
53 | 50 | zip_path = "win32/nightly/data.php?zip="
|
| 51 | + else: |
| 52 | + raise ValueError("Installation for {} is not supported".format(platform.system())) |
54 | 53 | language_zip = {}
|
55 | 54 | 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 |
58 | 56 |
|
59 |
| - self._download_zip(language_zip, self._download_path) |
| 57 | + self._download_zips(language_zip, self._download_path) |
60 | 58 |
|
61 | 59 | # move the extracted files to desired location
|
62 | 60 | lang_data_path = os.path.join(self._download_path, "usr", "share", "apertium")
|
@@ -89,7 +87,7 @@ def _edit_modes(self): # type: (Installer) -> None
|
89 | 87 | # Editing mode file to be compatible with windows platform
|
90 | 88 | for i, t in enumerate(contents):
|
91 | 89 | if len(t) > 2 and t[0] == "'" and t[1] == "/":
|
92 |
| - t = t.replace("/", "\\") |
| 90 | + t = t.replace("/", os.sep) |
93 | 91 | t = t.replace(r"\usr", self._apertium_path)
|
94 | 92 | contents[i] = t
|
95 | 93 | line = " ".join(contents)
|
|
0 commit comments