Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
robzor92 committed May 10, 2024
1 parent 8f4395a commit ca10acd
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions python/hopsworks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
_secrets_api = None
_project_api = None


def hw_formatwarning(message, category, filename, lineno, line=None):
return "{}: {}\n".format(category.__name__, message)

Expand Down Expand Up @@ -144,7 +145,7 @@ def login(
elif host is None: # Always do a fallback to Serverless Hopsworks if not defined
host = constants.HOSTS.APP_HOST

is_app = (host == constants.HOSTS.APP_HOST)
is_app = host == constants.HOSTS.APP_HOST

# If port same as default, get HOPSWORKS_HOST environment variable
if port == 443 and "HOPSWORKS_PORT" in os.environ:
Expand Down Expand Up @@ -211,7 +212,9 @@ def login(
raise e

if _connected_project is None:
print("Could not find any project, use hopsworks.create_project('my_project') to create one")
print(
"Could not find any project, use hopsworks.create_project('my_project') to create one"
)
else:
print("\nLogged in to project, explore it here " + _connected_project.get_url())

Expand Down Expand Up @@ -273,7 +276,9 @@ def _prompt_project(valid_connection, project, is_app):
for index in range(len(saas_projects)):
print("\t (" + str(index + 1) + ") " + saas_projects[index].name)
while True:
project_index = input("\nEnter number corresponding to the project to use: ")
project_index = input(
"\nEnter number corresponding to the project to use: "
)
# Handle invalid input type
try:
project_index = int(project_index)
Expand All @@ -300,8 +305,7 @@ def _prompt_project(valid_connection, project, is_app):


def logout():
"""Cleans up and closes the connection for the hopsworks, hsfs and hsml libraries.
"""
"""Cleans up and closes the connection for the hopsworks, hsfs and hsml libraries."""
global _hw_connection
global _project_api
global _secrets_api
Expand All @@ -314,10 +318,12 @@ def logout():
_secrets_api = None
_hw_connection = Connection.connection


def _is_connection_active():
global _hw_connection
return isinstance(_hw_connection, Connection)


def get_current_project() -> project.Project:
"""Get a reference to the current logged in project.
Expand All @@ -341,15 +347,15 @@ def get_current_project() -> project.Project:
raise ProjectException("No project is set for this session")
return _connected_project


def _initialize_module_apis():
global _project_api
global _secrets_api
_project_api = project_api.ProjectApi()
_secrets_api = secret_api.SecretsApi()

def create_project(
name: str, description: str = None, feature_store_topic: str = None
):

def create_project(name: str, description: str = None, feature_store_topic: str = None):
"""Create a new project.
Example for creating a new project
Expand Down Expand Up @@ -377,13 +383,24 @@ def create_project(
if not _is_connection_active():
raise NoHopsworksConnectionError()

new_project = _hw_connection._project_api._create_project(name, description, feature_store_topic)
new_project = _hw_connection._project_api._create_project(
name, description, feature_store_topic
)
if _connected_project is None:
_connected_project = new_project
print("Setting {} as the current project, a reference can be retrieved by calling hopsworks.get_current_project()".format(_connected_project.name))
print(
"Setting {} as the current project, a reference can be retrieved by calling hopsworks.get_current_project()".format(
_connected_project.name
)
)
return _connected_project
else:
print("You are already using the project {}, to access the new project use hopsworks.login(..., project='{}')".format(_connected_project.name, new_project.name))
print(
"You are already using the project {}, to access the new project use hopsworks.login(..., project='{}')".format(
_connected_project.name, new_project.name
)
)


def get_secrets_api():
"""Get the secrets api.
Expand All @@ -394,4 +411,4 @@ def get_secrets_api():
global _secrets_api
if not _is_connection_active():
raise NoHopsworksConnectionError()
return _secrets_api
return _secrets_api

0 comments on commit ca10acd

Please sign in to comment.