[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 monkeypatchpull/2/head
parent
f734af3a0b
commit
a78ef7f841
|
|
@ -174,15 +174,39 @@ class Lattice(BaseParser):
|
||||||
return t
|
return t
|
||||||
|
|
||||||
def _generate_image(self):
|
def _generate_image(self):
|
||||||
# TODO: hacky, get rid of ghostscript #96
|
# TODO: get rid of ghostscript #96
|
||||||
def get_platform():
|
def get_executable():
|
||||||
import platform
|
import platform
|
||||||
|
from distutils.spawn import find_executable
|
||||||
|
|
||||||
info = {
|
class GhostscriptNotFound(Exception): pass
|
||||||
'system': platform.system().lower(),
|
|
||||||
'machine': platform.machine().lower()
|
gs = None
|
||||||
}
|
system = platform.system().lower()
|
||||||
return info
|
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'])
|
self.imagename = ''.join([self.rootname, '.png'])
|
||||||
gs_call = [
|
gs_call = [
|
||||||
|
|
@ -193,15 +217,9 @@ class Lattice(BaseParser):
|
||||||
'-r600',
|
'-r600',
|
||||||
self.filename
|
self.filename
|
||||||
]
|
]
|
||||||
info = get_platform()
|
gs = get_executable()
|
||||||
if info['system'] == 'windows':
|
gs_call.insert(0, gs)
|
||||||
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")
|
|
||||||
subprocess.call(
|
subprocess.call(
|
||||||
gs_call, stdout=open(os.devnull, 'w'),
|
gs_call, stdout=open(os.devnull, 'w'),
|
||||||
stderr=subprocess.STDOUT)
|
stderr=subprocess.STDOUT)
|
||||||
|
|
|
||||||
|
|
@ -60,3 +60,18 @@ def test_no_tables_found_warnings_suppressed():
|
||||||
except Warning as e:
|
except Warning as e:
|
||||||
warning_text = str(e)
|
warning_text = str(e)
|
||||||
pytest.fail('Unexpected warning: {}'.format(warning_text))
|
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)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue