Skip to content

Commit fc82918

Browse files
committed
feat(pynml): improve checks on file types being passed
1 parent e4fb698 commit fc82918

File tree

1 file changed

+71
-35
lines changed

1 file changed

+71
-35
lines changed

pyneuroml/pynml.py

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -561,9 +561,13 @@ def _evaluate_arguments(args):
561561
pre_args = ""
562562
post_args = ""
563563
exit_on_fail = True
564+
# type of files being passed to the command
565+
# by default, lems
566+
file_types = ["xml"]
564567

565568
# Deal with the SBML validation option which doesn't call run_jneuroml
566569
if args.validate_sbml or args.validate_sbml_units:
570+
file_types = ["sbml"]
567571
try:
568572
from pyneuroml.sbml import validate_sbml_files
569573
except Exception:
@@ -597,6 +601,7 @@ def _evaluate_arguments(args):
597601

598602
# Deal with the SEDML validation option which doesn't call run_jneuroml
599603
if args.validate_sedml:
604+
file_types = ["sedml"]
600605
try:
601606
from pyneuroml.sedml import validate_sedml_files
602607
except Exception:
@@ -623,6 +628,7 @@ def _evaluate_arguments(args):
623628

624629
# Deal with the -run-tellurium option which doesn't call run_jneuroml
625630
if args.run_tellurium is not None:
631+
file_types = ["sedml"]
626632
try:
627633
from pyneuroml.tellurium import run_from_sedml_file
628634
except Exception:
@@ -654,16 +660,21 @@ def _evaluate_arguments(args):
654660
# TODO: handle these better
655661
if args.sbml_import or args.sbml_import_units or args.vhdl:
656662
if args.sbml_import:
663+
file_types = ["sbml"]
657664
pre_args = "-sbml-import"
658665
f = args.sbml_import[0]
659666
post_args = " ".join(args.sbml_import[1:])
667+
confirm_file_type(f, file_types, sys_error=True)
660668
elif args.sbml_import_units:
669+
file_types = ["sbml"]
661670
pre_args = "-smbl-import-units"
662671
f = args.sbml_import_units[0]
663672
post_args = " ".join(args.sbml_import_units[1:])
673+
confirm_file_type(f, file_types, sys_error=True)
664674
elif args.vhdl:
675+
file_types = ["xml"]
665676
f = args.vhdl[1]
666-
confirm_lems_file(f)
677+
confirm_lems_file(f, True)
667678
post_args = "-vhdl %s" % args.vhdl[0]
668679

