forked from fzyzcjy/grafana_dashboard_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
61 lines (43 loc) · 1.51 KB
/
utils.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
import itertools
from dataclasses import dataclass
from enum import Enum
from typing import TYPE_CHECKING
from pydantic import BaseModel, Extra
if TYPE_CHECKING:
from grafana_dashboard.model.dashboard_types_gen import Dashboard
class MyBaseModel(BaseModel):
def __repr_args__(self):
return [
(k, _repr_transform_arg_value(v))
for k, v in super().__repr_args__()
if _repr_should_keep_arg(self.__class__, k, v)
]
class Config:
extra = Extra.allow # #10131
# https://github.com/fzyzcjy/yplusplus/issues/10176#issuecomment-1634063216
allow_population_by_field_name = True
def _repr_transform_arg_value(v):
# #10118
if isinstance(v, Enum):
return _Reprable(str(v))
return v
def _repr_should_keep_arg(cls, k: str, v):
# https://github.com/fzyzcjy/yplusplus/issues/10117#issuecomment-1630217216
if v is None:
return False
info = cls.schema().get('properties', {}).get(k)
# print(f'hi cls={cls} k={k} v={v} info={info}')
if info is None or 'default' not in info:
return True
return v != info['default']
@dataclass
class _Reprable:
content: str
def __repr__(self):
return self.content
def dashboard_auto_panel_ids(dashboard: 'Dashboard'):
ids = set([panel.id for panel in dashboard.panels if panel.id])
auto_ids = (i for i in itertools.count(1) if i not in ids)
for panel in dashboard.panels:
if panel.id is None:
panel.id = next(auto_ids)