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 Helpers to interpet command line parameters. 

4""" 

5import os 

6import importlib 

7from ..tests import get_game 

8 

9 

10def name2activity(name): 

11 """ 

12 Converts something like ``mathenjeu.tests.qcms.py:simple_french_qcm`` 

13 into a class name. Calls function *simple_french_qcm*. 

14 

15 :param name: name 

16 :return: result of simple_french_qcm 

17 

18 It works if *name* contains ``':'`` otherwise 

19 it returns *name*. 

20 """ 

21 if ':' in name: 

22 spl = name.split(':') 

23 modname = os.path.splitext(spl[0])[0] 

24 try: 

25 mod = importlib.import_module(modname) 

26 except ImportError as e: # pragma: no cover 

27 raise ImportError("Unable to import '{0}'".format(spl[0])) from e 

28 if not hasattr(mod, spl[1]): 

29 raise NameError( 

30 "Unable to find '{0}' in '{1}'".format(spl[1], spl[0])) 

31 return spl[1], getattr(mod, spl[1])() 

32 if isinstance(name, str): 

33 raise TypeError( # pragma: no cover 

34 "name '{0}' cannot be a string.".format(name)) 

35 return name.__class__.__name__, name # pragma: no cover 

36 

37 

38def build_games(games, fct_game): 

39 """ 

40 Interprets parameters. 

41 

42 :param games: string 

43 :param fct_game: function which returns a game 

44 based on its name 

45 :return: modified *games*, *fct_game* 

46 """ 

47 if isinstance(games, str) and fct_game is None: 

48 apps = [el.strip().split(',') for el in games.split(';')] 

49 games = {} 

50 games_obj = {} 

51 

52 for k, n, p in apps: 

53 try: 

54 name, obj = name2activity(k) 

55 games[name] = (n, p) 

56 games_obj[name] = obj 

57 except TypeError: 

58 games[k] = (n, p) 

59 games_obj[k] = get_game(k) 

60 

61 def get_games2(name): 

62 return games_obj[name] 

63 

64 return games, get_games2 

65 return games, fct_game