Changeset 498 for trunk/email2trac.py.in
- Timestamp:
- 12/28/10 18:17:50 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/email2trac.py.in
r497 r498 75 75 import logging.handlers 76 76 import UserDict 77 import tempfile 77 78 78 79 from datetime import tzinfo, timedelta, datetime … … 409 410 ########## DEBUG functions ########################################################### 410 411 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: 414 414 body_file = tempfile.mktemp('.email2trac') 415 415 else: … … 487 487 488 488 if create_tempfile: 489 import tempfile490 489 msg_file = tempfile.mktemp('.email2trac') 491 490 else: … … 1759 1758 1760 1759 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) 1763 1765 1764 1766 ## Check content type 1765 1767 # 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())) 1768 1770 continue 1769 1771 1770 1772 ## Catch some mulitpart execptions 1771 1773 # 1772 if part.get_content_type()== 'multipart/alternative':1774 if content_type == 'multipart/alternative': 1773 1775 ALTERNATIVE_MULTIPART = True 1774 1776 continue … … 1776 1778 ## Skip multipart containers 1777 1779 # 1778 if part.get_content_maintype()== 'multipart':1780 if content_maintype == 'multipart': 1779 1781 self.logger.debug("Skipping multipart container") 1780 1781 1782 continue 1782 1783 … … 1788 1789 # 1789 1790 if ALTERNATIVE_MULTIPART and self.parameters.drop_alternative_html_version: 1790 if part.get_content_type()== 'text/html':1791 if content_type == 'text/html': 1791 1792 self.logger.debug('Skipping alternative HTML message') 1792 1793 ALTERNATIVE_MULTIPART = False 1793 1794 continue 1794 1795 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 1795 1815 ## Inline text parts are where the body is 1796 1816 # 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)) 1799 1819 1800 1820 ## Try to decode, if fails then do not decode … … 1804 1824 body_text = part.get_payload(decode=0) 1805 1825 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 1806 1854 format = email.Utils.collapse_rfc2231_value(part.get_param('Format', 'fixed')).lower() 1807 1855 delsp = email.Utils.collapse_rfc2231_value(part.get_param('DelSp', 'no')).lower() … … 1841 1889 else: 1842 1890 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 filename1850 #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))1857 1891 1858 1892 return message_parts … … 1961 1995 1962 1996 def get_body_text(self, message_parts): 1997 """ 1998 """ 1999 self.logger.debug('function get_body_text()') 2000 1963 2001 body_text = [] 1964 2002 … … 1975 2013 inline = self.inline_part(part) 1976 2014 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 1990 2021 1991 2022 if part.get_content_maintype() == 'image' and inline: 1992 2023 if self.system != 'discussion': 2024 s = 'wiki image link for: %s' %(filename) 2025 self.print_unicode(s) 1993 2026 body_text.append('[[Image(%s)]]' % filename) 1994 2027 body_text.append("") 1995 2028 else: 1996 2029 if self.system != 'discussion': 2030 s = 'wiki attachment link for: %s' %(filename) 2031 self.print_unicode(s) 1997 2032 body_text.append('[attachment:"%s"]' % filename) 1998 2033 body_text.append("") 1999 2034 2035 ## Convert list body_texts to string 2036 # 2000 2037 body_text = '\r\n'.join(body_text) 2001 2038 return body_text … … 2159 2196 2160 2197 if not os.path.isabs(parameters.log_file): 2161 import tempfile2162 2198 parameters.log_file = os.path.join(tempfile.gettempdir(), parameters.log_file) 2163 2199
Note: See TracChangeset
for help on using the changeset viewer.