Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2@file 

3@brief Defines helpers used in different places of the module. 

4""" 

5import importlib 

6import logging 

7import os 

8import sys 

9import pandas 

10 

11 

12def read_options(config): 

13 """ 

14 Reads configuration from a file or a dictionary. 

15 """ 

16 config_options = {} 

17 if config is not None: 

18 if isinstance(config, dict): 

19 config_options.update(config) 

20 elif isinstance(config, str) and os.path.exists(config): 

21 app_log = logging.getLogger("tornado.application") 

22 app_log.info("[LightMLBoard] read file '{0}'".format(config)) 

23 obj = None 

24 fname = os.path.splitext(config)[0] 

25 path, name = os.path.split(fname) 

26 if path: 

27 sys.path.append(path) 

28 mod = importlib.import_module(name) 

29 del sys.path[-1] 

30 else: 

31 mod = importlib.import_module(name) 

32 for k, v in mod.__dict__.items(): 

33 if isinstance(v, pandas.DataFrame.__class__): 

34 obj = v 

35 break 

36 if obj is None: 

37 raise ValueError( 

38 "Unable to read configuration '{0}'.".format(config)) 

39 for k, v in obj.__dict__.items(): 

40 if not k.startswith('_'): 

41 config_options[k] = v 

42 else: 

43 raise ValueError( 

44 "Unable to interpret config\ncwd: '{0}'\n{1}".format(os.getcwd(), config)) 

45 return config_options 

46 

47 

48def read_users(filename): 

49 """ 

50 Reads users definition. 

51 """ 

52 df = pandas.read_csv(filename) 

53 cols = list(sorted(df.columns)) 

54 df = df[cols] # pylint: disable=E1136 

55 exp = "login,mail,name,pwd,team".split(",") 

56 has = list(df.columns) 

57 if exp != has: 

58 raise ValueError( 

59 "Users should be defined in CSV file with columns: {0}, not {1}".format(exp, has)) 

60 users = {} 

61 for i in range(0, df.shape[0]): 

62 name, login, mail, pwd, team = (df.loc[i, 'name'], df.loc[i, 'login'], 

63 df.loc[i, 'mail'], df.loc[i, 'pwd'], df.loc[i, 'team']) 

64 if login in users: 

65 raise ValueError("Duplicated user: '{0}'.".format(login)) 

66 users[login] = dict(mail=mail, pwd=pwd, team=team, 

67 name=name, login=login) 

68 return users