diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2020-04-15 11:05:19 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2020-04-15 11:05:19 +0100 |
| commit | e0aba92623d9961602d37a5e3f6ce01403e3598a (patch) | |
| tree | 0a06bd64c226238bd14f80b19df2a8c67eafcc67 | |
| parent | remove unnecessary logging (diff) | |
| download | paper2remarkable-e0aba92623d9961602d37a5e3f6ce01403e3598a.tar.gz paper2remarkable-e0aba92623d9961602d37a5e3f6ce01403e3598a.zip | |
Properly check for the installed pdf tool
This fixes #42.
| -rw-r--r-- | paper2remarkable/utils.py | 14 | ||||
| -rw-r--r-- | tests/test_utils.py | 21 |
2 files changed, 31 insertions, 4 deletions
diff --git a/paper2remarkable/utils.py b/paper2remarkable/utils.py index cca904b..791e81a 100644 --- a/paper2remarkable/utils.py +++ b/paper2remarkable/utils.py @@ -175,16 +175,22 @@ def check_pdftool(pdftk_path, qpdf_path): pdftk_path = pdftk_path or "false" qpdf_path = qpdf_path or "false" - status = subprocess.call( - [pdftk_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL - ) + try: + status = subprocess.call( + [pdftk_path], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) + except FileNotFoundError: + status = 1 if status == 0: return "pdftk" - status = subprocess.call( + try: + status = subprocess.call( [qpdf_path, "--help"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) + except FileNotFoundError: + status = 1 if status == 0: return "qpdf" raise NoPDFToolError diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 0000000..4c122e0 --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +import unittest + +from paper2remarkable.exceptions import NoPDFToolError +from paper2remarkable.utils import check_pdftool + + +class TestUtils(unittest.TestCase): + def test_check_pdftool(self): + # Needs a system with both pdftk and qpdf available + self.assertEqual(check_pdftool("pdftk", "qpdf"), "pdftk") + self.assertEqual(check_pdftool("pdftk_xyz", "qpdf"), "qpdf") + self.assertEqual(check_pdftool("pdftk", "qpdf_xyz"), "pdftk") + with self.assertRaises(NoPDFToolError): + check_pdftool("pdftk_xyz", "qpdf_xyz") + + +if __name__ == "__main__": + unittest.main() |
