diff options
| author | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2021-05-29 15:07:54 +0100 |
|---|---|---|
| committer | Gertjan van den Burg <gertjanvandenburg@gmail.com> | 2021-05-29 15:07:54 +0100 |
| commit | d1772da6b86c58b4dbc9fb514d151b2fcbf1672d (patch) | |
| tree | d3ef1649b01f27590b222c4da4adf999682b03bb | |
| parent | Bump version and update changelog (diff) | |
| parent | Test and fix uploading multiple files (fixes #110) (diff) | |
| download | paper2remarkable-d1772da6b86c58b4dbc9fb514d151b2fcbf1672d.tar.gz paper2remarkable-d1772da6b86c58b4dbc9fb514d151b2fcbf1672d.zip | |
Merge branch 'bugfix/upload_multi'
| -rw-r--r-- | paper2remarkable/providers/_base.py | 59 | ||||
| -rw-r--r-- | paper2remarkable/ui.py | 46 | ||||
| -rw-r--r-- | paper2remarkable/utils.py | 16 | ||||
| -rw-r--r-- | tests/test_ui.py | 90 | ||||
| -rw-r--r-- | tests/test_utils.py | 22 |
5 files changed, 184 insertions, 49 deletions
diff --git a/paper2remarkable/providers/_base.py b/paper2remarkable/providers/_base.py index 369d566..56d61e5 100644 --- a/paper2remarkable/providers/_base.py +++ b/paper2remarkable/providers/_base.py @@ -20,6 +20,7 @@ from ..log import Logger from ..pdf_ops import prepare_pdf, blank_pdf, shrink_pdf from ..utils import ( assert_file_is_pdf, + chdir, check_pdftool, download_url, follow_redirects, @@ -211,33 +212,33 @@ class Provider(metaclass=abc.ABCMeta): self.initial_dir = os.getcwd() with tempfile.TemporaryDirectory(prefix="p2r_") as working_dir: - os.chdir(working_dir) - self.retrieve_pdf(pdf_url, tmp_filename) - - assert_file_is_pdf(tmp_filename) - - intermediate_fname = tmp_filename - for opname, op in self.operations: - intermediate_fname = op(intermediate_fname) - - shutil.copy(intermediate_fname, clean_filename) - - if self.debug: - print("Paused in debug mode in dir: %s" % working_dir) - print("Press enter to exit.") - return input() - - if self.upload: - return upload_to_remarkable( - clean_filename, - remarkable_dir=self.remarkable_dir, - rmapi_path=self.rmapi_path, - ) - - target_path = os.path.join(self.initial_dir, clean_filename) - while os.path.exists(target_path): - base = os.path.splitext(target_path)[0] - target_path = base + "_.pdf" - shutil.move(clean_filename, target_path) - os.chdir(self.initial_dir) + with chdir(working_dir): + self.retrieve_pdf(pdf_url, tmp_filename) + + assert_file_is_pdf(tmp_filename) + + intermediate_fname = tmp_filename + for opname, op in self.operations: + intermediate_fname = op(intermediate_fname) + + shutil.copy(intermediate_fname, clean_filename) + + if self.debug: + print("Paused in debug mode in dir: %s" % working_dir) + print("Press enter to exit.") + return input() + + if self.upload: + return upload_to_remarkable( + clean_filename, + remarkable_dir=self.remarkable_dir, + rmapi_path=self.rmapi_path, + ) + + target_path = os.path.join(self.initial_dir, clean_filename) + while os.path.exists(target_path): + base = os.path.splitext(target_path)[0] + target_path = base + "_.pdf" + shutil.move(clean_filename, target_path) + return target_path diff --git a/paper2remarkable/ui.py b/paper2remarkable/ui.py index 1d1e011..c05961e 100644 --- a/paper2remarkable/ui.py +++ b/paper2remarkable/ui.py @@ -280,6 +280,31 @@ def set_excepthook(debug): sys.excepthook = exception_handler +def runner(inputs, filenames, options, remarkable_dir="/", debug=False): + if not len(inputs) == len(filenames): + raise ValueError("Number of inputs and filenames must be the same") + for cli_input, filename in zip(inputs, filenames): + provider, new_input, cookiejar = choose_provider(cli_input) + prov = provider( + verbose=options["core"]["verbose"], + upload=options["core"]["upload"], + debug=debug, + experimental=options["core"]["experimental"], + crop=options["core"]["crop"], + blank=options["core"]["blank"], + remarkable_dir=remarkable_dir, + rmapi_path=options["system"]["rmapi"], + pdftoppm_path=options["system"]["pdftoppm"], + pdftk_path=options["system"]["pdftk"], + qpdf_path=options["system"]["qpdf"], + gs_path=options["system"]["gs"], + css=options["html"]["css"], + font_urls=options["html"]["font_urls"], + cookiejar=cookiejar, + ) + prov.run(new_input, filename=filename) + + def main(): args = parse_args() set_excepthook(args.debug) @@ -305,23 +330,4 @@ def main(): [None] * len(args.input) if not args.filename else args.filename ) - for cli_input, filename in zip(args.input, filenames): - provider, new_input, cookiejar = choose_provider(cli_input) - prov = provider( - verbose=options["core"]["verbose"], - upload=options["core"]["upload"], - debug=args.debug, - experimental=options["core"]["experimental"], - crop=options["core"]["crop"], - blank=options["core"]["blank"], - remarkable_dir=args.remarkable_dir, - rmapi_path=options["system"]["rmapi"], - pdftoppm_path=options["system"]["pdftoppm"], - pdftk_path=options["system"]["pdftk"], - qpdf_path=options["system"]["qpdf"], - gs_path=options["system"]["gs"], - css=options["html"]["css"], - font_urls=options["html"]["font_urls"], - cookiejar=cookiejar, - ) - prov.run(new_input, filename=filename) + runner(args.input, filenames, options, debug=args.debug) diff --git a/paper2remarkable/utils.py b/paper2remarkable/utils.py index 0003103..5ea25fd 100644 --- a/paper2remarkable/utils.py +++ b/paper2remarkable/utils.py @@ -8,6 +8,7 @@ Copyright: 2019, G.J.J. van den Burg """ +import os import regex import requests import string @@ -203,3 +204,18 @@ def check_pdftool(pdftk_path, qpdf_path): if status == 0: return "qpdf" raise NoPDFToolError + + +class chdir: + """Change directory in context and return to original on exit or failure""" + + def __init__(self, target: str): + self._target = target + self._original_dir = None + + def __enter__(self): + self._original_dir = os.getcwd() + os.chdir(self._target) + + def __exit__(self, exc_type, exc_value, traceback): + os.chdir(self._original_dir) diff --git a/tests/test_ui.py b/tests/test_ui.py index 317352f..86a3c8e 100644 --- a/tests/test_ui.py +++ b/tests/test_ui.py @@ -38,7 +38,9 @@ from paper2remarkable.ui import ( build_argument_parser, choose_provider, merge_options, + runner, ) +from paper2remarkable.utils import chdir class TestUI(unittest.TestCase): @@ -365,6 +367,94 @@ class TestUI(unittest.TestCase): self.assertEquals(opts["html"]["css"], "Hello, World!\n") self.assertEquals(opts["html"]["font_urls"], ["url_1", "url_2"]) + def test_runner_1(self): + inputs = [ + "https://arxiv.org/abs/1811.11242v1", + ] + filenames = [None] + options = { + "core": { + "blank": False, + "verbose": False, + "upload": False, + "experimental": False, + "crop": "none", + }, + "system": { + "gs": "gs", + "pdftoppm": "pdftoppm", + "pdftk": "pdftk", + "qpdf": "qpdf", + "rmapi": "rmapi", + }, + "html": {"css": None, "font_urls": None}, + } + + test_dir = tempfile.mkdtemp() + with chdir(test_dir): + runner(inputs, filenames, options) + + pth = os.path.join( + test_dir, + "Burg_Nazabal_Sutton_-_Wrangling_Messy_CSV_Files_by_Detecting_Row_and_Type_Patterns_2018.pdf", + ) + self.assertTrue(os.path.exists(pth)) + + def test_runner_2(self): + test_upload = False # Enable to test uploading to reMarkable + + if test_upload: + upload = True + rm_dir = "/p2r_test" + else: + upload = False + rm_dir = "/" + + inputs = [ + "https://arxiv.org/abs/1811.11242v1", + "https://www.jmlr.org/papers/volume17/14-526/14-526.pdf", + ] + filenames = [None, None] + options = { + "core": { + "blank": False, + "verbose": False, + "upload": upload, + "experimental": False, + "crop": "none", + }, + "system": { + "gs": "gs", + "pdftoppm": "pdftoppm", + "pdftk": "pdftk", + "qpdf": "qpdf", + "rmapi": "rmapi", + }, + "html": {"css": None, "font_urls": None}, + } + + test_dir = tempfile.mkdtemp() + with chdir(test_dir): + runner(inputs, filenames, options, remarkable_dir=rm_dir) + + pth = os.path.join( + test_dir, + "Burg_Nazabal_Sutton_-_Wrangling_Messy_CSV_Files_by_Detecting_Row_and_Type_Patterns_2018.pdf", + ) + if test_upload: + self.assertFalse(os.path.exists(pth)) + else: + self.assertTrue(os.path.exists(pth)) + + pth = os.path.join( + test_dir, + "Burg_Groenen_-_GenSVM_a_Generalized_Multiclass_Support_Vector_Machine_2016.pdf", + ) + if test_upload: + self.assertFalse(os.path.exists(pth)) + else: + self.assertTrue(os.path.exists(pth)) + if __name__ == "__main__": unittest.main() diff --git a/tests/test_utils.py b/tests/test_utils.py index 4c122e0..903d8b3 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,9 +1,13 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import os +import tempfile import unittest from paper2remarkable.exceptions import NoPDFToolError + +from paper2remarkable.utils import chdir from paper2remarkable.utils import check_pdftool @@ -16,6 +20,24 @@ class TestUtils(unittest.TestCase): with self.assertRaises(NoPDFToolError): check_pdftool("pdftk_xyz", "qpdf_xyz") + def test_chdir_1(self): + start_dir = os.getcwd() + tmpdir1 = tempfile.mkdtemp(prefix="p2r_test_chdir_") + with chdir(tmpdir1): + pwd = os.getcwd() + self.assertEqual(pwd, tmpdir1) + self.assertEqual(start_dir, os.getcwd()) + + def test_chdir_2(self): + start_dir = os.getcwd() + tmpdir1 = tempfile.mkdtemp(prefix="p2r_test_chdir_") + with self.assertRaises(ValueError): + with chdir(tmpdir1): + pwd = os.getcwd() + raise ValueError + self.assertEqual(pwd, tmpdir1) + self.assertEqual(start_dir, os.getcwd()) + if __name__ == "__main__": unittest.main() |
