mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2024-11-10 02:39:40 +00:00
Use match statements instead of if-elif chains
This commit is contained in:
parent
81224d24e7
commit
6ce522f012
166
sync.py
166
sync.py
|
@ -193,14 +193,15 @@ 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:
|
||||||
status_str = (f"[green]{_str_status}[/green]")
|
case DiffStatus.MATCH:
|
||||||
elif diff.status is DiffStatus.PERMISSION_ERROR:
|
status_str = (f"[green]{_str_status}[/green]")
|
||||||
status_str = f"[bold yellow]{_str_status}[/bold yellow]"
|
case DiffStatus.PERMISSION_ERROR:
|
||||||
elif diff.status is DiffStatus.NOT_FOUND:
|
status_str = f"[bold yellow]{_str_status}[/bold yellow]"
|
||||||
status_str = f"[bold orange_red1]{_str_status}[/bold orange_red1]"
|
case DiffStatus.NOT_FOUND:
|
||||||
else:
|
status_str = f"[bold orange_red1]{_str_status}[/bold orange_red1]"
|
||||||
status_str = f"[bold red]{_str_status}[/bold red]"
|
case _:
|
||||||
|
status_str = f"[bold red]{_str_status}[/bold red]"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Unexpand home (/home/xyz/foo -> ~/foo)
|
# Unexpand home (/home/xyz/foo -> ~/foo)
|
||||||
|
@ -244,84 +245,85 @@ 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:
|
||||||
print("Skipping fix: insufficient permissions")
|
case DiffStatus.PERMISSION_ERROR:
|
||||||
|
print("Skipping fix: insufficient permissions")
|
||||||
elif diff.status is DiffStatus.UNEXPECTED_DIRECTORY:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
shutil.rmtree(diff.sys_file)
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
shutil.rmtree(diff.sys_file)
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
diff.dot_file.unlink()
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
shutil.copytree(diff.sys_file, diff.dot_file, symlinks=True)
|
diff.dot_file.unlink()
|
||||||
|
shutil.copytree(diff.sys_file, diff.dot_file, symlinks=True)
|
||||||
elif diff.status is DiffStatus.UNEXPECTED_SYMLINK:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
diff.sys_file.unlink()
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
diff.sys_file.unlink()
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
diff.dot_file.unlink()
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
diff.dot_file.unlink()
|
||||||
|
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
||||||
elif diff.status is DiffStatus.EXPECTED_SYMLINK:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
diff.sys_file.unlink()
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
diff.sys_file.unlink()
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
diff.dot_file.unlink()
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
diff.dot_file.unlink()
|
||||||
|
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
||||||
elif diff.status is DiffStatus.SYMLINK_DIFFERS:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
diff.sys_file.unlink()
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
diff.sys_file.unlink()
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
diff.dot_file.unlink()
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
diff.dot_file.unlink()
|
||||||
|
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
||||||
elif diff.status is DiffStatus.NOT_FOUND:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
diff.sys_file.parent.mkdir(parents=True, exist_ok=True)
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
diff.sys_file.parent.mkdir(parents=True, exist_ok=True)
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
diff.dot_file.unlink()
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
|
diff.dot_file.unlink()
|
||||||
elif diff.status is DiffStatus.CONTENT_DIFFERS:
|
case 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:
|
||||||
return
|
case FixChoice.SKIP:
|
||||||
elif _choice is FixChoice.OVERWRITE_SYSTEM:
|
return
|
||||||
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
case FixChoice.OVERWRITE_SYSTEM:
|
||||||
elif _choice is FixChoice.OVERWRITE_DOTFILE:
|
shutil.copy(diff.dot_file, diff.sys_file, follow_symlinks=False)
|
||||||
shutil.copy(diff.sys_file, diff.dot_file, follow_symlinks=False)
|
case FixChoice.OVERWRITE_DOTFILE:
|
||||||
|
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:
|
||||||
continue
|
case DiffStatus.MATCH:
|
||||||
|
continue
|
||||||
if diff.status is DiffStatus.CONTENT_DIFFERS:
|
case 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}")
|
||||||
|
|
||||||
if apply_fix_prompt:
|
if apply_fix_prompt:
|
||||||
apply_fix(diff)
|
apply_fix(diff)
|
||||||
|
|
Loading…
Reference in a new issue