def body(self, msg, section):
        """
        Fetch the appropriate part of the message and return it to the
        user.
        """

        msg_text = None
        g = None
        if len(section) == 0:
            # They want the entire message.
            #
            # This really only ever is the case as our first invocation of the
            # body() method. All further recursive calls will always have at
            # least one element in the section list when they are called.
            #
            msg_text = email.utils.fix_eols(msg.as_string())
            # msg_text = msg.as_string()
        else:
            if len(section) == 1:
                fp = StringIO()
                if isinstance(section[0], int):
                    # The want a sub-part. Get that sub-part. Watch
                    # out for messages that are not multipart. You can
                    # get a sub-part of these as long as it is only
                    # sub-part #1.
                    #
                    g = TextGenerator(fp, headers = False)

                    if section[0] == 1 and not msg.is_multipart():
                        # The logic is simpler to read this way.. if
                        # they want section 1 and this message is NOT
                        # a multipart message, then do the same as
                        # 'TEXT' ie: We will fall through to the end of this
                        # function where we will take the generator
                        # already created above and use it.
                        #
                        pass
                    else:
                        # Otherwise, get the sub-part they are after
                        # as the message to pass to the generator.
                        #
                        if section[0] != 1 and not msg.is_multipart():
                            raise BadSection("Trying to retrieve section %d "
                                             "and this message is not "
                                             "multipart" % section[0])
                        try:
                            msg = msg.get_payload(section[0]-1)
                        except IndexError:
                            raise BadSection("Section %d does not exist in "
                                             "this message sub-part" % \
                                             section[0])
                elif isinstance(section[0],str) and section[0].upper() == 'TEXT':
                    g = TextGenerator(fp, headers = False)
                elif isinstance(section[0], (list,tuple)):
                    if section[0][0].upper() == "HEADER.FIELDS":
                        g = HeaderGenerator(fp, headers = section[0][1],
                                            skip = False)
                    elif section[0][0].upper() == "HEADER.FIELDS.NOT":
                        g = HeaderGenerator(fp, headers = section[0][1],
                                            skip = True)
                    else:
                        raise BadSection("Section value must be either "
                                         "HEADER.FIELDS or HEADER.FIELDS.NOT, "
                                         "not: %s" % section[0][0])
                else:
                    g = HeaderGenerator(fp)
                    if isinstance(section[0], str) and section[0].upper() == 'MIME':
                        # XXX just use the generator as it is for MIME.. I know
                        # this is not quite right in that it will accept more
                        # then it should, but it will otherwise work.
                        #
                        pass
                    elif isinstance(section[0],str) and section[0].upper() == 'HEADER':
                        # if the content type is message/rfc822 then to
                        # get the headers we need to use the first
                        # sub-part of this message.
                        #
                        if msg.is_multipart() and \
                           msg.get_content_type() == 'message/rfc822':
                            msg = msg.get_payload(0)
                    else:
                        self.log.warn("body: Unexpected section[0] value: %s" %\
                                          repr(section))
                        raise BadSection("%s: Unexpected section value: %s" % \
                                             (str(self),repr(section[0])))
            elif isinstance(section[0], int):
                # We have an integer sub-section. This means that we
                # need to pull apart the message (it MUST be a
                # multi-part message) and pass to a recursive
                # invocation of this function the sub-part and the
                # section list (with the top element of the section
                # list removed.)
                #
                if not msg.is_multipart():
                    raise BadSection("Message does not contain subsection %d "
                                     "(section list: %s" % section[0],
                                     section)
                try:
                    msg = msg.get_payload(section[0]-1)
                except TypeError:
                    raise BadSection("Message does not contain subsection %d "
                                     "(section list: %s)" % section[0],
                                     section)
                return self.body(msg, section[1:])

            # We should have a generator that will give us the text
            # that we want. If we do not then we were not able to find
            # an appropriate section to parse.
            #
            if g is None:
                raise BadSection("Section selector '%s' can not be parsed" % \
                                 section)
            g.flatten(msg)
            msg_text = fp.getvalue()

        # We have our message text we need to return to our caller.
        # truncate if it we also have a 'partial' defined.
        #
        # Convert \n in to \r\n
        #
        msg_text = email.utils.fix_eols(msg_text)
        if self.partial:
            msg_text = msg_text[self.partial[0]:self.partial[1]]

        # We return all of these body sections as a length prefixed IMAP
        # string.
        #
        return "{%d}\r\n%s" % (len(msg_text), str(msg_text))

    #######################################################################
    #
dataset/ETHPy150Open scanner/asimap/asimap/fetch.py/FetchAtt.body/original