def __init__(self, config=None, mission_phase='BEPICRUISE'): if config: self.conf = config else: self.conf = { "username": "******", "password": "******", "base_url": "https://bepicolombo.esac.esa.int/webclient-must/mustlink" } self.data_provider = mission_phase pd.options.display.max_rows = 10000 pd.options.display.max_columns = 5000 # Initialize internal pynk status with the default Mustlink instance pynk.init_defaults(self.conf) # Instantiate a MUSTLink object. self.mlink = pynk.get(self.conf) self.bepic = self.mlink.data_providers.load_data_provider(mission_phase)
def usermod(login, email=None, name=None, password=None, add_groups=None, remove_groups=None, mustlink=None): ml = pynk.get(mustlink) user = ml.users.load(login) if not user: raise ValueError("User %s not found" % login) if add_groups or remove_groups: _all_groups = add_groups if add_groups else [] + remove_groups if remove_groups else [] for group in [ml.groups.load(g) for g in _all_groups]: if not group: raise ValueError("Group %s not found", group) if add_groups: ml.groups.add_user(group, user) elif remove_groups: ml.groups.remove_user(group, user) if email or name: if name: user['name'] = name if email: user['email'] = email ml.users.update(user) if password: ml.users.change_password(user['login'], password)
def get_statistics(data_provider, parameter_names, start, end, mustlink=None, **kwargs): dp = pynk.get(mustlink).data_providers.load_data_provider(data_provider) stats = dp.get_statistics(parameter_names, start, end, **kwargs) dataframe = pd.DataFrame(stats) dataframe.set_index('parameter', inplace=True) return dataframe
def get_parameters(data_provider, key=None, value=None, parameter_type=None, mustlink=None): dp = pynk.get(mustlink).data_providers.load_data_provider(data_provider) params = dp.get_parameters(key=key, value=value, parameter_type=parameter_type) dataframe = pd.DataFrame(params) dataframe.set_index('Id', inplace=True) return dataframe
def list_projects(name=None, mustlink=None): ml = pynk.get(mustlink) ProjectTuple = collections.namedtuple('ProjectTuple', 'id,name,description') table = [] for project in sorted(ml.projects.search(name), key=lambda p: p['name'].lower()): table.append( ProjectTuple(project['id'], project['name'], project['description'])) return table
def list_users(name=None, group=None, email=None, mustlink=None): ml = pynk.get(mustlink) UserTuple = collections.namedtuple( 'UserTuple', 'id,login,email,name,groups,projects,roles') table = [] for user in sorted(ml.users.search(name, group, email), key=lambda x: x['login']): table.append( UserTuple(user['id'], user['login'], user['email'], user['name'], [g['name'] for g in user['groups']], [p['name'] for p in user['projects']], [r['name'] for r in user['roles']])) return table
def list_groups(name=None, mustlink=None): ml = pynk.get(mustlink) GroupTuple = collections.namedtuple('GroupTuple', 'id,name,description,projects,roles') table = [] for group in sorted(ml.groups.search(name), key=lambda g: g['name'].lower()): table.append( GroupTuple( group['id'], group['name'], group['description'], [p['name'] for p in group['projects']], [r['name'] for r in group['roles']], )) return table
def get_table_data(data_provider, table, start, end, mode='BRIEF', max_rows=5000, filter_keys=[' '], filter_values=[' '], mustlink=None): dp = pynk.get(mustlink).data_providers.load_data_provider(data_provider) table = dp.get_table_data(table, start, end, mode=mode, max_rows=max_rows, filter_keys=filter_keys, filter_values=filter_values) headers_dict = table['headers'] data_dict = table['data'] dataframe = pd.DataFrame(data_dict) dataframe = dataframe.rename(columns=headers_dict) return dataframe
def groupmod(group_name, description=None, add_users=None, remove_users=None, add_projects=None, remove_projects=None, add_roles=None, remove_roles=None, mustlink=None): ml = pynk.get(mustlink) group = ml.groups.load(group_name) if not group: raise ValueError("Group %s doesn't exist" % group_name) if description: group['description'] = description ml.groups.update(group) def do_link(entities, manager, subject, func): if entities: _list = [] for _e, _entity in ((e, manager.load(e)) for e in entities): if not _entity: raise ValueError("%s %s doesn't exist" % (subject, _e)) _list.append(_entity) for _e in _list: func(group, _e) do_link(add_users, ml.users, 'User', ml.groups.add_user) do_link(remove_users, ml.users, 'User', ml.groups.remove_user) do_link(add_projects, ml.projects, 'Project', ml.groups.add_project) do_link(remove_projects, ml.projects, 'Project', ml.groups.remove_project) do_link(add_roles, ml.roles, 'Role', ml.groups.add_role) do_link(remove_roles, ml.roles, 'Role', ml.groups.remove_role)
def useradd(login, name, email, password, groups, force_email=False, mustlink=None): ml = pynk.get(mustlink) if ml.users.load(login): raise ValueError("User %s already exists" % login) if len(ml.users.search(email=email)) > 0 and force_email is False: raise ValueError("Users with email %s already exist" % email) user_groups = [] for g in groups: _g = ml.groups.load(g) if _g: user_groups.append(_g) else: raise ValueError("Group %s doesn't exist" % g) new_user = { "login": login, "name": name, "email": email, "password": password, "roles": [], "groups": user_groups, "projects": [] } # the user creation end point doesn't link the user to the groups ml.users.create(new_user) # thus provide linking groups and users for group in user_groups: ml.groups.add_user(group, new_user)
def userdel(login, mustlink=None): ml = pynk.get(mustlink) if ml.users.load(login) is None: raise ValueError("User %s doesn't exists" % login) ml.users.delete(login)
def check_user(login, mustlink=None): user = pynk.get(mustlink).users.load(login) if not user: raise ValueError("User %s doesn't exists" % login)
def load_data(data_provider, param_names, start, end, time_slice_seconds=None, mustlink=None, **kwargs): """ Retrieve data from MUSTLINK and transform metadata/values lists into object with named tuples Column and Data :param data_provider: :param param_names: :param start: :param end: :param time_slice_seconds: :param mustlink: :param kwargs: see at pynk.DataProvider.load_parameters_data: calibrate=None, aggregation_function=None, aggregation_type=None, aggregation_value=None, compression_error=None, chunk_count=None :return: a list of tuples for each column, with Column tuple as definition and a list of Data tuples """ Data = collections.namedtuple('Data', 'date, value, calibrated_value') dp = pynk.get(mustlink).data_providers.load_data_provider(data_provider) if not dp: raise ValueError("Data provider %s doesn't exist" % data_provider) def retrieve(_start, _end): def map_result(api_result): #metadata is variable. Different dataproviders supply different metadata parameters, so we can not predefine it metadata = list(api_result.keys()) metadata.remove('data') # create the row as a list to ensure that we can populate the namedtuple row = [api_result[key] for key in metadata] Column = collections.namedtuple('Column', metadata) #build the namedtuple from the given list col = Column(*row) values = [ Data(int(d['date']), d['value'], d['calibratedValue']) for d in api_result['data'] ] return col, values return [ map_result(param_value) for param_value in dp.get_parameters_data( param_names, _start, _end, **kwargs) ] if time_slice_seconds: real_start = start if type(start) == datetime else datetime.strptime( start, pynk.DT_FMT) real_end = end if type(end) == datetime else datetime.strptime( end, pynk.DT_FMT) return _TimeSlicerIterable2(retrieve, real_start, real_end, time_slice_seconds) else: return retrieve(start, end)