Make --empty delete dangling files in trash folder not associated with metadata file, edit completer function's name to be reusable
This commit is contained in:
@@ -269,20 +269,23 @@ def restore(identifier: str):
|
|||||||
|
|
||||||
def empty_trash():
|
def empty_trash():
|
||||||
"""Permanently remove all trashed files and clear metadata."""
|
"""Permanently remove all trashed files and clear metadata."""
|
||||||
|
|
||||||
|
# Remove everything inside the trash directory
|
||||||
count = 0
|
count = 0
|
||||||
for entry in list(meta):
|
for item in TRASH_DIR.iterdir():
|
||||||
f = TRASH_DIR / entry["id"]
|
|
||||||
try:
|
try:
|
||||||
if f.exists():
|
if item.is_dir():
|
||||||
if f.is_dir():
|
shutil.rmtree(item, ignore_errors=True)
|
||||||
shutil.rmtree(f, ignore_errors=True)
|
else:
|
||||||
else:
|
item.unlink(missing_ok=True)
|
||||||
f.unlink(missing_ok=True)
|
|
||||||
meta.remove(entry)
|
|
||||||
count += 1
|
count += 1
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Failed to remove {f}: {e}")
|
print(f"Failed to remove {item}: {e}")
|
||||||
|
|
||||||
|
# Clear metadata
|
||||||
|
meta.clear()
|
||||||
save_meta(meta)
|
save_meta(meta)
|
||||||
|
|
||||||
print(f"Trash emptied ({count} entries removed).")
|
print(f"Trash emptied ({count} entries removed).")
|
||||||
|
|
||||||
|
|
||||||
@@ -436,7 +439,7 @@ def inspect_entry(identifier: str):
|
|||||||
group = st.st_gid
|
group = st.st_gid
|
||||||
owner = f"{user}:{group}"
|
owner = f"{user}:{group}"
|
||||||
|
|
||||||
# Size (bytes for file, recursive for directories optional)
|
# Size (bytes for file, recursive for directories)
|
||||||
size = st.st_size
|
size = st.st_size
|
||||||
|
|
||||||
print(f"Type: {ftype}")
|
print(f"Type: {ftype}")
|
||||||
@@ -445,7 +448,7 @@ def inspect_entry(identifier: str):
|
|||||||
print(f"Ownership: {owner}")
|
print(f"Ownership: {owner}")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Unknown stats for {e})")
|
print(f"Unknown stats for {e}")
|
||||||
|
|
||||||
|
|
||||||
def main(argv: Optional[List[str]] = None):
|
def main(argv: Optional[List[str]] = None):
|
||||||
@@ -461,7 +464,7 @@ def main(argv: Optional[List[str]] = None):
|
|||||||
"--skip-trash", action="store_true", help="permanent delete"
|
"--skip-trash", action="store_true", help="permanent delete"
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
inspect_arg = parser.add_argument(
|
||||||
"--inspect",
|
"--inspect",
|
||||||
"-I",
|
"-I",
|
||||||
nargs="+",
|
nargs="+",
|
||||||
@@ -476,8 +479,8 @@ def main(argv: Optional[List[str]] = None):
|
|||||||
help="restore by id or basename",
|
help="restore by id or basename",
|
||||||
)
|
)
|
||||||
|
|
||||||
# restore completer
|
# completer
|
||||||
def restore_completer(prefix, parsed_args, **kwargs):
|
def id_name_completer(prefix, parsed_args, **kwargs):
|
||||||
return [
|
return [
|
||||||
short_id(m["id"])
|
short_id(m["id"])
|
||||||
for m in meta
|
for m in meta
|
||||||
@@ -488,7 +491,8 @@ def main(argv: Optional[List[str]] = None):
|
|||||||
if Path(m["orig_path"]).name.startswith(prefix)
|
if Path(m["orig_path"]).name.startswith(prefix)
|
||||||
]
|
]
|
||||||
|
|
||||||
restore_arg.completer = restore_completer
|
restore_arg.completer = id_name_completer
|
||||||
|
inspect_arg.completer = id_name_completer
|
||||||
parser.add_argument("-l", "--list", action="store_true", help="list trash")
|
parser.add_argument("-l", "--list", action="store_true", help="list trash")
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--empty", action="store_true", help="empty the trash permanently"
|
"--empty", action="store_true", help="empty the trash permanently"
|
||||||
|
|||||||
Reference in New Issue
Block a user