Completed
Pull Request — master (#22)
by Jerome
01:25
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
19
import click, os
20
21
from msquaredc.project import FileNotFoundError
22
from msquaredc.project import Project
23
from msquaredc.project import ProjectBuilder
24
from msquaredc.ui.interfaces import AbstractMenu
25
from msquaredc.ui.interfaces import AbstractPresenter
26
27
28
@click.command()
29
@click.option('--config-file', default=None, help="Location of the project configuration file.")
30
@click.option("--data-file", default=None, help="Location of the data file.")
31
@click.option("--user-interface", default="gui", help="User interface to start. [tui | gui | web]")
32
@click.option("--loglevel", default="warning", help="On which level to log. [debug | info | warning | error | critical]")
33
@click.option("--logfile", default="logfile.log", help="Where to log.")
34
@click.option("--coder", default=None, help="Current coder.")
35
def main(config_file=None, data_file=None, user_interface="gui", loglevel="warning", logfile="logfile.log",coder = None):
36
    """Command line interface to msquaredc."""
37
    setup_logging(loglevel,logfile)
38
    presenter = None
39
40
    pb = ProjectBuilder(data=data_file,config=config_file,coder=coder)
41
42
    if user_interface == "gui":
43
        from msquaredc.ui.gui.presenter import GUIPresenter
44
        from msquaredc.ui.gui.menu import GUIMenu
45
        presenter = GUIPresenter(menuclass=GUIMenu,projectbuilder=pb)
46
    elif user_interface == "tui":
47
        from msquaredc.ui.tui.presenter import TUIPresenter
48
        from msquaredc.ui.tui.menu import TUIMenu
49
        presenter = TUIPresenter(menuclass=TUIMenu,projectbuilder=pb)
50
    elif user_interface == "web":
51
        print("NotSupportedYet")
52
        exit(0)
53
    else:
54
        presenter = AbstractPresenter(projectbuilder=pb)
55
56
    presenter.run()
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
def check_file(filename):
76
    if filename is None or not os.path.exists(filename):
77
        logging.log(logging.CRITICAL,"Couldn't find the file '{}'.".format(filename))
78
        import sys
79
        sys.exit(1)
80
    else:
81
        logging.log(logging.DEBUG, "Found the file '{}'.".format(filename))
82
83
if __name__ == "__main__":  # pragma no cover
84
    main()
85