serverLibary
This commit is contained in:
commit
d24dbe5a5b
3 files changed
+63
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
|
||||||
|
from .core import safe_rename
|
||||||
|
from .core import safe_delete
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
|
||||||
|
from pathlib import Path
|
||||||
|
import shutil
|
||||||
|
|
||||||
|
# -----------------------------
|
||||||
|
# Helpers
|
||||||
|
# -----------------------------
|
||||||
|
def safe_delete(path: Path):
|
||||||
|
if path.is_dir():
|
||||||
|
shutil.rmtree(path)
|
||||||
|
else:
|
||||||
|
path.unlink()
|
||||||
|
|
||||||
|
def safe_rename(src: Path, dst: Path, dry: bool=False, confirm: bool=False):
|
||||||
|
"""
|
||||||
|
Safely rename a file or directory without overwriting existing files.
|
||||||
|
Automatically appends _1, _2, ... if needed.
|
||||||
|
"""
|
||||||
|
|
||||||
|
src = Path(src)
|
||||||
|
dst = Path(dst)
|
||||||
|
|
||||||
|
# nothing to do
|
||||||
|
if src.resolve() == dst.resolve():
|
||||||
|
return
|
||||||
|
|
||||||
|
directory = dst.parent
|
||||||
|
base = dst.stem
|
||||||
|
ext = dst.suffix
|
||||||
|
|
||||||
|
final_dst = dst
|
||||||
|
counter = 1
|
||||||
|
|
||||||
|
# find free name
|
||||||
|
while final_dst.exists():
|
||||||
|
final_dst = directory / f"{base}_{counter}{ext}"
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
print(f"{src} → {dst}")
|
||||||
|
|
||||||
|
if dry:
|
||||||
|
return
|
||||||
|
|
||||||
|
if confirm:
|
||||||
|
input("Press Enter to confirm...")
|
||||||
|
|
||||||
|
dst.parent.mkdir(parents=True, exist_ok=True)
|
||||||
|
src.rename(dst)
|
||||||
|
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
[build-system]
|
||||||
|
requires = ["setuptools", "wheel"]
|
||||||
|
build-backend = "setuptools.build_meta"
|
||||||
|
|
||||||
|
[project]
|
||||||
|
name = "serverLibary"
|
||||||
|
version = "0.1.0"
|
||||||
|
description = "Serverhost Python library"
|
||||||
|
authors = [{ name = "Lucas" }]
|
||||||
|
readme = "README.md"
|
||||||
|
requires-python = ">=3.10"
|
||||||
Reference in new issue
Block a user