Completed
Push — master ( db34c1...589162 )
by Jerome
12s
created

check_file()   A

Complexity

Conditions 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
"""
2
Module that contains the command line app.
3
4
Why does this file exist, and why not put this in __main__?
5
6
  You might be tempted to import things from __main__ later, but that will cause
7
  problems: the code will get executed twice:
8
9
  - When you run `python -mmsquaredc` python will execute
10
    ``__main__.py`` as a script. That means there won't be any
11
    ``msquaredc.__main__`` in ``sys.modules``.
12
  - When you import __main__ it will get executed again (as a module) because
13
    there's no ``msquaredc.__main__`` in ``sys.modules``.
14
15
  Also see (1) from http://click.pocoo.org/5/setuptools/#setuptools-integration
16
"""
17
import logging
18
import os
19
20
import click
21
22
from msquaredc.project import ProjectBuilder
23
from msquaredc.ui.interfaces import AbstractPresenter
24
25
26
@click.command()
27
@click.option('--config-file', default=None, help="Location of the project configuration file.")
28
@click.option("--data-file", default=None, help="Location of the data file.")
29
@click.option("--user-interface", default="gui", help="User interface to start. [tui | gui | web]")
30
@click.option("--loglevel", default="warning",
31
              help="On which level to log. [debug | info | warning | error | critical]")
32
@click.option("--logfile", default="logfile.log", help="Where to log.")
33
@click.option("--coder", default=None, help="Current coder.")
34
def main(config_file=None, data_file=None, user_interface="gui", loglevel="warning", logfile="logfile.log", coder=None):
35
    """Command line interface to msquaredc."""
36
    setup_logging(loglevel, logfile)
37
    presenter = None
38
39
    pb = ProjectBuilder(data=data_file, config=config_file, coder=coder)
40
41
    if user_interface == "gui":
42
        from msquaredc.ui.gui.presenter import GUIPresenter
43
        from msquaredc.ui.gui.menu import GUIMenu
44
        presenter = GUIPresenter(menuclass=GUIMenu, projectbuilder=pb)
45
    elif user_interface == "tui":
46
        from msquaredc.ui.tui.presenter import TUIPresenter
47
        from msquaredc.ui.tui.menu import TUIMenu
48
        presenter = TUIPresenter(menuclass=TUIMenu, projectbuilder=pb)
49
    elif user_interface == "web":
50
        print("NotSupportedYet")
51
        exit(0)
52
    else:
53
        presenter = AbstractPresenter(projectbuilder=pb)
54
55
    presenter.run()
56
57
58
def setup_logging(loglevel, logfile):
59
    translated = {"debug": logging.DEBUG,
60
                  "info": logging.INFO,
61
                  "warning": logging.WARNING,
62
                  "error": logging.ERROR,
63
                  "critical": logging.CRITICAL}
64
    logging.basicConfig(level=logging.DEBUG,
65
                        format='%(asctime)s %(name)-30s %(levelname)-8s %(message)s',
66
                        datefmt='%Y-%d-%m %H:%M:%S',
67
                        filename=logfile,
68
                        filemode="a")
69
    console = logging.StreamHandler()
70
    console.setLevel(translated.get(loglevel, logging.WARNING))
71
    formatter = logging.Formatter('%(name)-30s %(levelname)-8s %(message)s')
72
    console.setFormatter(formatter)
73
    logging.getLogger("").addHandler(console)
74
75
76
def check_file(filename):
77
    if filename is None or not os.path.exists(filename):
78
        logging.log(logging.CRITICAL, "Couldn't find the file '{}'.".format(filename))
79
        import sys
80
        sys.exit(1)
81
    else:
82
        logging.log(logging.DEBUG, "Found the file '{}'.".format(filename))
83
84
85
if __name__ == "__main__":  # pragma no cover
86
    main()
87