/ scripts / Retired / realign.py
realign.py
 1  #!/usr/bin/python3
 2  #
 3  # Copyright (c) 2013-2019 The Khronos Group Inc.
 4  #
 5  # Licensed under the Apache License, Version 2.0 (the "License");
 6  # you may not use this file except in compliance with the License.
 7  # You may obtain a copy of the License at
 8  #
 9  #     http://www.apache.org/licenses/LICENSE-2.0
10  #
11  # Unless required by applicable law or agreed to in writing, software
12  # distributed under the License is distributed on an "AS IS" BASIS,
13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  # See the License for the specific language governing permissions and
15  # limitations under the License.
16  
17  # Usage: realign [infile] > outfile
18  # Used to realign XML tags in the Vulkan registry after it's operated on by
19  # some other filter, since whitespace inside a tag isn't part of the
20  # internal representation.
21  
22  import copy, sys, string, re
23  
24  def realignXML(fp):
25      patterns = [
26          [ r'(^ *\<type .*)(category=[\'"]bitmask[\'"].*)', 58 ],
27          [ r'(^ *\<enum [bv].*)(name=.*)',     28 ],
28          [ r'(^ *\<enum [bv].*)(comment=.*)',  85 ]
29      ]
30  
31      # Assemble compiled expressions to match and alignment columns
32      numpat = len(patterns)
33      regexp = [ re.compile(patterns[i][0]) for i in range(0,numpat)]
34      column = [ patterns[i][1] for i in range(0,numpat)]
35  
36      lines = fp.readlines()
37      for line in lines:
38          emitted = False
39          for i in range(0,len(patterns)):
40              match = regexp[i].match(line)
41              if (match):
42                  if (not emitted):
43                      #print('# While processing line: ' + line, end='')
44                      emitted = True
45                  #print('# matched expression: ' + patterns[i][0])
46                  #print('# clause 1 = ' + match.group(1))
47                  #print('# clause 2 = ' + match.group(2))
48                  line = match.group(1).ljust(column[i]) + match.group(2)
49          if (emitted):
50              print(line)
51          else:
52              print(line, end='')
53  
54  if __name__ == '__main__':
55      if (len(sys.argv) > 1):
56          realignXML(open(sys.argv[1], 'r', encoding='utf-8'))
57      else:
58          realignXML(sys.stdin)