Add method SerenaAgent.load_project_from_path_or_name

This commit is contained in:
Dominik Jain
2025-07-19 22:36:57 +02:00
committed by Dominik Jain
parent 4506c87d99
commit fcca03db9c
+25 -13
View File
@@ -399,27 +399,39 @@ class SerenaAgent:
if self._project_activation_callback is not None:
self._project_activation_callback()
def activate_project_from_path_or_name(self, project_root_or_name: str) -> Project:
def load_project_from_path_or_name(self, project_root_or_name: str, autogenerate: bool) -> Project | None:
"""
Activate a project from a path or a name.
If the project was already registered, it will just be activated. If it was not registered,
the project will be registered and activated. After that, the project can be activated again
by name (not just by path).
Get a project instance from a path or a name.
:return: a tuple of the project instance and a Boolean indicating whether the project was newly
created
:param project_root_or_name: the path to the project root or the name of the project
:param autogenerate: whether to autogenerate the project for the case where first argument is a directory
which does not yet contain a Serena project configuration file
:return: the project instance if it was found/could be created, None otherwise
"""
project_instance: Project | None = self.serena_config.get_project(project_root_or_name)
if project_instance is not None:
log.info(f"Found registered project {project_instance.project_name} at path {project_instance.project_root}.")
else:
if not os.path.isdir(project_root_or_name):
raise ProjectNotFoundError(
f"Project '{project_root_or_name}' not found: Not a valid project name or directory. "
f"Existing project names: {self.serena_config.project_names}"
)
elif autogenerate and os.path.isdir(project_root_or_name):
project_instance = self.serena_config.add_project_from_path(project_root_or_name)
log.info(f"Added new project {project_instance.project_name} for path {project_instance.project_root}.")
return project_instance
def activate_project_from_path_or_name(self, project_root_or_name: str) -> Project:
"""
Activate a project from a path or a name.
If the project was already registered, it will just be activated.
If the argument is a path at which no Serena project previously existed, the project will be created beforehand.
Raises ProjectNotFoundError if the project could neither be found nor created.
:return: a tuple of the project instance and a Boolean indicating whether the project was newly
created
"""
project_instance: Project | None = self.load_project_from_path_or_name(project_root_or_name, autogenerate=True)
if project_instance is None:
raise ProjectNotFoundError(
f"Project '{project_root_or_name}' not found: Not a valid project name or directory. "
f"Existing project names: {self.serena_config.project_names}"
)
self._activate_project(project_instance)
return project_instance