src/joystick/sort_controllers.py
author Sam Lantinga <slouken@libsdl.org>
Fri, 12 Jul 2013 10:44:55 -0700
changeset 7422 172b0c6e51d6
child 8053 495f4a047be1
permissions -rwxr-xr-x
Moved the game controller database to a separate file and added a script to sort the entries so we can easily check for duplicates
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"
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
    32
        if (entry[1] in 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
    33
            print "Warning: entry '%s' is duplicate of entry '%s'" % (entry[2], controller_guids[entry[1]][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
    34
        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
    35
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
        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
    37
    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
    38
    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
    39
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
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
    41
    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
    42
        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
    43
            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
    44
        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
    45
            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
    46
            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
    47
            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
    48
        elif (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
    49
            print "Parsing " + line.strip()
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
            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
    51
            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
    52
        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
    53
            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
    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
        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
    56
            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
    57
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
        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
    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.close()
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
print "Finished writing %s.new" % filename