commit e3ac62dc29dfe56be52ce0f633e9381d764767b5 Author: Vaydheesh Date: Sun Apr 7 07:24:24 2019 +0530 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__ diff --git a/installer.py b/installer.py index 91f23c9..12e81a2 100644 --- a/installer.py +++ b/installer.py @@ -1,21 +1,20 @@ -from urllib.request import urlretrieve -from zipfile import ZipFile +import logging import os -from os import path import platform -from distutils.dir_util import copy_tree -from shutil import rmtree -import logging +import shutil import tempfile - +from distutils.dir_util import copy_tree +from urllib.request import urlretrieve +from zipfile import ZipFile class Installer: + base_link = "http://apertium.projectjj.com/{}{}" + def __init__(self, languages): # type: (Installer, tuple) -> None self._install_path = os.getenv('LOCALAPPDATA') - self._apertium_path = path.join(self._install_path, 'apertium-all-dev') + self._apertium_path = os.path.join(self._install_path, 'apertium-all-dev') self._download_path = tempfile.mkdtemp() - self._language_link = 'http://apertium.projectjj.com/win32/nightly/data.php?zip={}' self._languages = languages logging.basicConfig(filename='installer.log', format='%(asctime)s %(message)s', filemode='w', level=logging.DEBUG) @@ -26,7 +25,7 @@ class Installer: # type: (Installer, dict, str, str) -> None for zip_name, zip_link in download_files.items(): - zip_download_path = path.join(download_dir, zip_name) + zip_download_path = os.path.join(download_dir, zip_name) urlretrieve(zip_link, filename=zip_download_path) self._logger.info('%s download completed', zip_name) @@ -54,28 +53,31 @@ class Installer: def download_language_data(self): # type: (Installer) -> None """Installs Language Data to Apertium""" + zip_path = "" + if platform.system() == 'Windows': + zip_path = "win32/nightly/data.php?zip=" download_dir = self._download_path extract_path = self._download_path language_zip = dict() for curr_lang in self._languages: - language_link = self._language_link.format(curr_lang) + language_link = Installer.base_link.format(zip_path, curr_lang) language_zip[curr_lang] = language_link self._download_zip(language_zip, download_dir, extract_path) # move the extracted files to desired location - lang_data_path = path.join(self._download_path, 'usr', 'share', 'apertium') + lang_data_path = os.path.join(self._download_path, 'usr', 'share', 'apertium') self._logger.info("Copying Language Data to Apertium") for directory in os.listdir(lang_data_path): - source = path.join(lang_data_path, directory) - destination = path.join(self._apertium_path, 'share', 'apertium', directory) + source = os.path.join(lang_data_path, directory) + destination = os.path.join(self._apertium_path, 'share', 'apertium', directory) copy_tree(source, destination) self._logger.info('%s -> %s', source, destination) - rmtree(path.join(extract_path, 'usr')) + shutil.rmtree(os.path.join(extract_path, 'usr')) - def mode_editor(self): # type: (Installer) -> None + def edit_modes(self): # type: (Installer) -> None """The mode files need to be modified before being used on Windows System 1. Replace /usr/share with %localappdata%\apertium-all-dev\share @@ -84,19 +86,19 @@ class Installer: """ # List of Mode Files - mode_path = path.join(self._apertium_path, 'share', 'apertium', 'modes') - only_files = [file for file in os.listdir(mode_path) - if path.isfile(path.join(mode_path, file)) and - 'mode' in file] + mode_path = os.path.join(self._apertium_path, 'share', 'apertium', 'modes') + only_files = [f for f in os.listdir(mode_path) + if os.path.isfile(os.path.join(mode_path, f)) and + 'mode' in f] for file in only_files: - self._logger.info("Opening %s for editing", file) - infile = open(path.join(mode_path, file), 'r') - line = infile.read() - infile.close() + self._logger.info("Editing mode %s ", file) + with open(os.path.join(mode_path, file), 'r') as infile: + line = infile.read() + contents = line.split(' ') # Editing mode file to be compatible with windows platform - for index, t in enumerate(contents): + for i, t in enumerate(contents): if len(t) > 2 and t[0] == "'" and t[1] == "/": t = t.replace('/', '\\') t = t.replace(r'\usr', self._apertium_path) @@ -104,23 +106,17 @@ class Installer: # Raising Error: 'File can't be opened error' # Hence removing quotes from file t = t.replace("'", '') - contents[index] = t + contents[i] = t line = ' '.join(contents) - outfile = open(path.join(mode_path, file), 'w') - outfile.write(line) - outfile.close() - self._logger.info("Closing %s", file) - + with open(os.path.join(mode_path, file), 'w') as outfile: + outfile.write(line) + outfile.close() def install_apertium_windows(): - # Download ApertiumWin64 and Move to %localappdata% + """Download ApertiumWin64 and Move to %localappdata%""" if platform.system() == 'Windows': p = Installer(('apertium-eng', 'apertium-en-es')) p.download_apertium_windows() p.download_language_data() - p.mode_editor() - - -if __name__ == '__main__': - install_apertium_windows() + p.edit_modes()