Skip to content

Commit e3ac62d

Browse files
Implemented changes suggested
Sorted the import statements Edited: import shutil Removed: from os import path Added: class variable with base link for downloading Edited: with open instead of open Edited: logging messages Removed: blank lines around class definition Renamed: mode_editor() -> edit_modes() Removed: Redundant if __name__
1 parent ff12a5f commit e3ac62d

File tree

1 file changed

+33
-37
lines changed

1 file changed

+33
-37
lines changed

installer.py

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
from urllib.request import urlretrieve
2-
from zipfile import ZipFile
1+
import logging
32
import os
4-
from os import path
53
import platform
6-
from distutils.dir_util import copy_tree
7-
from shutil import rmtree
8-
import logging
4+
import shutil
95
import tempfile
10-
6+
from distutils.dir_util import copy_tree
7+
from urllib.request import urlretrieve
8+
from zipfile import ZipFile
119

1210
class Installer:
1311

12+
base_link = "http://apertium.projectjj.com/{}{}"
13+
1414
def __init__(self, languages): # type: (Installer, tuple) -> None
1515
self._install_path = os.getenv('LOCALAPPDATA')
16-
self._apertium_path = path.join(self._install_path, 'apertium-all-dev')
16+
self._apertium_path = os.path.join(self._install_path, 'apertium-all-dev')
1717
self._download_path = tempfile.mkdtemp()
18-
self._language_link = 'http://apertium.projectjj.com/win32/nightly/data.php?zip={}'
1918
self._languages = languages
2019
logging.basicConfig(filename='installer.log', format='%(asctime)s %(message)s',
2120
filemode='w', level=logging.DEBUG)
@@ -26,7 +25,7 @@ def _download_zip(self, download_files, download_dir, extract_path):
2625
# type: (Installer, dict, str, str) -> None
2726

2827
for zip_name, zip_link in download_files.items():
29-
zip_download_path = path.join(download_dir, zip_name)
28+
zip_download_path = os.path.join(download_dir, zip_name)
3029
urlretrieve(zip_link, filename=zip_download_path)
3130
self._logger.info('%s download completed', zip_name)
3231

@@ -54,28 +53,31 @@ def download_apertium_windows(self): # type: (Installer) -> None
5453
def download_language_data(self): # type: (Installer) -> None
5554
"""Installs Language Data to Apertium"""
5655

56+
zip_path = ""
57+
if platform.system() == 'Windows':
58+
zip_path = "win32/nightly/data.php?zip="
5759
download_dir = self._download_path
5860
extract_path = self._download_path
5961
language_zip = dict()
6062
for curr_lang in self._languages:
61-
language_link = self._language_link.format(curr_lang)
63+
language_link = Installer.base_link.format(zip_path, curr_lang)
6264
language_zip[curr_lang] = language_link
6365

6466
self._download_zip(language_zip, download_dir, extract_path)
6567

6668
# move the extracted files to desired location
67-
lang_data_path = path.join(self._download_path, 'usr', 'share', 'apertium')
69+
lang_data_path = os.path.join(self._download_path, 'usr', 'share', 'apertium')
6870

6971
self._logger.info("Copying Language Data to Apertium")
7072
for directory in os.listdir(lang_data_path):
71-
source = path.join(lang_data_path, directory)
72-
destination = path.join(self._apertium_path, 'share', 'apertium', directory)
73+
source = os.path.join(lang_data_path, directory)
74+
destination = os.path.join(self._apertium_path, 'share', 'apertium', directory)
7375
copy_tree(source, destination)
7476
self._logger.info('%s -> %s', source, destination)
7577

76-
rmtree(path.join(extract_path, 'usr'))
78+
shutil.rmtree(os.path.join(extract_path, 'usr'))
7779

78-
def mode_editor(self): # type: (Installer) -> None
80+
def edit_modes(self): # type: (Installer) -> None
7981
"""The mode files need to be modified before being used on Windows System
8082
8183
1. Replace /usr/share with %localappdata%\apertium-all-dev\share
@@ -84,43 +86,37 @@ def mode_editor(self): # type: (Installer) -> None
8486
"""
8587

8688
# List of Mode Files
87-
mode_path = path.join(self._apertium_path, 'share', 'apertium', 'modes')
88-
only_files = [file for file in os.listdir(mode_path)
89-
if path.isfile(path.join(mode_path, file)) and
90-
'mode' in file]
89+
mode_path = os.path.join(self._apertium_path, 'share', 'apertium', 'modes')
90+
only_files = [f for f in os.listdir(mode_path)
91+
if os.path.isfile(os.path.join(mode_path, f)) and
92+
'mode' in f]
9193

9294
for file in only_files:
93-
self._logger.info("Opening %s for editing", file)
94-
infile = open(path.join(mode_path, file), 'r')
95-
line = infile.read()
96-
infile.close()
95+
self._logger.info("Editing mode %s ", file)
96+
with open(os.path.join(mode_path, file), 'r') as infile:
97+
line = infile.read()
98+
9799
contents = line.split(' ')
98100
# Editing mode file to be compatible with windows platform
99-
for index, t in enumerate(contents):
101+
for i, t in enumerate(contents):
100102
if len(t) > 2 and t[0] == "'" and t[1] == "/":
101103
t = t.replace('/', '\\')
102104
t = t.replace(r'\usr', self._apertium_path)
103105
# Instead of calling eng.autogen.bin, cmd calls 'eng.autogen.bin'
104106
# Raising Error: 'File can't be opened error'
105107
# Hence removing quotes from file
106108
t = t.replace("'", '')
107-
contents[index] = t
109+
contents[i] = t
108110
line = ' '.join(contents)
109-
outfile = open(path.join(mode_path, file), 'w')
110-
outfile.write(line)
111-
outfile.close()
112-
self._logger.info("Closing %s", file)
113-
111+
with open(os.path.join(mode_path, file), 'w') as outfile:
112+
outfile.write(line)
113+
outfile.close()
114114

115115
def install_apertium_windows():
116-
# Download ApertiumWin64 and Move to %localappdata%
116+
"""Download ApertiumWin64 and Move to %localappdata%"""
117117

118118
if platform.system() == 'Windows':
119119
p = Installer(('apertium-eng', 'apertium-en-es'))
120120
p.download_apertium_windows()
121121
p.download_language_data()
122-
p.mode_editor()
123-
124-
125-
if __name__ == '__main__':
126-
install_apertium_windows()
122+
p.edit_modes()

0 commit comments

Comments
 (0)