From f67a2715c579b123ac02bb194cdc05ff4dfb1543 Mon Sep 17 00:00:00 2001 From: dormieriancitizen Date: Fri, 10 Oct 2025 17:17:00 -0400 Subject: [PATCH 1/2] Pathlib initial changes --- dexi/commands/installer.py | 45 ++++++++++++++++++++------------------ dexi/core/errors.py | 6 ++--- dexi/core/utils.py | 41 +++++++++++++++++++--------------- 3 files changed, 50 insertions(+), 42 deletions(-) diff --git a/dexi/commands/installer.py b/dexi/commands/installer.py index f94f384..a76a614 100644 --- a/dexi/commands/installer.py +++ b/dexi/commands/installer.py @@ -1,5 +1,6 @@ import io import os +from pathlib import Path import random import shutil import zipfile @@ -50,9 +51,9 @@ def uninstall_package(package: str): if data.app is not None and not app_operations_supported(): return - desination = f"{os.getcwd()}/ballsdex/packages/{data.package.target}" + destination = Path.cwd() / "ballsdex" / "packages" / data.package.target - if not os.path.isdir(desination): + if not destination.is_dir(): return if data.app is not None: @@ -65,7 +66,7 @@ def uninstall_package(package: str): remove_list_entry("packages", f"ballsdex.packages.{data.package.target}") - shutil.rmtree(desination) + shutil.rmtree(destination) def install_package( @@ -93,16 +94,16 @@ def install_package( author, repository = repository.split("/") zip_url = f"https://github.com/{author}/{repository}/archive/refs/heads/{branch}.zip" - desination = f"{os.getcwd()}/ballsdex/packages/{data.package.target}" + destination = Path.cwd() / "ballsdex" / "packages" / data.package.target name = package_name(repository, branch) - if os.path.isdir(desination): + if destination.is_dir(): if cancel_if_exists: return False replaced = True - shutil.rmtree(desination) + shutil.rmtree(destination) if data.app is not None: if not app_operations_supported(): @@ -111,15 +112,15 @@ def install_package( f"on Ballsdex v$BD_V, please update to v{SUPPORTED_APP_VERSION}+" ) - app_desination = f"{os.getcwd()}/admin_panel/{data.app.target}" + app_destination = Path.cwd() / "admin_panel" / data.app.target - if os.path.isdir(app_desination): + if app_destination.is_dir(): replaced = True - shutil.rmtree(app_desination) + shutil.rmtree(app_destination) - os.makedirs(app_desination, exist_ok=True) + app_destination.mkdir(parents=True, exist_ok=True) - os.makedirs(desination, exist_ok=True) + destination.mkdir(parents=True, exist_ok=True) response = requests.get(zip_url) @@ -131,10 +132,7 @@ def install_package( for member in z.namelist(): if member[-7:] in ["LICENSE", "LICENCE"]: - with ( - z.open(member) as src, - open(f"{desination}/{member[-7:]}", "wb") as dst, - ): + with z.open(member) as src, (destination / member[-7:]).open("wb") as dst: shutil.copyfileobj(src, dst) continue @@ -147,7 +145,7 @@ def install_package( if not relative_path or relative_path in data.package.exclude: continue - target_path = os.path.join(desination, relative_path) + target_path = destination / relative_path if member.endswith("/"): os.makedirs(target_path, exist_ok=True) @@ -159,7 +157,12 @@ def install_package( shutil.copyfileobj(src, dst) if data.app is not None: # I'll refactor this later - app_desination = cast(str, app_desination) # type: ignore + if not app_destination: + # something has gone remarkably wrong + # (shouldnt be possible) + raise Exception( + "Somehow app_destination has gone missing while copying files" + ) for member in z.namelist(): if not member.startswith(f"{base_folder}{data.app.source}/"): @@ -170,15 +173,15 @@ def install_package( if not relative_path: continue - target_path = os.path.join(app_desination, relative_path) + target_path = app_destination / relative_path if member.endswith("/"): - os.makedirs(target_path, exist_ok=True) + target_path.mkdir(parents=True, exist_ok=True) continue - os.makedirs(os.path.dirname(target_path), exist_ok=True) + target_path.parent.mkdir(parents=True, exist_ok=True) - with z.open(member) as src, open(target_path, "wb") as dst: + with z.open(member) as src, target_path.open("wb") as dst: shutil.copyfileobj(src, dst) add_list_entry( diff --git a/dexi/core/errors.py b/dexi/core/errors.py index 89cd1e0..3400235 100644 --- a/dexi/core/errors.py +++ b/dexi/core/errors.py @@ -1,9 +1,9 @@ -import os from dataclasses import dataclass, field from packaging.version import parse as parse_version from .utils import error, fetch_ballsdex_version +from pathlib import Path SUPPORTED_VERSION = "2.22.0" @@ -18,14 +18,14 @@ class Errors: @staticmethod def invalid_project() -> None: - if os.path.isdir("ballsdex") and os.path.isfile("pyproject.toml"): + if Path("ballsdex").is_dir() and Path("pyproject.toml").is_file(): return error("Attempted to use DexI command on an invalid project") @staticmethod def no_config_found() -> None: - if os.path.isfile("config.yml"): + if Path("config.yml").is_file(): return error("No 'config.yml' file detected") diff --git a/dexi/core/utils.py b/dexi/core/utils.py index 8528a9d..c2b6906 100644 --- a/dexi/core/utils.py +++ b/dexi/core/utils.py @@ -1,4 +1,5 @@ import os +from pathlib import Path import re import sys from typing import cast @@ -50,7 +51,7 @@ def fetch_pyproject(package: str, branch: str) -> dict: return data -def parse_pyproject(path: str | None = None) -> TOMLDocument: +def parse_pyproject(path: Path | None = None) -> TOMLDocument: """ Parses a pyproject file and returns it. @@ -60,14 +61,14 @@ def parse_pyproject(path: str | None = None) -> TOMLDocument: The path that holds the pyproject file. """ if path is None: - path = os.getcwd() + path = Path.cwd() - path = f"{path}/pyproject.toml" + path = path / "pyproject.toml" - if not os.path.isfile(path): + if not path.is_file(): error("Failed to find [red]pyproject.toml[/red] in the current directory") - with open(path) as file: + with path.open() as file: return parse(file.read()) @@ -78,7 +79,7 @@ def app_operations_supported() -> bool: return parse_version(fetch_ballsdex_version()) >= parse_version(SUPPORTED_APP_VERSION) -def fetch_ballsdex_version(path: str | None = None) -> str: +def fetch_ballsdex_version(path: Path | None = None) -> str: """ Returns the Ballsdex version. @@ -88,14 +89,14 @@ def fetch_ballsdex_version(path: str | None = None) -> str: The path that will be checked. """ if path is None: - path = os.getcwd() + path = Path.cwd() - path = f"{path}/ballsdex/__init__.py" + path = path / "ballsdex/__init__.py" - if not os.path.isfile(path): + if not path.is_file(): error("Failed to find [red]ballsdex/__init__.py[/red] in the current directory") - with open(path) as file: + with path.open() as file: return file.read().replace('__version__ = "', "").rstrip()[:-1] @@ -150,7 +151,7 @@ def fetch_all_packages() -> list[PackageEntry]: return cast(list[PackageEntry], packages) -def add_list_entry(section: str, entry: str, path: str | None = None): +def add_list_entry(section: str, entry: str, path: Path | None = None): """ Adds an item to a list in the config file. @@ -164,9 +165,11 @@ def add_list_entry(section: str, entry: str, path: str | None = None): The config file path. """ if path is None: - path = os.getcwd() + path = Path.cwd() - with open(f"{path}/config.yml") as file: + path = path / "config.yml" + + with path.open() as file: lines = file.readlines() item = f" - {entry}\n" @@ -179,11 +182,11 @@ def add_list_entry(section: str, entry: str, path: str | None = None): lines.insert(i + 1, item) break - with open(f"{path}/config.yml", "w") as file: + with path.open("w") as file: file.writelines(lines) -def remove_list_entry(section: str, entry: str, path: str | None = None): +def remove_list_entry(section: str, entry: str, path: Path | None = None): """ Removes an item from a list in the config file. @@ -197,9 +200,11 @@ def remove_list_entry(section: str, entry: str, path: str | None = None): The config file path. """ if path is None: - path = os.getcwd() + path = Path.cwd() + + path = path / "config.yml" - with open(f"{path}/config.yml") as file: + with path.open() as file: lines = file.readlines() item = f" - {entry}\n" @@ -209,7 +214,7 @@ def remove_list_entry(section: str, entry: str, path: str | None = None): lines.remove(item) - with open(f"{path}/config.yml", "w") as file: + with path.open("w") as file: file.writelines(lines) From cfe75724f43811291f7828aa1258293ba5e41100 Mon Sep 17 00:00:00 2001 From: dormieriancitizen Date: Fri, 10 Oct 2025 17:27:23 -0400 Subject: [PATCH 2/2] I forgot to ruff --- dexi/commands/installer.py | 2 +- dexi/core/errors.py | 2 +- dexi/core/utils.py | 3 +-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/dexi/commands/installer.py b/dexi/commands/installer.py index a76a614..704d43e 100644 --- a/dexi/commands/installer.py +++ b/dexi/commands/installer.py @@ -1,9 +1,9 @@ import io import os -from pathlib import Path import random import shutil import zipfile +from pathlib import Path from typing import cast import requests diff --git a/dexi/core/errors.py b/dexi/core/errors.py index 3400235..74e044f 100644 --- a/dexi/core/errors.py +++ b/dexi/core/errors.py @@ -1,9 +1,9 @@ from dataclasses import dataclass, field +from pathlib import Path from packaging.version import parse as parse_version from .utils import error, fetch_ballsdex_version -from pathlib import Path SUPPORTED_VERSION = "2.22.0" diff --git a/dexi/core/utils.py b/dexi/core/utils.py index c2b6906..53a3b5d 100644 --- a/dexi/core/utils.py +++ b/dexi/core/utils.py @@ -1,7 +1,6 @@ -import os -from pathlib import Path import re import sys +from pathlib import Path from typing import cast import requests