1
- from urllib .request import urlretrieve
2
- from zipfile import ZipFile
1
+ import logging
3
2
import os
4
- from os import path
5
3
import platform
6
- from distutils .dir_util import copy_tree
7
- from shutil import rmtree
8
- import logging
4
+ import shutil
9
5
import tempfile
10
-
6
+ from distutils .dir_util import copy_tree
7
+ from urllib .request import urlretrieve
8
+ from zipfile import ZipFile
11
9
12
10
class Installer :
13
11
12
+ base_link = "http://apertium.projectjj.com/{}{}"
13
+
14
14
def __init__ (self , languages ): # type: (Installer, tuple) -> None
15
15
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' )
17
17
self ._download_path = tempfile .mkdtemp ()
18
- self ._language_link = 'http://apertium.projectjj.com/win32/nightly/data.php?zip={}'
19
18
self ._languages = languages
20
19
logging .basicConfig (filename = 'installer.log' , format = '%(asctime)s %(message)s' ,
21
20
filemode = 'w' , level = logging .DEBUG )
@@ -26,7 +25,7 @@ def _download_zip(self, download_files, download_dir, extract_path):
26
25
# type: (Installer, dict, str, str) -> None
27
26
28
27
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 )
30
29
urlretrieve (zip_link , filename = zip_download_path )
31
30
self ._logger .info ('%s download completed' , zip_name )
32
31
@@ -54,28 +53,31 @@ def download_apertium_windows(self): # type: (Installer) -> None
54
53
def download_language_data (self ): # type: (Installer) -> None
55
54
"""Installs Language Data to Apertium"""
56
55
56
+ zip_path = ""
57
+ if platform .system () == 'Windows' :
58
+ zip_path = "win32/nightly/data.php?zip="
57
59
download_dir = self ._download_path
58
60
extract_path = self ._download_path
59
61
language_zip = dict ()
60
62
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 )
62
64
language_zip [curr_lang ] = language_link
63
65
64
66
self ._download_zip (language_zip , download_dir , extract_path )
65
67
66
68
# 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' )
68
70
69
71
self ._logger .info ("Copying Language Data to Apertium" )
70
72
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 )
73
75
copy_tree (source , destination )
74
76
self ._logger .info ('%s -> %s' , source , destination )
75
77
76
- rmtree (path .join (extract_path , 'usr' ))
78
+ shutil . rmtree (os . path .join (extract_path , 'usr' ))
77
79
78
- def mode_editor (self ): # type: (Installer) -> None
80
+ def edit_modes (self ): # type: (Installer) -> None
79
81
"""The mode files need to be modified before being used on Windows System
80
82
81
83
1. Replace /usr/share with %localappdata%\a pertium-all-dev\share
@@ -84,43 +86,37 @@ def mode_editor(self): # type: (Installer) -> None
84
86
"""
85
87
86
88
# 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 ]
91
93
92
94
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
+
97
99
contents = line .split (' ' )
98
100
# Editing mode file to be compatible with windows platform
99
- for index , t in enumerate (contents ):
101
+ for i , t in enumerate (contents ):
100
102
if len (t ) > 2 and t [0 ] == "'" and t [1 ] == "/" :
101
103
t = t .replace ('/' , '\\ ' )
102
104
t = t .replace (r'\usr' , self ._apertium_path )
103
105
# Instead of calling eng.autogen.bin, cmd calls 'eng.autogen.bin'
104
106
# Raising Error: 'File can't be opened error'
105
107
# Hence removing quotes from file
106
108
t = t .replace ("'" , '' )
107
- contents [index ] = t
109
+ contents [i ] = t
108
110
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 ()
114
114
115
115
def install_apertium_windows ():
116
- # Download ApertiumWin64 and Move to %localappdata%
116
+ """ Download ApertiumWin64 and Move to %localappdata%"""
117
117
118
118
if platform .system () == 'Windows' :
119
119
p = Installer (('apertium-eng' , 'apertium-en-es' ))
120
120
p .download_apertium_windows ()
121
121
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