Completed
Pull Request — master (#22)
by Jerome
25s
created

check_file()   A

Complexity

Conditions 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
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.ui.interfaces import AbstractMenu
24
from msquaredc.ui.interfaces import AbstractPresenter
25
26
27
@click.command()
28
@click.option('--config-file', default=None, help="Location of the project configuration file.")
29
@click.option("--data-file", default="data.txt", help="Location of the data file.")
30
@click.option("--user-interface", default="gui", help="User interface to start. [tui | gui | web]")
31
@click.option("--loglevel", default="warning", 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="data.txt", user_interface="gui", loglevel="warning", logfile="logfile.log",coder = None):
35
    """Command line interface to msquaredc."""
36
    setup_logging(loglevel,logfile)
37
    check_file(config_file)
38
    check_file(data_file)
39
    presenter = None
40
    if user_interface == "gui":
41
        from msquaredc.ui.gui.presenter import GUIPresenter
42
        from msquaredc.ui.gui.menu import GUIMenu
43
        presenter = GUIPresenter(menuclass=GUIMenu)
44
    elif user_interface == "tui":
45
        from msquaredc.ui.tui.presenter import TUIPresenter
46
        from msquaredc.ui.tui.menu import TUIMenu
47
        presenter = TUIPresenter(menuclass=TUIMenu)
48
    elif user_interface == "web":
49
        print("NotSupportedYet")
50
    else:
51
        presenter = AbstractPresenter()
52
    if config_file is None:
53
        project = presenter.new_project_wizard()
54
    else:
55
        try:
56
            #coder,disamb = presenter.getCoder()
57
            project = Project(data=data_file, questions=config_file, coder=coder)
58
        except FileNotFoundError:
59
            project = presenter.new_project_wizard(path=config_file, questions=config_file)
60
    presenter.load_mainframe(project)
61
    presenter.run()
62
63
def setup_logging(loglevel,logfile):
64
    translated = {"debug": logging.DEBUG,
65
                  "info": logging.INFO,
66
                  "warning": logging.WARNING,
67
                  "error": logging.ERROR,
68
                  "critical": logging.CRITICAL}
69
    logging.basicConfig(level=logging.DEBUG,
70
                        format='%(asctime)s %(name)-30s %(levelname)-8s %(message)s',
71
                        datefmt='%Y-%d-%m %H:%M:%S',
72
                        filename=logfile,
73
                        filemode="a")
74
    console = logging.StreamHandler()
75
    console.setLevel(translated.get(loglevel, logging.WARNING))
76
    formatter = logging.Formatter('%(name)-30s %(levelname)-8s %(message)s')
77
    console.setFormatter(formatter)
78
    logging.getLogger("").addHandler(console)
79
80
def check_file(filename):
81
    if not os.path.exists(filename):
82
        logging.log(logging.CRITICAL,"Couldn't find the file '{}'.".format(filename))
83
        import sys
84
        sys.exit(1)
85
    else:
86
        logging.log(logging.DEBUG, "Found the file '{}'.".format(filename))
87
88
if __name__ == "__main__":  # pragma no cover
89
    main()
90