commit d24dbe5a5bea1f9f67aa22a726b6b5fb22417fcc Author: Lucas Faber Date: Tue Apr 14 23:53:03 2026 +0200 serverLibary diff --git a/serverLibary/__init__.py b/serverLibary/__init__.py new file mode 100644 index 0000000..0fae776 --- /dev/null +++ b/serverLibary/__init__.py @@ -0,0 +1,3 @@ + +from .core import safe_rename +from .core import safe_delete diff --git a/serverLibary/core.py b/serverLibary/core.py new file mode 100644 index 0000000..4a57986 --- /dev/null +++ b/serverLibary/core.py @@ -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) + diff --git a/serverLibary/pyproject.toml b/serverLibary/pyproject.toml new file mode 100644 index 0000000..098b04b --- /dev/null +++ b/serverLibary/pyproject.toml @@ -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"