home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 June / maximum-cd-2009-06.iso / DiscContents / digsby_setup.exe / lib / gui / imwin / emoticons.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-02-26  |  8.2 KB  |  303 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. from __future__ import with_statement
  5. from traceback import print_exc
  6. from util import Storage as S, odict, traceguard
  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. emoticon_loaders = [
  16.     'digsby',
  17.     'pidgin',
  18.     'adium']
  19.  
  20. def is_emoticon_dir(d):
  21.     G = globals()
  22.     for loader in emoticon_loaders:
  23.         traceguard.__enter__()
  24.         
  25.         try:
  26.             if G['is_emoticons_' + loader](d):
  27.                 return True
  28.         finally:
  29.             pass
  30.  
  31.     
  32.  
  33.  
  34. def is_emoticons_digsby(emoticon_dir):
  35.     emoticons_txt = emoticon_dir / 'emoticons.txt'
  36.     return emoticons_txt.isfile()
  37.  
  38.  
  39. def load_emoticons_digsby(emoticon_dir):
  40.     emoticons_txt = emoticon_dir / 'emoticons.txt'
  41.     if not emoticons_txt.isfile():
  42.         return (None, None)
  43.     
  44.     title = emoticon_dir.name
  45.     emoticons = { }
  46.     bitmaps = odict()
  47.     
  48.     try:
  49.         f = _[2]
  50.         for line in f:
  51.             line = line.strip()
  52.             if line:
  53.                 title = line
  54.                 break
  55.                 continue
  56.             file(emoticons_txt)
  57.         
  58.         for line in f:
  59.             line = line.strip()
  60.             if not line:
  61.                 continue
  62.             
  63.             content = line.split()
  64.             image_filename = content[0]
  65.             smileys = content[1:]
  66.             imgpath = emoticon_dir / image_filename
  67.             if imgpath.exists():
  68.                 for smiley in smileys:
  69.                     emoticons[smiley.encode('xml')] = S(path = imgpath)
  70.                     if imgpath not in bitmaps:
  71.                         bitmaps[imgpath] = [
  72.                             smiley]
  73.                         continue
  74.                     bitmaps[imgpath].append(smiley)
  75.                 
  76.     finally:
  77.         pass
  78.  
  79.     return (emoticons, bitmaps)
  80.  
  81.  
  82. def is_emoticons_pidgin(emoticon_dir):
  83.     return (emoticon_dir / 'theme').isfile()
  84.  
  85.  
  86. def load_emoticons_pidgin(emoticon_dir):
  87.     theme = emoticon_dir / 'theme'
  88.     if not theme.isfile():
  89.         return (None, None)
  90.     
  91.     smileys = defaultdict(list)
  92.     emoticons = { }
  93.     bitmaps = odict()
  94.     
  95.     try:
  96.         f = _[2]
  97.         for line in f:
  98.             if line.count('\t') > 0:
  99.                 seq = filter(None, line.strip().split('\t'))
  100.                 if len(seq) >= 2:
  101.                     (img, smiley) = seq[:2]
  102.                     imgpath = emoticon_dir / img
  103.                     if imgpath.exists():
  104.                         emoticons[smiley.encode('xml')] = S(path = imgpath)
  105.                         if imgpath not in bitmaps:
  106.                             bitmaps[imgpath] = [
  107.                                 smiley]
  108.                         else:
  109.                             bitmaps[imgpath].append(smiley)
  110.                     
  111.                 
  112.             len(seq) >= 2
  113.     finally:
  114.         pass
  115.  
  116.     return (emoticons, bitmaps)
  117.  
  118.  
  119. def is_emoticons_adium(emoticon_dir):
  120.     return (emoticon_dir / 'Emoticons.plist').isfile()
  121.  
  122.  
  123. def load_emoticons_adium(emoticon_dir):
  124.     emoticons_txt = emoticon_dir / 'Emoticons.plist'
  125.     if not emoticons_txt.isfile():
  126.         return (None, None)
  127.     
  128.     
  129.     try:
  130.         f = _[2]
  131.         plist = tag(f.read())
  132.         toplevel = plist._children[0]
  133.         converted = plisttype_to_pytype(toplevel)
  134.         emoticons = { }
  135.         bitmaps = odict()
  136.         for img_name, info in converted['Emoticons'].items():
  137.             smileys = info['Equivalents']
  138.             imgpath = emoticon_dir / img_name
  139.             if imgpath.exists():
  140.                 for smiley in smileys:
  141.                     if not smiley:
  142.                         continue
  143.                     
  144.                     emoticons[smiley.encode('xml')] = S(path = imgpath)
  145.                     if imgpath not in bitmaps:
  146.                         bitmaps[imgpath] = [
  147.                             smiley]
  148.                         continue
  149.                     bitmaps[imgpath].append(smiley)
  150.                 
  151.     finally:
  152.         pass
  153.  
  154.     return (emoticons, bitmaps)
  155.  
  156.  
  157. def get_emoticon_dirs():
  158.     import stdpaths
  159.     dirs = []
  160.     emotidir = 'emoticons'
  161.     for dir in (stdpaths.userdata, stdpaths.config, resdir()):
  162.         pth = dir / emotidir
  163.         
  164.         try:
  165.             if not pth.isdir():
  166.                 pth.makedirs()
  167.         except Exception:
  168.             print_exc()
  169.             continue
  170.  
  171.         dirs.append(pth)
  172.     
  173.     return dirs
  174.  
  175.  
  176. def load_emoticons(emoticon_pack = 'default'):
  177.     log.info('load_emoticons: %s', emoticon_pack)
  178.     emoticons = None
  179.     for emoticon_dir in get_emoticon_dirs():
  180.         emoticon_packdir = emoticon_dir / emoticon_pack
  181.         for loader in emoticon_loaders:
  182.             
  183.             try:
  184.                 (emoticons, bitmaps) = globals()['load_emoticons_' + loader](emoticon_packdir)
  185.             except Exception:
  186.                 print_exc()
  187.  
  188.             if emoticons:
  189.                 break
  190.                 continue
  191.         
  192.         if emoticons:
  193.             break
  194.             continue
  195.     
  196.     if not emoticons:
  197.         return (emoticons, { }, [])
  198.     
  199.     emotitems = sorted(emoticons.items(), key = (lambda e: len(e[0])), reverse = True)
  200.     smiley_regex = '|'.join((lambda .0: for smiley, emoticon in .0:
  201. '(?:%s)' % re.escape(smiley))(emotitems))
  202.     patterns = [
  203.         '(?:^|\\s)(%s)(?:\\s|$)']
  204.     compiled_patterns = [ re.compile(r % smiley_regex, re.MULTILINE) for r in patterns ]
  205.     return (emoticons, bitmaps, compiled_patterns)
  206.  
  207. _emoticons = sentinel
  208. _emoticons_pack = sentinel
  209. _emoticon_patterns = []
  210. _bitmaps = []
  211.  
  212. def first_non_none(seq):
  213.     for i, elem in enumerate(seq):
  214.         if elem is not None:
  215.             return i
  216.             continue
  217.     
  218.     raise AssertionError
  219.  
  220.  
  221. def repl(emoticons):
  222.     
  223.     def _repl(m, e = emoticons):
  224.         (x, y) = m.span()
  225.         (i, j) = m.span(1)
  226.         s = m.string
  227.         emot = e[s[i:j]]
  228.         size = imgsize(emot.path)
  229.         emottext = s[i:j].encode('xml')
  230.         replacement = '<img src="%s" width="%d" height="%d" alt="%s" title="%s" />' % (emot.path.url(), size[0], size[1], emottext, emottext)
  231.         return ''.join([
  232.             s[x:i],
  233.             replacement,
  234.             s[j:y]])
  235.  
  236.     return _repl
  237.  
  238.  
  239. def load_pack(emoticon_pack):
  240.     global _emoticons, _bitmaps, _emoticon_patterns, _emoticons_pack, _bitmaps
  241.     if not isinstance(emoticon_pack, basestring):
  242.         raise TypeError('emoticon_pack must be a string')
  243.     
  244.     if _emoticons is sentinel or emoticon_pack != _emoticons_pack:
  245.         (_emoticons, _bitmaps, _emoticon_patterns) = load_emoticons(emoticon_pack)
  246.         _emoticons_pack = emoticon_pack
  247.         _bitmaps = list(_bitmaps.items())
  248.     
  249.  
  250.  
  251. def apply_emoticons(s, emoticon_pack = 'default'):
  252.     load_pack(emoticon_pack)
  253.     for p in _emoticon_patterns:
  254.         s = p.sub(repl(_emoticons), s)
  255.         s = p.sub(repl(_emoticons), s)
  256.     
  257.     return s
  258.  
  259.  
  260. def get_emoticon_bitmaps(emoticon_pack = 'default'):
  261.     load_pack(emoticon_pack)
  262.     return _bitmaps
  263.  
  264.  
  265. def findsets():
  266.     sets = []
  267.     for d in get_emoticon_dirs():
  268.         for subdir in d.dirs():
  269.             if is_emoticon_dir(subdir):
  270.                 sets.append(subdir)
  271.                 continue
  272.         
  273.     
  274.     return sets
  275.  
  276.  
  277. def imgsize(p, _cache = { }):
  278.     key = (p, getmtime(p))
  279.     
  280.     try:
  281.         return _cache[key]
  282.     except KeyError:
  283.         size = Image.open(p).size
  284.         return _cache.setdefault(key, size)
  285.  
  286.  
  287. if __name__ == '__main__':
  288.     from path import path
  289.     
  290.     def resdir():
  291.         return path('../../../res')
  292.  
  293. else:
  294.     
  295.     def resdir():
  296.         skin = skin
  297.         import gui
  298.         return skin.resourcedir()
  299.  
  300. if __name__ == '__main__':
  301.     print load_emoticons_pidgin(path('../../../res/emoticons/sa'))
  302.  
  303.