File: gencontrollerdb.py

package info (click to toggle)
pyglet 2.0.17%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,560 kB
  • sloc: python: 80,579; xml: 50,988; ansic: 171; makefile: 146
file content (83 lines) | stat: -rwxr-xr-x 2,703 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3

import os
import datetime

from urllib.request import urlopen


# This script parses the SDL game controller db header file from the
# SDL mercurial repository, and generates a list of mappings to be used
# internally by pyglet. This script may need to be updated if the
# format or url changes.


# Get the raw database header file:
DB_URL = "https://hg.libsdl.org/SDL/raw-file/default/src/joystick/SDL_gamepad_db.h"
raw = urlopen(DB_URL).read().decode('ascii')

# Format the output directory for the pyglet module:
script_dir = os.path.abspath(os.path.dirname(__file__))
output_dir = os.path.join(script_dir, os.path.pardir, 'pyglet', 'input')
output_file = os.path.join(output_dir, 'controller_db.py')

# Parse the Windows section:
win_string = '#ifdef SDL_JOYSTICK_DINPUT'
win_start = raw.find(win_string) + len(win_string)
win_end = raw.find('#endif', win_start)
win_raw = raw[win_start:win_end]

# Parse the Mac OSX section:
mac_string = '#ifdef SDL_PLATFORM_MACOS'
mac_start = raw.find(mac_string) + len(mac_string)
mac_end = raw.find('#endif', mac_start)
mac_raw = raw[mac_start:mac_end]

# Parse the Linux section:
lin_string = '#if defined(SDL_JOYSTICK_LINUX) || defined(SDL_PLATFORM_OPENBSD)'
lin_start = raw.find(lin_string) + len(lin_string)
lin_end = raw.find('#endif', lin_start)
lin_raw = raw[lin_start:lin_end]


def strip_lines(raw_lines):
    """Strip invalid lines and C comments"""
    lines = []
    for line in raw_lines.splitlines():
        line = line.strip()
        if '"' in line:
            line = line.split('"')[1]
            lines.append(line)
    return lines


# Create lists of formatted lines:
win_lines = strip_lines(win_raw)
mac_lines = strip_lines(mac_raw)
lin_lines = strip_lines(lin_raw)


# Write out a valid Python module in the pyglet/input directory:
with open(output_file, 'w+') as f:
    f.write("from pyglet import compat_platform\n\n\n")
    f.write("# This file is automatically generated by 'pyglet/tools/gen_controller_db.py'\n")
    f.write("# Generated on: {0}\n\n".format(datetime.datetime.now().strftime('%c')))

    f.write("""if compat_platform.startswith("linux"):\n    mapping_list = [\n""")
    for lin_line in lin_lines:
        f.write(f'"{lin_line}",\n')
    f.write("]\n")

    f.write("""elif compat_platform.startswith("darwin"):\n    mapping_list = [\n""")
    for mac_line in mac_lines:
        f.write(f'"{mac_line}",\n')
    f.write("]\n")

    f.write("""elif compat_platform.startswith("win"):\n    mapping_list = [\n""")
    for win_line in win_lines:
        f.write(f'"{win_line}",\n')
    f.write("]\n")

    f.write("else:\n    mapping_list = []\n")

print("Saved 'pyglet/input/controller_db.py'")