Changeset 498 for trunk/email2trac.py.in


Ignore:
Timestamp:
12/28/10 18:17:50 (13 years ago)
Author:
bas
Message:

added support for html to text conversion, in email2trac.conf:

  • html2text_cmd: /usr/bin/html2text -nobs

see #152

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/email2trac.py.in

    r497 r498  
    7575import logging.handlers
    7676import UserDict
     77import tempfile
    7778
    7879from datetime import tzinfo, timedelta, datetime
     
    409410########## DEBUG functions  ###########################################################
    410411
    411         def debug_body(self, message_body, tempfile=False):
    412                 if tempfile:
    413                         import tempfile
     412        def debug_body(self, message_body, temporary_file=False):
     413                if temporary_file:
    414414                        body_file = tempfile.mktemp('.email2trac')
    415415                else:
     
    487487
    488488                if create_tempfile:
    489                         import tempfile
    490489                        msg_file = tempfile.mktemp('.email2trac')
    491490                else:
     
    17591758
    17601759                for part in msg.walk():
    1761                         self.logger.debug('Message part: Main-Type: %s' % part.get_content_maintype())
    1762                         self.logger.debug('Message part: Content-Type: %s' % part.get_content_type())
     1760                        content_maintype = part.get_content_maintype()
     1761                        content_type =  part.get_content_type()
     1762
     1763                        self.logger.debug('Message part: Main-Type: %s' % content_maintype)
     1764                        self.logger.debug('Message part: Content-Type: %s' % content_type)
    17631765
    17641766                        ## Check content type
    17651767                        #
    1766                         if part.get_content_type() in self.STRIP_CONTENT_TYPES:
    1767                                 self.logger.debug("A %s attachment named '%s' was skipped" %(part.get_content_type(), part.get_filename()))
     1768                        if content_type in self.STRIP_CONTENT_TYPES:
     1769                                self.logger.debug("A %s attachment named '%s' was skipped" %(content_type, part.get_filename()))
    17681770                                continue
    17691771
    17701772                        ## Catch some mulitpart execptions
    17711773                        #
    1772                         if part.get_content_type() == 'multipart/alternative':
     1774                        if content_type == 'multipart/alternative':
    17731775                                ALTERNATIVE_MULTIPART = True
    17741776                                continue
     
    17761778                        ## Skip multipart containers
    17771779                        #
    1778                         if part.get_content_maintype() == 'multipart':
     1780                        if content_maintype == 'multipart':
    17791781                                self.logger.debug("Skipping multipart container")
    1780 
    17811782                                continue
    17821783                       
     
    17881789                        #
    17891790                        if ALTERNATIVE_MULTIPART and self.parameters.drop_alternative_html_version:
    1790                                 if part.get_content_type() == 'text/html':
     1791                                if content_type == 'text/html':
    17911792                                        self.logger.debug('Skipping alternative HTML message')
    17921793                                        ALTERNATIVE_MULTIPART = False
    17931794                                        continue
    17941795
     1796
     1797                        ## Save all non plain text message as attachment
     1798                        #
     1799                        if not content_type in ['text/plain']:
     1800
     1801                                if self.parameters.debug:
     1802                                        s = '              Filename: %s' % part.get_filename()
     1803                                        self.print_unicode(s)
     1804
     1805                                ## First try to use email header function to convert filename.
     1806                                #  If this fails the use the plain filename
     1807                                #
     1808                                try:
     1809                                        filename = self.email_to_unicode(part.get_filename())
     1810                                except UnicodeEncodeError, detail:
     1811                                        filename = part.get_filename()
     1812
     1813                                message_parts.append((filename, part))
     1814
    17951815                        ## Inline text parts are where the body is
    17961816                        #
    1797                         if part.get_content_type() == 'text/plain' and inline:
    1798                                 self.logger.debug('               Inline body part')
     1817                        if (content_type in ['text/plain', 'text/html']) and inline:
     1818                                self.logger.debug('               Inline body part (content_type = %s)' %(content_type))
    17991819
    18001820                                ## Try to decode, if fails then do not decode
     
    18041824                                        body_text = part.get_payload(decode=0)
    18051825
     1826                                #print type(body_text) must be string
     1827
     1828                                ## Convert html message to text if a conversion command is specified
     1829                                #
     1830                                if content_type == 'text/html' and self.parameters.html2text_cmd:
     1831                                        tmp_file = tempfile.mktemp('.email2trac')
     1832                                        cmd = '%s %s' %(self.parameters.html2text_cmd, tmp_file)
     1833
     1834                                        self.logger.debug('html2text conversion %s'%(cmd))
     1835
     1836                                        if self.parameters.dry_run:
     1837                                                print 'DRY_RUN: html2text conversion command: %s\n' %(cmd)
     1838                                        else:
     1839                                                f = open(tmp_file, "w+")
     1840                                                f.write(body_text)
     1841                                                f.close()
     1842
     1843                                                lines = os.popen(cmd).readlines()
     1844                                                body_text = ''.join(lines)
     1845
     1846                                                os.unlink(tmp_file)
     1847
     1848
     1849                                else:
     1850                                        ## Save only as attachment. No further processing
     1851                                        #
     1852                                        continue
     1853
    18061854                                format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower()
    18071855                                delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower()
     
    18411889                                else:
    18421890                                        message_parts.append('%s' %ubody_text)
    1843                         else:
    1844                                 if self.parameters.debug:
    1845                                         s = '              Filename: %s' % part.get_filename()
    1846                                         self.print_unicode(s)
    1847 
    1848                                 ## First try to use email header function to convert filename.
    1849                                 #  If this fails the use the plan filename
    1850                                 #
    1851                                 try:
    1852                                         filename = self.email_to_unicode(part.get_filename())
    1853                                 except UnicodeEncodeError, detail:
    1854                                         filename = part.get_filename()
    1855 
    1856                                 message_parts.append((filename, part))
    18571891
    18581892                return message_parts
     
    19611995                       
    19621996        def get_body_text(self, message_parts):
     1997                """
     1998                """
     1999                self.logger.debug('function get_body_text()')
     2000
    19632001                body_text = []
    19642002               
     
    19752013                        inline = self.inline_part(part)
    19762014
    1977 #                       if part.get_content_type() == 'text/html':
    1978 #               
    1979 #                               body_text.append('{{{\n')
    1980 #                               body_text.append('#!html\n')
    1981 #
    1982 #                               ## Try to decode, if fails then do not decode
    1983 #                               #
    1984 #                               tmp  = part.get_payload(decode=1)
    1985 #                               if not tmp:                     
    1986 #                                       tmp = part.get_payload(decode=0)
    1987 #                               
    1988 #                               body_text.append(tmp)
    1989 #                               body_text.append('}}}\n')
     2015                        ## Skip generation of attachment link if html is converted to text
     2016                        #
     2017                        if part.get_content_type() == 'text/html' and self.parameters.html2text_cmd and inline:
     2018                                s = 'Skipping attachment link for html part: %s' %(filename)
     2019                                self.print_unicode(s)
     2020                                continue
    19902021                       
    19912022                        if part.get_content_maintype() == 'image' and inline:
    19922023                                if self.system != 'discussion':
     2024                                        s = 'wiki image link for: %s' %(filename)
     2025                                        self.print_unicode(s)
    19932026                                        body_text.append('[[Image(%s)]]' % filename)
    19942027                                body_text.append("")
    19952028                        else:
    19962029                                if self.system != 'discussion':
     2030                                        s = 'wiki attachment link for: %s' %(filename)
     2031                                        self.print_unicode(s)
    19972032                                        body_text.append('[attachment:"%s"]' % filename)
    19982033                                body_text.append("")
    19992034
     2035                ## Convert list body_texts to string
     2036                #
    20002037                body_text = '\r\n'.join(body_text)
    20012038                return body_text
     
    21592196
    21602197                if not os.path.isabs(parameters.log_file):
    2161                         import tempfile
    21622198                        parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file)
    21632199
Note: See TracChangeset for help on using the changeset viewer.