This repository has been archived by the owner on Jul 23, 2022. It is now read-only.
/
cli_helper.py
65 lines (55 loc) · 1.85 KB
/
cli_helper.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
"""
@file
@brief Helpers to interpet command line parameters.
"""
import os
import importlib
from ..tests import get_game
def name2activity(name):
"""
Converts something like ``mathenjeu.tests.qcms.py:simple_french_qcm``
into a class name. Calls function *simple_french_qcm*.
:param name: name
:return: result of simple_french_qcm
It works if *name* contains ``':'`` otherwise
it returns *name*.
"""
if ':' in name:
spl = name.split(':')
modname = os.path.splitext(spl[0])[0]
try:
mod = importlib.import_module(modname)
except ImportError as e: # pragma: no cover
raise ImportError("Unable to import '{0}'".format(spl[0])) from e
if not hasattr(mod, spl[1]):
raise NameError(
"Unable to find '{0}' in '{1}'".format(spl[1], spl[0]))
return spl[1], getattr(mod, spl[1])()
if isinstance(name, str):
raise TypeError( # pragma: no cover
"name '{0}' cannot be a string.".format(name))
return name.__class__.__name__, name # pragma: no cover
def build_games(games, fct_game):
"""
Interprets parameters.
:param games: string
:param fct_game: function which returns a game
based on its name
:return: modified *games*, *fct_game*
"""
if isinstance(games, str) and fct_game is None:
apps = [el.strip().split(',') for el in games.split(';')]
games = {}
games_obj = {}
for k, n, p in apps:
try:
name, obj = name2activity(k)
games[name] = (n, p)
games_obj[name] = obj
except TypeError:
games[k] = (n, p)
games_obj[k] = get_game(k)
def get_games2(name):
return games_obj[name]
return games, get_games2
return games, fct_game