669680
run_jneuroml(
@@ -678,21 +689,25 @@ def _evaluate_arguments(args):
678689

679690
# Process bits that process the file list provided as the shared option
680691
if len(args.input_files) == 0:
681-
logger.critical("Please specify NeuroML/LEMS files to process")
692+
logger.error(
693+
"Please specify files and options to process. Run `pynml -h` to see usage help text."
694+
)
682695
return
683696

697+
# some commands can be run on lists of files
684698
run_multi = False
685699

700+
# for commands to be run on each file individually
686701
for f in args.input_files:
687702
if args.nogui:
688703
post_args = "-nogui"
689704

690705
if args.sedml:
691-
confirm_lems_file(f)
706+
file_types = ["xml"]
692707
post_args = "-sedml"
693708
elif args.neuron is not None:
694709
# Note: either a lems file or nml2 file is allowed here...
695-
confirm_file_exists(f)
710+
file_types = ["xml", "nml"]
696711

697712
num_neuron_args = len(args.neuron)
698713
if num_neuron_args < 0 or num_neuron_args > 4:
@@ -707,7 +722,7 @@ def _evaluate_arguments(args):
707722

708723
elif args.netpyne is not None:
709724
# Note: either a lems file or nml2 file is allowed here...
710-
confirm_file_exists(f)
725+
file_types = ["xml", "nml"]
711726

712727
num_netpyne_args = len(args.netpyne)
713728

@@ -722,7 +737,7 @@ def _evaluate_arguments(args):
722737
post_args = "-netpyne %s" % " ".join(other_args)
723738

724739
elif args.eden is not None:
725-
confirm_lems_file(f)
740+
file_types = ["xml"]
726741

727742
num_eden_args = len(args.eden)
728743

@@ -736,53 +751,60 @@ def _evaluate_arguments(args):
736751
other_args = [(a if a != "-eden" else "") for a in args.eden]
737752
post_args = "-eden %s" % " ".join(other_args)
738753

739-
elif args.svg:
740-
confirm_neuroml_file(f)
741-
post_args = "-svg"
742-
elif args.png:
743-
confirm_neuroml_file(f)
744-
post_args = "-png"
745754
elif args.dlems:
746-
confirm_lems_file(f)
755+
file_types = ["xml"]
747756
post_args = "-dlems"
748757
elif args.vertex:
749-
confirm_lems_file(f)
758+
file_types = ["xml"]
750759
post_args = "-vertex"
751760
elif args.xpp:
752-
confirm_lems_file(f)
761+
file_types = ["xml"]
753762
post_args = "-xpp"
754763
elif args.dnsim:
755-
confirm_lems_file(f)
764+
file_types = ["xml"]
756765
post_args = "-dnsim"
757766
elif args.brian:
758-
confirm_lems_file(f)
767+
file_types = ["xml"]
759768
post_args = "-brian"
760769
elif args.brian2:
761-
confirm_lems_file(f)
770+
file_types = ["xml"]
762771
post_args = "-brian2"
763772
elif args.moose:
764-
confirm_lems_file(f)
773+
file_types = ["xml"]
765774
post_args = "-moose"
766775
elif args.sbml:
767-
confirm_lems_file(f)
776+
file_types = ["xml"]
768777
post_args = "-sbml"
769778
elif args.sbml_sedml:
770-
confirm_lems_file(f)
779+
file_types = ["xml"]
771780
post_args = "-sbml-sedml"
772781
elif args.matlab:
773-
confirm_lems_file(f)
782+
file_types = ["xml"]
774783
post_args = "-matlab"
775784
elif args.cvode:
776-
confirm_lems_file(f)
785+
file_types = ["xml"]
777786
post_args = "-cvode"
778787
elif args.nineml:
779-
confirm_lems_file(f)
788+
file_types = ["xml"]
780789
post_args = "-nineml"
781790
elif args.spineml:
782-
confirm_lems_file(f)
791+
file_types = ["xml"]
783792
post_args = "-spineml"
793+
elif args.lems_graph:
794+
file_types = ["xml"]
795+
pre_args = ""
796+
post_args = "-lems-graph"
797+
exit_on_fail = True
798+
elif args.svg:
799+
file_types = ["nml"]
800+
post_args = "-svg"
801+
elif args.png:
802+
file_types = ["nml"]
803+
post_args = "-png"
784804
elif args.graph:
785-
confirm_neuroml_file(f)
805+
# not using jneuroml
806+
file_types = ["nml"]
807+
confirm_neuroml_file(f, True)
786808
from neuromllite.GraphVizHandler import engines
787809

788810
engine = "dot"
@@ -818,13 +840,8 @@ def _evaluate_arguments(args):
818840

819841
generate_nmlgraph(f, level, engine)
820842
sys.exit(0)
821-
elif args.lems_graph:
822-
confirm_lems_file(f)
823-
pre_args = ""
824-
post_args = "-lems-graph"
825-
exit_on_fail = True
826843
elif args.matrix:
827-
confirm_neuroml_file(f)
844+
confirm_neuroml_file(f, True)
828845
from neuromllite.MatrixHandler import MatrixHandler
829846

830847
level = int(args.matrix[0])
@@ -845,35 +862,54 @@ def _evaluate_arguments(args):
845862

846863
exit(0)
847864
elif args.validate:
848-
confirm_neuroml_file(f)
865+
file_types = ["nml"]
849866
pre_args = "-validate"
850867
exit_on_fail = True
851868
run_multi = True
852869

853870
elif args.validatev1:
854-
confirm_neuroml_file(f)
871+
file_types = ["nml"]
855872
pre_args = "-validatev1"
856873
exit_on_fail = True
857874
run_multi = True
858875

859876
elif args.swc:
860877
convert_count = 0
878+
file_types = ["nml"]
861879
for f in args.input_files:
862-
confirm_neuroml_file(f)
880+
confirm_neuroml_file(f, True)
863881
logger.info(f"Trying to convert {f} to swc format...")
864882
convert_count += 1 if convert_to_swc(f) else 0
865883
logger.info(f"Converted {convert_count} file(s) to swc format")
866884
sys.exit(0)
867885

868886
if run_multi is False:
887+
# check that the right file type has been passed to jNeuroML
888+
if file_types == ["xml"]:
889+
confirm_lems_file(f, True)
890+
elif file_types == ["nml"]:
891+
confirm_neuroml_file(f, True)
892+
else:
893+
confirm_file_type(f, file_types, sys_error=True)
894+
869895
run_jneuroml(
870896
pre_args,
871897
f,
872898
post_args,
873899
max_memory=args.java_max_memory,
874900
exit_on_fail=exit_on_fail,
875901
)
902+
876903
if run_multi:
904+
for f in args.input_files:
905+
# check that the right file type has been passed to jNeuroML
906+
if file_types == ["xml"]:
907+
confirm_lems_file(f, True)
908+
elif file_types == ["nml"]:
909+
confirm_neuroml_file(f, True)
910+
else:
911+
confirm_file_type(f, file_types, sys_error=True)
912+
877913
run_jneuroml(
878914
pre_args,
879915
" ".join(args.input_files),

0 commit comments

Comments
 (0)