-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpi_treedata.py
136 lines (105 loc) · 6.01 KB
/
pi_treedata.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from PySimpleGUI import TreeData
from table import Row
from image_collection import ImageCollection
from pi_folder_stats import FolderStats
'''
Subclass of TreeData that Loads an Image Table into TreeData.
Any table will work as long as it has four columns:
- 'file_location' - the directory name relative to a given home directory
- 'file_name' - the name of the image file
- 'img_status' - status of the image (tbd,etc.)
- 'rvw_lvl' - highest review level
A set of TreeData entries is created,
one for each folder represented by 'file_location'.
A TreeData entry is then created for each file, with the 'file_location' as the parent key.
Creating the 'file_location' nodes is a two step process, first creating
a hierarchical dictionary representing the folders, then
traversing that dictionary to create a node for each directory.
For example, the file location '/2018-11/ipod touch/2018-11-01' becomes
the dictionary:
{"2018-11":{
"ipod touch": {
"2018-11-01": {}
}
}
}
That dictionary is then traversed to insert TreeData nodes for:
parent="", key="/2018-11", text="2018-11"
parent="/2018-11", key="/2018-11/ipod touch", text="ipod touch"
parent="/2018-11/ipod touch", key="/2018-11/ipod touch/2018-11-01", text="2018-11-01"
Once that is done, an image can be inserted with the 'file_location' as the parent key. For example:
parent"/2018-11/ipod touch/2018-11-01", key="/2018-11/ipod touch/2018-11-01/IMG_2770.JPG", text="IMG_2770.JPG"
'''
# dictionary key for folder stats
# since we split the path by '/', no folder name in the path will contain a '/'
_STATS_ = '/stats/'
class PiTreeData(TreeData):
folder_icon = b'iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCA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eN9kkCwxsNkAqRWy7+B7Z00G3xVc2wZeMSI4S7sVYkSk5Z/4PyBWROqvox3A28PN2cjUwinQC9QyckKALxj4kv2auK0xAAAAAElFTkSuQmCC'
@staticmethod
def _add_dict_folder(dict:dict[str:dict],nodes:list[str],idx:int,row:Row):
''' Recursively add the a list of directory names to a hierarchicial dictionary
incrementing the node index for each recursion.
While doing this, it is keeping track of file statistics for the directory
using the FolderStats class and FolderStats add_stats(row) method '''
if idx >= len(nodes):
# all done
return
key = nodes[idx]
if not key in dict:
dict[key] = {_STATS_: FolderStats()}
dict[key][_STATS_].add_stats(row)
idx += 1
PiTreeData._add_dict_folder(dict[key],nodes,idx,row)
def __init__(self,rows:list[Row]=[],stats=None):
super().__init__()
self.rows = rows
self._insert_folders()
self._insert_files()
def _insert_folders(self):
''' insert folders into parent TreeData object'''
dict = {}
# add folders to a hierarchical dictionary
for row in self.rows:
img_folder = row['file_location']
nodes = img_folder.split('/')
PiTreeData._add_dict_folder(dict,nodes,1,row)
# insert "root" folders and children into Parent TreeData object
for k,v in dict.items():
self._insert_treedata_folder(k,v,"")
def _insert_treedata_folder(self,key,value,parent):
''' Traverse a hierarchical dictionary inserting the
keys into the TreeData '''
td_key = f'{parent}/{key}'
values = value[_STATS_].get_stats()
cnt = values[2]
values = ImageCollection.translate_status_lvl(values[0],values[1])
values.append(str(cnt))
self.insert(parent,td_key,key,values=values,icon=self.folder_icon)
for k,v in value.items():
if k != _STATS_:
self._insert_treedata_folder(k,v,td_key)
def _insert_files(self):
''' Insert a treedata node for each row in the table.
All parents should have been inserted already via
_insert_tree_data_folders '''
for row in self.rows:
parent = row['file_location']
v = row['file_name']
values = row.get_status_lvl()
self.insert(parent, f'{parent}/{v}',v,values=values, icon=self.file_icon)
def _update_rows(self,tree,key_id_dict,rows):
''' Update the treedata for a list of rows.
tree - the PySimpleGUI tree object
'key_id_dict' - dictionary of item id to tkkinter id
rows - rows which were updated
TODO: propagate possible status change to parent folders
'''
for row in rows:
parent = row['file_location']
v = row['file_name']
values = row.get_status_lvl()
key = f'{parent}/{v}'
id = key_id_dict[key]
tree.Widget.set(id,'#1',values[0])
tree.Widget.set(id,'#2',values[1])