00a78cc99ae48c0db87d25108d9153a543562a19
[csit.git] / resources / tools / presentation / generator_files.py
1 # Copyright (c) 2018 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 """Algorithms to generate files.
15 """
16
17
18 import logging
19
20 from pal_utils import get_files, get_rst_title_char
21
22
23 RST_INCLUDE_TABLE = (u"\n.. only:: html\n\n"
24                      u"    .. csv-table::\n"
25                      u"        :header-rows: 1\n"
26                      u"        :widths: auto\n"
27                      u"        :align: center\n"
28                      u"        :file: {file_html}\n"
29                      u"\n.. only:: latex\n\n"
30                      u"\n  .. raw:: latex\n\n"
31                      u"      \\csvautolongtable{{{file_latex}}}\n\n")
32
33
34 def generate_files(spec, data):
35     """Generate all files specified in the specification file.
36
37     :param spec: Specification read from the specification file.
38     :param data: Data to process.
39     :type spec: Specification
40     :type data: InputData
41     """
42
43     generator = {
44         u"file_test_results": file_test_results
45     }
46
47     logging.info(u"Generating the files ...")
48     for file_spec in spec.files:
49         try:
50             generator[file_spec[u"algorithm"]](file_spec, data)
51         except (NameError, KeyError) as err:
52             logging.error(
53                 f"Probably algorithm {file_spec[u'algorithm']} is not defined: "
54                 f"{repr(err)}"
55             )
56     logging.info(u"Done.")
57
58
59 def _tests_in_suite(suite_name, tests):
60     """Check if the suite includes tests.
61
62     :param suite_name: Name of the suite to be checked.
63     :param tests: Set of tests
64     :type suite_name: str
65     :type tests: pandas.Series
66     :returns: True if the suite includes tests.
67     :rtype: bool
68     """
69
70     for key in tests.keys():
71         if suite_name == tests[key][u"parent"]:
72             return True
73     return False
74
75
76 def file_test_results(file_spec, input_data):
77     """Generate the file(s) with algorithms
78     - file_test_results
79     specified in the specification file.
80
81     :param file_spec: File to generate.
82     :param input_data: Data to process.
83     :type file_spec: pandas.Series
84     :type input_data: InputData
85     """
86
87     file_name = f"{file_spec[u'output-file']}{file_spec[u'output-file-ext']}"
88     rst_header = file_spec[u"file-header"]
89
90     logging.info(f"  Generating the file {file_name} ...")
91
92     table_lst = get_files(file_spec[u"dir-tables"], u".csv", full_path=True)
93     if not table_lst:
94         logging.error(
95             f"  No tables to include in {file_spec[u'dir-tables']}. Skipping."
96         )
97         return
98
99     logging.info(f"    Writing file {file_name}")
100
101     logging.info(
102         f"    Creating the tests data set for the "
103         f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
104     )
105     tests = input_data.filter_data(file_spec)
106     tests = input_data.merge_data(tests)
107
108     logging.info(
109         f"    Creating the suites data set for the "
110         f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
111     )
112     file_spec[u"filter"] = u"all"
113     suites = input_data.filter_data(file_spec, data_set=u"suites")
114     suites = input_data.merge_data(suites)
115     suites.sort_index(inplace=True)
116
117     with open(file_name, u"w") as file_handler:
118         file_handler.write(rst_header)
119         for suite_longname, suite in suites.items():
120             if len(suite_longname.split(u".")) <= \
121                     file_spec[u"data-start-level"]:
122                 continue
123
124             title_line = \
125                 get_rst_title_char(
126                     suite[u"level"] - file_spec[u"data-start-level"] - 1
127                 ) * len(suite[u"name"])
128             if not (u"-ndrpdr" in suite[u"name"] or
129                     u"-mrr" in suite[u"name"] or
130                     u"-func" in suite[u"name"] or
131                     u"-device" in suite[u"name"]):
132                 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
133
134             if _tests_in_suite(suite[u"name"], tests):
135                 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
136                 file_handler.write(
137                     f"\n{suite[u'doc']}\n".replace(u'|br|', u'\n\n -')
138                 )
139                 for tbl_file in table_lst:
140                     if suite[u"name"] in tbl_file:
141                         file_handler.write(
142                             RST_INCLUDE_TABLE.format(
143                                 file_latex=tbl_file,
144                                 file_html=tbl_file.split(u"/")[-1]))
145
146     logging.info(u"  Done.")