File: profile.py

package info (click to toggle)
python-tempestconf 3.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 964 kB
  • sloc: python: 4,530; makefile: 18; sh: 9
file content (114 lines) | stat: -rw-r--r-- 3,387 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# Copyright 2018 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import yaml


def _convert_remove_append(options):
    """Convert append and remove dicts read from .yaml file.

    :param options: {'section.key': 'value1,value2', ...}
    :type options: dict
    :return: ['section.key=value1,value2', ...]
    :rtype: list
    """
    converted = []
    for key in options:
        v = options[key]
        if isinstance(v, list):
            v = ','.join(v)
        converted.append(key + "=" + v)
    return converted


def _read_yaml_file(path):
    """Read a .yaml file.

    :param path: path to the profile.yaml file
    :type path: string
    :return: profile arguments
    :rtype: dict
    """
    with open(path, 'r') as stream:
        return yaml.safe_load(stream)


def read_profile_file(path):
    """Read python-tempestconf arguments from a .yaml file.

    :param path: path to the profile.yaml file
    :type path: string
    :return: profile arguments
    :rtype: dict
    """
    profile_args = _read_yaml_file(path)
    # convert overrides, to a list of tuples (s, k, v)
    overrides = []
    if 'overrides' in profile_args:
        for key in profile_args['overrides']:
            s, k = key.split('.')
            v = profile_args['overrides'][key]
            if isinstance(v, list):
                v = ','.join(v)
            overrides.append((s, k, str(v)))
    profile_args['overrides'] = overrides
    # convert remove
    remove = []
    if 'remove' in profile_args:
        remove = _convert_remove_append(profile_args['remove'])
    profile_args['remove'] = remove
    # convert append values
    append = []
    if 'append' in profile_args:
        append = _convert_remove_append(profile_args['append'])
    profile_args['append'] = append
    return profile_args


def generate_profile(args, path):
    """Generates a sample profile.yaml file.

    :type args: argparse.Namespace
    :param path: Specifies where the sample file will be generated in.
    :type path: string
    """
    iterable_args = vars(args)
    # pop following arguments as they are written
    # more detailed to the file below
    iterable_args.pop('append')
    iterable_args.pop('overrides')
    iterable_args.pop('remove')
    # pop profile as that shouldn't be in a profile.yaml
    iterable_args.pop('profile')
    with open(path, 'w') as outfile:
        yaml.safe_dump(iterable_args, outfile, default_flow_style=False)
        outfile.write("""append: {}
  #identity.username: username
  #compute-feature-enabled.api_extensions:
  #  - dvr
  #  - extension
overrides: {}
  #identity.username: username
  #identity.password:
  #  - my_password
  #compute-feature-enabled.api_extensions:
  #  - dvr
  #  - extension
remove: {}
  #identity.username: username
  #compute-feature-enabled.api_extensions:
  #  - dvr
  #  - extension
""")