godot/core/input/input_builders.py
Rémi Verschelde cd4e46ee65 SCons: Format buildsystem files with psf/black
Configured for a max line length of 120 characters.

psf/black is very opinionated and purposely doesn't leave much room for
configuration. The output is mostly OK so that should be fine for us,
but some things worth noting:

- Manually wrapped strings will be reflowed, so by using a line length
  of 120 for the sake of preserving readability for our long command
  calls, it also means that some manually wrapped strings are back on
  the same line and should be manually merged again.

- Code generators using string concatenation extensively look awful,
  since black puts each operand on a single line. We need to refactor
  these generators to use more pythonic string formatting, for which
  many options are available (`%`, `format` or f-strings).

- CI checks and a pre-commit hook will be added to ensure that future
  buildsystem changes are well-formatted.
2020-03-30 09:05:53 +02:00

82 lines
2.9 KiB
Python

"""Functions used to generate source files during build time
All such functions are invoked in a subprocess on Windows to prevent build flakiness.
"""
from platform_methods import subprocess_main
from collections import OrderedDict
def make_default_controller_mappings(target, source, env):
dst = target[0]
g = open(dst, "w")
g.write("/* THIS FILE IS GENERATED DO NOT EDIT */\n")
g.write('#include "core/typedefs.h"\n')
g.write('#include "core/input/default_controller_mappings.h"\n')
# ensure mappings have a consistent order
platform_mappings = OrderedDict()
for src_path in source:
with open(src_path, "r") as f:
# read mapping file and skip header
mapping_file_lines = f.readlines()[2:]
current_platform = None
for line in mapping_file_lines:
if not line:
continue
line = line.strip()
if len(line) == 0:
continue
if line[0] == "#":
current_platform = line[1:].strip()
if current_platform not in platform_mappings:
platform_mappings[current_platform] = {}
elif current_platform:
line_parts = line.split(",")
guid = line_parts[0]
if guid in platform_mappings[current_platform]:
g.write(
"// WARNING - DATABASE {} OVERWROTE PRIOR MAPPING: {} {}\n".format(
src_path, current_platform, platform_mappings[current_platform][guid]
)
)
valid_mapping = True
for input_map in line_parts[2:]:
if "+" in input_map or "-" in input_map or "~" in input_map:
g.write(
"// WARNING - DISCARDED UNSUPPORTED MAPPING TYPE FROM DATABASE {}: {} {}\n".format(
src_path, current_platform, line
)
)
valid_mapping = False
break
if valid_mapping:
platform_mappings[current_platform][guid] = line
platform_variables = {
"Linux": "#if X11_ENABLED",
"Windows": "#ifdef WINDOWS_ENABLED",
"Mac OS X": "#ifdef OSX_ENABLED",
"Android": "#if defined(__ANDROID__)",
"iOS": "#ifdef IPHONE_ENABLED",
"Javascript": "#ifdef JAVASCRIPT_ENABLED",
"UWP": "#ifdef UWP_ENABLED",
}
g.write("const char* DefaultControllerMappings::mappings[] = {\n")
for platform, mappings in platform_mappings.items():
variable = platform_variables[platform]
g.write("{}\n".format(variable))
for mapping in mappings.values():
g.write('\t"{}",\n'.format(mapping))
g.write("#endif\n")
g.write("\tNULL\n};\n")
g.close()
if __name__ == "__main__":
subprocess_main(globals())