def c2json(cli): """Generate a keymap.json from a keymap.c file. This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided. """ cli.args.filename = qmk.path.normpath(cli.args.filename) # Error checking if not cli.args.filename.exists(): cli.log.error('C file does not exist!') cli.print_usage() exit(1) if str(cli.args.filename) == '-': # TODO(skullydazed/anyone): Read file contents from STDIN cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() exit(1) # Environment processing if cli.args.output == ('-'): cli.args.output = None # Parse the keymap.c keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp) # Generate the keymap.json try: keymap_json = qmk.keymap.generate(keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers'], type='json', keymap=keymap_json['keymap']) except KeyError: cli.log.error('Something went wrong. Try to use --no-cpp.') sys.exit(1) if cli.args.output: cli.args.output.parent.mkdir(parents=True, exist_ok=True) if cli.args.output.exists(): cli.args.output.replace(cli.args.output.name + '.bak') cli.args.output.write_text(json.dumps(keymap_json)) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(json.dumps(keymap_json))
def c2json(cli): """Generate a keymap.json from a keymap.c file. This command uses the `qmk.keymap` module to generate a keymap.json from a keymap.c file. The generated keymap is written to stdout, or to a file if -o is provided. """ if cli.args.filename != '-': cli.args.filename = qmk.path.normpath(cli.args.filename) # Error checking if not cli.args.filename.exists(): cli.log.error('C file does not exist!') cli.print_usage() return False # Environment processing if cli.args.output == ('-'): cli.args.output = None # Parse the keymap.c keymap_json = qmk.keymap.c2json(cli.args.keyboard, cli.args.keymap, cli.args.filename, use_cpp=cli.args.no_cpp) # Generate the keymap.json try: keymap_json = qmk.keymap.generate_json(keymap_json['keymap'], keymap_json['keyboard'], keymap_json['layout'], keymap_json['layers']) except KeyError: cli.log.error('Something went wrong. Try to use --no-cpp.') return False if cli.args.output: cli.args.output.parent.mkdir(parents=True, exist_ok=True) if cli.args.output.exists(): cli.args.output.replace(cli.args.output.parent / (cli.args.output.name + '.bak')) cli.args.output.write_text(json.dumps(keymap_json, cls=InfoJSONEncoder)) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(json.dumps(keymap_json))
def json_keymap(cli): """Generate a keymap.c from a configurator export. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. """ # Error checking if cli.args.filename == ('-'): cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() exit(1) if not os.path.exists(qmk.path.normpath(cli.args.filename)): cli.log.error('JSON file does not exist!') cli.print_usage() exit(1) # Environment processing if cli.args.output == ('-'): cli.args.output = None # Parse the configurator json with open(qmk.path.normpath(cli.args.filename), 'r') as fd: user_keymap = json.load(fd) # Generate the keymap keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) if cli.args.output: output_dir = os.path.dirname(cli.args.output) if not os.path.exists(output_dir): os.makedirs(output_dir) output_file = qmk.path.normpath(cli.args.output) with open(output_file, 'w') as keymap_fd: keymap_fd.write(keymap_c) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(keymap_c)
def json2c(cli): """Generate a keymap.c from a configurator export. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. """ cli.args.filename = qmk.path.normpath(cli.args.filename) # Error checking if not cli.args.filename.exists(): cli.log.error('JSON file does not exist!') cli.print_usage() exit(1) if str(cli.args.filename) == '-': # TODO(skullydazed/anyone): Read file contents from STDIN cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() exit(1) # Environment processing if cli.args.output == ('-'): cli.args.output = None # Parse the configurator json with cli.args.filename.open('r') as fd: user_keymap = json.load(fd) # Generate the keymap keymap_c = qmk.keymap.generate(user_keymap['keyboard'], user_keymap['layout'], user_keymap['layers']) if cli.args.output: cli.args.output.parent.mkdir(parents=True, exist_ok=True) if cli.args.output.exists(): cli.args.output.replace(cli.args.output.name + '.bak') cli.args.output.write_text(keymap_c) if not cli.args.quiet: cli.log.info('Wrote keymap to %s.', cli.args.output) else: print(keymap_c)
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()
@cli.argument('-o', '--output', arg_only=True, type=qmk.path.normpath, help='File to write to') @cli.argument('-q', '--quiet', arg_only=True, action='store_true', help="Quiet mode, only output error messages") @cli.argument('filename', type=qmk.path.FileType('r'), arg_only=True, completer=FilesCompleter('.json'), help='Configurator JSON file') @cli.subcommand('Creates a keymap.c from a QMK Configurator export.') def json2c(cli): """Generate a keymap.c from a configurator export. This command uses the `qmk.keymap` module to generate a keymap.c from a configurator export. The generated keymap is written to stdout, or to a file if -o is provided. """ <<<<<<< HEAD <<<<<<< HEAD # Error checking if cli.args.filename and cli.args.filename.name == '-': # TODO(skullydazed/anyone): Read file contents from STDIN cli.log.error('Reading from STDIN is not (yet) supported.') cli.print_usage() return False if not cli.args.filename.exists(): cli.log.error('JSON file does not exist!') cli.print_usage() ======= ======= >>>>>>> acdcc622028a7c8e6ec086a5da2bff67fd137445 try: # Parse the configurator from json file (or stdin) user_keymap = json.load(cli.args.filename) except json.decoder.JSONDecodeError as ex: cli.log.error('The JSON input does not appear to be valid.')