Debian Codegraph @

config_tempest/profile.py

raw
1 # Copyright 2018 Red Hat, Inc.
2 # All Rights Reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License"); you may
5 # not use this file except in compliance with the License. You may obtain
6 # a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 # License for the specific language governing permissions and limitations
14 # under the License.
15
16 import yaml
17
18
19 def _convert_remove_append(options):
20 """Convert append and remove dicts read from .yaml file.
21
22 :param options: {'section.key': 'value1,value2', ...}
23 :type options: dict
24 :return: ['section.key=value1,value2', ...]
25 :rtype: list
26 """
27 converted = []
28 for key in options:
29 v = options[key]
30 if isinstance(v, list):
31 v = ','.join(v)
32 converted.append(key + "=" + v)
33 return converted
34
35
36 def _read_yaml_file(path):
37 """Read a .yaml file.
38
39 :param path: path to the profile.yaml file
40 :type path: string
41 :return: profile arguments
42 :rtype: dict
43 """
44 with open(path, 'r') as stream:
45 return yaml.safe_load(stream)
46
47
48 def read_profile_file(path):
49 """Read python-tempestconf arguments from a .yaml file.
50
51 :param path: path to the profile.yaml file
52 :type path: string
53 :return: profile arguments
54 :rtype: dict
55 """
56 profile_args = _read_yaml_file(path)
57 # convert overrides, to a list of tuples (s, k, v)
58 overrides = []
59 if 'overrides' in profile_args:
60 for key in profile_args['overrides']:
61 s, k = key.split('.')
62 v = profile_args['overrides'][key]
63 if isinstance(v, list):
64 v = ','.join(v)
65 overrides.append((s, k, str(v)))
66 profile_args['overrides'] = overrides
67 # convert remove
68 remove = []
69 if 'remove' in profile_args:
70 remove = _convert_remove_append(profile_args['remove'])
71 profile_args['remove'] = remove
72 # convert append values
73 append = []
74 if 'append' in profile_args:
75 append = _convert_remove_append(profile_args['append'])
76 profile_args['append'] = append
77 return profile_args
78
79
80 def generate_profile(args, path):
81 """Generates a sample profile.yaml file.
82
83 :type args: argparse.Namespace
84 :param path: Specifies where the sample file will be generated in.
85 :type path: string
86 """
87 iterable_args = vars(args)
88 # pop following arguments as they are written
89 # more detailed to the file below
90 iterable_args.pop('append')
91 iterable_args.pop('overrides')
92 iterable_args.pop('remove')
93 # pop profile as that shouldn't be in a profile.yaml
94 iterable_args.pop('profile')
95 with open(path, 'w') as outfile:
96 yaml.safe_dump(iterable_args, outfile, default_flow_style=False)
97 outfile.write("""append: {}
98 #identity.username: username
99 #compute-feature-enabled.api_extensions:
100 # - dvr
101 # - extension
102 overrides: {}
103 #identity.username: username
104 #identity.password:
105 # - my_password
106 #compute-feature-enabled.api_extensions:
107 # - dvr
108 # - extension
109 remove: {}
110 #identity.username: username
111 #compute-feature-enabled.api_extensions:
112 # - dvr
113 # - extension
114 """)