⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 message.py

📁 mallet是自然语言处理、机器学习领域的一个开源项目。
💻 PY
📖 第 1 页 / 共 3 页
字号:
# Copyright (C) 2001,2002 Python Software Foundation# Author: barry@zope.com (Barry Warsaw)"""Basic message object for the email package object model."""import reimport uuimport binasciiimport warningsfrom cStringIO import StringIOfrom types import ListType, TupleType, StringType# Intrapackage importsfrom email import Utilsfrom email import Errorsfrom email import CharsetSEMISPACE = '; 'try:    True, Falseexcept NameError:    True = 1    False = 0# Regular expression used to split header parameters.  BAW: this may be too# simple.  It isn't strictly RFC 2045 (section 5.1) compliant, but it catches# most headers found in the wild.  We may eventually need a full fledged# parser eventually.paramre = re.compile(r'\s*;\s*')# Regular expression that matches `special' characters in parameters, the# existance of which force quoting of the parameter value.tspecials = re.compile(r'[ \(\)<>@,;:\\"/\[\]\?=]')# Helper functionsdef _formatparam(param, value=None, quote=True):    """Convenience function to format and return a key=value pair.    This will quote the value if needed or if quote is true.    """    if value is not None and len(value) > 0:        # TupleType is used for RFC 2231 encoded parameter values where items        # are (charset, language, value).  charset is a string, not a Charset        # instance.        if isinstance(value, TupleType):            # Encode as per RFC 2231            param += '*'            value = Utils.encode_rfc2231(value[2], value[0], value[1])        # BAW: Please check this.  I think that if quote is set it should        # force quoting even if not necessary.        if quote or tspecials.search(value):            return '%s="%s"' % (param, Utils.quote(value))        else:            return '%s=%s' % (param, value)    else:        return paramdef _unquotevalue(value):    if isinstance(value, TupleType):        return value[0], value[1], Utils.unquote(value[2])    else:        return Utils.unquote(value)class Message:    """Basic message object.    A message object is defined as something that has a bunch of RFC 2822    headers and a payload.  It may optionally have an envelope header    (a.k.a. Unix-From or From_ header).  If the message is a container (i.e. a    multipart or a message/rfc822), then the payload is a list of Message    objects, otherwise it is a string.    Message objects implement part of the `mapping' interface, which assumes    there is exactly one occurrance of the header per message.  Some headers    do in fact appear multiple times (e.g. Received) and for those headers,    you must use the explicit API to set or get all the headers.  Not all of    the mapping methods are implemented.    """    def __init__(self):        self._headers = []        self._unixfrom = None        self._payload = None        self._charset = None        # Defaults for multipart messages        self.preamble = self.epilogue = None        # Default content type        self._default_type = 'text/plain'    def __str__(self):        """Return the entire formatted message as a string.        This includes the headers, body, and envelope header.        """        return self.as_string(unixfrom=True)    def as_string(self, unixfrom=False):        """Return the entire formatted message as a string.        Optional `unixfrom' when True, means include the Unix From_ envelope        header.        This is a convenience method and may not generate the message exactly        as you intend.  For more flexibility, use the flatten() method of a        Generator instance.        """        from email.Generator import Generator        fp = StringIO()        g = Generator(fp)        g.flatten(self, unixfrom=unixfrom)        return fp.getvalue()    def is_multipart(self):        """Return True if the message consists of multiple parts."""        if isinstance(self._payload, ListType):            return True        return False    #    # Unix From_ line    #    def set_unixfrom(self, unixfrom):        self._unixfrom = unixfrom    def get_unixfrom(self):        return self._unixfrom    #    # Payload manipulation.    #    def add_payload(self, payload):        """Add the given payload to the current payload.        If the current payload is empty, then the current payload will be made        a scalar, set to the given value.        Note: This method is deprecated.  Use .attach() instead.        """        warnings.warn('add_payload() is deprecated, use attach() instead.',                      DeprecationWarning, 2)        if self._payload is None:            self._payload = payload        elif isinstance(self._payload, ListType):            self._payload.append(payload)        elif self.get_main_type() not in (None, 'multipart'):            raise Errors.MultipartConversionError(                'Message main content type must be "multipart" or missing')        else:            self._payload = [self._payload, payload]    def attach(self, payload):        """Add the given payload to the current payload.        The current payload will always be a list of objects after this method        is called.  If you want to set the payload to a scalar object, use        set_payload() instead.        """        if self._payload is None:            self._payload = [payload]        else:            self._payload.append(payload)    def get_payload(self, i=None, decode=False):        """Return a reference to the payload.        The payload will either be a list object or a string.  If you mutate        the list object, you modify the message's payload in place.  Optional        i returns that index into the payload.        Optional decode is a flag indicating whether the payload should be        decoded or not, according to the Content-Transfer-Encoding header        (default is False).        When True and the message is not a multipart, the payload will be        decoded if this header's value is `quoted-printable' or `base64'.  If        some other encoding is used, or the header is missing, or if the        payload has bogus data (i.e. bogus base64 or uuencoded data), the        payload is returned as-is.        If the message is a multipart and the decode flag is True, then None        is returned.        """        if i is None:            payload = self._payload        elif not isinstance(self._payload, ListType):            raise TypeError, i        else:            payload = self._payload[i]        if decode:            if self.is_multipart():                return None            cte = self.get('content-transfer-encoding', '').lower()            if cte == 'quoted-printable':                return Utils._qdecode(payload)            elif cte == 'base64':                try:                    return Utils._bdecode(payload)                except binascii.Error:                    # Incorrect padding                    return payload            elif cte in ('x-uuencode', 'uuencode', 'uue', 'x-uue'):                sfp = StringIO()                try:                    uu.decode(StringIO(payload+'\n'), sfp)                    payload = sfp.getvalue()                except uu.Error:                    # Some decoding problem                    return payload        # Everything else, including encodings with 8bit or 7bit are returned        # unchanged.        return payload    def set_payload(self, payload, charset=None):        """Set the payload to the given value.        Optional charset sets the message's default character set.  See        set_charset() for details.        """        self._payload = payload        if charset is not None:            self.set_charset(charset)    def set_charset(self, charset):        """Set the charset of the payload to a given character set.        charset can be a Charset instance, a string naming a character set, or        None.  If it is a string it will be converted to a Charset instance.        If charset is None, the charset parameter will be removed from the        Content-Type field.  Anything else will generate a TypeError.        The message will be assumed to be of type text/* encoded with        charset.input_charset.  It will be converted to charset.output_charset        and encoded properly, if needed, when generating the plain text        representation of the message.  MIME headers (MIME-Version,        Content-Type, Content-Transfer-Encoding) will be added as needed.        """        if charset is None:            self.del_param('charset')            self._charset = None            return        if isinstance(charset, StringType):            charset = Charset.Charset(charset)        if not isinstance(charset, Charset.Charset):            raise TypeError, charset        # BAW: should we accept strings that can serve as arguments to the        # Charset constructor?        self._charset = charset        if not self.has_key('MIME-Version'):            self.add_header('MIME-Version', '1.0')        if not self.has_key('Content-Type'):            self.add_header('Content-Type', 'text/plain',                            charset=charset.get_output_charset())        else:            self.set_param('charset', charset.get_output_charset())        if not self.has_key('Content-Transfer-Encoding'):            cte = charset.get_body_encoding()            if callable(cte):                cte(self)            else:                self.add_header('Content-Transfer-Encoding', cte)    def get_charset(self):        """Return the Charset instance associated with the message's payload.        """        return self._charset    #    # MAPPING INTERFACE (partial)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -