<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def search(self, ** args):
 """ Checks email inbox every 15 seconds that match the criteria up until timeout. Search criteria should be keyword args eg TO="selenium@gmail.com". See __imap_search docstring for list of valid criteria. If content_type is not defined, will return a list of msg numbers. Options: - fetch: will return a dict of Message objects, keyed on msgnum, which can be used to look at headers and other parts of the complete message. (http://docs.python.org/library/email.message.html) - timeout: will replace the default module timeout with the value in SECONDS. - content_type: should be either "PLAIN" or "HTML". If defined returns the source of the matched messages as a dict of msgnum:content. If not defined we return a list of msg nums. """


        if "content_type" not in args.keys():
            content_type = None
        elif "HTML" in args["content_type"]:
            content_type = self.HTML
            del args["content_type"]
        elif "PLAIN" in args["content_type"]:
            content_type = self.PLAIN
            del args["content_type"]
        elif args["content_type"]:
            content_type = args['content_type']
            del args["content_type"]

        if "timeout" not in args.keys():
            timeout = self.TIMEOUT
        elif "timeout" in args:
            timeout = args["timeout"]
            del args["timeout"]

        fetch = False
        if "fetch" in args.keys():
            fetch = True
            del args["fetch"]

        results = None
        timer = timeout
        count = 0
        while count < timer:
            results = self.__imap_search(** args)
            if len(results) > 0:
                if fetch:
                    msgs = {}
                    for msgnum in results:
                        msgs[msgnum] = self.fetch_message(msgnum)
                    return msgs
                elif not content_type:
                    return results
                else:
                    return self.__imap_fetch_content_type(results,
                                                          content_type)
            else:
                time.sleep(15)
                count += 15
        if count >= timer:
            raise EmailException(
                "Failed to find message for criteria %s in %s minutes" %
                (args, timeout / 60))