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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Filename: lrcShow-X.py
# Develop CodeName: Shiningirl
# Version: 2.0.1
# Author: Xu Jia (sanfanling) and OutLikeAShoe
# Lisence: GPL-2.0
# Bug report: xujia19@gmail.com; https://bugs.launchpad.net/lrcshow-x
#lrcShow-X: General application of lrc for Audio players under linux
#Copyright (C) 2007 - 2011 Xu Jia <xujia19@gmail.com> & OutLikeAShoe <outlikeashoe@gmail.com>
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
import os, sys
import gettext
from commands import getoutput
import signal
import players
import ConfigParser
import choosePlayer
from time import sleep
import readConfig
import updateConfig
def killHandler(signum, frame):
try:
os.remove('%s/.lrcShow-X/lrcShow-X.lock' %os.getenv('HOME'))
except:
pass
sys.exit()
def checkLockfile():
if(os.path.exists(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))):
f = open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
pid = f.read()
f.close()
output = getoutput('ps -C -p %s' %pid)
if('lrcShow-X.py' in output):
return False
else:
os.remove(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
return True
else:
return True
def creatLockfile(pid):
f = open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'), 'w')
f.write(pid)
f.close()
def checkCommandLine():
supportedPlayer = players.supportedPlayers
usage = 'Usage: lrcShow-X [--help] [%s]' % ("] [".join(["--%s" % i for i in supportedPlayer]))
if len(sys.argv)>2:
sys.stdout.write(usage+'\n')
sys.exit()
elif len(sys.argv) == 2:
if(sys.argv[1] == '--help' or sys.argv[1][:2]!='--'):
sys.stdout.write(usage+'\n')
sys.exit()
else:
argv = sys.argv[1].replace('-','')
if(argv not in supportedPlayer):
sys.stdout.write('Error: %s is not supported at present\n' %argv.title())
sys.exit()
else:
argv = None
return argv
def checkFile():
if(not os.path.isdir(os.path.expanduser('~/.lrcShow-X'))):
os.mkdir(os.path.expanduser('~/.lrcShow-X'))
if(not os.path.isdir(os.path.expanduser('~/.lrcShow-X/lrc'))):
os.mkdir(os.path.expanduser('~/.lrcShow-X/lrc'))
if(not os.path.exists(os.path.expanduser('~/.lrcShow-X/lrcShow-X.db'))):
import pickle
mydb = open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.db'), 'w')
pickler = pickle.Pickler(mydb, True)
pickler.dump({})
mydb.close()
if(not os.path.exists(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf'))):
f = open('lrcShow-X.conf.example')
conf = f.read()
f.close()
wd = settingWizard.settingWizard()
r = wd.exec_()
if(r == 1):
s1 = ''
s3 = ''
for j in range(0,wd.path.lrcPath.rowNumber):
s1 += '%s,' %wd.path.lrcPath.table.item(j,0).text().toUtf8()
s3 += '%s,' %wd.path.lrcPath.table.cellWidget(j, 1).currentText()
s2 = wd.path.lrcPath.savePath.text().toUtf8()
if(wd.path.lrcPath.saveToMediaPath.isChecked()):
saveToMediaPath = 'yes'
else:
saveToMediaPath = 'no'
if(wd.path.lrcPath.searchMediaPath.isChecked()):
searchMediaPath = 'yes'
else:
searchMediaPath = 'no'
for j in range(12):
rr = eval('wd.engine.radioButton%d.isChecked()' %j)
if rr:
exec 'searchEngine = str(wd.engine.radioButton%d.text().toUtf8())' %(j, )
break
conf = conf.replace('LRC SEARCH PATH,', str(s1))
conf = conf.replace('LRC SAVE PATH', str(s2))
conf = conf.replace('searchengine = ttPlayer', 'searchengine = %s' %searchEngine)
conf = conf.replace('savetomediapath = no', 'savetomediapath = %s' %saveToMediaPath)
conf = conf.replace('searchmediapath = no', 'searchmediapath = %s' %searchMediaPath)
conf = conf.replace('recursiondepth = 0,', 'recursiondepth = %s' %str(s3))
#config = initConfigFile(str(s1), str(s2), searchEngine, saveToMediaPath, searchMediaPath, str(s3))
else:
conf = conf.replace('LRC SEARCH PATH', os.path.expanduser('~/.lrcShow-X/lrc'))
conf = conf.replace('LRC SAVE PATH', os.path.expanduser('~/.lrcShow-X/lrc'))
f = open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf') ,'w')
f.write(conf)
f.close()
def launchPlayer(playerConfigList):
argvStartup = playerConfigList[0]
playerPathStartup= playerConfigList[1]
autoLaunch = playerConfigList[2]
try:
argv = players.autodetect()
return argv
except Exception, e:
sys.stdout.write(str(e)+'\n'+'Now choose a player you need to launch...'+'\n')
if(autoLaunch == 'yes'):
sys.stdout.write('Now launch the player...'+'\n')
os.spawnv(os.P_NOWAIT, playerPathStartup, ('',))
return argvStartup
else:
cp = choosePlayer.choosePlayer()
cp.playerList.setCurrentIndex(cp.supportedPlayers.index(argvStartup))
cp.pathBox.setText(unicode(playerPathStartup, sys.getfilesystemencoding()))
result = cp.exec_()
if(result == QDialog.Accepted):
argv = str(cp.playerList.currentText())
playerPath = str(cp.pathBox.text()).strip()
if(cp.rememberPlayer.isChecked()):
l = ConfigParser.ConfigParser()
l.read(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf'))
l.set('player', 'autolaunch', 'yes')
l.set('player', 'launchplayer', argv)
l.set('player', 'playerpath', playerPath)
l.write(open(os.path.expanduser('~/.lrcShow-X/lrcShow-X.conf'), 'w'))
sys.stdout.write('Now launch the player...'+'\n')
#sub = subprocess.Popen(args = playerPath)
os.spawnv(os.P_NOWAIT, playerPath, ('',))
return argv
else:
try:
os.remove(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
except:
pass
sys.exit()
def main(argv, optionConfList, shortcutConfigList, playerConfigList, VERSION, AUTHORS):
win = mainGui(optionConfList, shortcutConfigList, playerConfigList, VERSION, AUTHORS)
win.player = argv
playerControl = getattr(players,win.player)
playerControl = getattr(playerControl,win.player)
if(len(sys.argv) == 2):
try:
win.di = playerControl()
except:
#sys.stdout.write(str(e)+'\n')
if(win.player=='amarok'):
sys.stdout.write('Error: Amarok2 is not running; or this application does not support Amarok-1.x, you could use lrcShow-II script instead\n')
try:
os.remove(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
except:
pass
sys.exit()
else:
sys.stdout.write('Error: %s is not running\n' %win.player.title())
try:
os.remove(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
except:
pass
sys.exit()
else:
con = 0
while True:
sleep(1)
try:
win.di = playerControl()
except:
if(con == 20):
b = QMessageBox(QMessageBox.Critical, _('Dbus error'), _('Can not invoke the dbus interface of specific player'))
bu = QPushButton(QIcon('./icon/ok.png'), _('Ok'))
b.addButton(bu, QMessageBox.AcceptRole)
b.exec_()
os.remove(os.path.expanduser('~/.lrcShow-X/lrcShow-X.lock'))
sys.exit()
else:
con += 1
continue
else:
break
thread = listenThread(win,win.player)
thread.start()
win.show()
sys.exit(app.exec_())
def getConfig():
try:
p = readConfig.readConfig()
optionConfList = p.parseOption()
shortcutConfigList = p.parseShortcut()
playerConfigList = p.parsePlayer()
except:
dd = updateConfig.updateConfig()
dd.update()
p = readConfig.readConfig()
optionConfList = p.parseOption()
shortcutConfigList = p.parseShortcut()
playerConfigList = p.parsePlayer()
return optionConfList, shortcutConfigList, playerConfigList
if(__name__ == "__main__"):
VERSION = '2.0.1'
AUTHORS = 'sanfanling, OutLikeAShoe'
os.chdir(os.path.join(os.getcwd(), os.path.dirname(sys.argv[0])))
signal.signal(signal.SIGINT, killHandler)
# check command line
argv = checkCommandLine()
# check modules
try:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
except:
sys.stdout.write('lrcShow-X: Please install PyQt4 (Python and Qt4 bindings) first!\n')
sys.exit()
else:
from mainGui import mainGui
from defineThread import listenThread
import settingWizard
app = QApplication(sys.argv)
# get translation
try:
gettext.translation('lrcShow-X', 'locale').install(unicode=True)
except IOError:
gettext.translation('lrcShow-X', 'locale',languages=['en_US']).install(unicode=True)
# check needed config file, database and paths. If there is no config file, create it
checkFile()
# check configuration, if no error, get config, otherwise, update the config file
optionConfList, shortcutConfigList, playerConfigList = getConfig()
# create a lock, which could ensure there is only one instance of lrcShow-X
lock = checkLockfile()
if(lock):
creatLockfile(str(os.getpid()))
else:
sys.stdout.write('Error: lrcShow-X allows only one instance\n')
sys.exit()
# launch the specific player if the command line did not specify a player
if(argv == None):
argv = launchPlayer(playerConfigList)
# start GUI
main(argv, optionConfList, shortcutConfigList, playerConfigList, VERSION, AUTHORS)
|