Formatting vlc.py

This commit is contained in:
Etoh 2014-04-19 19:49:04 +01:00
parent 07684254d1
commit 89e5af0499

View File

@ -19,7 +19,7 @@ class VlcPlayer(BasePlayer):
SLAVE_ARGS.extend(constants.VLC_SLAVE_NONOSX_ARGS)
random.seed()
vlcport = random.randrange(constants.VLC_MIN_PORT, constants.VLC_MAX_PORT) if (constants.VLC_MIN_PORT < constants.VLC_MAX_PORT) else constants.VLC_MIN_PORT
def __init__(self, client, playerPath, filePath, args):
from twisted.internet import reactor
self.reactor = reactor
@ -29,7 +29,7 @@ class VlcPlayer(BasePlayer):
self._filename = None
self._filepath = None
self._filechanged = False
self._durationAsk = threading.Event()
self._filenameAsk = threading.Event()
self._pathAsk = threading.Event()
@ -42,7 +42,7 @@ class VlcPlayer(BasePlayer):
except ValueError:
self._client.ui.showErrorMessage(getMessage("en", "vlc-failed-connection"), True)
self.reactor.callFromThread(self._client.stop, (True),)
return
return
self._listener.setDaemon(True)
self._listener.start()
if(not self._vlcready.wait(constants.VLC_OPEN_MAX_WAIT_TIME)):
@ -50,7 +50,7 @@ class VlcPlayer(BasePlayer):
self._client.ui.showErrorMessage(getMessage("en", "vlc-failed-connection"), True)
self.reactor.callFromThread(self._client.stop, (True),)
self.reactor.callFromThread(self._client.initPlayer, (self),)
def _fileUpdateClearEvents(self):
self._durationAsk.clear()
self._filenameAsk.clear()
@ -67,7 +67,7 @@ class VlcPlayer(BasePlayer):
self._fileUpdateWaitEvents()
args = (self._filename, self._duration, self._filepath)
self.reactor.callFromThread(self._client.updateFile, *args)
self.setPaused(self._client.getGlobalPaused())
self.setPaused(self._client.getGlobalPaused())
self.setPosition(self._client.getGlobalPosition())
def askForStatus(self):
@ -75,49 +75,49 @@ class VlcPlayer(BasePlayer):
self._positionAsk.clear()
self._pausedAsk.clear()
self._listener.sendLine(".")
if self._filechanged == False:
if self._filechanged == False:
self._positionAsk.wait()
self._pausedAsk.wait()
self._client.updatePlayerStatus(self._paused, self._position)
else:
self._client.updatePlayerStatus(self._client.getGlobalPaused(), self._client.getGlobalPosition())
def displayMessage(self, message, duration = constants.OSD_DURATION * 1000):
def displayMessage(self, message, duration=constants.OSD_DURATION * 1000):
duration /= 1000
self._listener.sendLine('display-osd: {}, {}, {}'.format('top-right', duration, message.encode('ascii','ignore'))) #TODO: Proper Unicode support
def setSpeed(self, value):
self._listener.sendLine('display-osd: {}, {}, {}'.format('top-right', duration, message.encode('ascii', 'ignore'))) #TODO: Proper Unicode support
def setSpeed(self, value):
self._listener.sendLine("set-rate: {:.2n}".format(value))
def setPosition(self, value):
self._position = value
self._listener.sendLine("set-position: {:n}".format(value))
def setPaused(self, value):
self._paused = value
self._listener.sendLine('set-playstate: {}'.format("paused" if value else "playing"))
def _isASCII (self, s):
return all(ord(c) < 256 for c in s)
def openFile(self, filePath):
if (self._isASCII(filePath) == True):
self._listener.sendLine('load-file: {}'.format(filePath.encode('ascii','ignore'))) #TODO: Proper Unicode support
self._listener.sendLine('load-file: {}'.format(filePath.encode('ascii', 'ignore'))) #TODO: Proper Unicode support
else:
self._client.ui.showErrorMessage(getMessage("en", "vlc-unicode-loadfile-error"), True)
def _getFileInfo(self):
self._listener.sendLine("get-duration")
self._listener.sendLine("get-filepath")
self._listener.sendLine("get-filename")
def lineReceived(self, line):
def lineReceived(self, line):
match, name, value = self.RE_ANSWER.match(line), "", ""
if match:
name, value = match.group('command'), match.group('argument')
if(line == "filepath-change-notification"):
self._filechanged = True
self._filechanged = True
t = threading.Thread(target=self._onFileUpdate)
t.setDaemon(True)
t.start()
@ -144,11 +144,11 @@ class VlcPlayer(BasePlayer):
self._filenameAsk.set()
elif(line.startswith("interface-version: ")):
interface_version = line[19:24]
if (int(interface_version.replace(".","")) < int(constants.VLC_INTERFACE_MIN_VERSION.replace(".",""))):
if (int(interface_version.replace(".", "")) < int(constants.VLC_INTERFACE_MIN_VERSION.replace(".", ""))):
self._client.ui.showErrorMessage(getMessage("en", "vlc-interface-version-mismatch").format(str(interface_version), str(constants.VLC_INTERFACE_MIN_VERSION)))
elif (line[:16] == "VLC media player"):
vlc_version = line[17:22]
if (int(vlc_version.replace(".","")) < int(constants.VLC_MIN_VERSION.replace(".",""))):
if (int(vlc_version.replace(".", "")) < int(constants.VLC_MIN_VERSION.replace(".", ""))):
self._client.ui.showErrorMessage(getMessage("en", "vlc-version-mismatch").format(str(vlc_version), str(constants.VLC_MIN_VERSION)))
self._vlcready.set()
self._listener.sendLine("get-interface-version")
@ -158,26 +158,26 @@ class VlcPlayer(BasePlayer):
def run(client, playerPath, filePath, args):
vlc = VlcPlayer(client, VlcPlayer.getExpandedPath(playerPath), filePath, args)
return vlc
@staticmethod
def getDefaultPlayerPathsList():
l = []
for path in constants.VLC_PATHS:
p = VlcPlayer.getExpandedPath(path)
if p:
l.append(p)
l.append(p)
return l
@staticmethod
def isValidPlayerPath(path):
if("vlc" in path.lower() and VlcPlayer.getExpandedPath(path)):
return True
return False
@staticmethod
def getIconPath(path):
return constants.VLC_ICONPATH
@staticmethod
def getExpandedPath(playerPath):
if not os.path.isfile(playerPath):
@ -222,7 +222,7 @@ class VlcPlayer(BasePlayer):
for line in interfacefile:
if "local connectorversion" in line:
interface_version = line[26:31]
if (int(interface_version.replace(".","")) >= int(constants.VLC_INTERFACE_MIN_VERSION.replace(".",""))):
if (int(interface_version.replace(".", "")) >= int(constants.VLC_INTERFACE_MIN_VERSION.replace(".", ""))):
return True
else:
playerController._client.ui.showErrorMessage(getMessage("en", "vlc-interface-oldversion-ignored"))
@ -234,7 +234,7 @@ class VlcPlayer(BasePlayer):
elif sys.platform.startswith('darwin'):
playerController.vlcIntfPath = "/Applications/VLC.app/Contents/MacOS/share/lua/intf/"
else:
playerController.vlcIntfPath = os.path.dirname(playerPath).replace("\\","/") + "/lua/intf/" # TODO: Make Mac version use /Applications/VLC.app/Contents/MacOS/share/lua/intf/
playerController.vlcIntfPath = os.path.dirname(playerPath).replace("\\", "/") + "/lua/intf/" # TODO: Make Mac version use /Applications/VLC.app/Contents/MacOS/share/lua/intf/
playerController.vlcModulePath = playerController.vlcIntfPath + "modules/?.luac"
if _usevlcintf(playerController.vlcIntfPath) == True:
playerController.SLAVE_ARGS.append('--lua-config=syncplay={{port=\"{}\"}}'.format(str(playerController.vlcport)))
@ -242,18 +242,18 @@ class VlcPlayer(BasePlayer):
if sys.platform.startswith('linux'):
playerController.vlcDataPath = "/usr/lib/syncplay/resources"
else:
playerController.vlcDataPath = utils.findWorkingDir()+"\\resources"
playerController.vlcDataPath = utils.findWorkingDir() + "\\resources"
playerController.SLAVE_ARGS.append('--data-path={}'.format(playerController.vlcDataPath))
playerController.SLAVE_ARGS.append('--lua-config=syncplay={{modulepath=\"{}\",port=\"{}\"}}'.format(playerController.vlcModulePath,str(playerController.vlcport)))
playerController.SLAVE_ARGS.append('--lua-config=syncplay={{modulepath=\"{}\",port=\"{}\"}}'.format(playerController.vlcModulePath, str(playerController.vlcport)))
call.extend(playerController.SLAVE_ARGS)
if(args):
if(args):
call.extend(args)
self._vlcready = vlcReady
self._vlcclosed = vlcClosed
self.__process = subprocess.Popen(call, stderr=subprocess.PIPE)
for line in iter(self.__process.stderr.readline,''):
for line in iter(self.__process.stderr.readline, ''):
if "[syncplay]" in line:
if "Listening on host" in line:
break
@ -271,27 +271,27 @@ class VlcPlayer(BasePlayer):
self._ibuffer = []
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self._sendingData = threading.Lock()
def initiate_send(self):
with self._sendingData:
asynchat.async_chat.initiate_send(self)
def run(self):
self._vlcready.clear()
self.connect(('localhost', self.__playerController.vlcport))
asyncore.loop()
def handle_connect(self):
asynchat.async_chat.handle_connect(self)
self._vlcready.set()
def collect_incoming_data(self, data):
self._ibuffer.append(data)
def handle_close(self):
asynchat.async_chat.handle_close(self)
self.__playerController.drop()
def found_terminator(self):
# print "received: {}".format("".join(self._ibuffer))
self.__playerController.lineReceived("".join(self._ibuffer))