-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathprivilege.py
More file actions
88 lines (78 loc) · 2.43 KB
/
Copy pathprivilege.py
File metadata and controls
88 lines (78 loc) · 2.43 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
from typing import Any, Iterator
from tabulate import tabulate
class _Privilege:
def __init__(self, privilege) -> None:
self.scope: str = ""
self.org_id: str = ""
self.org_name: str = ""
self.msp_id: str = ""
self.msp_name: str = ""
self.orggroup_ids: list[str] = []
self.name: str = ""
self.role: str = ""
self.site_id: str = ""
self.sitegroup_ids: list[str] = []
self.views: list[str] = []
for key, val in privilege.items():
setattr(self, key, val)
def __str__(self) -> str:
fields = [
"scope",
"role",
"org_id",
"org_name",
"msp_id",
"msp_name",
"orggroup_ids",
"name",
"role",
"site_id",
"sitegroup_ids",
]
string = ""
for field in fields:
if getattr(self, field) != "":
string += f"{field}: {getattr(self, field)} \r\n"
return string
def get(self, key: str, default: Any | None = None) -> Any:
if hasattr(self, key) and getattr(self, key):
return getattr(self, key)
else:
return default
class Privileges:
def __init__(self, privileges: list[dict | _Privilege]) -> None:
self.privileges: list[_Privilege] = []
for privilege in privileges:
if isinstance(privilege, _Privilege):
self.privileges.append(privilege)
else:
self.privileges.append(_Privilege(privilege))
def __iter__(self) -> Iterator[_Privilege]:
"""Return an iterator over the privileges."""
if not self.privileges:
return iter([])
return iter(self.privileges)
def __str__(self) -> str:
columns_headers = [
"scope",
"role",
"name",
"site_id",
"org_name",
"org_id",
"msp_name",
"msp_id",
"views",
]
table = []
for entry in self.privileges:
temp = []
for field in columns_headers:
if hasattr(entry, field):
temp.append(str(getattr(entry, field)))
else:
temp.append("")
table.append(temp)
return tabulate(table, columns_headers)
def display(self):
return str(self)