src/joystick/sort_controllers.py
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 29 Oct 2014 20:35:21 +0100
changeset 9203 874b59c43a3f
parent 8053 495f4a047be1
child 9205 5302aee2916a
permissions -rwxr-xr-x
Updated gamecontroller database sorting script to work with Python 3.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7422
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
#!/usr/bin/env python
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     2
#
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
# Script to sort the game controller database entries in SDL_gamecontroller.c
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
import re
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
filename = "SDL_gamecontrollerdb.h"
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
input = open(filename)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
output = open(filename + ".new", "w")
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
parsing_controllers = False
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
controllers = []
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
controller_guids = {}
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
split_pattern = re.compile(r'([^"]*")([^,]*,)([^,]*,)([^"]*)(".*)')
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
def save_controller(line):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
    global controllers
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    18
    match = split_pattern.match(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    19
    entry = [ match.group(1), match.group(2), match.group(3) ]
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    20
    bindings = sorted(match.group(4).split(","))
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    21
    if (bindings[0] == ""):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
        bindings.pop(0)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
    entry.extend(",".join(bindings) + ",")
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
    entry.append(match.group(5))
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
    controllers.append(entry)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
def write_controllers():
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    28
    global controllers
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    29
    global controller_guids
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    30
    for entry in sorted(controllers, key=lambda entry: entry[2]):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    31
        line = "".join(entry) + "\n"
8053
495f4a047be1 Fixed detecting the wired XBox 360 controller on Linux
Sam Lantinga <slouken@libsdl.org>
parents: 7422
diff changeset
    32
        if not line.endswith(",\n") and not line.endswith("*/\n"):
9203
874b59c43a3f Updated gamecontroller database sorting script to work with Python 3.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8053
diff changeset
    33
            print("Warning: '%s' is missing a comma at the end of the line" % (line))
7422
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    34
        if (entry[1] in controller_guids):
9203
874b59c43a3f Updated gamecontroller database sorting script to work with Python 3.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8053
diff changeset
    35
            print("Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][2]))
7422
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
        controller_guids[entry[1]] = entry
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
        output.write(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
    controllers = []
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
    controller_guids = {}
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
for line in input:
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
    if ( parsing_controllers ):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
        if (line.startswith("{")):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
            output.write(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
        elif (line.startswith("#endif")):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
            parsing_controllers = False
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
            write_controllers()
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
            output.write(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
        elif (line.startswith("#")):
9203
874b59c43a3f Updated gamecontroller database sorting script to work with Python 3.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8053
diff changeset
    51
            print("Parsing " + line.strip())
7422
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
            write_controllers()
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
            output.write(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
        else:
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
            save_controller(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    else:
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
        if (line.startswith("static const char *s_ControllerMappings")):
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
            parsing_controllers = True
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
        output.write(line)
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
172b0c6e51d6 Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
output.close()
9203
874b59c43a3f Updated gamecontroller database sorting script to work with Python 3.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 8053
diff changeset
    63
print("Finished writing %s.new" % filename)