Skip to content

Commit

Permalink
Raise a warning when importing py2opsin if Java is not installed (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonBurns authored Jun 9, 2023
1 parent fd438fa commit 45bf384
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</p>

## Installation
`py2opsin` can be installed with `pip install py2opsin`. It has _zero_ dependencies (`OPSIN v2.7.0` is included in the PyPI package) and should work inside any environment running modern Python.
`py2opsin` can be installed with `pip install py2opsin`. It has _zero_ Python dependencies (`OPSIN v2.7.0` is included in the PyPI package) and should work inside any environment running modern Python. Java 8+ is required to run OPSIN.

Try a demo of `py2opsin` live on your browser (no installation required!): [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/JacksonBurns/py2opsin/HEAD?labpath=examples%2Fpy2opsin_example.ipynb)

Expand Down
2 changes: 1 addition & 1 deletion py2opsin/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .py2opsin import py2opsin

__version__ = "1.0.3"
__version__ = "1.0.4"
21 changes: 19 additions & 2 deletions py2opsin/py2opsin.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import warnings
from difflib import get_close_matches
from typing import Union
from subprocess import CalledProcessError

try:
from importlib.resources import files
Expand All @@ -14,6 +15,21 @@

pkg_fopen = lambda fname: resource_filename(__name__, fname)

# check if java is installed
try:
result = subprocess.run(
["java", "-version"],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
except Exception as e:
warnings.warn(
"Java may not be installed/accessible (java -version raised exception). "
"Java 8 or newer is required to use py2opsin. Original Error:\n" + repr(e),
category=RuntimeWarning,
)


def py2opsin(
chemical_name: Union[str, list],
Expand All @@ -37,8 +53,9 @@ def py2opsin(
jar_fpath (str, optional): Filepath to OPSIN jar file. Defaults to "default", which causes py2opsin to use its included jar.
Returns:
str: Species in requested format, or False if not found or an error occoured. List of strings if input is list.
str: Species in requested format, or False if not found or an error ocurred. List of strings if input is list.
"""
# path to OPSIN jar
if jar_fpath == "default":
jar_fpath = pkg_fopen("opsin-cli-2.7.0-jar-with-dependencies.jar")

Expand Down Expand Up @@ -134,7 +151,7 @@ def py2opsin(
)

except Exception as e:
warnings.warn("Unexpected error occured! " + e)
warnings.warn("Unexpected error ocurred! " + e)
return False
finally:
os.remove(temp_f)

0 comments on commit 45bf384

Please sign in to comment.