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

  1. #
  2. #  Preferences.py
  3. #  JunkMatcher
  4. #
  5. #  Created by Benjamin Han on 2/1/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. #!/usr/bin/env python
  24.  
  25. import string
  26.  
  27. from consts import *
  28. from utilities import *
  29.  
  30.  
  31. class Preferences:
  32.     """
  33.     Global Preferences of JunkMatcher
  34.     ---------------------------------
  35.     fn: filename of the prefs file
  36.     siteLimit: (int) the size limit for SiteDB
  37.     mode: (int) matching mode for Matcher
  38.     modeArgs: (list of strings) optional arguments for matching mode
  39.     junkMessage: (bool) whether to mark matched messages as junk
  40.     recycleDays: (int) # of days to periodically recycle log
  41.     """
  42.     def __init__ (self, fn):
  43.         self.fn = fn
  44.         self.load()
  45.  
  46.     def __getattr__ (self, name):
  47.         # if a name is referenced but does not yet exist, try to query the defaults
  48.         defaultPrefs = Preferences('%sprefs' % DEFAULTS_PATH)
  49.         for k in filter(lambda k: not self.__dict__.has_key(k), defaultPrefs.__dict__.keys()):
  50.             setattr(self, k, getattr(defaultPrefs, k))
  51.  
  52.         if self.__dict__.has_key(name): return self.__dict__[name]
  53.         else: raise AttributeError(name)
  54.  
  55.     def load (self):
  56.         mode = 0
  57.         for l in filter(lambda l:len(l) and l[0] != '#',  map(string.strip, openFile(self.fn))):
  58.             if mode == 0:
  59.                 self.siteLimit = int(l)
  60.             elif mode == 1:
  61.                 l = l.split(' ')
  62.                 self.mode = int(l[0])
  63.                 self.modeArgs = l[1:]
  64.             elif mode == 2:
  65.                 self.junkMessage = (l[0] == '1')
  66.             elif mode == 3:
  67.                 self.recycleDays = int(l)
  68.  
  69.             mode += 1        
  70.  
  71.     def writeToFile (self):
  72.         openFile(self.fn, 'w').write('%d\n%d %s\n%d\n%d' %\
  73.                                      (self.siteLimit,
  74.                                       self.mode, encodeText(' '.join(self.modeArgs)),
  75.                                       int(self.junkMessage),
  76.                                       int(self.recycleDays)))
  77.  
  78.  
  79. if __name__ == '__main__':
  80.     prefs = Preferences('%sprefs' % CONF_PATH)
  81.     #print prefs.foo
  82.