commit b158840ea7294f6d0a3d78e676d5f593996becbe Author: Vaydheesh Date: Sat Apr 6 17:53:19 2019 +0530 Combine the import statements to improve readability diff --git a/apertium/__init__.py b/apertium/__init__.py index 3e0caa5..788ebcc 100644 --- a/apertium/__init__.py +++ b/apertium/__init__.py @@ -1,11 +1,9 @@ +import platform +import os from apertium.mode_search import search_path from apertium.analysis import Analyzer, analyze # noqa: F401 from apertium.generation import Generator, generate # noqa: F401 from apertium.translation import Translator, translate # noqa: F401 -from platform import system -from os import getenv -from os.path import join -from os.path import isdir if False: @@ -43,11 +41,11 @@ def append_pair_path(pair_path): # type: (str) -> None def append_pair_path_windows(): - if system() == 'Windows': - install_path = getenv('LOCALAPPDATA') + if platform.system() == 'Windows': + install_path = os.getenv('LOCALAPPDATA') apertium_lang_path = \ - join(install_path, 'apertium-all-dev', 'share', 'apertium') - if isdir(apertium_lang_path): + os.path.join(install_path, 'apertium-all-dev', 'share', 'apertium') + if os.path.isdir(apertium_lang_path): append_pair_path(apertium_lang_path) diff --git a/apertium/utils.py b/apertium/utils.py index fd1ff21..067957b 100644 --- a/apertium/utils.py +++ b/apertium/utils.py @@ -1,10 +1,8 @@ import subprocess import re -from platform import system -from os import getenv -from os import putenv -from os.path import join -from os.path import isdir +import platform +import os +from os import path if False: @@ -44,14 +42,14 @@ def execute(inp, commands): # type: (str, List[List[str]]) -> str procs = [] end = inp.encode() - # Adding the Apertium Binaries to Process' Path - if system() == 'Windows': - install_path = getenv('LOCALAPPDATA') - current = getenv('path') - apertium_path = join(install_path, 'apertium-all-dev', 'bin') - if isdir(apertium_path): + # Adding the Apertium Binaries to PATH + if platform.system() == 'Windows': + install_path = os.getenv('LOCALAPPDATA') + current = os.getenv('path') + apertium_path = path.join(install_path, 'apertium-all-dev', 'bin') + if path.isdir(apertium_path): update = f'{current}{apertium_path};' - putenv('path', update) + os.putenv('path', update) for i, command in enumerate(commands): procs.append( subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE), diff --git a/installation.py b/installation.py index bab4c1c..3a938f5 100644 --- a/installation.py +++ b/installation.py @@ -1,13 +1,8 @@ from urllib.request import urlretrieve from zipfile import ZipFile -from platform import system -from os.path import join -from os.path import isdir -from os.path import isfile -from os import remove -from os import mkdir -from os import getenv -from os import listdir +import os +from os import path +import platform from distutils.dir_util import copy_tree from shutil import rmtree @@ -15,26 +10,22 @@ from shutil import rmtree class Installation: def __init__(self, languages: tuple): - self._install_path = getenv('LOCALAPPDATA') - self._apertium_path = join(self._install_path, 'apertium-all-dev') - - self._temp_path = getenv('TEMP') - - self._download_path = join(self._temp_path, 'apertium_temp') + self._install_path = os.getenv('LOCALAPPDATA') + self._apertium_path = path.join(self._install_path, 'apertium-all-dev') + self._temp_path = os.getenv('TEMP') + self._download_path = path.join(self._temp_path, 'apertium_temp') # Remove abandoned files from previous incomplete install - if isdir(self._download_path): + if path.isdir(self._download_path): rmtree(self._download_path) - mkdir(self._download_path) - + os.mkdir(self._download_path) self._languages = languages @staticmethod def _download_zip(download_files: dict, download_dir, extract_path): - for zip_name, zip_link in download_files.items(): - zip_download_path = join(download_dir, zip_name) + zip_download_path = path.join(download_dir, zip_name) urlretrieve(zip_link, filename=zip_download_path) print("{} download completed".format(zip_name)) @@ -43,7 +34,7 @@ class Installation: zip_obj.extractall(path=extract_path) zip_obj.close() print("Extraction completed") - remove(zip_download_path) + os.remove(zip_download_path) print("zip removed") def download_apertium_windows(self): @@ -64,7 +55,6 @@ class Installation: download_dir = self._download_path extract_path = self._download_path - language_zip = dict() for curr_lang in self._languages: language_link = f'http://apertium.projectjj.com/win32/nightly/data.php?zip={curr_lang}' @@ -73,16 +63,16 @@ class Installation: self._download_zip(language_zip, download_dir, extract_path) # move the extracted files to desired location - lang_data_path = join(self._download_path, 'usr', 'share', 'apertium') + lang_data_path = path.join(self._download_path, 'usr', 'share', 'apertium') print("Copying Language Data to Apertium") - for directory in listdir(lang_data_path): - source = join(lang_data_path, directory) - destination = join(self._apertium_path, 'share', 'apertium', directory) + for directory in os.listdir(lang_data_path): + source = path.join(lang_data_path, directory) + destination = path.join(self._apertium_path, 'share', 'apertium', directory) copy_tree(source, destination) print(source, '->', destination) - rmtree(join(extract_path, 'usr')) + rmtree(path.join(extract_path, 'usr')) def mode_editor(self): """The mode files need to be modified before being used on Windows System @@ -93,38 +83,35 @@ class Installation: """ # List of Mode Files - mode_path = join(self._apertium_path, 'share', 'apertium', 'modes') - only_files = [f for f in listdir(mode_path) if isfile(join(mode_path, f)) and - 'mode' in f] + 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] for file in only_files: print(f"Opening {file} for editing") - infile = open(join(mode_path, file), 'r') + infile = open(path.join(mode_path, file), 'r') line = infile.read() infile.close() contents = line.split(' ') - # Editing mode file to be compatible with windows platform for index, 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 - line = ' '.join(contents) - outfile = open(join(mode_path, file), 'w') + outfile = open(path.join(mode_path, file), 'w') outfile.write(line) outfile.close() print(f"Closing {file}") def main(): - # Download ApertiumWin64 and Move to %localappdata% p = Installation(('apertium-eng', 'apertium-en-es')) @@ -134,5 +121,5 @@ def main(): if __name__ == '__main__': - if system() == 'Windows': + if platform.system() == 'Windows': main()