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

📄 message.py

📁 mallet是自然语言处理、机器学习领域的一个开源项目。
💻 PY
📖 第 1 页 / 共 3 页
字号:
        split on the `=' sign.  The left hand side of the `=' is the key,        while the right hand side is the value.  If there is no `=' sign in        the parameter the value is the empty string.  The value is as        described in the get_param() method.        Optional failobj is the object to return if there is no Content-Type        header.  Optional header is the header to search instead of        Content-Type.  If unquote is True, the value is unquoted.        """        missing = []        params = self._get_params_preserve(missing, header)        if params is missing:            return failobj        if unquote:            return [(k, _unquotevalue(v)) for k, v in params]        else:            return params    def get_param(self, param, failobj=None, header='content-type',                  unquote=True):        """Return the parameter value if found in the Content-Type header.        Optional failobj is the object to return if there is no Content-Type        header, or the Content-Type header has no such parameter.  Optional        header is the header to search instead of Content-Type.        Parameter keys are always compared case insensitively.  The return        value can either be a string, or a 3-tuple if the parameter was RFC        2231 encoded.  When it's a 3-tuple, the elements of the value are of        the form (CHARSET, LANGUAGE, VALUE), where LANGUAGE may be the empty        string.  Your application should be prepared to deal with these, and        can convert the parameter to a Unicode string like so:            param = msg.get_param('foo')            if isinstance(param, tuple):                param = unicode(param[2], param[0])        In any case, the parameter value (either the returned string, or the        VALUE item in the 3-tuple) is always unquoted, unless unquote is set        to False.        """        if not self.has_key(header):            return failobj        for k, v in self._get_params_preserve(failobj, header):            if k.lower() == param.lower():                if unquote:                    return _unquotevalue(v)                else:                    return v        return failobj    def set_param(self, param, value, header='Content-Type', requote=True,                  charset=None, language=''):        """Set a parameter in the Content-Type header.        If the parameter already exists in the header, its value will be        replaced with the new value.        If header is Content-Type and has not yet been defined for this        message, it will be set to "text/plain" and the new parameter and        value will be appended as per RFC 2045.        An alternate header can specified in the header argument, and all        parameters will be quoted as necessary unless requote is False.        If charset is specified, the parameter will be encoded according to RFC        2231.  Optional language specifies the RFC 2231 language, defaulting        to the empty string.  Both charset and language should be strings.        """        if not isinstance(value, TupleType) and charset:            value = (charset, language, value)        if not self.has_key(header) and header.lower() == 'content-type':            ctype = 'text/plain'        else:            ctype = self.get(header)        if not self.get_param(param, header=header):            if not ctype:                ctype = _formatparam(param, value, requote)            else:                ctype = SEMISPACE.join(                    [ctype, _formatparam(param, value, requote)])        else:            ctype = ''            for old_param, old_value in self.get_params(header=header,                                                        unquote=requote):                append_param = ''                if old_param.lower() == param.lower():                    append_param = _formatparam(param, value, requote)                else:                    append_param = _formatparam(old_param, old_value, requote)                if not ctype:                    ctype = append_param                else:                    ctype = SEMISPACE.join([ctype, append_param])        if ctype <> self.get(header):            del self[header]            self[header] = ctype    def del_param(self, param, header='content-type', requote=True):        """Remove the given parameter completely from the Content-Type header.        The header will be re-written in place without the parameter or its        value. All values will be quoted as necessary unless requote is        False.  Optional header specifies an alternative to the Content-Type        header.        """        if not self.has_key(header):            return        new_ctype = ''        for p, v in self.get_params(header, unquote=requote):            if p.lower() <> param.lower():                if not new_ctype:                    new_ctype = _formatparam(p, v, requote)                else:                    new_ctype = SEMISPACE.join([new_ctype,                                                _formatparam(p, v, requote)])        if new_ctype <> self.get(header):            del self[header]            self[header] = new_ctype    def set_type(self, type, header='Content-Type', requote=True):        """Set the main type and subtype for the Content-Type header.        type must be a string in the form "maintype/subtype", otherwise a        ValueError is raised.        This method replaces the Content-Type header, keeping all the        parameters in place.  If requote is False, this leaves the existing        header's quoting as is.  Otherwise, the parameters will be quoted (the        default).        An alternative header can be specified in the header argument.  When        the Content-Type header is set, we'll always also add a MIME-Version        header.        """        # BAW: should we be strict?        if not type.count('/') == 1:            raise ValueError        # Set the Content-Type, you get a MIME-Version        if header.lower() == 'content-type':            del self['mime-version']            self['MIME-Version'] = '1.0'        if not self.has_key(header):            self[header] = type            return        params = self.get_params(header, unquote=requote)        del self[header]        self[header] = type        # Skip the first param; it's the old type.        for p, v in params[1:]:            self.set_param(p, v, header, requote)    def get_filename(self, failobj=None):        """Return the filename associated with the payload if present.        The filename is extracted from the Content-Disposition header's        `filename' parameter, and it is unquoted.        """        missing = []        filename = self.get_param('filename', missing, 'content-disposition')        if filename is missing:            return failobj        if isinstance(filename, TupleType):            # It's an RFC 2231 encoded parameter            newvalue = _unquotevalue(filename)            return unicode(newvalue[2], newvalue[0])        else:            newvalue = _unquotevalue(filename.strip())            return newvalue    def get_boundary(self, failobj=None):        """Return the boundary associated with the payload if present.        The boundary is extracted from the Content-Type header's `boundary'        parameter, and it is unquoted.        """        missing = []        boundary = self.get_param('boundary', missing)        if boundary is missing:            return failobj        if isinstance(boundary, TupleType):            # RFC 2231 encoded, so decode.  It better end up as ascii            return unicode(boundary[2], boundary[0]).encode('us-ascii')        return _unquotevalue(boundary.strip())    def set_boundary(self, boundary):        """Set the boundary parameter in Content-Type to 'boundary'.        This is subtly different than deleting the Content-Type header and        adding a new one with a new boundary parameter via add_header().  The        main difference is that using the set_boundary() method preserves the        order of the Content-Type header in the original message.        HeaderParseError is raised if the message has no Content-Type header.        """        missing = []        params = self._get_params_preserve(missing, 'content-type')        if params is missing:            # There was no Content-Type header, and we don't know what type            # to set it to, so raise an exception.            raise Errors.HeaderParseError, 'No Content-Type header found'        newparams = []        foundp = False        for pk, pv in params:            if pk.lower() == 'boundary':                newparams.append(('boundary', '"%s"' % boundary))                foundp = True            else:                newparams.append((pk, pv))        if not foundp:            # The original Content-Type header had no boundary attribute.            # Tack one one the end.  BAW: should we raise an exception            # instead???            newparams.append(('boundary', '"%s"' % boundary))        # Replace the existing Content-Type header with the new value        newheaders = []        for h, v in self._headers:            if h.lower() == 'content-type':                parts = []                for k, v in newparams:                    if v == '':                        parts.append(k)                    else:                        parts.append('%s=%s' % (k, v))                newheaders.append((h, SEMISPACE.join(parts)))            else:                newheaders.append((h, v))        self._headers = newheaders    try:        from email._compat22 import walk    except SyntaxError:        # Must be using Python 2.1        from email._compat21 import walk    def get_content_charset(self, failobj=None):        """Return the charset parameter of the Content-Type header.        The returned string is always coerced to lower case.  If there is no        Content-Type header, or if that header has no charset parameter,        failobj is returned.        """        missing = []        charset = self.get_param('charset', missing)        if charset is missing:            return failobj        if isinstance(charset, TupleType):            # RFC 2231 encoded, so decode it, and it better end up as ascii.            charset = unicode(charset[2], charset[0]).encode('us-ascii')        # RFC 2046, $4.1.2 says charsets are not case sensitive        return charset.lower()    def get_charsets(self, failobj=None):        """Return a list containing the charset(s) used in this message.        The returned list of items describes the Content-Type headers'        charset parameter for this message and all the subparts in its        payload.        Each item will either be a string (the value of the charset parameter        in the Content-Type header of that part) or the value of the        'failobj' parameter (defaults to None), if the part does not have a        main MIME type of "text", or the charset is not defined.        The list will contain one string for each part of the message, plus        one for the container message (i.e. self), so that a non-multipart        message will still return a list of length 1.        """        return [part.get_content_charset(failobj) for part in self.walk()]

⌨️ 快捷键说明

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