def set_up():
    # Create an example file with key/value pairs, as well as a comment
    # The old to new value mappings are written to a file, then later the
    # file is inspected to ensure no old values remain.
    var_lines = ["{}: {}".format(key, val) for
                 key, val in VAR_MAPPINGS.items()]
    var_lines.append('# A test comment')
    sample = VAR_MAPPINGS.items()[0]
    var_lines.append('# {} / {}'.format(*sample))
    with open(FILE_NAME, 'w') as f:
def test():

    with open(FILE_NAME, 'r') as f:
        contents = f.readlines()

    for line in contents:
        # only split lines that look like a key/value pair.
        if ':' in line:
            var, value = line.split(':', 1)
            value = value.strip()
        elif '/' in line:
            # For the comment containing a variable, clean up the list
            # contents before assigning the parts we want to test.
            parts = line.split()
            var, value = parts
            var = value = line

        # Once run through the 'main' function, the keys and values should
        # match
        if not value == var:
            import pdb; pdb.set_trace()  # NOQA
            print("Var and value don't match.")
            print("Var: {}, Value: {}".format(var, value))

        invalid_variable = var not in VAR_MAPPINGS.values()
        # Comments aren't in our test mapping, so make sure we ignore them
        is_comment = line.startswith('#')

        if invalid_variable and not is_comment:
            err = "Variable {} doesn't appear to be a valid new name."

    print("Tests passed")
Пример #3
# This file is used to generate a ReStructured Text table suitable for
# documentating the variable name changes. Its contents are meant to be
# inserted into doc/source/upgrade-guide/scripts.rst.

# As of right now, running this script and inserting the output into
# the file is manual.

from migrate_openstack_vars import VAR_MAPPINGS

# Print old/new values in each row, right aligned.
row_format = "| {:>40} | {:>40} |"

# For the line separators, move the dividing '+' sign over so it's aligned
# with the '|' in the rows.
divider_format = "+-{:->42}---{:->40}"
header_divide_format = "+={:=>42}==={:=>40}"

# Header info
print(divider_format.format('+', '+'))
print(row_format.format('Old Value', 'New Value'))
print(header_divide_format.format('+', '+'))

# If we just used the items method, we'd get an unsorted output.
keys = VAR_MAPPINGS.keys()

for key in keys:
    print(row_format.format(key, VAR_MAPPINGS[key]))
    print(divider_format.format('+', '+'))