| 
15 | 15 | import typing  | 
16 | 16 | from typing import Optional  | 
17 | 17 | 
 
  | 
 | 18 | +import lems.model.model as lems_model  | 
18 | 19 | import neuroml.loaders as loaders  | 
19 | 20 | import neuroml.writers as writers  | 
20 | 21 | from neuroml import NeuroMLDocument  | 
21 | 22 | 
 
  | 
22 |  | -import lems.model.model as lems_model  | 
23 |  | -from pyneuroml.errors import FILE_NOT_FOUND_ERR  | 
 | 23 | +from pyneuroml.errors import ARGUMENT_ERR, FILE_NOT_FOUND_ERR, NMLFileTypeError  | 
24 | 24 | from pyneuroml.validators import validate_neuroml2  | 
25 | 25 | 
 
  | 
26 | 26 | logger = logging.getLogger(__name__)  | 
27 | 27 | logger.setLevel(logging.INFO)  | 
28 | 28 | 
 
  | 
29 | 29 | 
 
  | 
 | 30 | +# extension: standard  | 
 | 31 | +pynml_file_type_dict = {  | 
 | 32 | +    "xml": "LEMS",  | 
 | 33 | +    "nml": "NeuroML",  | 
 | 34 | +    "sedml": "SED-ML",  | 
 | 35 | +    "sbml": "SBML",  | 
 | 36 | +}  | 
 | 37 | + | 
 | 38 | + | 
