def getMapDocsForPresetTemplates(replaceList, layoutMxd, outputFolder, extent, mapScale, lodsArray):

    mapDocList = []
    log("Replacement layers found. Webmap layers will be ignored")
    for replaceLayerOrMapDocPath in replaceList:

        layersToCopy = []
        title = fileUtils.getName(replaceLayerOrMapDocPath)
        log("Processing map: " + title)

        if fileUtils.getExtension(replaceLayerOrMapDocPath) == "mxd":
            replaceMapDoc = mapping.MapDocument(replaceLayerOrMapDocPath)
            layersToCopy = mapping.ListLayers(replaceMapDoc)

        elif fileUtils.getExtension(replaceLayerOrMapDocPath) == "lyr":
            replaceLayer = mapping.Layer(replaceLayerOrMapDocPath)
            layersToCopy.append(replaceLayer)

        log("Copying layers from: " + replaceLayerOrMapDocPath)
        outMapDoc = mapUtils.copyLayers(layersToCopy, layoutMxd, outputFolder, True)
        outMapDoc = mapUtils.setExtentAndScale(outMapDoc, extent, mapScale, lodsArray)

        log("Processing text elements...")
        mapUtils.processTextElements(outMapDoc, {settings.REPLACE_LAYER_ELEMENT_NAME: title})
        mapDocList.append(outMapDoc)

    return mapDocList
def getMapDocListWithLegends(outputMapDocs, outputFolder, webMapObj, layoutItemLimit, templateRootPath,
                             newExtent, mapScale, lodsArray, textElementsList,
                             layoutNameStr, legendMxdList):

    legendExcludeLayers = settings.LEGEND_EXCLUDE_LAYERS
    styleFile = settings.LEGEND_STYLE_FILE
    styleName = settings.LEGEND_STYLE_NAME
    legendTemplateConfig = settings.LEGEND_TEMPLATE_CONFIG

    outMapDocsWithLegends = []

    for outMapDoc in outputMapDocs:

        switchToNoLegendMxd = False
        legendItemCount = 0
        if includeLegend:
            # get swatch count for map doc
            mapDocCloneForLegend = mapUtils.getMapDocForLegend(outMapDoc, legendExcludeLayers, outputFolder, log, webMapObj)
            legendLayers = mapping.ListLayers(mapDocCloneForLegend)
            # get approx swatch count, used for selecting legend mxd
            legendItemCount = mapUtils.getSwatchCount(legendLayers, log)
            log("Legend swatch count estimate: " + str(legendItemCount))

            legendIsOverflowing = mapUtils.isLegendOverflowing(mapDocCloneForLegend, log)
            log("Legend is overflowing (arcpy): " + str(legendIsOverflowing))

            # secondary check as sometimes the arcpy appraoch allows overflowing
            if not legendIsOverflowing:
                # get legend item count from webmap details
                clientSideLegendItemCount = mapUtils.getSwatchCountFromWebmap(webMapObj, log)

                #check against client side item count limit
                log("Legend items client side count : " + str(clientSideLegendItemCount) + " - limit " + str(layoutItemLimit))
                if layoutItemLimit > -1 and clientSideLegendItemCount > layoutItemLimit:
                    legendIsOverflowing = True
                    log("Overflowing based on client side count")

            if not legendIsOverflowing:
                outMapDoc = mapUtils.cloneMapDoc(outMapDoc, outputFolder)
                mapUtils.processInlineLegend(outMapDoc, True, legendExcludeLayers, webMapObj, log)
            else:
                switchToNoLegendMxd = True
        else:
            switchToNoLegendMxd = True

        if switchToNoLegendMxd:
            # either no legend requested or a second page legend is being used
            # clone document onto new legend layout
            newLayoutName = layoutNameStr.replace(".mxd", noLegendMxdNameSuffix + ".mxd")
            newLayoutMxd = fileUtils.getLayoutMapDoc(templateRootPath, newLayoutName)
            if newLayoutMxd:
                fromLayers = mapping.ListLayers(outMapDoc)
                outMapDoc = mapUtils.copyLayers(fromLayers, newLayoutMxd, outputFolder)
                outMapDoc = mapUtils.setExtentAndScale(outMapDoc, newExtent, mapScale, lodsArray)

        log("Processing custom text elements...")
        mapUtils.processTextElements(outMapDoc, textElementsList)

        # export maing print file
        log("Exporting file...")
        outMapDocsWithLegends.append(outMapDoc)

        # process mxd legends, attached as second page
        if includeLegend and switchToNoLegendMxd:
            log("Processing legends")
            # references an mxd on disk to use for legend
            targetLegendMxd = mapUtils.getTargetLegendMxd(legendItemCount, legendTemplateConfig, layoutNameStr)
            log("Using legend template: " + targetLegendMxd)
            processedLegendMxds = mapUtils.getMxdLegends(legendMxdList, outMapDoc, layoutNameStr, outputFolder, log,
                                                         legendTemplateConfig, legendExcludeLayers, webMapObj, styleFile, styleName)
            for legendMxd in processedLegendMxds:
                outMapDocsWithLegends.append(legendMxd)


    return outMapDocsWithLegends