Completed
Push — master ( 27a8f9...db34c1 )
by Jerome
55s queued 19s
created

setup_logging()   A

Complexity

Conditions 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 16
rs 9.4285
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
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('--project-file', default="", help="Location of the project file.")
29
@click.option("--user-interface", default="gui", help="User interface to start. [tui | gui | web]")
30
@click.option("--loglevel", default="warning", help="On which level to log. [debug | info | warning | error | critical]")
31
@click.option("--logfile", default="logfile.log", help="Where to log.")
32
def main(project_file="", user_interface="gui", loglevel="warning", logfile="logfile.log"):
33
    """Command line interface to msquaredc."""
34
    setup_logging(loglevel,logfile)
35
    presenter = None
36
    if user_interface == "gui":
37
        from msquaredc.ui.gui.presenter import GUIPresenter
38
        from msquaredc.ui.gui.menu import GUIMenu
39
        presenter = GUIPresenter(menuclass=GUIMenu)
40
    elif user_interface == "tui":
41
        from msquaredc.ui.tui.presenter import TUIPresenter
42
        from msquaredc.ui.tui.menu import TUIMenu
43
        presenter = TUIPresenter(menuclass=TUIMenu)
44
    elif user_interface == "web":
45
        print("NotSupportedYet")
46
    else:
47
        presenter = AbstractPresenter()
48
    if project_file is None:
49
        project = presenter.new_project_wizard()
50
    else:
51
        try:
52
            project = Project(path=project_file)
53
        except FileNotFoundError:
54
            project = presenter.new_project_wizard(path=project_file)
55
    presenter.load_mainframe(project)
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
if __name__ == "__main__":  # pragma no cover
76
    main()
77