SDL  2.0
sort_controllers.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Script to sort the game controller database entries in SDL_gamecontroller.c
4 
5 import re
6 
7 
8 filename = "SDL_gamecontrollerdb.h"
9 input = open(filename)
10 output = open(filename + ".new", "w")
11 parsing_controllers = False
12 controllers = []
13 controller_guids = {}
14 sdk_conditionals = []
15 split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
16 
17 def save_controller(line):
18  global controllers
19  match = split_pattern.match(line)
20  entry = [ match.group(1), match.group(2), match.group(3) ]
21  bindings = sorted(match.group(4).split(","))
22  if (bindings[0] == ""):
23  bindings.pop(0)
24  entry.extend(",".join(bindings) + ",")
25  entry.append(match.group(5))
26  controllers.append(entry)
27 
28  if ',sdk' in line:
29  sdk_conditionals.append(entry[1])
30 
32  global controllers
33  global controller_guids
34  # Check for duplicates
35  for entry in controllers:
36  if (entry[1] in controller_guids and entry[1] not in sdk_conditionals):
37  current_name = entry[2]
38  existing_name = controller_guids[entry[1]][2]
39  print("Warning: entry '%s' is duplicate of entry '%s'" % (current_name, existing_name))
40 
41  if (not current_name.startswith("(DUPE)")):
42  entry[2] = "(DUPE) " + current_name
43 
44  if (not existing_name.startswith("(DUPE)")):
45  controller_guids[entry[1]][2] = "(DUPE) " + existing_name
46 
47  controller_guids[entry[1]] = entry
48 
49  for entry in sorted(controllers, key=lambda entry: entry[2]+"-"+entry[1]):
50  line = "".join(entry) + "\n"
51  line = line.replace("\t", " ")
52  if not line.endswith(",\n") and not line.endswith("*/\n"):
53  print("Warning: '%s' is missing a comma at the end of the line" % (line))
54  output.write(line)
55 
56  controllers = []
57  controller_guids = {}
58 
59 for line in input:
60  if (parsing_controllers):
61  if (line.startswith("{")):
62  output.write(line)
63  elif (line.startswith(" NULL")):
64  parsing_controllers = False
66  output.write(line)
67  elif (line.startswith("#if")):
68  print("Parsing " + line.strip())
69  output.write(line)
70  elif (line.startswith("#endif")):
72  output.write(line)
73  else:
74  save_controller(line)
75  else:
76  if (line.startswith("static const char *s_ControllerMappings")):
77  parsing_controllers = True
78 
79  output.write(line)
80 
81 output.close()
82 print("Finished writing %s.new" % filename)
sort_controllers.write_controllers
def write_controllers()
Definition: sort_controllers.py:31
sort_controllers.save_controller
def save_controller(line)
Definition: sort_controllers.py:17