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

  1. #
  2. #  utilities.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 sets
  26.  
  27. from consts import *
  28.  
  29. # for some reason cjkcodecs is only in effect when doing the following
  30. import codecs
  31. import cjkcodecs
  32. from cjkcodecs import aliases
  33.  
  34. import objc
  35. from Foundation import *  # for NSLog
  36.  
  37. # for correcting common mispellings of charsets
  38. charsetMispellings = {'big-5':'big5'}
  39.  
  40. _DECODE_ERROR_STRATEGY = 'ignore'
  41. _ENCODE_ERROR_STRATEGY = 'ignore'
  42. _DEFAULT_ENCODING = 'utf8'        # for strings with None encoding, treat them like UTF-8
  43.  
  44.  
  45. class Lazy (object):
  46.     """A discriptor class used for decorator: for lazy initialization."""
  47.     def __init__ (self, calculate_function):
  48.         self._calculate = calculate_function
  49.  
  50.     def __get__ (self, obj, _=None):
  51.         #if obj is None:
  52.         #    return self
  53.         value = self._calculate(obj)
  54.         setattr(obj, self._calculate.func_name, value)
  55.         return value
  56.  
  57.  
  58. def decodeText (text, encoding = None):
  59.     """Decode a text encoded by 'encoding' - if 'encoding' is None, decode using UTF-8
  60.     encoding; returns a Unicode object."""
  61.     if encoding:
  62.         try:
  63.             return text.decode(encoding, _DECODE_ERROR_STRATEGY)
  64.         except:
  65.             return text.decode(_DEFAULT_ENCODING, _DECODE_ERROR_STRATEGY)
  66.  
  67.     return text.decode(_DEFAULT_ENCODING, _DECODE_ERROR_STRATEGY)
  68.  
  69. def decodeTextList (textList, encodingSet):
  70.     """Decode textList: textList is a list of tuples (text, encoding); encodings will be
  71.     added to encodingSet; returns a Unicode object."""
  72.     # correct possible mispellings
  73.     strList = []
  74.     for t, e in textList:
  75.         mispelling = charsetMispellings.get(e)
  76.         if mispelling: e = mispelling
  77.         if e: encodingSet.add(e)
  78.         strList.append(decodeText(t, e))
  79.         
  80.     return ''.join(strList)
  81.  
  82. def encodeText (text, encoding = _DEFAULT_ENCODING):
  83.     """Encode text using the given encoding; if encoding is None use
  84.     the _DEFAULT_ENCODING."""
  85.     if encoding:
  86.         try:
  87.             return text.encode(encoding, _ENCODE_ERROR_STRATEGY)
  88.         except:
  89.             return text.encode(_DEFAULT_ENCODING, _ENCODE_ERROR_STRATEGY)
  90.  
  91.     return text.encode(_DEFAULT_ENCODING, _ENCODE_ERROR_STRATEGY)
  92.  
  93. def openFile (fn, mode = 'r', encoding = _DEFAULT_ENCODING):
  94.     """Open a file using an encoding - every user editable file (via GUI) should
  95.     be opened using this with a proper encoding (default: utf8)!"""
  96.     return codecs.open(fn, mode, encoding, 'strict')   # don't allow errors!
  97.  
  98. def printException (msg, e):
  99.     """Use NSLog to print information about Exception e."""
  100.     info = str(e)
  101.     if info:
  102.         NSLog(u'%s: %s' % (msg, info))
  103.     else:
  104.         NSLog(u'%s' % msg)
  105.