Skip to content

Commit

Permalink
Implemented changes suggested
Browse files Browse the repository at this point in the history
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__
  • Loading branch information
singh-lokendra committed Apr 7, 2019
1 parent ff12a5f commit e3ac62d
Showing 1 changed file with 33 additions and 37 deletions.
70 changes: 33 additions & 37 deletions 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)
Expand All @@ -26,7 +25,7 @@ def _download_zip(self, download_files, download_dir, extract_path):
# 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)

Expand Down Expand Up @@ -54,28 +53,31 @@ def download_apertium_windows(self): # type: (Installer) -> None
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
Expand All @@ -84,43 +86,37 @@ def mode_editor(self): # type: (Installer) -> None
"""

# 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)
# Instead of calling eng.autogen.bin, cmd calls 'eng.autogen.bin'
# 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()

0 comments on commit e3ac62d

Please sign in to comment.