CSIT-755: Presentation and analytics layer
[csit.git] / resources / tools / presentation / presentation.py
1 # Copyright (c) 2017 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """CSIT Presentation and analytics layer.
15
16 TODO: Write the description.
17
18 TODO: Write the description and specification of YAML configuration file.
19
20 """
21
22 import sys
23 import argparse
24 import logging
25
26 from errors import PresentationError
27 from environment import Environment, clean_environment
28 from configuration import Configuration
29 from inputs import download_data_files, unzip_files
30 from data import InputData
31
32
33 def parse_args():
34     """Parse arguments from cmd line.
35
36     :returns: Parsed arguments.
37     :rtype: ArgumentParser
38     """
39
40     parser = argparse.ArgumentParser(description=__doc__,
41                                      formatter_class=argparse.
42                                      RawDescriptionHelpFormatter)
43     parser.add_argument("-c", "--configuration",
44                         required=True,
45                         type=argparse.FileType('r'),
46                         help="Configuration YAML file.")
47     parser.add_argument("-l", "--logging",
48                         choices=["DEBUG", "INFO", "WARNING",
49                                  "ERROR", "CRITICAL"],
50                         default="ERROR",
51                         help="Logging level.")
52     parser.add_argument("-f", "--force",
53                         action='store_true',
54                         help="Force removing the old build(s) if present.")
55
56     return parser.parse_args()
57
58
59 def main():
60     """Main function."""
61
62     log_levels = {"NOTSET": logging.NOTSET,
63                   "DEBUG": logging.DEBUG,
64                   "INFO": logging.INFO,
65                   "WARNING": logging.WARNING,
66                   "ERROR": logging.ERROR,
67                   "CRITICAL": logging.CRITICAL}
68
69     args = parse_args()
70     logging.basicConfig(format='%(asctime)s: %(levelname)s: %(message)s',
71                         datefmt='%Y/%m/%d %H:%M:%S',
72                         level=log_levels[args.logging])
73
74     logging.info("Application started.")
75     try:
76         config = Configuration(args.configuration)
77         config.parse_cfg()
78     except PresentationError:
79         logging.critical("Finished with error.")
80         sys.exit(1)
81
82     try:
83         env = Environment(config.environment, args.force)
84         env.set_environment()
85
86         if config.is_debug:
87             if config.debug["input-format"] == "zip":
88                 unzip_files(config)
89         else:
90             download_data_files(config)
91
92         data = InputData(config)
93         data.parse_input_data()
94
95         logging.info("Successfully finished.")
96
97     except (KeyError, ValueError, PresentationError) as err:
98         logging.info("Finished with an error.")
99         logging.critical(str(err))
100     except Exception as err:
101         logging.info("Finished with an error.")
102         logging.critical(str(err))
103
104     finally:
105         if config is not None and not config.is_debug:
106             clean_environment(config.environment)
107         sys.exit(1)
108
109
110 if __name__ == '__main__':
111     main()