Logo Search packages:      
Sourcecode: zope-attachmentfield version File versions  Download package

def AttachmentField::AttachmentHandler::AbstractHandler::_convertString (   self,
  content,
  filename,
  program,
  arguments,
  encoding 
) [private]

Utility method to convert stuff.
This method will ALWAYS return an unicode string
Encoding can be a string, a list, or None.

Definition at line 299 of file AttachmentHandler.py.

00299                                                                                :
        """
        Utility method to convert stuff.
        This method will ALWAYS return an unicode string
        Encoding can be a string, a list, or None.
        """
        # Basic checks.
        index = ""
        if arguments is None:
            return ""
        if not program:
            return ""

        f, fn = tempfile.mkstemp()
        try:
            # Write attachment in a temporary file
            os.close(f)
            f = open(fn, "w+b")
            f.write(content)
            f.close()

            # Call converter in the right directory
            Log(LOG_DEBUG, "Calling converter for", filename, "in directory", tempfile.tempdir)
            index = self.callConverter(
                program,
                arguments % (fn, ),
                )

        finally:
            # Delete attachment file
            if os.path.isfile(fn):
                os.unlink(fn)
                Log(LOG_DEBUG, "Removed attachment file '%s'" % (fn,))

        # Deal with various encodings
        if encoding:
            # Convert from encoded string to unicode
            if type(encoding) in (type(''), type(u''), ):
                Log(LOG_DEBUG, "Encoding", encoding)
                index = index.decode(encoding, "replace")

            elif type(encoding) in (type(None),):
                pass

            elif type(encoding) in (type([]), type(()), ):
                for enc in encoding:
                    try:
                        Log(LOG_DEBUG, "Trying encoding", enc)
                        index = index.decode(enc, "replace")
                        break

                    except UnicodeError:
                        Log(LOG_DEBUG, "Encoding", enc, "failed.")
                        pass

        # Return the string
        return index


    #                                                                   #
    #                         HTML PREVIEW SUPPORT                      #
    #                                                                   #

    _strip_style = re.compile(r"""style\s*=\s*["'][^"']*["']""", re.I | re.S)


Generated by  Doxygen 1.6.0   Back to index