'Implements DescribeStacks API action.
Gets detailed information for a stack (or all stacks).'

def describe(self, req):

    self._enforce(req, 'DescribeStacks')
    def format_stack_outputs(o):
        keymap = {rpc_api.OUTPUT_DESCRIPTION: 'Description', rpc_api.OUTPUT_KEY: 'OutputKey', rpc_api.OUTPUT_VALUE: 'OutputValue'}
        def replacecolon(d):
            return dict(map((lambda k_v: (k_v[0].replace(':', '.'), k_v[1])), d.items()))
        def transform(attrs):
            'Recursively    replace    all    `:`    with    `.`    in    dict    keys.\n\n                                                                After    that    they    are    not    interpreted    as    xml    namespaces.\n                                                                '
            new = replacecolon(attrs)
            for (key, value) in new.items():
                if isinstance(value, dict):
                    new[key] = transform(value)
            return new
        return api_utils.reformat_dict_keys(keymap, transform(o))
    def format_stack(s):
        'Reformat    engine    output    into    the    AWS    "StackSummary"    format.'
        keymap = {rpc_api.STACK_CAPABILITIES: 'Capabilities', rpc_api.STACK_CREATION_TIME: 'CreationTime', rpc_api.STACK_DESCRIPTION: 'Description', rpc_api.STACK_DISABLE_ROLLBACK: 'DisableRollback', rpc_api.STACK_NOTIFICATION_TOPICS: 'NotificationARNs', rpc_api.STACK_PARAMETERS: 'Parameters', rpc_api.STACK_ID: 'StackId', rpc_api.STACK_NAME: 'StackName', rpc_api.STACK_STATUS_DATA: 'StackStatusReason', rpc_api.STACK_TIMEOUT: 'TimeoutInMinutes'}
        if (s[rpc_api.STACK_UPDATED_TIME] is not None):
            keymap[rpc_api.STACK_UPDATED_TIME] = 'LastUpdatedTime'
        result = api_utils.reformat_dict_keys(keymap, s)
        action = s[rpc_api.STACK_ACTION]
        status = s[rpc_api.STACK_STATUS]
        result['StackStatus'] = '_'.join((action, status))
        result['Outputs'] = []
        if (rpc_api.STACK_OUTPUTS in s):
            for o in s[rpc_api.STACK_OUTPUTS]:
                result['Outputs'].append(format_stack_outputs(o))
        result['Parameters'] = [{'ParameterKey': k, 'ParameterValue': v} for (k, v) in result['Parameters'].items()]
        return self._id_format(result)
    con = req.context
    try:
        if ('StackName' in req.params):
            identity = self._get_identity(con, req.params['StackName'])
        else:
            identity = None
        stack_list = self.rpc_client.show_stack(con, identity)
    except Exception as ex:
        return exception.map_remote_error(ex)
    res = {'Stacks': [format_stack(s) for s in stack_list]}
    return api_utils.format_response('DescribeStacks', res)
