home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / emoticons.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  11.8 KB  |  217 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. from traceback import print_exc
  6. from util import Storage as S, odict
  7. from util.xml_tag import tag
  8. from util.plistutil import plisttype_to_pytype
  9. from collections import defaultdict
  10. from PIL import Image
  11. from os.path import getmtime
  12. import re
  13. from logging import getLogger
  14. log = getLogger('emoticons')
  15. DEFAULT_PACK = u'MSN Messenger'
  16.  
  17. def _default_emoticon_pack():
  18.     import common
  19.     return common.pref('appearance.conversations.emoticons.pack', type = unicode, default = DEFAULT_PACK)
  20.  
  21.  
  22. class EmoticonPack(object):
  23.     emoticon_loaders = []
  24.     
  25.     def __repr__(self):
  26.         return '<%s %r from %r>' % (type(self).__name__, self.name, self.path)
  27.  
  28.     
  29.     def __init__(self, path):
  30.         self.path = path
  31.         self.name = None
  32.         self.emoticons = None
  33.         self.bitmaps = None
  34.  
  35.     
  36.     def register_loader(cls, loader):
  37.         cls.emoticon_loaders.append(loader)
  38.  
  39.     register_loader = classmethod(register_loader)
  40.     
  41.     def is_emoticon_dir(cls, path):
  42.         loader = cls.get_loader(path)
  43.         return loader is not None
  44.  
  45.     is_emoticon_dir = classmethod(is_emoticon_dir)
  46.     
  47.     def get_loader(cls, path):
  48.         for loader in cls.emoticon_loaders:
  49.             if loader.is_emoticon_dir(path):
  50.                 return loader
  51.         
  52.  
  53.     get_loader = classmethod(get_loader)
  54.     
  55.     def load(self):
  56.         raise NotImplementedError
  57.  
  58.     
  59.     def post_load(self):
  60.         for key in list(self.emoticons.keys()):
  61.             self.emoticons[key.encode('xml')] = self.emoticons[key]
  62.         
  63.         emotitems = sorted(self.emoticons.items(), key = (lambda e: len(e[0])), reverse = True)
  64.         smiley_regex = '|'.join((lambda .0: for smiley, _EmoticonPack__emoticon in .0:
  65. '(?:%s)' % re.escape(smiley))(emotitems))
  66.         patterns = [
  67.             '(?:^|\\s)(%s)(?:\\s|$)']
  68.         compiled_patterns = [ re.compile(r % smiley_regex, re.MULTILINE) for r in patterns ]
  69.         self.patterns = compiled_patterns
  70.  
  71.  
  72. DigsbyEmoticonPack = EmoticonPack.register_loader(<NODE:12>)
  73. PidginEmoticonPack = EmoticonPack.register_loader(<NODE:12>)
  74. AdiumPlistEmoticonPack = EmoticonPack.register_loader(<NODE:12>)
  75. AdiumFilesysEmoticonPack = EmoticonPack.register_loader(<NODE:12>)
  76.  
  77. def get_emoticon_dirs():
  78.     import stdpaths
  79.     dirs = []
  80.     emotidir = 'emoticons'
  81.     for dir in (stdpaths.userdata, stdpaths.config, resdir()):
  82.         pth = dir / emotidir
  83.         
  84.         try:
  85.             if not pth.isdir():
  86.                 pth.makedirs()
  87.         except Exception:
  88.             print_exc()
  89.             continue
  90.  
  91.         dirs.append(pth)
  92.     
  93.     return dirs
  94.  
  95.  
  96. def load_emoticons(emoticon_pack = None):
  97.     if emoticon_pack is None:
  98.         emoticon_pack = _default_emoticon_pack()
  99.     
  100.     log.info('load_emoticons: %s', emoticon_pack)
  101.     emoticons = None
  102.     for emoticon_dir in get_emoticon_dirs():
  103.         emoticon_packdir = emoticon_dir / emoticon_pack
  104.         loader = EmoticonPack.get_loader(emoticon_packdir)
  105.         if loader is not None:
  106.             pack = loader(emoticon_packdir)
  107.             pack.load()
  108.             return pack
  109.     
  110.     log.info('emoticon pack %r could not be found', emoticon_pack)
  111.  
  112. _emoticons = sentinel
  113. _emoticons_pack = sentinel
  114.  
  115. def first_non_none(seq):
  116.     for i, elem in enumerate(seq):
  117.         if elem is not None:
  118.             return i
  119.     
  120.     raise AssertionError
  121.  
  122.  
  123. def repl(emoticons):
  124.     
  125.     def _repl(m, e = emoticons):
  126.         (x, y) = m.span()
  127.         (i, j) = m.span(1)
  128.         s = m.string
  129.         emot = e[s[i:j]]
  130.         size = imgsize(emot.path)
  131.         emottext = s[i:j].encode('xml')
  132.         replacement = '<img src="%s" width="%d" height="%d" alt="%s" title="%s" />' % (emot.path.url(), size[0], size[1], emottext, emottext)
  133.         return ''.join([
  134.             s[x:i],
  135.             replacement,
  136.             s[j:y]])
  137.  
  138.     return _repl
  139.  
  140.  
  141. def load_pack(emoticon_pack):
  142.     global _emoticons, _emoticons_pack
  143.     if emoticon_pack is None:
  144.         emoticon_pack = _default_emoticon_pack()
  145.     
  146.     if not isinstance(emoticon_pack, basestring):
  147.         raise TypeError('emoticon_pack must be a string')
  148.     isinstance(emoticon_pack, basestring)
  149.     if _emoticons is sentinel or emoticon_pack != _emoticons_pack:
  150.         _emoticons = load_emoticons(emoticon_pack)
  151.         _emoticons_pack = emoticon_pack
  152.     
  153.     return _emoticons
  154.  
  155.  
  156. def apply_emoticons(s, emoticon_pack = None):
  157.     load_pack(emoticon_pack)
  158.     if _emoticons is None:
  159.         return s
  160.     for p in _emoticons.patterns:
  161.         s = p.sub(repl(_emoticons.emoticons), s)
  162.         s = p.sub(repl(_emoticons.emoticons), s)
  163.     
  164.     return s
  165.  
  166.  
  167. def get_emoticon_bitmaps(emoticon_pack = None):
  168.     if emoticon_pack is None:
  169.         emoticon_pack = _default_emoticon_pack()
  170.     
  171.     load_pack(emoticon_pack)
  172.     return list(_emoticons.bitmaps.items())
  173.  
  174.  
  175. def findsets():
  176.     sets = []
  177.     for d in get_emoticon_dirs():
  178.         for subdir in d.dirs():
  179.             if EmoticonPack.is_emoticon_dir(subdir):
  180.                 pack = EmoticonPack.get_loader(subdir)(subdir)
  181.                 pack.load()
  182.                 sets.append(pack)
  183.                 log.info('discovered emoticon directory: %r', subdir)
  184.                 continue
  185.         
  186.     
  187.     log.info('all found emoticon sets: %r', sets)
  188.     return sets
  189.  
  190.  
  191. def imgsize(p, _cache = { }):
  192.     key = (p, getmtime(p))
  193.     
  194.     try:
  195.         return _cache[key]
  196.     except KeyError:
  197.         size = Image.open(p).size
  198.         return _cache.setdefault(key, size)
  199.  
  200.  
  201. if __name__ == '__main__':
  202.     from path import path
  203.     
  204.     def resdir():
  205.         return path('../../../res')
  206.  
  207. else:
  208.     
  209.     def resdir():
  210.         skin = skin
  211.         import gui
  212.         return skin.resourcedir()
  213.  
  214. if __name__ == '__main__':
  215.     print load_emoticons_pidgin(path('../../../res/emoticons/sa'))
  216.  
  217.