diff options
Diffstat (limited to 'sort-library.py')
-rwxr-xr-x | sort-library.py | 58 |
1 files changed, 56 insertions, 2 deletions
diff --git a/sort-library.py b/sort-library.py index 331c81a..aa6af3b 100755 --- a/sort-library.py +++ b/sort-library.py @@ -9,6 +9,59 @@ import re import sys import pprint + +def get_groups_in_order(lines): + order = ['#', 'DEF', 'F0' , 'F1', 'F2', 'F3', 'DRAW', + 'A', 'S', 'T', 'P', 'X', + 'ENDDRAW', 'ENDDEF', + ] + for this in order: + yield([x for x in lines if x.startswith(this)]) + +def sort_chunk(lines): + res = [] + for this in get_groups_in_order(lines): + if not this: + continue + if this[0].startswith('#'): + res += this + else: + # secondary sort of elements in a group + sortfun = None + if this[0].startswith('T'): + sortfun = lambda x: (x.split(' ')[8]) # text + if this[0].startswith('X'): + sortfun = lambda x: (x.split(' ')[2]) # pin + if this[0].startswith('S'): + sortfun = lambda x: (int(x.split(' ')[1]), + int(x.split(' ')[2]), + int(x.split(' ')[3]), + int(x.split(' ')[4]), + ) + if this[0].startswith('P'): + sortfun = lambda x: (int(x.split(' ')[1]), + int(x.split(' ')[5]), + int(x.split(' ')[6]), + int(x.split(' ')[7]), + int(x.split(' ')[8]), + ) + if this[0].startswith('A'): + sortfun = lambda x: (int(x.split(' ')[1]), + int(x.split(' ')[2]), + ) + if sortfun: + try: + this = sorted(this, key=sortfun) + except ValueError: + pass + #if this[0].startswith('P'): + # # XXX HACK WHILE DEVELOPING TO MINIMIZE DIFF + # for t in range(len(this)): + # if not this[t][-3:] in [' N\n', ' f\n', ' F\n'): + # this[t] = this[t][:-1] + ' N\n' + res += this + return res + def chunk_file(fd): res = [] for this in fd.readlines(): @@ -17,7 +70,8 @@ def chunk_file(fd): if res[0] != '#\n': res = ['#\n'] + res name = res[1].split(' ')[1][:-1] - yield(name, ''.join(res)) + sorted_lines = sort_chunk(res) + yield(name, ''.join(sorted_lines)) res = [] @@ -34,7 +88,7 @@ def sort_symbols(fn_in, fn_out, refdes): out.write(header) for k in sorted(symbols.keys()): out.write(symbols[k]) - out.write('#End Library\n') + out.write('#\n#End Library\n') return True |