Skip to content

Commit

Permalink
Do not fail when java is available but jmol is not.
Browse files Browse the repository at this point in the history
If java is not available, sagemath will fallback to tachyon gracefully.

When java is available, sagemath will assume jmol is available
and error if not. This commit fixes the issue by implementing a
method `is_jmol_available()` to replace `is_jvm_available()`.
  • Loading branch information
tornaria committed Nov 25, 2023
1 parent f10820f commit c08bd58
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
49 changes: 47 additions & 2 deletions src/sage/interfaces/jmoldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,52 @@ def is_jvm_available(self):
java_version_number = int(re.sub(r'.*version "(0\.|1\.)?(\d*)[\s\S]*', r'\2', version, flags=re.S))
return java_version_number >= 7

def jmolpath(self):
"""
Return the path to the jar file.
EXAMPLES::
sage: from sage.interfaces.jmoldata import JmolData
sage: JData = JmolData()
sage: JData.jmolpath()
'.../JmolData.jar'
"""
jmolpath = os.path.join(JMOL_DIR, "JmolData.jar")

if sys.platform == 'cygwin':
import cygwin
jmolpath = cygwin.cygpath(jmolpath, 'w')

Check warning on line 90 in src/sage/interfaces/jmoldata.py

View check run for this annotation

Codecov / codecov/patch

src/sage/interfaces/jmoldata.py#L89-L90

Added lines #L89 - L90 were not covered by tests

return jmolpath

def is_jmol_available(self):
"""
Returns True if jmol is available and False if not.
EXAMPLES:
Check that it returns a boolean::
sage: from sage.interfaces.jmoldata import JmolData
sage: JData = JmolData()
sage: type(JData.is_jmol_available())
<... 'bool'>
"""
if not os.path.isfile(self.jmolpath()):
return False

Check warning on line 108 in src/sage/interfaces/jmoldata.py

View check run for this annotation

Codecov / codecov/patch

src/sage/interfaces/jmoldata.py#L108

Added line #L108 was not covered by tests

if not self.is_jvm_available():
return False

try:
subprocess.check_call(['java', '-jar', self.jmolpath(), '-i'])
except (subprocess.CalledProcessError, OSError):
return False

Check warning on line 116 in src/sage/interfaces/jmoldata.py

View check run for this annotation

Codecov / codecov/patch

src/sage/interfaces/jmoldata.py#L113-L116

Added lines #L113 - L116 were not covered by tests

return True

Check warning on line 118 in src/sage/interfaces/jmoldata.py

View check run for this annotation

Codecov / codecov/patch

src/sage/interfaces/jmoldata.py#L118

Added line #L118 was not covered by tests

def export_image(self,
targetfile,
datafile, #name (path) of data file Jmol can read or script file telling it what to read or load
Expand Down Expand Up @@ -154,12 +200,11 @@ def export_image(self,
sage: archive.close()
"""
# Set up paths, file names and scripts
jmolpath = os.path.join(JMOL_DIR, "JmolData.jar")
jmolpath = self.jmolpath()

Check warning on line 203 in src/sage/interfaces/jmoldata.py

View check run for this annotation

Codecov / codecov/patch

src/sage/interfaces/jmoldata.py#L203

Added line #L203 was not covered by tests
target_native = targetfile

if sys.platform == 'cygwin':
import cygwin
jmolpath = cygwin.cygpath(jmolpath, 'w')
target_native = cygwin.cygpath(target_native, 'w')
if datafile_cmd != 'script':
datafile = cygwin.cygpath(datafile, 'w')
Expand Down
2 changes: 1 addition & 1 deletion src/sage/plot/plot3d/base.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ cdef class Graphics3d(SageObject):
T.export_jmol(scene_zip, **opts)
from sage.interfaces.jmoldata import JmolData
jdata = JmolData()
if not jdata.is_jvm_available():
if not jdata.is_jmol_available():
# We can only use JMol to generate preview if a jvm is installed
from sage.repl.rich_output.output_graphics import OutputImagePng
tachyon = self._rich_repr_tachyon(OutputImagePng, **opts)
Expand Down
2 changes: 1 addition & 1 deletion src/sage/repl/rich_output/backend_ipython.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ def launch_jmol(self, output_jmol, plain_text):
from sage.doctest import DOCTEST_MODE
from sage.interfaces.jmoldata import JmolData
jdata = JmolData()
if not jdata.is_jvm_available() and not DOCTEST_MODE:
if not jdata.is_jmol_available() and not DOCTEST_MODE:
raise RuntimeError('jmol cannot run, no suitable java version found')
launch_script = output_jmol.launch_script_filename()
jmol_cmd = 'jmol'
Expand Down

0 comments on commit c08bd58

Please sign in to comment.