Skip to content

Commit

Permalink
Fix test failures.
Browse files Browse the repository at this point in the history
  • Loading branch information
TaylorSMarks committed Jul 14, 2021
1 parent cfbb36a commit c643f43
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
45 changes: 28 additions & 17 deletions playsound.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,22 @@ def winCommand(*command):
# If it fails, there's nothing more that can be done...
pass

def _handlePathOSX(sound):
try:
from urllib.parse import quote # Try the Python 3 import first...
except ImportError:
from urllib import quote # Try using the Python 2 import before giving up entirely...

if '://' not in sound:
if not sound.startswith('/'):
from os import getcwd
sound = getcwd() + '/' + sound
sound = 'file://' + sound

parts = sound.split('://', 1)
return parts[0] + '://' + quote(parts[1].encode('utf-8')).replace(' ', '%20')


def _playsoundOSX(sound, block = True):
'''
Utilizes AppKit.NSSound. Tested and known to work with MP3 and WAVE on
Expand All @@ -65,21 +81,8 @@ def _playsoundOSX(sound, block = True):

from Foundation import NSURL
from time import sleep
from inspect import getsourcefile

try:
from urllib.parse import quote # Try the Python 3 import first...
except ImportError:
from urllib import quote # Try using the Python 2 import before giving up entirely...

if '://' not in sound:
if not sound.startswith('/'):
from os import getcwd
sound = getcwd() + '/' + sound
sound = 'file://' + sound

parts = sound.split('://', 1)
sound = parts[0] + '://' + quote(parts[1].encode('utf-8')).replace(' ', '%20')
sound = _handlePathOSX(sound)
url = NSURL.URLWithString_(sound)
if not url:
raise PlaysoundException('Cannot find a sound with filename: ' + sound)
Expand Down Expand Up @@ -139,13 +142,21 @@ def _playsoundNix(sound, block = True):
playbin.set_state(Gst.State.NULL)

def _playsoundAnotherPython(otherPython, sound, block = True):
'''
Mostly written so that when this is run on python3 on macOS, it can invoke
python2 on macOS... but maybe this idea could be useful on linux, too.
'''
from inspect import getsourcefile
from os.path import abspath
from os.path import abspath, exists
from subprocess import call
from threading import Thread

# Check if the file exists...
if not exists(abspath(sound)):
raise PlaysoundException('Cannot find a sound with filename: ' + sound)

playsoundPath = abspath(getsourcefile(lambda: 0))
t = Thread(target = lambda: call([otherPython, playsoundPath, sound]))
t = Thread(target = lambda: call([otherPython, playsoundPath, _handlePathOSX(sound)]))
t.start()
if block:
t.join()
Expand All @@ -163,7 +174,7 @@ def _playsoundAnotherPython(otherPython, sound, block = True):
from AppKit import NSSound
except ImportError:
print("playsound is relying on a python 2 subprocess. Please use `pip3 install PyObjC` if you want playsound to run more efficiently.")
playsound = lambda sound, block: _playsoundAnotherPython('/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python', sound, block)
playsound = lambda sound, block = True: _playsoundAnotherPython('/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python', sound, block)
else:
playsound = _playsoundNix

Expand Down
2 changes: 1 addition & 1 deletion test.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import unittest

durationMarginLow = 0.3
duratingMarginHigh = 1.6
duratingMarginHigh = 2.0
sawClose = False

def mockMciSendStringW(command, buf, bufLen, bufStart):
Expand Down

0 comments on commit c643f43

Please sign in to comment.