22import os , re
33import shutil
44import tempfile
5+ from typing import NoReturn
56
67from bionetgen .main import BioNetGen
78from bionetgen .core .exc import BNGFileError
9+ from bionetgen .core .utils .logging import BNGLogger
810from bionetgen .core .utils .utils import find_BNG_path , run_command , ActionList
911
1012# This allows access to the CLIs config setup
@@ -46,6 +48,7 @@ def __init__(
4648 self , path , BNGPATH = def_bng_path , generate_network = False , suppress = True
4749 ) -> None :
4850 self .path = path
51+ self .logger = BNGLogger ()
4952 self .generate_network = generate_network
5053 self .suppress = suppress
5154 AList = ActionList ()
@@ -60,6 +63,11 @@ def __init__(
6063 # the top-level ActionBlock.
6164 self .parsed_protocol_actions = []
6265
66+ def _raise_file_error (self , message , path = None , loc = None ) -> NoReturn :
67+ error_path = self .path if path is None else path
68+ self .logger .error (message , loc = loc )
69+ raise BNGFileError (error_path , message = message )
70+
6371 def generate_xml (self , xml_file , model_file = None ) -> bool :
6472 """
6573 generates an BNG-XML file from a given model file. Defaults
@@ -85,7 +93,12 @@ def generate_xml(self, xml_file, model_file=None) -> bool:
8593 ["perl" , self .bngexec , "--xml" , stripped_bngl ], suppress = self .suppress
8694 )
8795 if rc != 0 :
88- return False
96+ msg = f"BNG-XML generation failed for { model_file } "
97+ self ._raise_file_error (
98+ msg ,
99+ path = model_file ,
100+ loc = f"{ __file__ } : BNGFile.generate_xml()" ,
101+ )
89102
90103 # we should now have the XML file
91104 path , model_name = os .path .split (stripped_bngl )
@@ -102,7 +115,12 @@ def generate_xml(self, xml_file, model_file=None) -> bool:
102115 ]
103116 xml_path = preferred [0 ] if preferred else candidates [0 ]
104117 if not os .path .exists (xml_path ):
105- return False
118+ msg = f"BNG-XML generation did not produce an XML file for { model_file } "
119+ self ._raise_file_error (
120+ msg ,
121+ path = model_file ,
122+ loc = f"{ __file__ } : BNGFile.generate_xml()" ,
123+ )
106124 with open (xml_path , "r" , encoding = "UTF-8" ) as f :
107125 content = f .read ()
108126 xml_file .write (content )
@@ -268,14 +286,22 @@ def write_xml(self, open_file, xml_type="bngxml", bngl_str=None) -> bool:
268286 # run with --xml
269287 # TODO: Make output supression an option somewhere
270288 if xml_type == "bngxml" :
289+ if self .bngexec is None :
290+ msg = "BNG-XML generation requires BNG2.pl (BioNetGen) to be installed."
291+ self ._raise_file_error (msg , loc = f"{ __file__ } : BNGFile.write_xml()" )
271292 rc , _ = run_command (
272293 ["perl" , self .bngexec , "--xml" , "temp.bngl" ], suppress = self .suppress
273294 )
274295 if rc != 0 :
275- print ( " XML generation failed" )
276- return False
296+ msg = f"BNG- XML generation failed for { self . path } "
297+ self . _raise_file_error ( msg , loc = f" { __file__ } : BNGFile.write_xml()" )
277298 else :
278299 # we should now have the XML file
300+ if not os .path .exists ("temp.xml" ):
301+ msg = "BNG-XML generation did not produce temp.xml"
302+ self ._raise_file_error (
303+ msg , loc = f"{ __file__ } : BNGFile.write_xml()"
304+ )
279305 with open ("temp.xml" , "r" , encoding = "UTF-8" ) as f :
280306 content = f .read ()
281307 open_file .write (content )
@@ -284,25 +310,30 @@ def write_xml(self, open_file, xml_type="bngxml", bngl_str=None) -> bool:
284310 return True
285311 elif xml_type == "sbml" :
286312 if self .bngexec is None :
287- print (
313+ msg = (
288314 "SBML generation requires BNG2.pl (BioNetGen) to be installed."
289315 )
290- return False
316+ self . _raise_file_error ( msg , loc = f" { __file__ } : BNGFile.write_xml()" )
291317 command = ["perl" , self .bngexec , "temp.bngl" ]
292318 rc , _ = run_command (command , suppress = self .suppress )
293319 if rc != 0 :
294- print ( "SBML generation failed" )
295- return False
320+ msg = f "SBML generation failed for { self . path } "
321+ self . _raise_file_error ( msg , loc = f" { __file__ } : BNGFile.write_xml()" )
296322 else :
297323 # we should now have the SBML file
324+ if not os .path .exists ("temp_sbml.xml" ):
325+ msg = "SBML generation did not produce temp_sbml.xml"
326+ self ._raise_file_error (
327+ msg , loc = f"{ __file__ } : BNGFile.write_xml()"
328+ )
298329 with open ("temp_sbml.xml" , "r" , encoding = "UTF-8" ) as f :
299330 content = f .read ()
300331 open_file .write (content )
301332 open_file .seek (0 )
302333 return True
303334 else :
304- print ( "XML type {} not recognized" . format ( xml_type ))
305- return False
335+ msg = f "XML type { xml_type } not recognized"
336+ self . _raise_file_error ( msg , loc = f" { __file__ } : BNGFile.write_xml()" )
306337 finally :
307338 os .chdir (cur_dir )
308339 try :
0 commit comments