This repository has been archived by the owner on Jan 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
subget-translator.py
executable file
·147 lines (113 loc) · 5.02 KB
/
subget-translator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
import getopt, os, sys, time, hashlib
class SubgetTranlator:
languages = {'pl': 'pl_PL.UTF-8', 'da': 'da_DK.ISO-8859-1'}
lang = ""
editor = ""
lastsum = ""
user = ""
prefix = ""
lastHash = ""
runSubget = False
def usage(self):
return "Usage: subget-translator -[GNU option] --[long GNU option]\n\nOptions:\n--help, -h (this message)\n--list, -l (list avaliable languages)\n--set, -s (set language from list, eg. pl or da)\n--editor, -e (editor name to use, eg. geany or gedit, optional)\n--user, -u (run Subget as other user)\n--prefix, -p (is Subget placed in other directory? example of prefix: . for current directory\n--run, -r (run Subget automaticaly after compilation)"
def setLanguage(self, lang):
if lang in self.languages:
self.lang = lang
else:
print("Invalid language selected, try --list for avaliable languages.")
sys.exit(1)
def setPrefix(self, prefix):
if os.path.isfile(prefix+"/usr/bin/subget"):
self.prefix = prefix
else:
print("Warning: cannot find "+prefix+"/usr/bin/subget, disabling prefix")
def setUser(self, user):
self.user = "su "+user+" -c"
def setEditor(self, editor):
if os.path.isfile(editor):
self.editor = editor
elif os.path.isfile("/usr/bin/"+editor):
self.editor = "/usr/bin/"+editor
elif os.path.isfile("/usr/local/bin/"+editor):
self.editor = "/usr/local/bin/"+editor
else:
print("Cannot find editor executable")
sys.exit(1)
def listLanguages(self):
print("Avaliable languages: \n")
for i in self.languages:
print("["+i+"] "+self.languages[i])
def main(self):
try:
opts, args = getopt.getopt(sys.argv[1:], "hlre:u:p:s:", ["help", "list", "set=", "editor=", "user=", 'prefix=', 'run'])
except getopt.GetoptError as err:
print(self.usage())
sys.exit(2)
for o, a in opts:
if o in ('-h', '--help'):
print(self.usage())
sys.exit(0)
if o in ('-l', '--list'):
self.listLanguages()
sys.exit(0)
if o in ('--set', '-s'):
self.setLanguage(a)
if o in ('--editor', '-e'):
self.setEditor(a)
if o in ('--user', '-u'):
self.setUser(a)
if o in ('--prefix', '-p'):
self.setPrefix(a)
if o in ('--run', '-r'):
self.runSubget = True
if self.lang == "":
print("No language selected, use --list to check avaliable languages and -s/--set (set language for edition, --list for list avaliable options)")
print self.usage()
sys.exit(1)
self.mainLoop()
def checkFile(self, firstTime=False):
try:
f = open(self.prefix+"/usr/share/subget/locale/"+self.lang+"/LC_MESSAGES/subget-src.po", "r")
m = hashlib.md5()
m.update(f.read())
x = m.digest()
f.close()
except Exception as e:
print("Cannot check locale file, please check permissions. "+self.prefix+"/usr/share/subget/locale/"+self.lang+"/LC_MESSAGES/subget-src.po")
sys.exit(1)
if firstTime == True:
self.lastHash = x
return True
else:
if x == self.lastHash:
return True
else:
self.lastHash = x
return False
def compile(self, i):
os.system("msgfmt "+self.prefix+"/usr/share/subget/locale/"+self.lang+"/LC_MESSAGES/subget-src.po -o "+self.prefix+"/usr/share/subget/locale/"+self.lang+"/LC_MESSAGES/subget.mo")
print("["+str(i)+"] Commiting changes... please restart application.")
def runTest(self):
print("export LANG=\""+self.languages[self.lang]+"\"")
os.putenv("LANG", self.languages[self.lang]) # set language
cmd = self.user+" "+self.prefix+"/usr/bin/subget &> /dev/null &"
print(cmd)
os.system(cmd) # run subget
def mainLoop(self):
print("Language: "+self.lang)
if self.editor is not "":
print("Editor: "+self.editor)
os.system(self.editor+" "+self.prefix+"/usr/share/subget/locale/"+self.lang+"/LC_MESSAGES/subget-src.po &")
i = 0
self.checkFile(True)
while True:
i=i+1
check = self.checkFile()
if check == False:
self.compile(i)
if self.runSubget == True:
self.runTest()
time.sleep(3)
app = SubgetTranlator()
app.main()