Repository: chezou/tabula-py
Pull request #136 (2019-02-10 13:20:36)

Introduce an environment variable for JAR path
This is follow up for #130 


Patch:
diff --git a/README.md b/README.md
index 03a01dd..1463a05 100644
--- a/README.md
+++ b/README.md
@@ -220,6 +220,14 @@ Replace `65001` and `UTF-8` appropriately, if the file encoding isn't UTF-8.
 
 You should escape file/directory name yourself.
 
+
+### I want to use a different tabula .jar  file
+You can specify the jar location via enviroment variable
+```bash
+export TABULA_JAR=".../tabula-x.y.z-jar-with-dependencies.jar"
+```
+
+
 ## Contributing
 
 Interested in helping out? I'd love to have your help!
diff --git a/requirements.txt b/requirements.txt
index f56b860..9039aa1 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -3,4 +3,5 @@ pandas
 pytest
 flake8
 distro
-pathlib
\ No newline at end of file
+pathlib
+mock
diff --git a/setup.py b/setup.py
index d334aea..17b2459 100644
--- a/setup.py
+++ b/setup.py
@@ -43,13 +43,13 @@ setup(
     include_package_data=True,
     packages=find_packages(),
     keywords=['data frame', 'pdf', 'table'],
-    setup_requires=[
-        'flake8',
-    ],
     install_requires=[
         'pandas',
         'numpy',
         'urllib3',
         'distro',
     ],
+    test_requires=[
+        'mock'
+    ],
 )
diff --git a/tabula/wrapper.py b/tabula/wrapper.py
index 638fd26..708e9df 100644
--- a/tabula/wrapper.py
+++ b/tabula/wrapper.py
@@ -24,10 +24,10 @@ from .template import load_template
 TABULA_JAVA_VERSION = "1.0.2"
 JAR_NAME = "tabula-{}-jar-with-dependencies.jar".format(TABULA_JAVA_VERSION)
 JAR_DIR = os.path.abspath(os.path.dirname(__file__))
-JAR_PATH = os.path.join(JAR_DIR, JAR_NAME)
-
 JAVA_NOT_FOUND_ERROR = "`java` command is not found from this Python process. Please ensure Java is installed and PATH is set for `java`"
 
+DEFAULT_CONFIG = {"JAR_PATH": os.path.join(JAR_DIR, JAR_NAME)}
+
 
 # TODO: Remove this Python 2 compatibility code if possible
 try:
@@ -36,6 +36,10 @@ except NameError:
     FileNotFoundError = IOError
 
 
+def _jar_path():
+    return os.environ.get("TABULA_JAR", DEFAULT_CONFIG["JAR_PATH"])
+
+
 def _run(java_options, options, path=None, encoding='utf-8'):
     """Call tabula-java with the given lists of Java options and tabula-py
     options, as well as an optional path to pass to tabula-java as a regular
@@ -54,7 +58,7 @@ def _run(java_options, options, path=None, encoding='utf-8'):
         ))
 
     built_options = build_options(options)
-    args = ["java"] + java_options + ["-jar", JAR_PATH] + built_options
+    args = ["java"] + java_options + ["-jar", _jar_path()] + built_options
     if path:
         args.append(path)
 


Test patch:
diff --git a/tests/test_read_pdf_table.py b/tests/test_read_pdf_table.py
index cd09dbf..3907535 100644
--- a/tests/test_read_pdf_table.py
+++ b/tests/test_read_pdf_table.py
@@ -11,8 +11,10 @@ import subprocess
 # TODO: Remove this Python 2 compatibility code if possible
 try:
     FileNotFoundError
+    from unittest.mock import patch
 except NameError:
     FileNotFoundError = IOError
+    from mock import patch
 
 
 class TestReadPdfTable(unittest.TestCase):
@@ -177,6 +179,17 @@ class TestReadPdfTable(unittest.TestCase):
         self.assertTrue(dfs[0].equals(
             pd.read_csv(expected_csv1)))
 
+    @patch('subprocess.check_output')
+    @patch('tabula.wrapper._jar_path')
+    def test_read_pdf_with_jar_path(self, jar_func, mock_fun):
+        jar_func.return_value = '/tmp/tabula-java.jar'
+        pdf_path = 'tests/resources/data.pdf'
+
+        tabula.read_pdf(pdf_path)
+        mock_fun.assert_called_with(
+            ['java', '-Dfile.encoding=UTF8', '-jar',
+                '/tmp/tabula-java.jar', '--pages', '1', '--guess', 'tests/resources/data.pdf'])
+
 
 if __name__ == '__main__':
     unittest.main()