home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 108 / MacAddict108.iso / Software / Internet & Communication / JunkMatcher 1.5.5.dmg / JunkMatcher.app / Contents / Resources / backupOldSettings.py < prev    next >
Encoding:
Python Source  |  2005-06-01  |  7.2 KB  |  165 lines

  1. #
  2. #  backupOldSettings.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 3/11/05.
  6. #  Copyright (c) 2005 Benjamin Han. All rights reserved.
  7. #
  8.  
  9. # This program is free software; you can redistribute it and/or
  10. # modify it under the terms of the GNU General Public License
  11. # as published by the Free Software Foundation; either version 2
  12. # of the License, or (at your option) any later version.
  13.  
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18.  
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  
  23. import sys, shutil
  24.  
  25. sys.path.insert(0, '../Engine')
  26.  
  27. from consts import *
  28. from utilities import *
  29.  
  30. patternFiles = [('junkContent.txt', 'Patterns for message body', 'body'),
  31.                 ('junkFilename.txt', 'Patterns for attachment filenames', 'filenames'),
  32.                 ('junkHTML.txt', 'Patterns for message rendering', 'rendering'),
  33.                 ('junkHeaders.txt', 'Patterns for message headers', 'headers'),
  34.                 ('junkSender.txt', 'Patterns for senders', 'sender'),
  35.                 ('junkSubj.txt', 'Patterns for message subjects', 'subject'),
  36.                 ('metaPatterns.txt', 'Meta patterns', None),
  37.                 ('safeIP.txt', 'Patterns for Safe IPs', None),
  38.                 ('safeRecipients.txt', 'Patterns of your email addresses', None)]
  39.  
  40. htmlTemplate = '''<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  41. <html>
  42. <head>
  43. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  44. <link rel="stylesheet" href=".base.css" type="text/css">
  45. <title>%s</title>
  46. </head>
  47. <body>
  48. <h2>%s</h2>
  49. <p>%s</p>
  50. %s
  51. </body>
  52. </html>
  53. '''
  54.  
  55.  
  56. def readPatternsFromFN (fn):
  57.     try:
  58.         f = openFile(fn, 'r', 'macroman')
  59.     except:
  60.         # file might not exist
  61.         return None
  62.         
  63.     # old pattern file format: odd lines are patterns, even lines are comments,
  64.     # followed by (optionally) the active state (0/1)
  65.     ret = []
  66.     isComment = False
  67.     idx = 0
  68.     for line in f:
  69.         line = line.strip()
  70.         if isComment:
  71.             isComment = False
  72.             row = '<td>%s</td><td>%s</td>' % (line[1:line.rfind('"')], pat)
  73.             if idx % 2:
  74.                 row = '<tr>%s</tr>' % row
  75.             else:
  76.                 row = '<tr class="grayRow">%s</tr>' % row
  77.                 
  78.             idx += 1
  79.             ret.append(row)
  80.         else:            
  81.             pat = line[1:-1]
  82.             isComment = True
  83.  
  84.     return ret
  85.  
  86. def backupOldSettings (srcPath, destPath):
  87.     """Processes each configuration file of JM 1.19c/earlier, and put them in folder
  88.     destPath/Old JunkMatcher Settings."""
  89.     destPath = os.path.join(destPath, 'Old JunkMatcher Patterns')
  90.     if os.path.exists(destPath):
  91.         shutil.rmtree(destPath)
  92.     os.mkdir(destPath)
  93.     shutil.copy(ROOT_PATH + '.base.css', destPath)
  94.  
  95.     for srcFN, title, instruction in patternFiles:
  96.         try:
  97.             patternList = readPatternsFromFN(os.path.join(srcPath, srcFN))
  98.             if instruction is None:
  99.                 # special cases
  100.                 if srcFN == 'metaPatterns.txt':
  101.                     instruction = 'Please select the meta patterns you want to keep and copy/paste them into the Meta Patterns drawer in the new JunkMatcher.app.'
  102.                 elif srcFN == 'safeIP.txt':
  103.                     instruction = 'Please select the IP patterns you want to keep, and copy/paste them into the "Safe IP Patterns" window - this window is accessible via the Test Inspector window over the property "Open relay" in the new JunkMatcher.app.'
  104.                 elif srcFN == 'safeRecipients.txt':
  105.                     instruction = 'Please select the email address patterns that are used to send emails to you, and copy/paste them into the "Recipient Patterns" window - this window is accessible via the Test Inspector window over the property "Recipient(s) mismatch" in the new JunkMatcher.app.'
  106.             else:
  107.                 instruction = 'The following patterns were used to match message view: "%s". \
  108. Select the patterns you need and copy/paste them to target the same message view in the new JunkMatcher.app.' % instruction
  109.         
  110.             if patternList is not None:
  111.                 f = openFile(os.path.join(destPath, title + '.html'), 'w', 'utf8')            
  112.                 f.write(htmlTemplate % (title, 'Old patterns: %s' % title,
  113.                                         '<b>Instruction:</b> %s' % instruction,
  114.                                         '<div id="patternTable">\n<table>\n<tr><td><b>Pattern Names</b></td><td><b>Patterns</b></td></tr>%s</table>\n</div>' % '\n'.join(patternList)))
  115.  
  116.         except Exception, e:
  117.             printException('Exception when backing up %s' % srcFN, e)
  118.  
  119.     # whitelist
  120.     try:
  121.         srcFN = os.path.join(srcPath, 'safeSenders.txt')
  122.         if os.path.exists(srcFN):
  123.             f = openFile(srcFN, 'r', 'macroman')
  124.             patternList = []
  125.             for idx, l in enumerate(f.readlines()):
  126.                 if idx % 2:
  127.                     patternList.append('<tr><td>%s</td></tr>' % l.strip())
  128.                 else:
  129.                     patternList.append('<tr class="grayRow"><td>%s</td></tr>' % l.strip())
  130.             f.close()
  131.         
  132.             f = openFile(os.path.join(destPath, 'Whitelist.html'), 'w', 'utf8')
  133.             f.write(htmlTemplate % (title, 'Old Whitelist',
  134.                                     '<b>Instruction:</b> Please select the email addresses you want to whitelist into the Whitelist window of the new JunkMatcher.app (accessible via one of the toolbars).',
  135.                                     '<div id="patternTable">\n<table>\n<tr><td><b>Email Addresses</b></td></tr>%s</table>\n</div>' % '\n'.join(patternList)))
  136.  
  137.     except Exception, e:
  138.         printException('Exception when backing up safeSenders.txt', e)
  139.  
  140.     # safe sites
  141.     try:
  142.         srcFN = os.path.join(srcPath, 'safeSites.txt')
  143.         if os.path.exists(srcFN):
  144.             f = openFile(srcFN, 'r', 'macroman')
  145.             patternList = []
  146.             for idx, l in enumerate(f.readlines()):
  147.                 if idx % 2:
  148.                     patternList.append('<tr><td>%s</td></tr>' % l.strip())
  149.                 else:
  150.                     patternList.append('<tr class="grayRow"><td>%s</td></tr>' % l.strip())
  151.             f.close()
  152.         
  153.             f = openFile(os.path.join(destPath, 'Safe Sites.html'), 'w', 'utf8')
  154.             f.write(htmlTemplate % (title, 'Old list of Safe Sites',
  155.                                     '<b>Instruction:</b> Please select the URL patterns you want to avoid collecting and copy/paste them into the Safe Sites drawer in the new JunkMatcher.app (accessible via the Sites window, on one of the toolbars).',
  156.                                     '<div id="patternTable">\n<table>\n<tr><td><b>Email Addresses</b></td></tr>%s</table>\n</div>' % '\n'.join(patternList)))
  157.  
  158.     except Exception, e:
  159.         printException('Exception when backing up safeSites.txt', e)
  160.  
  161.  
  162. if __name__ == '__main__':
  163.     backupOldSettings('/Users/ben/Public/JunkMatcher.mailbundle/Contents/Resources/junkMatcher/conf',
  164.                       '/Users/ben/Desktop')
  165.