forked from Kitware/QuickView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_browser.py
More file actions
468 lines (418 loc) · 18.2 KB
/
file_browser.py
File metadata and controls
468 lines (418 loc) · 18.2 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import json
import re
from pathlib import Path
from paraview import simple
from trame.widgets import vuetify3 as v3, html
from trame.app import TrameComponent
DIRECTORY = dict(icon="mdi-folder", type="directory")
GROUP = dict(icon="mdi-file-document-multiple-outline", type="group")
FILE = dict(icon="mdi-file-document-outline", type="file")
HEADERS = [
{"title": "Name", "align": "start", "key": "name", "sortable": False},
{"title": "Size", "align": "end", "key": "size", "sortable": False},
{"title": "Date", "align": "end", "key": "modified", "sortable": False},
]
def sort_by_name(e):
return e.get("name")
def to_type(e):
return e.get("type", "")
def to_suffix(e):
return Path(e.get("name", "")).suffix
class ParaViewFileBrowser(TrameComponent):
def __init__(
self,
server,
prefix="pv_files",
home=None,
current=None,
exclude=r"^\.|~$|^\$",
group=r"[0-9]+\.",
):
super().__init__(server)
self._prefix = prefix
self._enable_groups = True
self._home_path = Path(home).resolve() if home else Path.home()
self._current_path = Path(current).resolve() if current else self._home_path
self.pattern_exclude = re.compile(exclude)
self.pattern_group = re.compile(group)
# Disable state import by default
self.set("is_state_file", False)
self._pxm = simple.servermanager.ProxyManager()
self._proxy_listing = self._pxm.NewProxy("misc", "ListDirectory")
self._proxy_directories = simple.servermanager.VectorProperty(
self._proxy_listing, self._proxy_listing.GetProperty("DirectoryList")
)
self._proxy_files = simple.servermanager.VectorProperty(
self._proxy_listing, self._proxy_listing.GetProperty("FileList")
)
# Initialize trame state
self.update_listing()
def name(self, name):
return f"{self._prefix}_{name}"
def set(self, name, value):
self.state[self.name(name)] = value
def get(self, name):
return self.state[self.name(name)]
def update_listing(self, selection=None):
with self.state:
self.set("active", -1)
self.set("listing", self.listing)
self.set("selected", selection)
@property
def enable_groups(self):
return self._enable_groups
@enable_groups.setter
def enable_groups(self, v):
self._enable_groups = v
@property
def listing(self):
directories = []
files = []
groups = []
g_map = {}
self._proxy_listing.List(str(self._current_path.resolve()))
self._proxy_listing.UpdatePropertyInformation()
# Files + Groups
file_listing = []
if len(self._proxy_files) > 1:
file_listing = self._proxy_files.GetData()
if len(self._proxy_files) == 1:
file_listing.append(self._proxy_files.GetData())
file_listing = [
file_name
for file_name in file_listing
if not re.search(self.pattern_exclude, file_name)
]
for file_name in file_listing:
f = self._current_path / file_name
stats = f.stat()
# Group or file?
file_split = re.split(self.pattern_group, file_name)
if self.enable_groups and len(file_split) == 2:
# Group
g_name = "*.".join(file_split)
if g_name not in g_map:
g_entry = dict(
name=g_name,
modified=stats.st_mtime,
size=0,
files=[],
**GROUP,
)
g_map[g_name] = g_entry
groups.append(g_entry)
g_map[g_name]["size"] += stats.st_size
g_map[g_name]["files"].append(file_name)
# Many need to sort files???
else:
# File
files.append(
dict(
name=f.name,
modified=stats.st_mtime,
size=stats.st_size,
**FILE,
)
)
# Directories
dir_listing = []
if len(self._proxy_directories) > 1:
dir_listing = self._proxy_directories.GetData()
if len(self._proxy_directories) == 1:
dir_listing.append(self._proxy_directories.GetData())
dir_listing = [
dir_name
for dir_name in dir_listing
if not re.search(self.pattern_exclude, dir_name)
]
for dir_name in dir_listing:
f = self._current_path / dir_name
directories.append(
dict(name=f.name, modified=f.stat().st_mtime, **DIRECTORY)
)
# Sort content
directories.sort(key=sort_by_name)
groups.sort(key=sort_by_name)
files.sort(key=sort_by_name)
return [
{**e, "index": i} for i, e in enumerate([*directories, *groups, *files])
]
def open_entry(self, entry):
entry_type = entry.get("type")
if entry_type == "directory":
self._current_path = self._current_path / entry.get("name")
self.update_listing()
return entry_type, str(self._current_path)
if entry_type == "group":
files = entry.get("files", [])
self.update_listing()
return entry, [str(self._current_path / f) for f in files]
if entry_type == "file":
file = self._current_path / entry.get("name")
file_name = file.name.lower()
full_path = str(file)
var_name = (
"data_connectivity"
if "connectivity_" in file_name
else "data_simulation"
)
self.set(var_name, full_path)
self.update_listing(full_path)
return entry_type, full_path
return None
@property
def active_path(self):
entry = self.get("listing")[self.get("active")]
return str(self._current_path / entry.get("name"))
def set_data_connectivity(self, value=None):
self.set("data_connectivity", value or self.active_path)
def set_data_simulation(self, value=None):
self.set("data_simulation", value or self.active_path)
def goto_home(self):
self._current_path = self._home_path
self.update_listing()
def goto_parent(self):
self._current_path = self._current_path.parent
self.update_listing()
def open_dataset(self, entry):
event = {}
if to_type(entry) == "group":
files = [str(self._current_path / f) for f in entry.get("files")]
source = simple.OpenDataFile(files)
representation = simple.Show(source)
view = simple.Render()
event = dict(
source=source, representation=representation, view=view, type="group"
)
else:
source = simple.OpenDataFile(str(self._current_path / entry.get("name")))
representation = simple.Show(source)
view = simple.Render()
event = dict(
source=source, representation=representation, view=view, type="dataset"
)
return event
def select_entry(self, entry):
with self.state as state:
state[f"{self._prefix}_active"] = entry.get("index", 0) if entry else -1
file_path = Path(self.active_path)
# Check if it is a state file
if file_path.suffix == ".json" and file_path.exists():
state_content = json.loads(file_path.read_text())
self.set(
"is_state_file",
all(
(
k in state_content
for k in [
"files",
"variables-selection",
"layout",
"data-selection",
"views",
]
)
),
)
else:
self.set("is_state_file", False)
def load_data_files(self, **_):
self.set("loading", True)
print("Load files:")
print(" - simulation:", self.get("data_simulation"))
print(" - connectivity:", self.get("data_connectivity"))
self.ctrl.file_selection_load(
self.get("data_simulation"), self.get("data_connectivity")
)
def import_state_file(self):
self.set("state_loading", True)
state_content = json.loads(Path(self.active_path).read_text())
self.ctrl.import_state(state_content)
def cancel(self):
self.ctrl.file_selection_cancel()
def loading_completed(self, valid):
with self.state:
self.set("loading", False)
self.set("error", not valid)
def ui(self):
with v3.VCard(rounded="lg"):
with v3.VCardTitle("File loading", classes="d-flex align-center px-3"):
v3.VSpacer()
with v3.VTooltip(text="Go to launched directory"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
icon="mdi-home",
variant="flat",
size="small",
click=self.goto_home,
)
with v3.VTooltip(text="Go up a directory"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
icon="mdi-folder-upload-outline",
variant="flat",
size="small",
click=self.goto_parent,
)
v3.VTextField(
v_model=self.name("filter"),
hide_details=True,
color="primary",
placeholder="filter",
density="compact",
variant="outlined",
classes="ml-2",
prepend_inner_icon="mdi-magnify",
clearable=True,
)
with v3.VCardText(
classes="rounded-lg border border-opacity-25 pa-0 mx-3 my-0 overflow-hidden"
):
style_align_center = "d-flex align-center "
with v3.VDataTable(
density="compact",
fixed_header=True,
headers=(self.name("headers"), HEADERS),
items=(self.name("listing"), []),
height="calc(80vh - 20rem)",
style="user-select: none; cursor: pointer;",
hover=True,
search=(self.name("filter"), ""),
items_per_page=-1,
):
v3.Template(raw_attrs=["v-slot:bottom"])
with v3.Template(raw_attrs=['v-slot:item="{ index, item }"']):
with v3.VDataTableRow(
index=("index",),
item=("item",),
click=(self.select_entry, "[item]"),
dblclick=(self.open_entry, "[item]"),
classes=(
f"{{ 'bg-grey': item.index === {self.name('active')}, 'cursor-pointer': 1 }}",
),
):
with v3.Template(raw_attrs=["v-slot:item.name"]):
with html.Div(classes=style_align_center):
v3.VIcon(
"{{ item.icon }}",
size="small",
classes="mr-2",
)
html.Div("{{ item.name }}")
with v3.Template(raw_attrs=["v-slot:item.size"]):
with html.Div(
classes=style_align_center + " justify-end",
):
html.Div(
"{{ utils.fmt.bytes(item.size, 0) }}",
v_if="item.size",
)
html.Div(" - ", v_else=True)
with v3.Template(raw_attrs=["v-slot:item.modified"]):
with html.Div(
classes=style_align_center + " justify-end",
):
html.Div(
"{{ new Date(item.modified * 1000).toDateString() }}"
)
with v3.VCol():
html.Label(
"Simulation File",
classes="text-subtitle-1 font-weight-medium d-block",
)
v3.VTextField(
v_model=(self.name("data_simulation"), ""),
density="compact",
variant="outlined",
disabled=True,
messages="EAM's history output on the physics grids (pg2 grids) written by EAMv2, v3, and an intermediate version towards v4 (EAMxx).",
)
html.Label(
"Connectivity File",
classes="text-subtitle-1 font-weight-medium d-block",
)
v3.VTextField(
v_model=(self.name("data_connectivity"), ""),
density="compact",
variant="outlined",
disabled=True,
messages="The horizontal grids used by EAM are cubed spheres. Since these are unstructed grids, QuickView needs to know how to map data to the globe. Therefore, for each simulation data file, a 'connectivity file' needs to be provided.",
)
v3.VDivider()
with v3.VCardActions(classes="pa-3"):
with v3.VTooltip(text="Set selected file as simulation file"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
classes="text-none",
variant="tonal",
text="Simulation",
prepend_icon="mdi-database-plus",
disabled=(
f"{self.name('listing')}[{self.name('active')}]?.type !== 'file'",
),
click=self.set_data_simulation,
)
with v3.VTooltip(text="Set selected file as connectivity file"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
classes="text-none",
text="Connectivity",
variant="tonal",
prepend_icon="mdi-vector-polyline-plus",
disabled=(
f"{self.name('listing')}[{self.name('active')}]?.type !== 'file'",
),
click=self.set_data_connectivity,
)
with v3.VTooltip(text="Clear selected files"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
classes="text-none",
text="Reset",
variant="tonal",
prepend_icon="mdi-close-octagon-outline",
click=f"{self.name('data_connectivity')}='';{self.name('data_simulation')}='';{self.name('error')}=false",
)
v3.VSpacer()
with v3.VTooltip(text="Cancel file loading"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
border=True,
classes="text-none",
color="surface",
text="Cancel",
variant="flat",
click=self.cancel,
)
with v3.VTooltip(text="Import previous state file"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
disabled=(f"!{self.name('is_state_file')}",),
loading=(self.name("state_loading"), False),
classes="text-none",
color="primary",
text="Import state file",
variant="flat",
click=self.import_state_file,
)
with v3.VTooltip(text="Load simulation and connectivity files"):
with v3.Template(v_slot_activator="{ props }"):
v3.VBtn(
v_bind="props",
classes="text-none",
color=(f"{self.name('error')} ? 'error' : 'primary'",),
text="Load files",
variant="flat",
disabled=(
f"!{self.name('data_simulation')} || !{self.name('data_connectivity')} || {self.name('error')}",
),
loading=(self.name("loading"), False),
click=self.load_data_files,
)