Use match statements instead of if-elif chains

This commit is contained in:
ItsDrike 2023-02-02 21:32:53 +01:00
parent 81224d24e7
commit 6ce522f012
No known key found for this signature in database
GPG key ID: B014E761034AF742

80
sync.py
View file

@ -193,13 +193,14 @@ def print_report(diffs: Iterable[FileDiff]) -> None:
for diff in diffs: for diff in diffs:
_str_status = diff.status.name.replace("_", " ") _str_status = diff.status.name.replace("_", " ")
if diff.status is DiffStatus.MATCH: match diff.status:
case DiffStatus.MATCH:
status_str = (f"[green]{_str_status}[/green]") status_str = (f"[green]{_str_status}[/green]")
elif diff.status is DiffStatus.PERMISSION_ERROR: case DiffStatus.PERMISSION_ERROR:
status_str = f"[bold yellow]{_str_status}[/bold yellow]" status_str = f"[bold yellow]{_str_status}[/bold yellow]"
elif diff.status is DiffStatus.NOT_FOUND: case DiffStatus.NOT_FOUND:
status_str = f"[bold orange_red1]{_str_status}[/bold orange_red1]" status_str = f"[bold orange_red1]{_str_status}[/bold orange_red1]"
else: case _:
status_str = f"[bold red]{_str_status}[/bold red]" status_str = f"[bold red]{_str_status}[/bold red]"
try: try:
@ -244,82 +245,83 @@ class FixChoice(Enum):
def apply_fix(diff: FileDiff) -> None: def apply_fix(diff: FileDiff) -> None:
if diff.status is DiffStatus.PERMISSION_ERROR: match diff.status:
case DiffStatus.PERMISSION_ERROR:
print("Skipping fix: insufficient permissions") print("Skipping fix: insufficient permissions")
case DiffStatus.UNEXPECTED_DIRECTORY:
elif diff.status is DiffStatus.UNEXPECTED_DIRECTORY:
_choice = FixChoice.pick(diff.sys_file, "directory", "file") _choice = FixChoice.pick(diff.sys_file, "directory", "file")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
shutil.rmtree(diff.sys_file) shutil.rmtree(diff.sys_file)
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
diff.dot_file.unlink() diff.dot_file.unlink()
shutil.copytree(diff.sys_file, diff.dot_file, symlinks=True) shutil.copytree(diff.sys_file, diff.dot_file, symlinks=True)
case DiffStatus.UNEXPECTED_SYMLINK:
elif diff.status is DiffStatus.UNEXPECTED_SYMLINK:
_choice = FixChoice.pick(diff.sys_file, "symlink", "file") _choice = FixChoice.pick(diff.sys_file, "symlink", "file")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
diff.sys_file.unlink() diff.sys_file.unlink()
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
diff.dot_file.unlink() diff.dot_file.unlink()
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False) shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
case DiffStatus.EXPECTED_SYMLINK:
elif diff.status is DiffStatus.EXPECTED_SYMLINK:
_choice = FixChoice.pick(diff.sys_file, "file", "symlink") _choice = FixChoice.pick(diff.sys_file, "file", "symlink")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
diff.sys_file.unlink() diff.sys_file.unlink()
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
diff.dot_file.unlink() diff.dot_file.unlink()
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False) shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
case DiffStatus.SYMLINK_DIFFERS:
elif diff.status is DiffStatus.SYMLINK_DIFFERS:
_choice = FixChoice.pick(diff.sys_file, "symlink", "file") _choice = FixChoice.pick(diff.sys_file, "symlink", "file")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
diff.sys_file.unlink() diff.sys_file.unlink()
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
diff.dot_file.unlink() diff.dot_file.unlink()
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False) shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
case DiffStatus.NOT_FOUND:
elif diff.status is DiffStatus.NOT_FOUND:
_choice = FixChoice.pick(diff.sys_file, None, "file") _choice = FixChoice.pick(diff.sys_file, None, "file")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
diff.sys_file.parent.mkdir(parents=True, exist_ok=True) diff.sys_file.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
diff.dot_file.unlink() diff.dot_file.unlink()
case DiffStatus.CONTENT_DIFFERS:
elif diff.status is DiffStatus.CONTENT_DIFFERS:
_choice = FixChoice.pick(diff.sys_file, "file", "file") _choice = FixChoice.pick(diff.sys_file, "file", "file")
if _choice is FixChoice.SKIP: match _choice:
case FixChoice.SKIP:
return return
elif _choice is FixChoice.OVERWRITE_SYSTEM: case FixChoice.OVERWRITE_SYSTEM:
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False) shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
elif _choice is FixChoice.OVERWRITE_DOTFILE: case FixChoice.OVERWRITE_DOTFILE:
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False) shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
def show_diffs(diffs: Iterable[FileDiff], ask_show_diff: bool, apply_fix_prompt: bool) -> None: def show_diffs(diffs: Iterable[FileDiff], ask_show_diff: bool, apply_fix_prompt: bool) -> None:
for diff in diffs: for diff in diffs:
if diff.status is DiffStatus.MATCH: match diff.status:
case DiffStatus.MATCH:
continue continue
case DiffStatus.CONTENT_DIFFERS:
if diff.status is DiffStatus.CONTENT_DIFFERS:
if ask_show_diff is False or yes_no(f"Show diff for {diff.sys_file}?"): if ask_show_diff is False or yes_no(f"Show diff for {diff.sys_file}?"):
subprocess.run(["git", "diff", str(diff.sys_file), str(diff.dot_file)]) subprocess.run(["git", "diff", str(diff.sys_file), str(diff.dot_file)])
else: case _:
_str_status = diff.status.name.replace("_", " ") _str_status = diff.status.name.replace("_", " ")
print(f"Skipping {diff.sys_file} diff for status: {_str_status}") print(f"Skipping {diff.sys_file} diff for status: {_str_status}")