def _close_scopes(hierarchy_string, target_depth=0):
    """ The string is hierarchically ordered. This function appends curly brackets to close open scopes.
    It takes the indentation of the last closed bracket as reference for the current level/depth of the hierarchy.
    :param hierarchy_string:
    :type hierarchy_string: str
    :param target_depth: The depth determines the target indentation.
    :type target_depth: int
    :return: string with closed scopes.
    :rtype: str
    """
    last_depth = hierarchy_string[hierarchy_string[:-1].rfind('\n'):].count(
        '  ')
    diff = last_depth - target_depth
    for depth in range(diff):
        hierarchy_string += '{0}}}\n'.format('  ' * (last_depth - depth - 1))
    return hierarchy_string
