Debian Codegraph @

nixio/cmd/validate.py

raw symbols SCIP
1 """
2 Validate NIX files for missing or inconsistent objects and annotations.
3 """
4 import os
5 import nixio as nix
6
7
8 def format_obj(obj):
9 return "{} '{}' (ID: {})".format(type(obj).__name__, obj.name, obj.id)
10
11
12 def print_header(text):
13 print(" {}".format(text))
14 print(" " + "-"*len(text))
15
16
17 def validate(filename):
18 nf = nix.File(filename, mode=nix.FileMode.ReadOnly)
19 results = nf.validate()
20 print("Results for '{}'".format(filename))
21 errors = results["errors"]
22 if errors:
23 plural = "s" if len(errors) > 1 else ""
24 print_header("{} object{} with errors".format(len(errors), plural))
25 for idx, (obj, messages) in enumerate(errors.items()):
26 print(" [{}] {}".format(idx+1, format_obj(obj)))
27 for msg in messages:
28 print(" {}".format(msg))
29
30 warnings = results["warnings"]
31 if warnings:
32 plural = "s" if len(warnings) > 1 else ""
33 print_header("{} object{} with warnings".format(len(warnings), plural))
34 for idx, (obj, messages) in enumerate(warnings.items()):
35 print(" [{}] {}".format(idx+1, format_obj(obj)))
36 for msg in messages:
37 print(" {}".format(msg))
38
39 print()
40
41
42 def create_subcmd_parser(parser):
43 parser.add_argument("file", type=str, nargs="+",
44 help="path to file to validate (at least one)")
45 return parser
46
47
48 def main(args):
49 filenames = args.file
50
51 for nixfn in filenames:
52 if os.path.exists(nixfn):
53 validate(nixfn)
54 else:
55 print("error: No such file '{}'".format(nixfn))