def process(templateRootPath,
            layoutNameStr,
            outputFolder,
            formatStr,
            quality,
            mapScale,
            extentObj,
            textElementsList,
            lodsArray,
            includeLegend,
            layoutItemLimit):



    log("Using template root: " + templateRootPath)
    if not path.isdir(templateRootPath):
        raise Exception("No template exists for: " + templateRootPath)

    # process file lists
    legendPdfList = fileUtils.getLegendPdfList(templateRootPath)
    legendMxdList = fileUtils.getLegendMxdList(templateRootPath)
    replaceList = fileUtils.getReplaceLayerOrMapDocList(templateRootPath)

    layoutMxd = fileUtils.getLayoutMapDoc(templateRootPath, layoutNameStr)
    log("Using layout map doc: " + layoutMxd.filePath)

    # webmaps
    log("Converting webmaps to map documents...")
    webmapMapDoc = mapUtils.webmapToMapDocument(webmapJson, outputFolder, settings.SERVER_CONNECTIONS)

    webmapDataframe = mapping.ListDataFrames(webmapMapDoc)[0]
    newExtent = webmapDataframe.extent
    if extentObj:
        newExtent.XMin = extentObj['xmin']
        newExtent.YMin = extentObj['ymin']
        newExtent.XMax = extentObj['xmax']
        newExtent.YMax = extentObj['ymax']


    # create list of output pdf or image files for concatenating later
    exportedImageFilePaths = []
    outputMapDocs = []

    ## if there are replace map docs or layers, replace entire dataframe
    if len(replaceList) == 0:
        outMapDoc = getMapDocForStandardTemplates(webmapMapDoc, outputFolder, layoutMxd, newExtent, mapScale, lodsArray)
        outputMapDocs.append(outMapDoc)

    else:
        outMapDocList = getMapDocsForPresetTemplates(replaceList, layoutMxd, outputFolder, newExtent, mapScale, lodsArray)
        outputMapDocs.extend(outMapDocList)

    mapDocListWithLegends = getMapDocListWithLegends(outputMapDocs, outputFolder, webMapObj, layoutItemLimit, templateRootPath, newExtent,
                                                     mapScale, lodsArray, textElementsList, layoutNameStr, legendMxdList)
    for legendMapDoc in mapDocListWithLegends:
        exportLegendFile = mapUtils.exportMapDocToFile(legendMapDoc, formatStr, outputFolder, quality)
        exportedImageFilePaths.append(exportLegendFile)


    # append pdf legends
    if includeLegend:
        for legendFile in legendPdfList:
            log("Appending legend file: " + legendFile)
            exportedImageFilePaths.append(legendFile)

    return exportedImageFilePaths
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