Repository: GjjvdBurg/paper2remarkable

Issue:
p2r asking for pdftk instead of using qpdf on ubuntu
I have kubuntu 18.04. When I run the example command 
`p2r -v https://arxiv.org/abs/1811.11242 `. It gives the error 
`[Errno 2] No such file or directory: 'pdftk': 'pdftk'`

Discussion:

Patch:
diff --git a/paper2remarkable/log.py b/paper2remarkable/log.py
index bae1cbf..3a2fcc5 100644
--- a/paper2remarkable/log.py
+++ b/paper2remarkable/log.py
@@ -38,19 +38,27 @@ class Logger(metaclass=Singleton):
     def disable(self):
         self.enabled = False
 
-    def _log(self, msg, mode):
+    def _log(self, msg, mode, end='\n', add_prefix=True):
         if not self.enabled:
             return
         if not mode in ("info", "warn"):
             raise ValueError("Unknown logging mode: %s" % mode)
         file = sys.stdout if mode == "info" else sys.stderr
-        now = datetime.datetime.now()
-        nowstr = now.strftime("%Y-%m-%d %H:%M:%S")
-        print("%s - %s - %s" % (nowstr, mode.upper(), msg), file=file)
+        if add_prefix:
+            now = datetime.datetime.now()
+            nowstr = now.strftime("%Y-%m-%d %H:%M:%S")
+            prefix = "%s - %s - " % (nowstr, mode.upper())
+        else:
+            prefix = ""
+        print("%s%s" % (prefix, msg), end=end, file=file)
         file.flush()
 
-    def info(self, msg):
-        self._log(msg, "info")
+    def info(self, msg, end='\n'):
+        self._log(msg, "info", end=end)
+
+    def warning(self, msg, end='\n'):
+        self._log(msg, "warn", end=end)
+
+    def append(self, msg, mode, end='\n'):
+        self._log(msg, mode, end=end, add_prefix=False)
 
-    def warning(self, msg):
-        self._log(msg, "warn")
diff --git a/paper2remarkable/providers/arxiv.py b/paper2remarkable/providers/arxiv.py
index 572c2bf..47da448 100644
--- a/paper2remarkable/providers/arxiv.py
+++ b/paper2remarkable/providers/arxiv.py
@@ -65,7 +65,7 @@ class Arxiv(Provider):
 
     def dearxiv(self, input_file):
         """Remove the arXiv timestamp from a pdf"""
-        logger.info("Removing arXiv timestamp")
+        logger.info("Removing arXiv timestamp ... ", end="")
         basename = os.path.splitext(input_file)[0]
 
         uncompress_file = basename + "_uncompress.pdf"
@@ -90,13 +90,13 @@ class Arxiv(Provider):
                     current_obj.append(line)
                     objid = int(line.split(b" ")[0])
                     xref[objid] = char_count
-                elif current_obj and line.startswith(b'endobj'):
+                elif current_obj and line.startswith(b"endobj"):
                     # End the current object. If needed, replace the arXiv
                     # stamp in the block (done only once). Reset current
                     # object.
                     current_obj.append(line)
                     block = b"".join(current_obj)
-                    if not replaced_arXiv:
+                    if not replaced_arXiv and b"arXivStAmP" in block:
                         # remove the text
                         block, n_subs1 = re.subn(
                             b"\(" + DEARXIV_TEXT_REGEX + b"\)Tj",
@@ -148,6 +148,8 @@ class Arxiv(Provider):
         output_file = basename + "_dearxiv.pdf"
         self.compress_pdf(removed_file, output_file)
 
+        logger.append("success" if replaced_arXiv else "failed", "info")
+
         return output_file
 
 
diff --git a/paper2remarkable/utils.py b/paper2remarkable/utils.py
index 38c8735..791e81a 100644
--- a/paper2remarkable/utils.py
+++ b/paper2remarkable/utils.py
@@ -150,7 +150,6 @@ def upload_to_remarkable(filepath, remarkable_dir="/", rmapi_path="rmapi"):
                 )
 
     # Upload the file
-    logger.info("%s put %s %s/" % (rmapi_path, filepath, remarkable_dir))
     status = subprocess.call(
         [rmapi_path, "put", filepath, remarkable_dir + "/"],
         stdout=subprocess.DEVNULL,
@@ -176,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


Test patch:
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()