home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / templatehelper.py < prev    next >
Encoding:
Python Source  |  2007-11-12  |  4.1 KB  |  112 lines

  1. # Miro - an RSS based video player application
  2. # Copyright (C) 2005-2007 Participatory Culture Foundation
  3. #
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  17.  
  18. # Contains template utility code shared by template_compiler and template
  19.  
  20. # Distutils needs this in a .py file, so it knows they're required
  21. # by the entension module. This place seems as good as any.
  22. import cPickle #for database.pyx
  23.  
  24. import inspect
  25. import shutil
  26. import re
  27. import traceback
  28. import random
  29. import types
  30. import logging
  31. from itertools import izip
  32. from xhtmltools import urlencode
  33.  
  34. HTMLPattern = re.compile("^.*<body.*?>(.*)</body\s*>", re.S)
  35. attrPattern = re.compile("^(.*?)@@@(.*?)@@@(.*)$")
  36. resourcePattern = re.compile("^resource:(.*)$")
  37. rawAttrPattern = re.compile("^(.*?)\*\*\*(.*?)\*\*\*(.*)$")
  38.  
  39. _unicache = {}
  40. _escapecache = {}
  41.  
  42. def quoteattr(orig):
  43.     orig = unicode(orig)
  44.     return orig.replace(u'"',u'"')
  45.  
  46. def escape(orig):
  47.     global _escapecache
  48.     orig = unicode(orig)
  49.     try:
  50.         return _escapecache[orig]
  51.     except:
  52.         _escapecache[orig] = orig.replace('&','&').replace('<','<').replace('>','>')
  53.         return _escapecache[orig]
  54.  
  55. # Takes a string and do whatever needs to be done to make it into a
  56. # UTF-8 string. If a Unicode string is given, it is just encoded in
  57. # UTF-8. Otherwise, if an encoding hint is given, first try to decode
  58. # the string as if it were in that encoding; if that fails (or the
  59. # hint isn't given), liberally (if necessary lossily) interpret it as
  60. # defaultEncoding, as declared on the next line:
  61. defaultEncoding = "iso-8859-1" # aka Latin-1
  62. _utf8cache = {}
  63.  
  64. def toUTF8Bytes(string, encoding=None):
  65.     global _utf8cache
  66.     try:
  67.         return _utf8cache[(string, encoding)]
  68.     except KeyError:
  69.         result = None
  70.         # If we got a Unicode string, half of our work is already done.
  71.         if isinstance(string, types.UnicodeType):
  72.             result = string.encode('utf-8')
  73.         elif not isinstance(string, types.StringType):
  74.             string = str(string)
  75.         if result is None and encoding is not None:
  76.             # If we knew the encoding of the string, try that.
  77.             try:
  78.                 decoded = string.decode(encoding,'replace')
  79.             except (UnicodeDecodeError, ValueError, LookupError):
  80.                 pass
  81.             else:
  82.                 result = decoded.encode('utf-8')
  83.         if result is None:
  84.             # Encoding wasn't provided, or it was wrong. Interpret provided string
  85.             # liberally as a fixed defaultEncoding (see above.)
  86.             result = string.decode(defaultEncoding, 'replace').encode('utf-8')
  87.  
  88.         _utf8cache[(string, encoding)] = result
  89.         return _utf8cache[(string, encoding)]
  90.  
  91. def toUni(orig, encoding = None):
  92.     global _unicache
  93.     try:
  94.         return _unicache[orig]
  95.     except:
  96.         if isinstance(orig, types.UnicodeType):
  97.             # Let's not bother putting this in the cache.  Calculating
  98.             # it is very fast, and since this is a very common case,
  99.             # not caching here should help with memory usage.
  100.             return orig
  101.         elif not isinstance(orig, types.StringType):
  102.             _unicache[orig] = unicode(orig)
  103.         else:
  104.             orig = toUTF8Bytes(orig, encoding)
  105.             _unicache[orig] = unicode(orig,'utf-8')
  106.         return _unicache[orig]
  107.  
  108.  
  109. # Generate an arbitrary string to use as an ID attribute.
  110. def generateId():
  111.     return "tmplcomp%08d" % random.randint(0,99999999)
  112.