Skip to content

Commit

Permalink
Change get_project to raise error if no project is set
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvmc committed Nov 1, 2024
1 parent b87b22c commit 839b024
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
17 changes: 8 additions & 9 deletions src/gretel_client/gretel/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
)
from gretel_client.gretel.exceptions import (
GretelJobSubmissionError,
GretelProjectNotSetError
GretelProjectNotSetError,
)
from gretel_client.gretel.job_results import (
GenerateJobResults,
Expand Down Expand Up @@ -149,16 +149,16 @@ def _assert_project_is_set(self):
def _generate_random_label(self) -> str:
return f"{uuid.uuid4().hex[:5]}-{self._user_id}"

def get_project(self, **kwargs) -> Project:
def get_project(self) -> Project:
"""Returns the current Gretel project.
If a project has not been set, a new one will be created. The optional
kwargs are the same as those available for the `set_project` method.
"""
if self._project is None:
logger.info("No project set -> creating a new one...")
self.set_project(**kwargs)
return self._project # type: ignore
if self._project:
return self._project # type: ignore

raise GretelProjectNotSetError("No Gretel Project Set")

def set_project(
self,
Expand All @@ -171,15 +171,15 @@ def set_project(
"""Set the current Gretel project.
If a project with the given name does not exist, and create_if_missing is set to true,
then it will be created.
then it will be created.
If the name is not unique, the user id will be appended to the name.
Args:
name: Name of new or existing project. If None, will create one.
desc: Project description.
display_name: Project display name. If None, will use project name.
create_if_missing: Whether to create a new project if one is missing
create_if_missing: Project display name. If None, will use project name.
Raises:
ApiException: If an error occurs while creating the project.
Expand All @@ -200,7 +200,6 @@ def set_project(
and "not found" not in exception.body # type: ignore
):
raise exception

logger.warning(
f"Project name `{name}` is not unique -> "
"appending your user id to the name."
Expand Down
4 changes: 2 additions & 2 deletions src/gretel_client/projects/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ def get_project(
resp = api.create_project(project=models.Project(**project_args))
project = api.get_project(project_id=resp.get(DATA).get("id"))

print(f'No project found with that name. Creating new project {name}')
logger.info(f'No project found. Creating new project {name}')

if name:
try:
Expand All @@ -486,7 +486,7 @@ def get_project(
resp = api.create_project(project=models.Project(**project_args))
project = api.get_project(project_id=resp.get(DATA).get("id"))

print(f'No project found with that name. Creating new project {name}')
logger.info(f'No project found. Creating new project {name}')
else:
raise GretelProjectError(f"Could not get project using '{name}'.")

Expand Down

0 comments on commit 839b024

Please sign in to comment.