Ejemplo n.º 1
0
def format_nodes(diff, key, properties):
    """Format nodes which have 'removed', 'added' or 'unchanged' status.

    Args:
        diff (dict): The difference of two ditcs.
        key (dict): The key of node.
        properties (dict): The properties of node.
    """
    sign = ''
    vl = ''
    if get_status(properties) == 'removed':
        sign = '-'
        vl = get_first_value(properties)
    elif get_status(properties) == 'added':
        sign = '+'
        vl = get_second_value(properties)
    else:
        sign = ' '
        vl = get_second_value(properties)
    diff.pop(key)
    diff.update({'{0} {1}'.format(sign, key): format_if_dict(vl)})
def add_key_if_removed(formatted_diff, ancestor_keys, key, properties):
    """Format nodes which have 'removed' status and add to formatted diff.

    Args:
        formatted_diff (dict): The formatted difference of two ditcs.
        ancestor_keys (list): Ancestor keys.
        key (str): Key of node.
        properties (dict): The properties of node.
    """
    if get_status(properties) == 'removed':
        formatted_diff.append(
            "Property '{0}' was removed\n".format(
                format_sequence_of_keys(ancestor_keys, key), ), )
Ejemplo n.º 3
0
def format_child_nodes(diff):
    """Format nodes which have 'child' status.

    Args:
        diff (dict): The difference of two ditcs.
    """
    dict_of_unchanged_keys = {
        '  {0}'.format(key): format_diff(diff[key]['value'])
        for key, properties in diff.items()
        if get_status(properties) == 'child'
    }
    for key in dict_of_unchanged_keys:
        diff.pop(key[2:])
    diff.update(dict_of_unchanged_keys)
def add_key_if_added(formatted_diff, ancestor_keys, key, properties):
    """Format nodes which have 'added' status and add to formatted diff.

    Args:
        formatted_diff (dict): The formatted difference of two ditcs.
        ancestor_keys (list): Ancestor keys.
        key (str): Key of node.
        properties (dict): The properties of node.
    """
    if get_status(properties) == 'added':
        formatted_diff.append(
            "Property '{0}' was added with value: {1}\n".format(
                format_sequence_of_keys(ancestor_keys, key),
                standardize_value(get_second_value(properties)),
            ), )
Ejemplo n.º 5
0
def format_value_if_child(diff, formatted_diff, ancestor_keys, key,
                          properties):
    """Format nodes which have 'child' status.

    Args:
        diff (dict): The difference of two dicts.
        formatted_diff (dict): The formatted difference of two ditcs.
        ancestor_keys (list): Ancestor keys.
        key (str): Key of node.
        properties (dict): The properties of node.
    """
    if get_status(properties) == 'child':
        sequence = ancestor_keys[:]
        sequence.append(key)
        formatted_diff.append(format_diff(diff[key]['value'], sequence))
Ejemplo n.º 6
0
def format_diff(diff):
    """Return formatted diff.

    Args:
        diff (dict): The difference of two ditcs.

    Returns:
        (dict): formatted diff.
    """
    format_child_nodes(diff)
    format_updated_nodes(diff)
    for key, properties in diff.copy().items():
        if isinstance(diff[key], dict):
            if get_status(properties) in {'added', 'removed', 'unchanged'}:
                format_nodes(diff, key, properties)
    return diff
Ejemplo n.º 7
0
def format_updated_nodes(diff):
    """Format nodes which have 'updated' status.

    Args:
        diff (dict): The difference of two ditcs.
    """
    for key, properties in diff.copy().items():
        if isinstance(diff[key], dict):
            if get_status(properties) == 'updated':
                diff.pop(key)
                diff.update({
                    '- {0}'.format(key):
                    format_if_dict(get_first_value(properties), )
                })
                diff.update({
                    '+ {0}'.format(key):
                    format_if_dict(get_second_value(properties), )
                })