-
Notifications
You must be signed in to change notification settings - Fork 0
/
accounts.py
79 lines (54 loc) · 2.35 KB
/
accounts.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
77
78
79
import logging
import pandas as pd
from hs_restclient import HydroShare, HydroShareAuthBasic
class HSAccount(object):
def __init__(self, uname, pwd, hs_url, port, use_https, verify_https, *args, **kargs):
self.uname = uname
self.pwd = pwd
self.hs_url = hs_url
self.port = port
self.use_https = use_https
self.verify_https = verify_https
self.hs_auth = self._get_hs_auth()
self.hs = self._get_hs()
def _get_hs_auth(self):
auth = HydroShareAuthBasic(username=self.uname, password=self.pwd)
return auth
def _get_hs(self):
try:
return HydroShare(auth=self.hs_auth, hostname=self.hs_url,
port=self.port, use_https=self.use_https, verify=self.verify_https)
except Exception as ex:
logging.error(ex)
class CZOHSAccount(object):
_uname_hs_dict = dict()
def __init__(self, accounts_info):
self._df = pd.DataFrame(accounts_info)
for account_dict in accounts_info:
hs_account = HSAccount(**account_dict)
self._uname_hs_dict[account_dict["uname"]] = hs_account
def get_hs_by_uname(self, uname):
# uname -> hs obj
return self._uname_hs_dict.get(uname).hs
def get_hs_by_czo(self, czo):
# czo -> uname
uname = self.query("czo", czo, "uname", case_sensitive=False)
# uname -> hs obj
hs_account_info = self._uname_hs_dict.get(uname)
if hs_account_info is None:
logging.warning("Not found HS account for CZO {}".format(czo))
hs_account_info = self._uname_hs_dict.get(self.get_uname_by_czo("default"))
logging.info("Connecting to {} with account {}".format(hs_account_info.hs_url, hs_account_info.uname))
return hs_account_info.hs
def get_group_by_uname(self, uname):
# uname -> group
return self.query("uname", uname, "group")
def get_uname_by_czo(self, czo):
# czo -> uname
return self.query("czo", czo, "uname", case_sensitive=False)
def query(self, in_column, in_value, out_column, case_sensitive=True):
if case_sensitive:
row = self._df.loc[self._df[in_column] == in_value]
else:
row = self._df.loc[self._df[in_column].str.lower() == in_value.lower()]
return row[out_column].values[0]