-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
76 lines (57 loc) · 1.96 KB
/
util.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
# coding: utf-8
from __future__ import annotations
__all__: list[str] = []
import os
import law # type: ignore[import-untyped]
def expand_path(path: str, abs: bool = False, dir: bool = False) -> str:
path = os.path.expandvars(os.path.expanduser(str(path)))
if abs:
path = os.path.abspath(path)
if dir:
path = os.path.dirname(path)
return path
class Model(object):
def __init__(self, model_file: str, name: str, label: str, **kwargs) -> None:
super().__init__(**kwargs)
self.model_file = expand_path(model_file, abs=True)
self.name = name
self.label = label
self._color = None
# cached data
self._all_data = None
self._data = None
@property
def data(self):
if self._data is None:
all_data = law.LocalFileTarget(self.model_file).load(formatter="yaml")
if "model" not in all_data:
raise Exception(f"model file '{self.model_file}' is missing 'model' field")
self._data = all_data["model"]
self._all_data = all_data
return self._data
@property
def full_name(self):
if self.name:
return self.name
# create a hash
name = os.path.splitext(os.path.basename(self.model_file))[0]
return f"{name}_{law.util.create_hash(self.model_file)}"
@property
def full_model_label(self):
if self.label:
return self.label
# get the model.label field in the model data
model_label = self.data.get("label")
if model_label:
return model_label
# get the model.name field in the model data
model_name = self.data.get("name")
if model_name:
return model_name
# fallback to the full model name
return self.full_name
@property
def color(self):
if self._color is None:
self._color = self.data.get("color")
return self._color