aboutsummaryrefslogtreecommitdiff
path: root/extract-labels.py
blob: b4d432836c3ecd92dba0f8fbe5f7cdc6de41cc40 (plain) (blame)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python

import os
import re
import sys
import pprint

import pcbnew


def extract_labels(fn_in, labels):
    in_ = open(fn_in)
    prev = None
    fn = os.path.basename(fn_in)
    for line in in_.readlines():
        #print('R: {!r}'.format(line))
        if prev is not None:
            text = line[:-1]
            if re.match('^[A-Z0-9_]+$', text):
                m = re.match('^Text Label (\d+) +(\d+) +(\d+) +(\d+) +~', prev)
                if m:
                    x, y, orient, dim = m.groups()
                    if text not in labels[fn]:
                        labels[fn][text] = []
                    labels[fn][text] += [{'t': 'GLabel',
                                          'dir': 'UnSpc',
                                          'x': x,
                                          'y': y,
                                          'ori': orient,
                                          'dim': dim}]
                    #print("Label: {!r}".format(text))
                else:
                    sys.stderr.write("Failed extracting data from previous line: {} - {}\n".format(text, prev))
                    return False
            prev = None
        elif line.startswith('Text Label '):
            prev = line

    return True


def main(schemas):
    labels = {}
    for this in schemas:
        labels[os.path.basename(this)] = {}
        if not extract_labels(this, labels):
            return False

    #print('{}'.format(pprint.pformat(labels)))

    print('labels = {')
    for this in sorted(schemas):
        fn = os.path.basename(this)
        if not len(labels[fn]):
            continue
        print('      {!r}: {{').format(fn)
        for text in sorted(labels[fn].keys()):
            sys.stdout.write('       {!r}: ['.format(text))
            first = True
            for v in labels[fn][text]:
                #sys.stdout.write(" x{}x ".format(first))
                if not first:
                    sys.stdout.write("\n{}".format(' ' * (len(text) + 16)))
                first = False
                if str(v['dim']) != '48':
                    sys.stdout.write("{{'t': {t!r}, 'dir': {dir!r}, 'x': {x!s}, 'y': {y!s}, 'ori': {ori!s}, 'dim': {dim!s}}},".format(**v))
                else:
                    sys.stdout.write("{{'t': {t!r}, 'dir': {dir!r}, 'x': {x!s}, 'y': {y!s}, 'ori': {ori!s}}},".format(**v))
            print('],')
        print('      },')
    print('    },')
    return True


if __name__ == '__main__':
    try:
        if len(sys.argv) == 0:
            sys.stderr.write('Syntax: fix-labels.py *.sch\n')
            sys.exit(1)
        schemas = [x for x in sys.argv if x.endswith('.sch') and not x.endswith('Cryptech Alpha.sch')]
        res = main(schemas)
        if res:
            sys.exit(0)
        sys.exit(1)
    except KeyboardInterrupt:
        pass