4
4
from diffgram .core .diffgram_dataset_iterator import DiffgramDatasetIterator
5
5
from multiprocessing .pool import ThreadPool as Pool
6
6
7
- def get_directory_list (self ):
8
- """
9
- Get a list of available directories for a project
10
7
11
- Arguments
12
- self
13
-
14
- Expects
15
- self.project_string_id
16
-
17
- Returns
18
- directory_list, array of dicts
19
-
20
- """
8
+ class Directory (DiffgramDatasetIterator ):
21
9
22
- if self .project_string_id is None :
23
- raise Exception ("No project string." + \
24
- "Set a project string using .auth()" )
10
+ def __init__ (self ,
11
+ client ,
12
+ file_id_list_sliced = None ,
13
+ init_file_ids = True ,
14
+ validate_ids = True ):
25
15
26
- if type (self .project_string_id ) != str :
27
- raise Exception ("project_string_id must be of type String" )
16
+ self .client = client
17
+ self .id = None
18
+ self .file_list_metadata = {}
19
+ self .nickname = None
20
+ if file_id_list_sliced is None and init_file_ids :
21
+ self .file_id_list = self .all_file_ids ()
22
+ elif not init_file_ids :
23
+ self .file_id_list = []
24
+ elif file_id_list_sliced is not None :
25
+ self .file_id_list = file_id_list_sliced
26
+ super (Directory , self ).__init__ (self .client , self .file_id_list , validate_ids )
28
27
29
- endpoint = "/api/v1/project/" + self .project_string_id + \
30
- "/directory/list"
31
28
32
- response = self .session .get (self .host + endpoint )
29
+ def get_directory_list (self ):
30
+ """
31
+ Get a list of available directories for a project
32
+ """
33
33
34
- self .handle_errors (response )
34
+ if self .client .project_string_id is None :
35
+ raise Exception ("No project string." + \
36
+ "Set a project string using .auth()" )
35
37
36
- directory_list = response .json ()
38
+ if type (self .client .project_string_id ) != str :
39
+ raise Exception ("project_string_id must be of type String" )
37
40
38
- return directory_list
41
+ endpoint = "/api/v1/project/" + self .client .project_string_id + \
42
+ "/directory/list"
39
43
44
+ response = self .client .session .get (self .client .host + endpoint )
40
45
41
- def set_directory_by_name (self , name ):
42
- """
46
+ self .client .handle_errors (response )
43
47
44
- Arguments
45
- self
46
- name, string
48
+ data = response .json ()
49
+
50
+ directory_list_json = data .get ('directory_list' )
51
+ default_directory_json = data .get ('default_directory' )
47
52
48
- """
53
+ if default_directory_json :
54
+ self .client .directory_id = default_directory_json .get ('id' )
49
55
50
- if name is None :
51
- raise Exception ("No name provided." )
56
+ directory_list = self .convert_json_to_sdk_object (directory_list_json )
52
57
53
- # Don't refresh by default, just set from existing
58
+ return directory_list
59
+
54
60
55
- names_attempted = []
56
- did_set = False
61
+ def convert_json_to_sdk_object (self , directory_list_json ):
57
62
58
- for directory in self . directory_list :
63
+ directory_list = []
59
64
60
- nickname = directory .get ("nickname" )
61
- if nickname == name :
62
- self .set_default_directory (directory .get ("id" ))
63
- did_set = True
64
- break
65
- else :
66
- names_attempted .append (nickname )
65
+ for directory_json in directory_list_json :
66
+ new_directory = Directory (
67
+ client = self .client ,
68
+ init_file_ids = False ,
69
+ validate_ids = False
70
+ )
71
+ refresh_from_dict (new_directory , directory_json )
72
+ directory_list .append (new_directory )
67
73
68
- if did_set is False :
69
- raise Exception (name , " does not exist. Valid names are: " +
70
- str (names_attempted ))
74
+ return directory_list
71
75
72
76
73
- class Directory ( DiffgramDatasetIterator ):
77
+ def set_default ( ):
74
78
75
- def __init__ (self , client , file_id_list_sliced = None , init_file_ids = True , validate_ids = True ):
79
+ if not self .client .directory_list :
80
+ self .client .directory_list = self .get_directory_list ()
76
81
77
- self .client = client
78
- self .id = None
79
- self .file_list_metadata = {}
80
- if file_id_list_sliced is None and init_file_ids :
81
- self .file_id_list = self .all_file_ids ()
82
- elif not init_file_ids :
83
- self .file_id_list = []
84
- elif file_id_list_sliced is not None :
85
- self .file_id_list = file_id_list_sliced
86
- super (Directory , self ).__init__ (self .client , self .file_id_list , validate_ids )
87
82
88
83
def all_files (self ):
89
84
"""
@@ -190,7 +185,7 @@ def new(self, name: str):
190
185
# generator expression returns True if the directory
191
186
# is not found. this is a bit awkward.
192
187
if next ((dir for dir in self .client .directory_list
193
- if dir [ ' nickname' ] == name ), True ) is not True :
188
+ if dir . nickname == name ), True ) is not True :
194
189
raise Exception (name , "Already exists" )
195
190
196
191
packet = {'nickname' : name }
@@ -208,18 +203,15 @@ def new(self, name: str):
208
203
209
204
project = data .get ('project' )
210
205
if project :
211
- directory_list = project .get ('directory_list' )
212
- # TODO upgrade directory_list here to be 1st class objects instead of JSON
213
- if directory_list :
214
- self .client .directory_list = directory_list
206
+ directory_list_json = project .get ('directory_list' )
207
+ if directory_list_json :
208
+ self .client .directory_list = self .convert_json_to_sdk_object (directory_list_json )
215
209
216
210
new_directory = None
217
211
# TODO the route about should return the newly created dataset directly
218
- for directory_json in self .client .directory_list :
219
- nickname = directory_json .get ("nickname" )
220
- if nickname == name :
221
- new_directory = Directory (client = self .client )
222
- refresh_from_dict (new_directory , directory_json )
212
+ for directory in self .client .directory_list :
213
+ if directory .nickname == name :
214
+ new_directory = directory
223
215
224
216
return new_directory
225
217
@@ -319,20 +311,16 @@ def get(self,
319
311
names_attempted = []
320
312
did_set = False
321
313
322
- for directory_json in self .client .directory_list :
323
-
324
- nickname = directory_json .get ("nickname" )
325
- if nickname == name :
326
- # TODO change the general directory_list
327
- # to use object approach (over dict)
314
+ if not self .client .directory_list :
315
+ self .client .directory_list = self .get_directory_list ()
328
316
329
- new_directory = Directory (client = self .client )
330
- refresh_from_dict (new_directory , directory_json )
317
+ for directory in self .client .directory_list :
331
318
332
- return new_directory
319
+ if directory .nickname == name :
320
+ return directory
333
321
334
322
else :
335
- names_attempted .append (nickname )
323
+ names_attempted .append (directory . nickname )
336
324
337
325
if did_set is False :
338
326
raise Exception (name , " does not exist. Valid names are: " +
0 commit comments