<commit_before># coding=utf-8
"""This module contains extended functionality for related to the arcpy.mp module."""

# Python 2/3 compatibility
# pylint: disable=wildcard-import,unused-wildcard-import,wrong-import-order,wrong-import-position
from __future__ import (absolute_import, division, print_function, unicode_literals)
from future.builtins.disabled import *
from future.builtins import *
from future.standard_library import install_aliases
install_aliases()
# pylint: enable=wildcard-import,unused-wildcard-import,wrong-import-order,wrong-import-position

# Standard lib imports
import logging
import re

from itertools import zip_longest

# Third-party imports
import arcpy

# Local imports
from ..exceptions import MapLayerError, DataSourceUpdateError, UnsupportedLayerError, ChangeDataSourcesError
from ..arcobjects import init_arcobjects_context, destroy_arcobjects_context, list_layers

# Configure module logging
logger = logging.getLogger("arcpyext.mp")


def change_data_sources(project, data_sources):
    """ """
    # make sure the project is a project, not a path
    project = open_document(project)

    errors = []

    # match map with data sources
    for proj_map, map_data_sources in zip_longest(project.listMaps(), data_sources):

        if not 'layers' in map_data_sources or not 'tableViews' in map_data_sources:
            raise ChangeDataSourcesError("Data sources dictionary does not contain both 'layers' and 'tableViews' keys")

        layers = proj_map.listLayers()
        layer_sources = map_data_sources["layers"]

        if layer_sources == None or len(layers) != len(layer_sources):
            raise ChangeDataSourcesError("Number of layers does not match number of data sources.")

        for layer, layer_source in zip_longest(layers, layer_sources):
            try:
                if layer_source == None:
                    continue

                logger.debug("Layer '{0}': Attempting to change workspace path".format(layer.longName).encode(
                    "ascii", "ignore"))
                logger.debug("Old connectionProperties {0}".format(layer.connectionProperties).encode(
                    "ascii", "ignore"))
                _change_data_source(layer, layer_source)
                logger.debug("Layer '{0}': connectionProperties updated to: '{1}'".format(layer.name,
                                                                                          layer_source).encode(
                                                                                              "ascii", "ignore"))

                if layer.supports("dataSource"):
                    logger.debug("Layer '{0}': New datasource: '{1}'".format(layer.longName, layer.dataSource).encode(
                        "ascii", "ignore"))

            #TODO: Handle KeyError and AttributeError for badly written configs
            except MapLayerError as e:
                errors.append(e)

        data_tables = proj_map.listTables()
        data_table_sources = map_data_sources["tableViews"]

        if not len(data_tables) == len(data_table_sources):
            raise ChangeDataSourcesError("Number of data tables does not match number of data table data sources.")

        for data_table, layer_source in zip_longest(data_tables, data_table_sources):
            try:
                if layer_source == None:
                    continue

                logger.debug("Data Table '{0}': Attempting to change workspace path".format(data_table.name).encode(
                    "ascii", "ignore"))
                logger.debug("Old connectionProperties {0}".format(data_table.connectionProperties).encode(
                    "ascii", "ignore"))
                _change_data_source(data_table, layer_source.get("connectionProperties"))
                logger.debug("Data Table '{0}': Workspace path updated to: '{1}'".format(
                    data_table.name, layer_source.get("connectionProperties")))

            except MapLayerError as mle:
                errors.append(mle)

    if not len(errors) == 0:
        raise ChangeDataSourcesError("A number of errors were encountered whilst change layer data sources.", errors)


def compare(map_a, map_b):
    """Compares two map documents.

    Outputs a list describing any differences

    :param map_a: A map to compare
    :type map_a: arcpy.mp.ArcGISProject
    :param map_b: Another version of the same map to compare
    :type map_b: arcpy.mp.ArcGISProject
    :returns: dict

    """

    #
    # Compare data frames
    #
    def compare_data_frames():

        map_count_changed = 301
        data_frame_cs_code_changed = 302<commit_msg>Made logging simpler<commit_after># coding=utf-8
