1
1
# Helper functions to create MDSPlus reference models
2
2
# and store them in a cache directory (.cache/imas/MDSPlus/name-HASH/)
3
- """Module for generating and working with MDSplus models.
4
- """
3
+ """Module for generating and working with MDSplus models."""
5
4
6
5
import errno
7
6
import getpass
@@ -235,6 +234,14 @@ def model_exists(path: Path) -> bool:
235
234
)
236
235
237
236
237
+ def transform_with_xslt (xslt_processor , source , xslfile , output_file ):
238
+ return xslt_processor .transform_to_file (
239
+ source_file = str (source ),
240
+ stylesheet_file = str (xslfile ),
241
+ output_file = str (output_file ),
242
+ )
243
+
244
+
238
245
def create_model_ids_xml (cache_dir_path , fname , version ):
239
246
"""Use Saxon/C to compile an ids.xml suitable for creating an MDSplus model."""
240
247
try :
@@ -243,40 +250,32 @@ def create_model_ids_xml(cache_dir_path, fname, version):
243
250
244
251
with PySaxonProcessor (license = False ) as proc :
245
252
xslt_processor = proc .new_xslt30_processor ()
246
-
247
- xslt_processor .compile_stylesheet (stylesheet_file = str (xslfile ))
248
-
249
- input_xml = get_dd_xml (version ) if version else None
250
- if fname :
251
- source_file = str (fname )
252
- elif input_xml :
253
- source_file = input_xml # Use standard input for the XML string
254
- else :
255
- raise ValueError (
256
- "Either 'fname' or 'version' must be provided to generate XML."
257
- )
258
-
259
- # xdm_ddgit = proc.make_string_value(str(version or fname))
260
- # xsltproc.set_parameter("DD_GIT_DESCRIBE", xdm_ddgit)
261
- # xdm_algit = proc.make_string_value(os.environ.get
262
- # ("AL_VERSION", "0.0.0"))
263
- # xsltproc.set_parameter("AL_GIT_DESCRIBE", xdm_algit)
264
- # Transform XML
265
- result = xslt_processor .transform_to_file (
266
- source_file = source_file ,
267
- output_file = str (output_file ),
268
- initial_template_params = {
269
- "DD_GIT_DESCRIBE" : str (version or fname ),
270
- "AL_GIT_DESCRIBE" : os .environ .get ("AL_VERSION" , "0.0.0" ),
271
- },
253
+ xdm_ddgit = proc .make_string_value (str (version ) or fname )
254
+ xslt_processor .set_parameter ("DD_GIT_DESCRIBE" , xdm_ddgit )
255
+ xdm_algit = proc .make_string_value (
256
+ os .environ .get ("AL_VERSION" , "0.0.0" )
272
257
)
273
-
274
- if result is False :
275
- logger .error (
276
- "Transformation failed: Check Saxon/C logs for details."
277
- )
278
- raise RuntimeError ("Saxon/C XSLT transformation failed." )
279
-
258
+ xslt_processor .set_parameter ("AL_GIT_DESCRIBE" , xdm_algit )
259
+ if (
260
+ fname is not None
261
+ and fname != "-"
262
+ and fname != ""
263
+ and os .path .exists (fname )
264
+ ):
265
+ transform_with_xslt (xslt_processor , fname , xslfile , output_file )
266
+ elif version is not None and version != "" :
267
+ xml_string = get_dd_xml (version )
268
+
269
+ with tempfile .NamedTemporaryFile (
270
+ delete = True , mode = "w+b"
271
+ ) as temp_file :
272
+ temp_file .write (xml_string )
273
+ temp_file .seek (0 )
274
+ transform_with_xslt (
275
+ xslt_processor , temp_file .name , xslfile , output_file
276
+ )
277
+ else :
278
+ raise MDSPlusModelError ("Either fname or version must be provided" )
280
279
except Exception as e :
281
280
if fname :
282
281
logger .error ("Error making MDSplus model IDS.xml for %s" , fname )
0 commit comments