98be9d34482376ab538023a9196742a63b7b268b
[csit.git] / resources / libraries / python / VatConfigGenerator.py
1 # Copyright (c) 2016 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 """Can be used to generate VAT scripts from VAT template files."""
15
16 from robot.api import logger
17
18
19 class VatConfigGenerator(object):
20     """Generates VAT configuration scripts from VAT script template files.
21     """
22     def __init__(self):
23         pass
24
25     @staticmethod
26     def generate_vat_config_file(template_file, env_var_dict, out_file):
27         """ Write VAT configuration script to out file.
28
29         Generates VAT configuration script from template using
30         dictionary containing environment variables
31         :param template_file: file that contains the VAT script template
32         :param env_var_dict: python dictionary that maps test
33         environment variables
34         """
35
36         template_data = open(template_file).read()
37         logger.trace("Loaded template file: \n '{0}'".format(template_data))
38         generated_config = template_data.format(**env_var_dict)
39         logger.trace("Generated script file: \n '{0}'".format(generated_config))
40         with open(out_file, 'w') as work_file:
41             work_file.write(generated_config)
42
43     @staticmethod
44     def generate_vat_config_string(template_file, env_var_dict):
45         """ Return wat config string generated from template.
46
47         Generates VAT configuration script from template using
48         dictionary containing environment variables
49         :param template_file: file that contains the VAT script template
50         :param env_var_dict: python dictionary that maps test
51         environment variables
52         """
53
54         template_data = open(template_file).read()
55         logger.trace("Loaded template file: \n '{0}'".format(template_data))
56         generated_config = template_data.format(**env_var_dict)
57         logger.trace("Generated script file: \n '{0}'".format(generated_config))
58         return generated_config