Skip to content

[CLI] Refactor open into pathlib.Path.open #23105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/python/qmk/c_parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pygments import lex
from itertools import islice
from pathlib import Path
from typing import List
import re

from milc import cli
Expand Down Expand Up @@ -44,7 +45,7 @@ def strip_multiline_comment(string):
return multi_comment_regex.sub('', string)


def c_source_files(dir_names):
def c_source_files(dir_names: List[str]) -> List[Path]:
"""Returns a list of all *.c, *.h, and *.cpp files for a given list of directories

Args:
Expand Down
11 changes: 6 additions & 5 deletions lib/python/qmk/cli/chibios/confmigrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import sys
import os
from pathlib import Path

from qmk.constants import QMK_FIRMWARE
from qmk.path import normpath
Expand Down Expand Up @@ -39,8 +40,8 @@ def eprint(*args, **kwargs):
"""


def collect_defines(filepath):
with open(filepath, 'r', encoding='utf-8') as f:
def collect_defines(filepath: Path):
with filepath.open('r', encoding='utf-8') as f:
content = f.read()
define_search = re.compile(r'(?m)^#\s*define\s+(?:.*\\\r?\n)*.*$', re.MULTILINE)
value_search = re.compile(r'^#\s*define\s+(?P<name>[a-zA-Z0-9_]+(\([^\)]*\))?)\s*(?P<value>.*)', re.DOTALL)
Expand Down Expand Up @@ -146,17 +147,17 @@ def chibios_confmigrate(cli):
if cli.args.input.name == "chconf.h" and ("CHCONF_H" in input_defs["dict"] or "_CHCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_chconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
with cli.args.input.open("w", encoding='utf-8') as out_file:
migrate_chconf_h(to_override, outfile=out_file)

elif cli.args.input.name == "halconf.h" and ("HALCONF_H" in input_defs["dict"] or "_HALCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_halconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
with cli.args.input.open("w", encoding='utf-8') as out_file:
migrate_halconf_h(to_override, outfile=out_file)

elif cli.args.input.name == "mcuconf.h" and ("MCUCONF_H" in input_defs["dict"] or "_MCUCONF_H_" in input_defs["dict"] or cli.args.force):
migrate_mcuconf_h(to_override, outfile=sys.stdout)
if cli.args.overwrite:
with open(cli.args.input, "w", encoding='utf-8') as out_file:
with cli.args.input.open("w", encoding='utf-8') as out_file:
migrate_mcuconf_h(to_override, outfile=out_file)
2 changes: 1 addition & 1 deletion lib/python/qmk/cli/format/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def format_json(cli):
output = json.dumps(json_data, cls=json_encoder, sort_keys=True)

if cli.args.inplace:
with open(cli.args.json_file, 'w+', encoding='utf-8', newline='\n') as outfile:
with cli.args.json_file.open('w+', encoding='utf-8', newline='\n') as outfile:
outfile.write(output + '\n')

# Display the results if print was set
Expand Down
9 changes: 5 additions & 4 deletions lib/python/qmk/cli/lint.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Command to look over a keyboard/keymap and check for common mistakes.
"""
from pathlib import Path
from typing import List

from milc import cli

Expand Down Expand Up @@ -32,7 +33,7 @@ def _list_defaultish_keymaps(kb):
return keymaps


def _get_code_files(kb, km=None):
def _get_code_files(kb, km=None) -> List[Path]:
"""Return potential keyboard/keymap code files
"""
search_path = locate_keymap(kb, km).parent if km else keyboard(kb)
Expand All @@ -47,12 +48,12 @@ def _get_code_files(kb, km=None):
return code_files


def _has_license(file):
def _has_license(file: Path) -> bool:
"""Check file has a license header
"""
# Crude assumption that first line of license header is a comment
fline = open(file).readline().rstrip()
return fline.startswith(("/*", "//"))
with file.open() as f:
return f.readline().rstrip().startswith(("/*", "//"))


def _handle_json_errors(kb, info):
Expand Down
2 changes: 1 addition & 1 deletion lib/python/qmk/cli/mass_compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def mass_compile_targets(targets: List[BuildTarget], clean: bool, dry_run: bool,
cli.run([make_cmd, 'clean'], capture_output=False, stdin=DEVNULL)

builddir.mkdir(parents=True, exist_ok=True)
with open(makefile, "w") as f:
with makefile.open("w") as f:
for target in sorted(targets, key=lambda t: (t.keyboard, t.keymap)):
keyboard_name = target.keyboard
keymap_name = target.keymap
Expand Down
6 changes: 3 additions & 3 deletions lib/python/qmk/cli/painter/convert_graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def painter_convert_graphics(cli):

if cli.args.raw:
raw_file = cli.args.output / (cli.args.input.stem + ".qgf")
with open(raw_file, 'wb') as raw:
with raw_file.open('wb') as raw:
raw.write(out_bytes)
return

Expand All @@ -79,15 +79,15 @@ def painter_convert_graphics(cli):
# 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:
with header_file.open('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:
with source_file.open('w') as source:
print(f"Writing {source_file}...")
source.write(source_text)
source.close()
6 changes: 3 additions & 3 deletions lib/python/qmk/cli/painter/make_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def painter_convert_font_image(cli):

if cli.args.raw:
raw_file = cli.args.output / (cli.args.input.stem + ".qff")
with open(raw_file, 'wb') as raw:
with raw_file.open('wb') as raw:
raw.write(out_bytes)
return

Expand All @@ -81,15 +81,15 @@ def painter_convert_font_image(cli):
# 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:
with header_file.open('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:
with source_file.open('w') as source:
print(f"Writing {source_file}...")
source.write(source_text)
source.close()