4343
4444
4545
46- def make_container_tree (folders ,files ,path_delim = "/" ,parse_files = True ):
47- '''make_container_tree will convert a list of folders and files into a json structure that represents a graph.
48- :param folders: a list of folders in the image
49- :param files: a list of files in the folder
50- :param parse_files: return 'files' lookup in result, to associate ID of node with files (default True)
51- :param path_delim: the path delimiter, default is '/'
46+ def make_container_tree (files ,
47+ folders = None ,
48+ path_delim = "/" ,
49+ parse_files = True ,
50+ labels = None ):
51+
52+ '''make_container_tree will convert a list of folders and files into a
53+ json structure that represents a graph.
54+
55+ Parameters
56+ ==========
57+ folders: a list of folders in the image
58+ files: a list of files in the folder
59+ parse_files: return 'files' lookup in result, to associate ID of node with files (default True)
60+ path_delim: the path delimiter, default is '/'
61+ labels = dict() a lookup dictionary of labels to add to file nodes, if
62+ defined.
63+
5264 '''
65+ if folders == None :
66+ folders = files .copy ()
67+
5368 nodes = {} # first we will make a list of nodes
5469 lookup = {}
5570 count = 1 # count will hold an id for nodes
5671 max_depth = 0
57- for folder in folders :
58- if folder != "." :
59- folder = re .sub ("^[.]/" ,"" ,folder )
72+
73+ for folder_path in folders :
74+ if folder_path != "." :
75+ folder = re .sub ("^[.]/" ,"" , folder_path )
6076 path_components = folder .split (path_delim )
77+
6178 for p in range (len (path_components )):
6279 path_component = path_components [p ]
6380 fullpath = path_delim .join (path_components [0 :p + 1 ])
81+
6482 # Have we created the node yet?
6583 if fullpath not in lookup :
6684 lookup [fullpath ] = count
67- node = {"id" :count ,"name" :path_component ,"path" :fullpath ,"level" :p ,"children" :[]}
85+ node = {"id" : count ,
86+ "name" :path_component ,
87+ "path" :fullpath ,
88+ "level" :p ,
89+ "children" :[]}
6890 count += 1
91+ if labels is not None :
92+ if folder_path in labels :
93+ node ['labels' ] = labels [folder_path ]
94+
6995 # Did we find a deeper level?
7096 if p > max_depth :
7197 max_depth = p
@@ -77,7 +103,7 @@ def make_container_tree(folders,files,path_delim="/",parse_files=True):
77103 parent_id = lookup [parent_path ]
78104 node ["parent" ] = parent_id
79105 nodes [node ['id' ]] = node
80-
106+
81107 # Now make the graph, we simply append children to their parents
82108 seen = []
83109 graph = []
@@ -104,8 +130,8 @@ def make_container_tree(folders,files,path_delim="/",parse_files=True):
104130 # Parse files to include in tree
105131 if parse_files == True :
106132 file_lookup = {}
107- for filey in files :
108- filey = re .sub ("^[.]/" ,"" ,filey )
133+ for file_name in files :
134+ filey = re .sub ("^[.]/" ,"" ,file_name )
109135 filepath ,filename = os .path .split (filey )
110136 if filepath in lookup :
111137 folder_id = lookup [filepath ]
0 commit comments