[MRG] Use find_executable for gs and raise error if not found (#166)

* Use find_executable for gs and raise error if not found

* Remove unused variable

* Add test

* Use pytest monkeypatch
pull/2/head
Vinayak Mehta 2018-10-23 21:12:43 +05:30 committed by GitHub
parent f734af3a0b
commit a78ef7f841
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 16 deletions

View File

@ -174,15 +174,39 @@ class Lattice(BaseParser):
return t
def _generate_image(self):
# TODO: hacky, get rid of ghostscript #96
def get_platform():
# TODO: get rid of ghostscript #96
def get_executable():
import platform
from distutils.spawn import find_executable
info = {
'system': platform.system().lower(),
'machine': platform.machine().lower()
}
return info
class GhostscriptNotFound(Exception): pass
gs = None
system = platform.system().lower()
try:
if system == 'windows':
if find_executable('gswin32c.exe'):
gs = 'gswin32c.exe'
elif find_executable('gswin64c.exe'):
gs = 'gswin64c.exe'
else:
raise ValueError
else:
if find_executable('gs'):
gs = 'gs'
elif find_executable('gsc'):
gs = 'gsc'
else:
raise ValueError
if 'ghostscript' not in subprocess.check_output(
[gs, '-version']).decode('utf-8').lower():
raise ValueError
except ValueError:
raise GhostscriptNotFound(
'Please make sure that Ghostscript is installed'
' and available on the PATH environment variable')
return gs
self.imagename = ''.join([self.rootname, '.png'])
gs_call = [
@ -193,15 +217,9 @@ class Lattice(BaseParser):
'-r600',
self.filename
]
info = get_platform()
if info['system'] == 'windows':
bit = info['machine'][-2:]
gs_call.insert(0, 'gswin{}c.exe'.format(bit))
else:
if 'ghostscript' in subprocess.check_output(['gs', '-version']).decode('utf-8').lower():
gs_call.insert(0, 'gs')
else:
gs_call.insert(0, "gsc")
gs = get_executable()
gs_call.insert(0, gs)
subprocess.call(
gs_call, stdout=open(os.devnull, 'w'),
stderr=subprocess.STDOUT)

View File

@ -60,3 +60,18 @@ def test_no_tables_found_warnings_suppressed():
except Warning as e:
warning_text = str(e)
pytest.fail('Unexpected warning: {}'.format(warning_text))
def test_ghostscript_not_found(monkeypatch):
import distutils
def _find_executable_patch(arg):
return ''
monkeypatch.setattr(distutils.spawn, 'find_executable', _find_executable_patch)
message = ('Please make sure that Ghostscript is installed and available'
' on the PATH environment variable')
filename = os.path.join(testdir, 'foo.pdf')
with pytest.raises(Exception, message=message):
tables = camelot.read_pdf(filename)