def cformat(cli): """Format C code according to QMK's style. """ # Empty array for files files = [] # Core directories for formatting core_dirs = ['drivers', 'quantum', 'tests', 'tmk_core', 'platforms'] ignores = [ 'tmk_core/protocol/usb_hid', 'quantum/template', 'platforms/chibios' ] # Find the list of files to format if cli.args.files: files.extend(normpath(file) for file in cli.args.files) # If -a is specified elif cli.args.all_files: all_files = c_source_files(core_dirs) # The following statement checks each file to see if the file path is in the ignored directories. files.extend(file for file in all_files if not any(i in str(file) for i in ignores)) # No files specified & no -a flag else: base_args = ['git', 'diff', '--name-only', cli.args.base_branch] out = subprocess.run(base_args + core_dirs, check=True, stdout=subprocess.PIPE) changed_files = filter(None, out.stdout.decode('UTF-8').split('\n')) filtered_files = [ normpath(file) for file in changed_files if not any(i in file for i in ignores) ] files.extend(file for file in filtered_files if file.exists() and file.suffix in ['.c', '.h', '.cpp']) # Run clang-format on the files we've found cformat_run(files, cli.args.all_files)
def painter_make_font_image(cli): # Create the font object font = QFFFont(cli) # Read from the input file cli.args.font = normpath(cli.args.font) font.generate_image(cli.args.font, cli.args.size, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs, use_aa=(False if cli.args.no_aa else True)) # Render out the data font.save_to_image(normpath(cli.args.output))
def painter_convert_font_image(cli): # Work out the format format = valid_formats[cli.args.format] # Create the font object font = QFFFont(cli.log) # Read from the input file cli.args.input = normpath(cli.args.input) font.read_from_image(cli.args.input, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs) # Work out the output directory if len(cli.args.output) == 0: cli.args.output = cli.args.input.parent cli.args.output = normpath(cli.args.output) # Render out the data out_data = BytesIO() font.save_to_qff(format, (False if cli.args.no_rle else True), out_data) # Work out the text substitutions for rendering the output data subs = { 'generated_type': 'font', 'var_prefix': 'font', 'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}', 'year': datetime.date.today().strftime("%Y"), 'input_file': cli.args.input.name, 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), 'byte_count': out_data.getbuffer().nbytes, 'bytes_lines': render_bytes(out_data.getbuffer().tobytes()), 'format': cli.args.format, } # Render the license subs.update({'license': render_license(subs)}) # Render and write the header file header_text = render_header(subs) header_file = cli.args.output / (cli.args.input.stem + ".qff.h") with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) header.close() # Render and write the source file source_text = render_source(subs) source_file = cli.args.output / (cli.args.input.stem + ".qff.c") with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) source.close()
def filter_files(files): """Yield only files to be formatted and skip the rest """ for file in files: if file and normpath(file).name.split('.')[-1] in py_file_suffixes: yield file else: cli.log.debug('Skipping file %s', file)
def generate_info_json(cli): """Generate an info.json file for a keyboard """ # Determine our keyboard(s) if not cli.config.generate_info_json.keyboard: cli.log.error('Missing parameter: --keyboard') cli.subcommands['info'].print_help() return False if not is_keyboard(cli.config.generate_info_json.keyboard): cli.log.error('Invalid keyboard: "%s"', cli.config.generate_info_json.keyboard) return False if cli.args.overwrite: output_path = (Path('keyboards') / cli.config.generate_info_json.keyboard / 'info.json').resolve() if cli.args.output: cli.log.warning('Overwriting user supplied --output with %s', output_path) cli.args.output = output_path # Build the info.json file kb_info_json = info_json(cli.config.generate_info_json.keyboard) strip_info_json(kb_info_json) info_json_text = json.dumps(kb_info_json, indent=4, cls=InfoJSONEncoder) if cli.args.output: # Write to a file output_path = normpath(cli.args.output) if output_path.exists(): cli.log.warning('Overwriting output file %s', output_path) output_path.write_text(info_json_text + '\n') cli.log.info('Wrote info.json to %s.', output_path) else: # Display the results print(info_json_text)
def painter_convert_graphics(cli): """Converts an image file to a format that Quantum Painter understands. This command uses the `qmk.painter` module to generate a Quantum Painter image defintion from an image. The generated definitions are written to a files next to the input -- `INPUT.c` and `INPUT.h`. """ # Work out the input file if cli.args.input != '-': cli.args.input = normpath(cli.args.input) # Error checking if not cli.args.input.exists(): cli.log.error('Input image file does not exist!') cli.print_usage() return False # Work out the output directory if len(cli.args.output) == 0: cli.args.output = cli.args.input.parent cli.args.output = normpath(cli.args.output) # Ensure we have a valid format if cli.args.format not in valid_formats.keys(): cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys()))) cli.print_usage() return False # Work out the encoding parameters format = valid_formats[cli.args.format] # Load the input image input_img = Image.open(cli.args.input) # Convert the image to QGF using PIL out_data = BytesIO() input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose) out_bytes = out_data.getvalue() # Work out the text substitutions for rendering the output data subs = { 'generated_type': 'image', 'var_prefix': 'gfx', 'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}', 'year': datetime.date.today().strftime("%Y"), 'input_file': cli.args.input.name, 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), 'byte_count': len(out_bytes), 'bytes_lines': render_bytes(out_bytes), 'format': cli.args.format, } # Render the license subs.update({'license': render_license(subs)}) # Render and write the header file header_text = render_header(subs) header_file = cli.args.output / (cli.args.input.stem + ".qgf.h") with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) header.close() # Render and write the source file source_text = render_source(subs) source_file = cli.args.output / (cli.args.input.stem + ".qgf.c") with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) source.close()
def is_relative_to(file, other): """Provide similar behavior to PurePath.is_relative_to in Python > 3.9 """ return str(normpath(file).resolve()).startswith( str(normpath(other).resolve()))