Bases: object
This is the account manager of super-users.
Each set of super-users are related to a specific scientific instrument.
These super-users own full authority to their instruments, and are responsible for the instrument management such as
updating instruction manual, adding a new super-user, etc.
The accounts of super-users are stored as a dictionary as follows in a json format.
{instrument_id: [super_user_ids, ]}
Source code in labridge\accounts\super_users.py
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 | class InstrumentSuperUserManager(object):
r"""
This is the account manager of super-users.
Each set of super-users are related to a specific scientific instrument.
These super-users own full authority to their instruments, and are responsible for the instrument management such as
updating instruction manual, adding a new super-user, etc.
The accounts of super-users are stored as a dictionary as follows in a json format.
`{instrument_id: [super_user_ids, ]}`
"""
def __init__(self):
root = Path(__file__)
for idx in range(3):
root = root.parent
self.root = root
self.super_user_ids_path = str(root / SUPER_USER_IDS_PERSIS_PATH)
self.fs = fsspec.filesystem("file")
dir_path = str(Path(self.super_user_ids_path).parent)
if not self.fs.exists(dir_path):
self.fs.makedirs(dir_path)
def _get_user_ids_dict(self) -> Dict[str, List[str]]:
r""" Get the super-user accounts dictionary. """
if not self.fs.exists(self.super_user_ids_path):
return {}
with self.fs.open(self.super_user_ids_path, "rb") as f:
super_user_ids = json.load(f)
return super_user_ids
def get_super_users(self, instrument_id: str) -> List[str]:
r""" Get the super-users of a specific instrument. """
return list(self._get_user_ids_dict()[instrument_id])
def is_super_user(self, user_id: str, instrument_id: str) -> bool:
r""" Judge whether a user is the super-user of a instrument. """
super_user_list = self.get_super_users(instrument_id=instrument_id)
return user_id in super_user_list
@staticmethod
def check_users(user_id: Union[str, List[str]]):
r""" Check whether all given users have registered."""
user_manager = AccountManager()
if not isinstance(user_id, list):
user_id = [user_id]
for user in user_id:
user_manager.check_valid_user(user_id=user)
def add_super_user(self, user_id: str, instrument_id: str):
r""" Add a new super-user for the instrument. """
super_user_ids = self._get_user_ids_dict()
self.check_users(user_id=user_id)
if instrument_id not in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} is not registered yet.")
super_user_ids[instrument_id].append(user_id)
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
def delete_super_user(self, user_id: str, instrument_id: str):
r""" Delete a super-user of the instrument. """
super_user_ids = self._get_user_ids_dict()
if instrument_id not in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} is not registered yet.")
super_user_ids[instrument_id].remove(user_id)
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
def add_instrument(self, instrument_id: str, super_users: List[str]):
r""" Add a new instrument along with its super-users. """
super_user_ids = self._get_user_ids_dict()
if instrument_id in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} already exists.")
self.check_users(user_id=super_users)
super_user_ids[instrument_id] = super_users
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
|
labridge.accounts.super_users.InstrumentSuperUserManager.add_instrument(instrument_id, super_users)
Add a new instrument along with its super-users.
Source code in labridge\accounts\super_users.py
83
84
85
86
87
88
89
90
91
92 | def add_instrument(self, instrument_id: str, super_users: List[str]):
r""" Add a new instrument along with its super-users. """
super_user_ids = self._get_user_ids_dict()
if instrument_id in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} already exists.")
self.check_users(user_id=super_users)
super_user_ids[instrument_id] = super_users
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
|
labridge.accounts.super_users.InstrumentSuperUserManager.add_super_user(user_id, instrument_id)
Add a new super-user for the instrument.
Source code in labridge\accounts\super_users.py
62
63
64
65
66
67
68
69
70
71
72 | def add_super_user(self, user_id: str, instrument_id: str):
r""" Add a new super-user for the instrument. """
super_user_ids = self._get_user_ids_dict()
self.check_users(user_id=user_id)
if instrument_id not in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} is not registered yet.")
super_user_ids[instrument_id].append(user_id)
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
|
labridge.accounts.super_users.InstrumentSuperUserManager.check_users(user_id)
staticmethod
Check whether all given users have registered.
Source code in labridge\accounts\super_users.py
52
53
54
55
56
57
58
59
60 | @staticmethod
def check_users(user_id: Union[str, List[str]]):
r""" Check whether all given users have registered."""
user_manager = AccountManager()
if not isinstance(user_id, list):
user_id = [user_id]
for user in user_id:
user_manager.check_valid_user(user_id=user)
|
labridge.accounts.super_users.InstrumentSuperUserManager.delete_super_user(user_id, instrument_id)
Delete a super-user of the instrument.
Source code in labridge\accounts\super_users.py
| def delete_super_user(self, user_id: str, instrument_id: str):
r""" Delete a super-user of the instrument. """
super_user_ids = self._get_user_ids_dict()
if instrument_id not in super_user_ids.keys():
raise ValueError(f"The instrument {instrument_id} is not registered yet.")
super_user_ids[instrument_id].remove(user_id)
with self.fs.open(self.super_user_ids_path, "w") as f:
f.write(json.dumps(super_user_ids))
|
labridge.accounts.super_users.InstrumentSuperUserManager.get_super_users(instrument_id)
Get the super-users of a specific instrument.
Source code in labridge\accounts\super_users.py
| def get_super_users(self, instrument_id: str) -> List[str]:
r""" Get the super-users of a specific instrument. """
return list(self._get_user_ids_dict()[instrument_id])
|
labridge.accounts.super_users.InstrumentSuperUserManager.is_super_user(user_id, instrument_id)
Judge whether a user is the super-user of a instrument.
Source code in labridge\accounts\super_users.py
| def is_super_user(self, user_id: str, instrument_id: str) -> bool:
r""" Judge whether a user is the super-user of a instrument. """
super_user_list = self.get_super_users(instrument_id=instrument_id)
return user_id in super_user_list
|