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 utils import get_files, get_rst_title_char
22 RST_INCLUDE_TABLE = ("\n.. only:: html\n\n"
27 " :file: {file_html}\n"
28 "\n.. only:: latex\n\n"
29 "\n .. raw:: latex\n\n"
30 " \csvautolongtable{{{file_latex}}}\n\n")
33 def generate_files(spec, data):
34 """Generate all files specified in the specification file.
36 :param spec: Specification read from the specification file.
37 :param data: Data to process.
38 :type spec: Specification
42 logging.info("Generating the files ...")
43 for file_spec in spec.files:
45 eval(file_spec["algorithm"])(file_spec, data)
46 except NameError as err:
47 logging.error("Probably algorithm '{alg}' is not defined: {err}".
48 format(alg=file_spec["algorithm"], err=repr(err)))
52 def _tests_in_suite(suite_name, tests):
53 """Check if the suite includes tests.
55 :param suite_name: Name of the suite to be checked.
56 :param tests: Set of tests
58 :type tests: pandas.Series
59 :returns: True if the suite includes tests.
63 for key in tests.keys():
64 if suite_name == tests[key]["parent"]:
69 def file_test_results(file_spec, input_data):
70 """Generate the file(s) with algorithms
72 specified in the specification file.
74 :param file_spec: File to generate.
75 :param input_data: Data to process.
76 :type file_spec: pandas.Series
77 :type input_data: InputData
80 file_name = "{0}{1}".format(file_spec["output-file"],
81 file_spec["output-file-ext"])
82 rst_header = file_spec["file-header"]
84 logging.info(" Generating the file {0} ...".format(file_name))
86 table_lst = get_files(file_spec["dir-tables"], ".csv", full_path=True)
87 if len(table_lst) == 0:
88 logging.error(" No tables to include in '{0}'. Skipping.".
89 format(file_spec["dir-tables"]))
92 logging.info(" Writing file '{0}'".format(file_name))
94 logging.info(" Creating the 'tests' data set for the {0} '{1}'.".
95 format(file_spec.get("type", ""), file_spec.get("title", "")))
96 tests = input_data.filter_data(file_spec)
97 tests = input_data.merge_data(tests)
99 logging.info(" Creating the 'suites' data set for the {0} '{1}'.".
100 format(file_spec.get("type", ""), file_spec.get("title", "")))
101 file_spec["filter"] = "all"
102 suites = input_data.filter_data(file_spec, data_set="suites")
103 suites = input_data.merge_data(suites)
104 suites.sort_index(inplace=True)
106 with open(file_name, "w") as file_handler:
107 file_handler.write(rst_header)
108 for suite_longname, suite in suites.iteritems():
109 if len(suite_longname.split(".")) <= file_spec["data-start-level"]:
111 file_handler.write("\n{0}\n{1}\n".format(
112 suite["name"], get_rst_title_char(
113 suite["level"] - file_spec["data-start-level"] - 1) *
115 file_handler.write("\n{0}\n".format(
116 suite["doc"].replace('|br|', '\n\n -')))
117 if _tests_in_suite(suite["name"], tests):
118 for tbl_file in table_lst:
119 if suite["name"] in tbl_file:
121 RST_INCLUDE_TABLE.format(
123 file_html=tbl_file.split("/")[-1]))
124 logging.info(" Done.")