'Read a single record
@param r: the S3Request
@param attr: dictionary of parameters for the method handler'

def read(self, r, **attr):

    authorised = self._permitted()
    if (not authorised):
        r.unauthorised()
    request = self.request
    response = current.response
    resource = self.resource
    table = resource.table
    tablename = resource.tablename
    representation = r.representation
    output = dict()
    _config = self._config
    editable = _config('editable', True)
    record_id = self.record_id
    if r.interactive:
        if ((not record_id) and r.component and (not r.component.multiple)):
            authorised = self._permitted(method='create')
            if authorised:
                r.method = 'create'
                return self.create(r, **attr)
            else:
                return self.select(r, **attr)
        if (not r.method):
            authorised = self._permitted('update')
            if (authorised and (representation == 'html') and editable):
                return self.update(r, **attr)
        subheadings = _config('subheadings')
        crud_string = self.crud_string
        title = crud_string(r.tablename, 'title_display')
        output['title'] = title
        if r.component:
            subtitle = crud_string(tablename, 'title_display')
            output['subtitle'] = subtitle
        output['title_list'] = crud_string(tablename, 'title_list')
        if record_id:
            try:
                item = self.sqlform(request=request, resource=resource, record_id=record_id, readonly=True, subheadings=subheadings, format=representation)
            except HTTP as e:
                message = (current.ERROR.BAD_RECORD if (e.status == 404) else e.message)
                r.error(e.status, message)
        else:
            item = DIV(crud_string(tablename, 'msg_list_empty'), _class='empty')
        if (representation == 'html'):
            response.view = self._view(r, 'display.html')
            output['item'] = item
        elif (representation == 'popup'):
            response.view = self._view(r, 'popup.html')
            output['form'] = item
            caller = attr.get('caller', None)
            output['caller'] = caller
        elif (representation == 'iframe'):
            response.view = self._view(r, 'iframe.html')
            output['form'] = item
        buttons = self.render_buttons(r, ['edit', 'delete', 'list', 'summary'], record_id=record_id, **attr)
        if buttons:
            output['buttons'] = buttons
        last_update = self.last_update()
        if last_update:
            try:
                output['modified_on'] = last_update['modified_on']
            except:
                pass
            try:
                output['modified_by'] = last_update['modified_by']
            except:
                pass
        from s3merge import S3Merge
        output['deduplicate'] = S3Merge.bookmark(r, tablename, record_id)
    elif (representation == 'plain'):
        T = current.T
        fields = [f for f in table if f.readable]
        if r.component:
            if record_id:
                record = current.db((table._id == record_id)).select(limitby=(0, 1), *fields).first()
            else:
                record = None
        else:
            record = r.record
        if record:
            for field in fields:
                try:
                    value = record[field]
                except:
                    value = None
                if ((value is None) or (value == '') or (value == [])):
                    field.readable = False
            item = self.sqlform(request=request, resource=resource, record_id=record_id, readonly=True, format=representation)
            popup_edit_url = _config('popup_edit_url', None)
            if (popup_edit_url and current.auth.s3_has_permission('update', table, record_id)):
                details_btn = A(T('Edit'), _href=popup_edit_url, _class='btn    iframe')
                output['details_btn'] = details_btn
            else:
                popup_url = _config('popup_url', None)
                if (popup_url is None):
                    popup_url = r.url(method='read', representation='html')
                if popup_url:
                    details_btn = A(T('Open'), _href=popup_url, _class='btn', _target='_blank')
                    output['details_btn'] = details_btn
            title = self.crud_string(r.tablename, 'title_display')
            output['title'] = title
        else:
            item = T('Record    not    found')
        output['item'] = item
        response.view = self._view(r, 'plain.html')
    elif (representation == 'csv'):
        exporter = S3Exporter().csv
        output = exporter(resource)
    elif (representation == 'pdf'):
        exporter = S3Exporter().pdf
        output = exporter(resource, request=r, **attr)
    elif (representation == 'shp'):
        list_fields = resource.list_fields()
        exporter = S3Exporter().shp
        output = exporter(resource, list_fields=list_fields, **attr)
    elif (representation == 'svg'):
        list_fields = resource.list_fields()
        exporter = S3Exporter().svg
        output = exporter(resource, list_fields=list_fields, **attr)
    elif (representation == 'xls'):
        list_fields = resource.list_fields()
        exporter = S3Exporter().xls
        output = exporter(resource, list_fields=list_fields)
    elif (representation == 'json'):
        exporter = S3Exporter().json
        get_vars = request.get_vars
        if ('tooltip' in get_vars):
            tooltip = get_vars['tooltip']
        else:
            tooltip = None
        output = exporter(resource, tooltip=tooltip)
    else:
        r.error(415, current.ERROR.BAD_FORMAT)
    return output
