From 981f560fa334ba52e9a2a45c702f23d971c9dcca Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Wed, 20 May 2026 15:02:58 +0200 Subject: [PATCH] Consolidate handling of resources paths * constants.py should remain the central place where a root path is determined based on source file location * SerenaPaths should consistently use strings --- src/serena/config/serena_config.py | 11 ++++++++--- src/serena/constants.py | 20 +++++++++++--------- src/serena/memories/memory_manager.py | 2 +- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/src/serena/config/serena_config.py b/src/serena/config/serena_config.py index e2a38621..ff63f523 100644 --- a/src/serena/config/serena_config.py +++ b/src/serena/config/serena_config.py @@ -26,6 +26,7 @@ from serena.constants import ( PROJECT_LOCAL_TEMPLATE_FILE, PROJECT_TEMPLATE_FILE, REPO_ROOT, + RESOURCES_DIR, SERENA_CONFIG_TEMPLATE_FILE, SERENA_FILE_ENCODING, SERENA_MANAGED_DIR_NAME, @@ -61,9 +62,10 @@ class SerenaPaths: home_dir = str(Path.home() / SERENA_MANAGED_DIR_NAME) else: home_dir = home_dir.strip() - self.serena_pkg_path = Path(__file__).parent.parent.resolve() - self.serena_resources_path = self.serena_pkg_path / "resources" - + self.resources_dir: str = RESOURCES_DIR + """ + the resources directory (within the `serena` package) + """ self.serena_user_home_dir: str = home_dir """ the path to the Serena home directory, where the user's configuration/data is stored. @@ -128,6 +130,9 @@ class SerenaPaths: self.last_returned_log_file_path = os.path.join(log_dir, prefix + "_" + datetime_tag() + f"_{os.getpid()}" + ".txt") return self.last_returned_log_file_path + def get_resource_path(self, *path_elems: str) -> Path: + return Path(os.path.join(self.resources_dir, *path_elems)) + # TODO: Paths from constants.py should be moved here diff --git a/src/serena/constants.py b/src/serena/constants.py index db4d1b35..043e0b07 100644 --- a/src/serena/constants.py +++ b/src/serena/constants.py @@ -2,20 +2,22 @@ from pathlib import Path _repo_root_path = Path(__file__).parent.parent.parent.resolve() _serena_pkg_path = Path(__file__).parent.resolve() +_resources_path = _serena_pkg_path / "resources" SERENA_MANAGED_DIR_NAME = ".serena" # TODO: Path-related constants should be moved to SerenaPaths; don't add further constants here. REPO_ROOT = str(_repo_root_path) -PROMPT_TEMPLATES_DIR_INTERNAL = str(_serena_pkg_path / "resources" / "config" / "prompt_templates") -SERENAS_OWN_CONTEXT_YAMLS_DIR = str(_serena_pkg_path / "resources" / "config" / "contexts") +RESOURCES_DIR = str(_resources_path) +PROMPT_TEMPLATES_DIR_INTERNAL = str(_resources_path / "config" / "prompt_templates") +SERENAS_OWN_CONTEXT_YAMLS_DIR = str(_resources_path / "config" / "contexts") """The contexts that are shipped with the Serena package, i.e. the default contexts.""" -SERENAS_OWN_MODE_YAMLS_DIR = str(_serena_pkg_path / "resources" / "config" / "modes") +SERENAS_OWN_MODE_YAMLS_DIR = str(_resources_path / "config" / "modes") """The modes that are shipped with the Serena package, i.e. the default modes.""" -INTERNAL_MODE_YAMLS_DIR = str(_serena_pkg_path / "resources" / "config" / "internal_modes") +INTERNAL_MODE_YAMLS_DIR = str(_resources_path / "config" / "internal_modes") """Internal modes, never overridden by user modes.""" -SERENA_DASHBOARD_DIR = str(_serena_pkg_path / "resources" / "dashboard") -SERENA_ICON_DIR = str(_serena_pkg_path / "resources" / "icons") +SERENA_DASHBOARD_DIR = str(_resources_path / "dashboard") +SERENA_ICON_DIR = str(_resources_path / "icons") DEFAULT_SOURCE_FILE_ENCODING = "utf-8" """The default encoding assumed for project source files.""" @@ -24,9 +26,9 @@ DEFAULT_CONTEXT = "desktop-app" SERENA_FILE_ENCODING = "utf-8" """The encoding used for Serena's own files, such as configuration files and memories.""" -PROJECT_TEMPLATE_FILE = str(_serena_pkg_path / "resources" / "project.template.yml") -PROJECT_LOCAL_TEMPLATE_FILE = str(_serena_pkg_path / "resources" / "project.local.template.yml") -SERENA_CONFIG_TEMPLATE_FILE = str(_serena_pkg_path / "resources" / "serena_config.template.yml") +PROJECT_TEMPLATE_FILE = str(_resources_path / "project.template.yml") +PROJECT_LOCAL_TEMPLATE_FILE = str(_resources_path / "project.local.template.yml") +SERENA_CONFIG_TEMPLATE_FILE = str(_resources_path / "serena_config.template.yml") SERENA_LOG_FORMAT = "%(levelname)-5s %(asctime)-15s [%(threadName)s] %(name)s:%(funcName)s:%(lineno)d - %(message)s" diff --git a/src/serena/memories/memory_manager.py b/src/serena/memories/memory_manager.py index 2da593d1..f3d4a22f 100644 --- a/src/serena/memories/memory_manager.py +++ b/src/serena/memories/memory_manager.py @@ -84,7 +84,7 @@ class MemoryManager: return cls._MEMORY_REF_PREFIX + name MEMORY_MAINTENANCE_NAME: str = "memory_maintenance" - _MEMORY_MAINTENANCE_TEMPLATE_PATH: Path = SerenaPaths().serena_resources_path / "memory_maintenance.md" + _MEMORY_MAINTENANCE_TEMPLATE_PATH: Path = SerenaPaths().get_resource_path("memory_maintenance.md") def ensure_memory_maintenance_memory(self) -> str: """