30 | 39 | def read_neuroml2_file(  | 
31 | 40 |     nml2_file_name: str,  | 
32 | 41 |     include_includes: bool = False,  | 
@@ -130,12 +139,12 @@ def read_neuroml2_file(  | 
130 | 139 | 
 
  | 
131 | 140 |         if fix_external_morphs_biophys:  | 
132 | 141 |             from neuroml.utils import fix_external_morphs_biophys_in_cell  | 
 | 142 | + | 
133 | 143 |             fix_external_morphs_biophys_in_cell(nml2_doc)  | 
134 | 144 | 
 
  | 
135 | 145 |     return nml2_doc  | 
136 | 146 | 
 
  | 
137 | 147 | 
 
  | 
138 |  | - | 
139 | 148 | def write_neuroml2_file(  | 
140 | 149 |     nml2_doc: NeuroMLDocument,  | 
141 | 150 |     nml2_file_name: str,  | 
@@ -228,49 +237,98 @@ def confirm_file_exists(filename: str) -> None:  | 
228 | 237 |         sys.exit(FILE_NOT_FOUND_ERR)  | 
229 | 238 | 
 
  | 
230 | 239 | 
 
  | 
231 |  | -def confirm_neuroml_file(filename: str) -> None:  | 
 | 240 | +def confirm_neuroml_file(filename: str, sys_error: bool = False) -> None:  | 
232 | 241 |     """Confirm that file exists and is a NeuroML file before proceeding with  | 
233 | 242 |     processing.  | 
234 | 243 | 
  | 
235 | 244 |     :param filename: Names of files to check  | 
236 | 245 |     :type filename: str  | 
 | 246 | +    :param sys_error: toggle whether function should exit or raise exception  | 
 | 247 | +    :type sys_error: bool  | 
237 | 248 |     """  | 
238 |  | -    # print('Checking file: %s'%filename)  | 
239 |  | -    # Some conditions to check if a LEMS file was entered  | 
240 |  | -    # TODO: Ideally we'd like to check the root node: checking file extensions is brittle  | 
241 |  | -    confirm_file_exists(filename)  | 
242 |  | -    if filename.startswith("LEMS_"):  | 
243 |  | -        logger.warning(  | 
244 |  | -            textwrap.dedent(  | 
245 |  | -                """  | 
246 |  | -            *************************************************************************************  | 
247 |  | -            **  Warning, you may be trying to use a LEMS XML file (containing <Simulation> etc.)  | 
248 |  | -            **  for a pyNeuroML option when a NeuroML2 file is required...  | 
249 |  | -            *************************************************************************************  | 
250 |  | -            """  | 
251 |  | -            )  | 
252 |  | -        )  | 
 | 249 | +    error_string = textwrap.dedent(  | 
 | 250 | +        """  | 
 | 251 | +    *************************************************************************************  | 
 | 252 | +    **  You may be trying to use a LEMS XML file (containing <Simulation> etc.)  | 
 | 253 | +    **  for a pyNeuroML option when a NeuroML2 file is required.  | 
 | 254 | +    *************************************************************************************  | 
 | 255 | +    """  | 
 | 256 | +    )  | 
 | 257 | + | 
 | 258 | +    try:  | 
 | 259 | +        confirm_file_type(filename, ["nml"])  | 
 | 260 | +    except NMLFileTypeError as e:  | 
 | 261 | +        if filename.startswith("LEMS_"):  | 
 | 262 | +            logger.warning(error_string)  | 
 | 263 | +        if sys_error is True:  | 
 | 264 | +            logger.error(e)  | 
 | 265 | +            sys.exit(ARGUMENT_ERR)  | 
 | 266 | +        else:  | 
 | 267 | +            raise e  | 
253 | 268 | 
 
  | 
254 | 269 | 
 
  | 
255 |  | -def confirm_lems_file(filename: str) -> None:  | 
 | 270 | +def confirm_lems_file(filename: str, sys_error: bool = False) -> None:  | 
256 | 271 |     """Confirm that file exists and is a LEMS file before proceeding with  | 
257 | 272 |     processing.  | 
258 | 273 | 
  | 
259 | 274 |     :param filename: Names of files to check  | 
260 | 275 |     :type filename: list of strings  | 
 | 276 | +    :param sys_error: toggle whether function should exit or raise exception  | 
 | 277 | +    :type sys_error: bool  | 
261 | 278 |     """  | 
262 | 279 |     # print('Checking file: %s'%filename)  | 
263 | 280 |     # Some conditions to check if a LEMS file was entered  | 
264 | 281 |     # TODO: Ideally we'd like to check the root node: checking file extensions is brittle  | 
 | 282 | +    error_string = textwrap.dedent(  | 
 | 283 | +        """  | 
 | 284 | +    *************************************************************************************  | 
 | 285 | +    **  You may be trying to use a NeuroML2 file for a pyNeuroML option  | 
 | 286 | +    **  when a LEMS XML file (containing <Simulation> etc.) is required.  | 
 | 287 | +    *************************************************************************************  | 
 | 288 | +    """  | 
 | 289 | +    )  | 
 | 290 | +    try:  | 
 | 291 | +        confirm_file_type(filename, ["xml"])  | 
 | 292 | +    except NMLFileTypeError as e:  | 
 | 293 | +        if filename.endswith("nml"):  | 
 | 294 | +            logger.warning(error_string)  | 
 | 295 | +        if sys_error is True:  | 
 | 296 | +            logger.error(e)  | 
 | 297 | +            sys.exit(ARGUMENT_ERR)  | 
 | 298 | +        else:  | 
 | 299 | +            raise e  | 
 | 300 | + | 
 | 301 | + | 
 | 302 | +def confirm_file_type(  | 
 | 303 | +    filename: str,  | 
 | 304 | +    file_exts: typing.List[str],  | 
 | 305 | +    error_str: typing.Optional[str] = None,  | 
 | 306 | +    sys_error: bool = False,  | 
 | 307 | +) -> None:  | 
 | 308 | +    """Confirm that a file exists and has the necessary extension  | 
 | 309 | +
  | 
 | 310 | +    :param filename: filename to confirm  | 
 | 311 | +    :type filename: str  | 
 | 312 | +    :param file_exts: list of valid file extensions, without the leading dot  | 
 | 313 | +    :type file_exts: list of strings  | 
 | 314 | +    :param error_str: an optional error string to print along with the thrown  | 
 | 315 | +        exception  | 
 | 316 | +    :type error_str: string (optional)  | 
 | 317 | +
  | 
 | 318 | +    :raises NMLFileTypeError: if file does not have one of the provided extensions  | 
 | 319 | +
  | 
 | 320 | +    """  | 
265 | 321 |     confirm_file_exists(filename)  | 
266 |  | -    if filename.endswith("nml"):  | 
267 |  | -        logger.warning(  | 
268 |  | -            textwrap.dedent(  | 
269 |  | -                """  | 
270 |  | -            *************************************************************************************  | 
271 |  | -            **  Warning, you may be trying to use a NeuroML2 file for a pyNeuroML option  | 
272 |  | -            **  when a LEMS XML file (containing <Simulation> etc.) is required...  | 
273 |  | -            *************************************************************************************  | 
274 |  | -            """  | 
275 |  | -            )  | 
 | 322 | +    filename_ext = filename.split(".")[-1]  | 
 | 323 | +    file_types = [f"{x} ({pynml_file_type_dict[x]})" for x in file_exts]  | 
 | 324 | +    if filename_ext not in file_exts:  | 
 | 325 | +        error_string = (  | 
 | 326 | +            f"Expected file extension(s): {', '.join(file_types)}; got {filename_ext}"  | 
276 | 327 |         )  | 
 | 328 | +        if error_str is not None:  | 
 | 329 | +            error_string += "\n" + error_str  | 
 | 330 | +        if sys_error is True:  | 
 | 331 | +            logger.error(error_string)  | 
 | 332 | +            sys.exit(ARGUMENT_ERR)  | 
 | 333 | +        else:  | 
 | 334 | +            raise NMLFileTypeError(error_string)  | 
0 commit comments