Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set encoding on SubprocessTabula initialization #371

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 13 additions & 22 deletions tabula/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@


def _run(
java_options: List[str],
options: TabulaOption,
java_options: Optional[List[str]] = None,
path: Optional[str] = None,
encoding: str = "utf-8",
force_subprocess: bool = False,
Expand All @@ -62,6 +62,8 @@ def _run(
"-Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog",
}

java_options = _build_java_options(java_options, encoding)

global _tabula_vm
if force_subprocess:
_tabula_vm = SubprocessTabula(
Expand Down Expand Up @@ -381,20 +383,6 @@ def read_pdf(
multiple_tables=multiple_tables,
)

if java_options is None:
java_options = []
elif isinstance(java_options, str):
java_options = shlex.split(java_options)

# to prevent tabula-py from stealing focus on every call on mac
if platform.system() == "Darwin":
if not any("java.awt.headless" in opt for opt in java_options):
java_options += ["-Djava.awt.headless=true"]

if encoding == "utf-8":
if not any("file.encoding" in opt for opt in java_options):
java_options += ["-Dfile.encoding=UTF8"]

path, temporary = localize_file(input_path, user_agent, use_raw_url=use_raw_url)

if not os.path.exists(path):
Expand All @@ -405,8 +393,8 @@ def read_pdf(

try:
output = _run(
java_options,
tabula_options,
java_options,
path,
encoding=encoding,
force_subprocess=force_subprocess,
Expand Down Expand Up @@ -827,7 +815,6 @@ def convert_into(
output_path=output_path,
options=options,
)
java_options = _build_java_options(java_options)

path, temporary = localize_file(input_path)

Expand All @@ -838,7 +825,7 @@ def convert_into(
raise ValueError(f"{path} is empty. Check the file, or download it manually.")

try:
_run(java_options, tabula_options, path, force_subprocess=force_subprocess)
_run(tabula_options, java_options, path, force_subprocess=force_subprocess)
finally:
if temporary:
os.unlink(path)
Expand Down Expand Up @@ -948,8 +935,6 @@ def convert_into_by_batch(

format = _extract_format_for_conversion(output_format)

java_options = _build_java_options(java_options)

tabula_options = TabulaOption(
pages=pages,
guess=guess,
Expand All @@ -967,10 +952,12 @@ def convert_into_by_batch(
options=options,
)

_run(java_options, tabula_options, force_subprocess=force_subprocess)
_run(tabula_options, java_options, force_subprocess=force_subprocess)


def _build_java_options(_java_options: Optional[List[str]] = None) -> List[str]:
def _build_java_options(
_java_options: Optional[List[str]] = None, encoding: str = "utf-8"
) -> List[str]:
if _java_options is None:
_java_options = []
elif isinstance(_java_options, str):
Expand All @@ -982,6 +969,10 @@ def _build_java_options(_java_options: Optional[List[str]] = None) -> List[str]:
if not any(filter(r.find, _java_options)): # type: ignore
_java_options = _java_options + ["-Djava.awt.headless=true"]

if encoding == "utf-8":
if not any("file.encoding" in opt for opt in _java_options):
_java_options += ["-Dfile.encoding=UTF8"]

return _java_options


Expand Down