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 authored and dimpase committed Dec 4, 2023
1 parent 2a9a426 commit c7670d0
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
44 changes: 42 additions & 2 deletions src/sage/interfaces/jmoldata.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,47 @@ 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')

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

if not self.is_jvm_available():
return False

return True

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 +195,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()
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 c7670d0

Please sign in to comment.