Getting extent of composer map in PyQGIS?

In order to use the result in an expression, a custom python expression is necessary. Here's the code I've come up with:

from qgis.core import *
from qgis.gui import *
from qgis.utils import iface

@qgsfunction(args='auto', group='Custom')
def getScale(composerName, mapName, feature, parent):
    # dictionary to store QgsComposerView.composition() items by name
    compDict = {}
    for comp in iface.activeComposers():
        # workaround to get name: read it from window title
        compDict[comp.composerWindow().windowTitle()] = comp.composition()
    mapScale = 0 # default to be returned if scale not found
    if composerName in compDict:
        mapItem = compDict[composerName].getComposerItemById(mapName)
        if mapItem:
            mapScale = mapItem.scale()
    return mapScale

The first challenge is getting a composer by name. Create a dictionary to hold the composers, then iterate over the activeComposers() list, storing them by name in the dictionary.

Next, if the composer name we're looking for is in the list, get the requested map and it's extent. Return either it or 0 to denote not found.

This works great with an atlas controlled map:

Scale shown for atlas item

In this case the expression used for the label is:

'scale' || getScale('Composer 1', 'Map 0')