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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Algorithms to generate files.
20 from pal_utils import get_files, get_rst_title_char
23 RST_INCLUDE_TABLE = (u"\n.. only:: html\n\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")
34 def generate_files(spec, data):
35 """Generate all files specified in the specification file.
37 :param spec: Specification read from the specification file.
38 :param data: Data to process.
39 :type spec: Specification
44 u"file_test_results": file_test_results,
45 u"file_test_results_html": file_test_results_html
48 logging.info(u"Generating the files ...")
49 for file_spec in spec.files:
51 generator[file_spec[u"algorithm"]](file_spec, data)
52 except (NameError, KeyError) as err:
54 f"Probably algorithm {file_spec[u'algorithm']} is not defined: "
57 logging.info(u"Done.")
60 def _tests_in_suite(suite_name, tests):
61 """Check if the suite includes tests.
63 :param suite_name: Name of the suite to be checked.
64 :param tests: Set of tests
66 :type tests: pandas.Series
67 :returns: True if the suite includes tests.
71 for key in tests.keys():
72 if suite_name == tests[key][u"parent"]:
77 def file_test_results(file_spec, input_data):
78 """Generate the file(s) with algorithms
80 specified in the specification file.
82 :param file_spec: File to generate.
83 :param input_data: Data to process.
84 :type file_spec: pandas.Series
85 :type input_data: InputData
88 file_name = f"{file_spec[u'output-file']}{file_spec[u'output-file-ext']}"
89 rst_header = file_spec[u"file-header"]
91 logging.info(f" Generating the file {file_name} ...")
93 table_lst = get_files(file_spec[u"dir-tables"], u".csv", full_path=True)
96 f" No tables to include in {file_spec[u'dir-tables']}. Skipping."
100 logging.info(f" Writing file {file_name}")
103 f" Creating the tests data set for the "
104 f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
106 tests = input_data.filter_data(file_spec)
107 tests = input_data.merge_data(tests)
110 f" Creating the suites data set for the "
111 f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
113 file_spec[u"filter"] = u"all"
114 suites = input_data.filter_data(file_spec, data_set=u"suites")
115 suites = input_data.merge_data(suites)
116 suites.sort_index(inplace=True)
118 with open(file_name, u"wt") as file_handler:
119 file_handler.write(rst_header)
120 for suite_longname, suite in suites.items():
121 if len(suite_longname.split(u".")) <= \
122 file_spec[u"data-start-level"]:
127 suite[u"level"] - file_spec[u"data-start-level"] - 1
128 ) * len(suite[u"name"])
129 if not (u"-ndrpdr" in suite[u"name"] or
130 u"-mrr" in suite[u"name"] or
131 u"-func" in suite[u"name"] or
132 u"-device" in suite[u"name"]):
133 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
135 if _tests_in_suite(suite[u"name"], tests):
136 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
138 f"\n{suite[u'doc']}\n".replace(u'|br|', u'\n\n -')
140 for tbl_file in table_lst:
141 if suite[u"name"] in tbl_file:
143 RST_INCLUDE_TABLE.format(
145 file_html=tbl_file.split(u"/")[-1]))
147 logging.info(u" Done.")
150 def file_test_results_html(file_spec, input_data):
151 """Generate the file(s) with algorithms
152 - file_test_results_html
153 specified in the specification file.
155 :param file_spec: File to generate.
156 :param input_data: Data to process.
157 :type file_spec: pandas.Series
158 :type input_data: InputData
161 file_name = f"{file_spec[u'output-file']}.rst"
162 rst_header = file_spec[u"file-header"]
164 logging.info(f" Generating the file {file_name} ...")
166 table_lst = get_files(file_spec[u"dir-tables"], u".rst", full_path=True)
169 f" No tables to include in {file_spec[u'dir-tables']}. Skipping."
173 logging.info(f" Writing file {file_name}")
176 f" Creating the tests data set for the "
177 f"{file_spec.get(u'type', u'')} {file_spec.get(u'title', u'')}."
180 tests = input_data.filter_data(
182 params=[u"name", u"parent", u"doc", u"type", u"level"],
183 continue_on_error=True
187 tests = input_data.merge_data(tests)
188 tests.sort_index(inplace=True)
190 suites = input_data.filter_data(
192 continue_on_error=True,
197 suites = input_data.merge_data(suites)
199 with open(file_name, u"wt") as file_handler:
200 file_handler.write(rst_header)
201 for suite_longname, suite in suites.items():
202 if len(suite_longname.split(u".")) <= \
203 file_spec[u"data-start-level"]:
208 suite[u"level"] - file_spec[u"data-start-level"] - 1
209 ) * len(suite[u"name"])
210 if not (u"-ndrpdr" in suite[u"name"] or
211 u"-mrr" in suite[u"name"] or
212 u"-func" in suite[u"name"] or
213 u"-device" in suite[u"name"]):
214 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
216 if _tests_in_suite(suite[u"name"], tests):
217 file_handler.write(f"\n{suite[u'name']}\n{title_line}\n")
219 f"\n{suite[u'doc']}\n".replace(u'|br|', u'\n\n -')
221 for tbl_file in table_lst:
222 if suite[u"name"] in tbl_file:
224 f"\n.. include:: {tbl_file.split(u'/')[-1]}\n"
227 logging.info(u" Done.")