1e5c31af7Sopenharmony_ci#!/usr/bin/python3
2e5c31af7Sopenharmony_ci#
3e5c31af7Sopenharmony_ci# Copyright 2013-2024 The Khronos Group Inc.
4e5c31af7Sopenharmony_ci#
5e5c31af7Sopenharmony_ci# SPDX-License-Identifier: Apache-2.0
6e5c31af7Sopenharmony_ci
7e5c31af7Sopenharmony_ci# Usage: realign [infile] > outfile
8e5c31af7Sopenharmony_ci# Used to realign XML tags in the Vulkan registry after it is operated on by
9e5c31af7Sopenharmony_ci# some other filter, since whitespace inside a tag is not part of the
10e5c31af7Sopenharmony_ci# internal representation.
11e5c31af7Sopenharmony_ci
12e5c31af7Sopenharmony_ciimport copy, sys, string, re
13e5c31af7Sopenharmony_ci
14e5c31af7Sopenharmony_cidef realignXML(fp):
15e5c31af7Sopenharmony_ci    patterns = [
16e5c31af7Sopenharmony_ci        [ r'(^ *\<type .*)(category=[\'"]bitmask[\'"].*)', 58 ],
17e5c31af7Sopenharmony_ci        [ r'(^ *\<enum [bv].*)(name=.*)',     28 ],
18e5c31af7Sopenharmony_ci        [ r'(^ *\<enum [bv].*)(comment=.*)',  85 ]
19e5c31af7Sopenharmony_ci    ]
20e5c31af7Sopenharmony_ci
21e5c31af7Sopenharmony_ci    # Assemble compiled expressions to match and alignment columns
22e5c31af7Sopenharmony_ci    numpat = len(patterns)
23e5c31af7Sopenharmony_ci    regexp = [ re.compile(patterns[i][0]) for i in range(0,numpat)]
24e5c31af7Sopenharmony_ci    column = [ patterns[i][1] for i in range(0,numpat)]
25e5c31af7Sopenharmony_ci
26e5c31af7Sopenharmony_ci    lines = fp.readlines()
27e5c31af7Sopenharmony_ci    for line in lines:
28e5c31af7Sopenharmony_ci        emitted = False
29e5c31af7Sopenharmony_ci        for i in range(0,len(patterns)):
30e5c31af7Sopenharmony_ci            match = regexp[i].match(line)
31e5c31af7Sopenharmony_ci            if (match):
32e5c31af7Sopenharmony_ci                if (not emitted):
33e5c31af7Sopenharmony_ci                    #print('# While processing line: ' + line, end='')
34e5c31af7Sopenharmony_ci                    emitted = True
35e5c31af7Sopenharmony_ci                #print('# matched expression: ' + patterns[i][0])
36e5c31af7Sopenharmony_ci                #print('# clause 1 = ' + match.group(1))
37e5c31af7Sopenharmony_ci                #print('# clause 2 = ' + match.group(2))
38e5c31af7Sopenharmony_ci                line = match.group(1).ljust(column[i]) + match.group(2)
39e5c31af7Sopenharmony_ci        if (emitted):
40e5c31af7Sopenharmony_ci            print(line)
41e5c31af7Sopenharmony_ci        else:
42e5c31af7Sopenharmony_ci            print(line, end='')
43e5c31af7Sopenharmony_ci
44e5c31af7Sopenharmony_ciif __name__ == '__main__':
45e5c31af7Sopenharmony_ci    if (len(sys.argv) > 1):
46e5c31af7Sopenharmony_ci        realignXML(open(sys.argv[1], 'r', encoding='utf-8'))
47e5c31af7Sopenharmony_ci    else:
48e5c31af7Sopenharmony_ci        realignXML(sys.stdin)
49