Пример #1
0
    def pt_upload(self, REQUEST, file='', encoding='utf-8'):
        """Replace the document with the text in file."""

        if self.wl_isLocked():
            raise ResourceLockedError("File is locked.")

        if not file:
            return self.pt_editForm(manage_tabs_message='No file specified',
                                    manage_tabs_type='warning')

        if hasattr(file, 'read'):
            text = file.read()
            filename = file.filename
        else:
            filename = None
            text = file

        if isinstance(text, binary_type):
            content_type = guess_type(filename, text)
            (text,
             source_encoding) = convertToUnicode(text, content_type,
                                                 preferred_encodings)
        elif isinstance(text, text_type):
            content_type = guess_type(filename, text.encode('utf-8'))

        self.pt_edit(text, content_type)
        return self.pt_editForm(manage_tabs_message='Saved changes')
Пример #2
0
    def write(self, text):
        if not isinstance(text, text_type):
            text, encoding = convertToUnicode(text, self.content_type,
                                              preferred_encodings)
            self.output_encoding = encoding

        self.ZCacheable_invalidate()
        ZopePageTemplate.inheritedAttribute('write')(self, text)
Пример #3
0
    def write(self, text):
        if not isinstance(text, text_type):
            text, encoding = convertToUnicode(text,
                                              self.content_type,
                                              preferred_encodings)
            self.output_encoding = encoding

        self.ZCacheable_invalidate()
        ZopePageTemplate.inheritedAttribute('write')(self, text)
Пример #4
0
 def __setstate__(self, state):
     # Perform on-the-fly migration to unicode.
     # Perhaps it might be better to work with the 'generation' module
     # here?
     if not isinstance(state['_text'], unicode):
         text, encoding = convertToUnicode(
             state['_text'], state.get('content_type', 'text/html'),
             preferred_encodings)
         state['_text'] = text
         state['output_encoding'] = encoding
     self.__dict__.update(state)
Пример #5
0
 def __setstate__(self, state):
     # Perform on-the-fly migration to unicode.
     # Perhaps it might be better to work with the 'generation' module 
     # here?
     _text = state.get('_text')
     if _text is not None and not isinstance(state['_text'], unicode):
         text, encoding = convertToUnicode(state['_text'], 
                                 state.get('content_type', 'text/html'), 
                                 preferred_encodings)
         state['_text'] = text
         state['output_encoding'] = encoding
     self.__dict__.update(state) 
Пример #6
0
    def pt_edit(self, text, content_type, keep_output_encoding=False):

        text = text.strip()
        
        is_unicode = isinstance(text, unicode)
        encoding = None
        output_encoding = None

        if content_type.startswith('text/xml'):

            if is_unicode:
                encoding = None
                output_encoding = 'utf-8'
            else:
                encoding = encodingFromXMLPreamble(text)
                output_encoding = 'utf-8'

        elif content_type.startswith('text/html'):

            charset = charsetFromMetaEquiv(text)

            if is_unicode:
                if charset:
                    encoding = None
                    output_encoding = charset
                else:
                    encoding = None
                    output_encoding = 'iso-8859-15'
            else:
                if charset:
                    encoding = charset
                    output_encoding = charset
                else:
                    encoding = 'iso-8859-15'
                    output_encoding = 'iso-8859-15'

        else:
            utext, encoding = convertToUnicode(text,
                                               content_type,
                                               preferred_encodings)
            output_encoding = encoding

        # for content updated through WebDAV, FTP 
        if not keep_output_encoding:
            self.output_encoding = output_encoding

        if not is_unicode:
            text = unicode(text, encoding)

        self.ZCacheable_invalidate()
        super(ZopePageTemplate, self).pt_edit(text, content_type)
Пример #7
0
    def pt_edit(self, text, content_type, keep_output_encoding=False):
        if not isinstance(text, text_type):
            text_decoded, source_encoding = convertToUnicode(
                text, content_type, preferred_encodings)
            output_encoding = source_encoding
        else:
            text_decoded = text
            source_encoding = None
            output_encoding = 'utf-8'

        # for content updated through WebDAV, FTP
        if not keep_output_encoding:
            self.output_encoding = output_encoding

        text_decoded = text_decoded.strip()

        self.ZCacheable_invalidate()
        super(ZopePageTemplate, self).pt_edit(text_decoded, content_type)
Пример #8
0
    def pt_edit(self, text, content_type, keep_output_encoding=False):
        if not isinstance(text, text_type):
            (text_decoded,
             source_encoding) = convertToUnicode(text, content_type,
                                                 preferred_encodings)
            output_encoding = source_encoding
        else:
            text_decoded = text
            source_encoding = None
            output_encoding = 'utf-8'

        # for content updated through WebDAV, FTP
        if not keep_output_encoding:
            self.output_encoding = output_encoding

        text_decoded = text_decoded.strip()

        self.ZCacheable_invalidate()
        super(ZopePageTemplate, self).pt_edit(text_decoded, content_type)
Пример #9
0
# PATCH END
    if content_type.startswith('text/xml'):
        encoding = encodingFromXMLPreamble(source)
        return unicode(source, encoding), encoding

    elif content_type.startswith('text/html'):
        encoding = charsetFromMetaEquiv(source)
        if encoding:
            return unicode(source, encoding), encoding

    # Try to detect the encoding by converting it unicode without raising
    # exceptions. There are some smarter Python-based sniffer methods
    # available however we have to check their licenses first before
    # including them into the Zope 2 core

    for enc in preferred_encodings:
        try:
            return unicode(source, enc), enc
        except UnicodeDecodeError:
            continue

    return unicode(source), None

try:
    convertToUnicode(u'', 'text/xml', ())
except TypeError:
    # We need to monkey patch in-place, as it is a top-level function and
    # already imported in other places.
    convertToUnicode.func_code = patched_convertToUnicode.func_code
Пример #10
0
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE
##############################################################################

NEED_PATCH = False
try:
    from Products.PageTemplates.utils import convertToUnicode
except ImportError:
    # Not present in Zope 2.8
    pass
else:
    try:
        convertToUnicode(u'')
    except TypeError:
        NEED_PATCH = True

def patched_convertToUnicode(source, content_type, preferred_encodings):
    """ Convert 'source' to unicode.
        Returns (unicode_str, source_encoding).
    """

# PATCH BEGINING
# See https://bugs.launchpad.net/zope2/+bug/706946
    if isinstance(source, unicode):
        return source, 'utf-8'
# PATCH END
    if content_type.startswith('text/xml'):
        encoding = encodingFromXMLPreamble(source)