"""This module contains extended functionality for related to the arcpy.mp module."""

# Python 2/3 compatibility
# pylint: disable=wildcard-import,unused-wildcard-import,wrong-import-order,wrong-import-position
from __future__ import (absolute_import, division, print_function, unicode_literals)
from future.builtins.disabled import *
from future.builtins import *
from future.standard_library import install_aliases
install_aliases()
# pylint: enable=wildcard-import,unused-wildcard-import,wrong-import-order,wrong-import-position

# Standard lib imports
import logging
import re

from itertools import zip_longest

# Third-party imports
import arcpy

# Local imports
from ..exceptions import MapLayerError, DataSourceUpdateError, UnsupportedLayerError, ChangeDataSourcesError
from ..arcobjects import init_arcobjects_context, destroy_arcobjects_context, list_layers

# Configure module logging
logger = logging.getLogger("arcpyext.mp")


def change_data_sources(project, data_sources):
    """ """
    # make sure the project is a project, not a path
    project = open_document(project)

    errors = []

    # match map with data sources
    for proj_map, map_data_sources in zip_longest(project.listMaps(), data_sources):

        if not 'layers' in map_data_sources or not 'tableViews' in map_data_sources:
            raise ChangeDataSourcesError("Data sources dictionary does not contain both layers and tableViews keys")

        layers = proj_map.listLayers()
        layer_sources = map_data_sources["layers"]

        if layer_sources == None or len(layers) != len(layer_sources):
            raise ChangeDataSourcesError("Number of layers does not match number of data sources.")

        for layer, layer_source in zip_longest(layers, layer_sources):
            try:
                if layer_source == None:
                    continue

                logger.debug("Layer {0}: Attempting to change workspace path".format(layer.longName))
                logger.debug("Old connectionProperties {0}".format(layer.connectionProperties))
                _change_data_source(layer, layer_source)
                logger.debug("Layer {0}: connectionProperties updated to: {1}".format(layer.name,
                                                                                          layer_source))

                if layer.supports("dataSource"):
                    logger.debug("Layer {0}: New datasource: {1}".format(layer.longName, layer.dataSource))

            #TODO: Handle KeyError and AttributeError for badly written configs
            except MapLayerError as e:
                errors.append(e)

        data_tables = proj_map.listTables()
        data_table_sources = map_data_sources["tableViews"]

        if not len(data_tables) == len(data_table_sources):
            raise ChangeDataSourcesError("Number of data tables does not match number of data table data sources.")

        for data_table, layer_source in zip_longest(data_tables, data_table_sources):
            try:
                if layer_source == None:
                    continue

                logger.debug("Data Table {0}: Attempting to change workspace path".format(data_table.name).encode(
                    "ascii", "ignore"))
                logger.debug("Old connectionProperties {0}".format(data_table.connectionProperties).encode(
                    "ascii", "ignore"))
                _change_data_source(data_table, layer_source.get("connectionProperties"))
                logger.debug("Data Table {0}: Workspace path updated to: {1}".format(
                    data_table.name, layer_source.get("connectionProperties")))

            except MapLayerError as mle:
                errors.append(mle)

    if not len(errors) == 0:
        raise ChangeDataSourcesError("A number of errors were encountered whilst change layer data sources.", errors)


def compare(map_a, map_b):
    """Compares two map documents.

    Outputs a list describing any differences

    :param map_a: A map to compare
    :type map_a: arcpy.mp.ArcGISProject
    :param map_b: Another version of the same map to compare
    :type map_b: arcpy.mp.ArcGISProject
    :returns: dict

    """

    #
    # Compare data frames
    #
    def compare_data_frames():

        map_count_changed = 301
        data_frame_cs_code_changed = 302
{'fraction_non_alphanumeric': 0.08578078244486313, 'fraction_numerical': 0.004433115371827552, 'mean_word_length': 2.6787607011822256, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '790272', 'n_tokens_mistral': 2401, 'n_tokens_neox': 2260, 'n_words': 655}