25
25
from hopsworks .client .exceptions import RestAPIError , ProjectException
26
26
from hopsworks import version , constants , client
27
27
from hopsworks .connection import Connection
28
+ from hopsworks .core import project_api , secret_api
29
+ from hopsworks .decorators import NoHopsworksConnectionError
28
30
29
31
# Needs to run before import of hsml and hsfs
30
32
warnings .filterwarnings (action = "ignore" , category = UserWarning , module = r".*psycopg2" )
39
41
_hw_connection = Connection .connection
40
42
41
43
_connected_project = None
42
-
44
+ _secrets_api = None
45
+ _project_api = None
43
46
44
47
def hw_formatwarning (message , category , filename , lineno , line = None ):
45
48
return "{}: {}\n " .format (category .__name__ , message )
@@ -110,6 +113,7 @@ def login(
110
113
if "REST_ENDPOINT" in os .environ :
111
114
_hw_connection = _hw_connection ()
112
115
_connected_project = _hw_connection .get_project ()
116
+ _initialize_module_apis ()
113
117
print ("\n Logged in to project, explore it here " + _connected_project .get_url ())
114
118
return _connected_project
115
119
@@ -173,6 +177,7 @@ def login(
173
177
"\n Logged in to project, explore it here "
174
178
+ _connected_project .get_url ()
175
179
)
180
+ _initialize_module_apis ()
176
181
return _connected_project
177
182
except RestAPIError :
178
183
logout ()
@@ -200,7 +205,12 @@ def login(
200
205
logout ()
201
206
raise e
202
207
203
- print ("\n Logged in to project, explore it here " + _connected_project .get_url ())
208
+ if _connected_project is None :
209
+ print ("Could not find any project, use hopsworks.create_project('my_project') to create one" )
210
+ else :
211
+ print ("\n Logged in to project, explore it here " + _connected_project .get_url ())
212
+
213
+ _initialize_module_apis ()
204
214
return _connected_project
205
215
206
216
@@ -246,7 +256,7 @@ def _prompt_project(valid_connection, project):
246
256
saas_projects = valid_connection .get_projects ()
247
257
if project is None :
248
258
if len (saas_projects ) == 0 :
249
- raise ProjectException ( "Could not find any project" )
259
+ return None
250
260
elif len (saas_projects ) == 1 :
251
261
return saas_projects [0 ]
252
262
else :
@@ -283,7 +293,72 @@ def _prompt_project(valid_connection, project):
283
293
284
294
def logout ():
285
295
global _hw_connection
286
- if isinstance (_hw_connection , Connection ):
296
+ global _project_api
297
+ global _secrets_api
298
+
299
+ if _is_connection_active ():
287
300
_hw_connection .close ()
301
+
288
302
client .stop ()
303
+ _project_api = None
304
+ _secrets_api = None
289
305
_hw_connection = Connection .connection
306
+
307
+ def _is_connection_active ():
308
+ global _hw_connection
309
+ return isinstance (_hw_connection , Connection )
310
+
311
+ def _initialize_module_apis ():
312
+ global _project_api
313
+ global _secrets_api
314
+ _project_api = project_api .ProjectApi ()
315
+ _secrets_api = secret_api .SecretsApi ()
316
+
317
+ def create_project (
318
+ name : str , description : str = None , feature_store_topic : str = None
319
+ ):
320
+ """Create a new project.
321
+
322
+ Example for creating a new project
323
+
324
+ ```python
325
+
326
+ import hopsworks
327
+
328
+ hopsworks.login()
329
+
330
+ hopsworks.create_project("my_hopsworks_project", description="An example Hopsworks project")
331
+
332
+ ```
333
+ # Arguments
334
+ name: The name of the project.
335
+ description: optional description of the project
336
+ feature_store_topic: optional feature store topic name
337
+
338
+ # Returns
339
+ `Project`. A project handle object to perform operations on.
340
+ """
341
+ global _hw_connection
342
+ global _connected_project
343
+
344
+ if not _is_connection_active ():
345
+ raise NoHopsworksConnectionError ()
346
+
347
+ new_project = _hw_connection ._project_api ._create_project (name , description , feature_store_topic )
348
+ if _connected_project is None :
349
+ _connected_project = new_project
350
+ print ("Setting {} as the active project" .format (_connected_project .name ))
351
+ return _connected_project
352
+ else :
353
+ print ("You are already using the project {}, to access the new project use hopsworks.login(project='{}')" .format (_connected_project .name , new_project .name ))
354
+
355
+ def get_secrets_api ():
356
+ """Get the secrets api.
357
+
358
+ # Returns
359
+ `SecretsApi`: The Secrets Api handle
360
+ """
361
+ global _secrets_api
362
+ if not _is_connection_active ():
363
+ raise NoHopsworksConnectionError ()
364
+ return _secrets_api
0 commit comments