Skip to content

Commit

Permalink
fix for msdplus model
Browse files Browse the repository at this point in the history
  • Loading branch information
prasad-sawantdesai committed Jan 22, 2025
1 parent 78ac135 commit 4c52cf7
Showing 1 changed file with 12 additions and 47 deletions.
59 changes: 12 additions & 47 deletions imas/backends/imas_core/mdsplus_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
except ImportError: # Python 3.8 support
from importlib_resources import as_file, files

import imas
from imas.dd_zip import get_dd_xml, get_dd_xml_crc
from imas.exception import MDSPlusModelError
from imas.ids_factory import IDSFactory
Expand Down Expand Up @@ -86,12 +85,7 @@ def mdsplus_model_dir(factory: IDSFactory) -> str:
Given a filename and xml contents create an xml
document for the mdsplus model by running a command like the below:
java net.sf.saxon.Transform -s:- -xsl: -o:${OUTPUT_FILE}
with ENV:
env={"CLASSPATH": saxon_jar_path, "PATH": os.environ.get("PATH", "")}
document for the mdsplus model by rusing saxonche
Args:
factory: IDSFactory indicating the DD version / XML to build models for.
Expand Down Expand Up @@ -241,62 +235,31 @@ def model_exists(path: Path) -> bool:
)


# def create_model_ids_xml(cache_dir_path, fname, version):
# """Use saxon to compile an ids.xml suitable for creating an mdsplus model."""

# try:
# # we have to be careful to have the same version of this file as in the access
# # layer:
# with as_file(files(imas) / "assets" / "IDSDef2MDSpreTree.xsl") as xslfile:
# check_output(
# [
# "java",
# "net.sf.saxon.Transform",
# "-s:" + str(fname),
# "-o:" + str(Path(cache_dir_path) / "ids.xml"),
# "DD_GIT_DESCRIBE=" + str(version or fname),
# # if this is expected as git describe it might break
# # if we just pass a filename
# "AL_GIT_DESCRIBE=" + os.environ.get("AL_VERSION", "0.0.0"),
# "-xsl:" + str(xslfile),
# ],
# input=get_dd_xml(version) if version else None,
# env={"CLASSPATH": get_saxon(), "PATH": os.environ.get("PATH", "")},
# )
# except CalledProcessError as e:
# if fname:
# logger.error("Error making MDSPlus model IDS.xml for %s", fname)
# else:
# logger.error("Error making MDSplus model IDS.xml for %s", version)
# raise e


def create_model_ids_xml(cache_dir_path, fname, version):
"""Use Saxon/C to compile an ids.xml suitable for creating an MDSplus model."""
try:
# Locate the XSL file within the package
with as_file(files("imas") / "assets" / "IDSDef2MDSpreTree.xsl") as xslfile:
output_file = Path(cache_dir_path) / "ids.xml"

# Initialize Saxon/C processor

with PySaxonProcessor(license=False) as proc:
xslt_processor = proc.new_xslt30_processor()

# Set the XSLT stylesheet
xslt_processor.compile_stylesheet(stylesheet_file=str(xslfile))

# Prepare the input XML source
input_xml = get_dd_xml(version) if version else None
if fname:
source_file = str(fname)
elif input_xml:
source_file = "-" # Use standard input for the XML string
source_file = input_xml # Use standard input for the XML string
else:
raise ValueError("Either 'fname' or 'version' must be provided to generate XML.")
raise ValueError(
"Either 'fname' or 'version' must be provided to generate XML."
)

# xdm_ddgit = proc.make_string_value(str(version or fname))
# xsltproc.set_parameter("DD_GIT_DESCRIBE", xdm_ddgit)
# xdm_algit = proc.make_string_value(os.environ.get("AL_VERSION", "0.0.0"))
# xdm_algit = proc.make_string_value(os.environ.get
# ("AL_VERSION", "0.0.0"))
# xsltproc.set_parameter("AL_GIT_DESCRIBE", xdm_algit)
# Transform XML
result = xslt_processor.transform_to_file(
Expand All @@ -309,7 +272,9 @@ def create_model_ids_xml(cache_dir_path, fname, version):
)

if result is False:
logger.error("Transformation failed: Check Saxon/C logs for details.")
logger.error(
"Transformation failed: Check Saxon/C logs for details."
)
raise RuntimeError("Saxon/C XSLT transformation failed.")

except Exception as e:
Expand All @@ -318,7 +283,7 @@ def create_model_ids_xml(cache_dir_path, fname, version):
else:
logger.error("Error making MDSplus model IDS.xml for %s", version)
raise e


def create_mdsplus_model(cache_dir_path: Path) -> None:
"""Use jtraverser to compile a valid MDS model file."""
Expand Down

0 comments on commit 4c52cf7

Please sign in to comment.