File: validate.py

package info (click to toggle)
python-nixio 1.5.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,888 kB
  • sloc: python: 12,527; cpp: 832; makefile: 25
file content (55 lines) | stat: -rw-r--r-- 1,612 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Validate NIX files for missing or inconsistent objects and annotations.
"""
import os
import nixio as nix


def format_obj(obj):
    return "{} '{}' (ID: {})".format(type(obj).__name__, obj.name, obj.id)


def print_header(text):
    print("  {}".format(text))
    print("  " + "-"*len(text))


def validate(filename):
    nf = nix.File(filename, mode=nix.FileMode.ReadOnly)
    results = nf.validate()
    print("Results for '{}'".format(filename))
    errors = results["errors"]
    if errors:
        plural = "s" if len(errors) > 1 else ""
        print_header("{} object{} with errors".format(len(errors), plural))
        for idx, (obj, messages) in enumerate(errors.items()):
            print(" [{}] {}".format(idx+1, format_obj(obj)))
            for msg in messages:
                print("    {}".format(msg))

    warnings = results["warnings"]
    if warnings:
        plural = "s" if len(warnings) > 1 else ""
        print_header("{} object{} with warnings".format(len(warnings), plural))
        for idx, (obj, messages) in enumerate(warnings.items()):
            print(" [{}] {}".format(idx+1, format_obj(obj)))
            for msg in messages:
                print("    {}".format(msg))

    print()


def create_subcmd_parser(parser):
    parser.add_argument("file", type=str, nargs="+",
                        help="path to file to validate (at least one)")
    return parser


def main(args):
    filenames = args.file

    for nixfn in filenames:
        if os.path.exists(nixfn):
            validate(nixfn)
        else:
            print("error: No such file '{}'".format(nixfn))