File: check_btable

package info (click to toggle)
wreport 3.36-1.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 17,572 kB
  • sloc: cpp: 18,927; python: 583; sh: 78; makefile: 13
file content (52 lines) | stat: -rwxr-xr-x 1,489 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/python3

import sys

def parse_line(line):
    if len(line) < 118:
        raise ValueError("bufr table line too short")
    return "0" + line[2:7]


def check_file(fname):
    is_ok = True

    last_bcode = 0
    with open(fname, "rb") as fd:
        for idx, line in enumerate(fd, start=1):
            try:
                line = line.decode("utf-8").rstrip()
            except UnicodeDecodeError as e:
                print("{}:{}:encoding error: {}".format(fname, idx, str(e)), file=sys.stderr)
                is_ok = False
                continue

            try:
                bcode = parse_line(line)
                int_bcode = int(bcode)
            except ValueError as e:
                print("{}:{}:{}".format(fname, idx, str(e)), file=sys.stderr)
                is_ok = False
                continue

            if int_bcode <= last_bcode:
                print("{}:{}:B code {} is not greater than the B code in the previous line".format(fname, idx, bcode), file=sys.stderr)
                is_ok = False
                continue
            else:
                last_bcode = int_bcode

    return is_ok


def main():
    import argparse
    parser = argparse.ArgumentParser(description="Validate BUFR table files")
    parser.add_argument('files', nargs='+', help='input file(s) to validate')
    args = parser.parse_args()
    all_ok = all([check_file(x) for x in args.files])
    sys.exit(0 if all_ok else 1)


if __name__ == "__main__":
    main()