home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / calibre-0.7.13.msi / file_1556 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-08-06  |  9.0 KB  |  250 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __all__ = [
  5.     'css',
  6.     'stylesheets',
  7.     'CSSParser',
  8.     'CSSSerializer']
  9. __docformat__ = 'restructuredtext'
  10. __author__ = 'Christof Hoeke with contributions by Walter Doerwald'
  11. __date__ = '$LastChangedDate:: 2009-12-30 22:26:29 +0100 #$:'
  12. VERSION = '0.9.7a2'
  13. __version__ = '%s $Id: __init__.py 1902 2009-12-30 21:26:29Z cthedot $' % VERSION
  14. import codec
  15. import os.path as os
  16. import urllib
  17. import urlparse
  18. import xml.dom as xml
  19. from helper import Deprecated
  20. import errorhandler
  21. log = errorhandler.ErrorHandler()
  22. import css
  23. import stylesheets
  24. import util
  25. from parse import CSSParser
  26. from serialize import CSSSerializer
  27. ser = CSSSerializer()
  28. from profiles import Profiles
  29. profile = Profiles(log = log)
  30. _ANYNS = -1
  31.  
  32. class DOMImplementationCSS(object):
  33.     _features = [
  34.         ('css', '1.0'),
  35.         ('css', '2.0'),
  36.         ('stylesheets', '1.0'),
  37.         ('stylesheets', '2.0')]
  38.     
  39.     def createCSSStyleSheet(self, title, media):
  40.         return css.CSSStyleSheet(title = title, media = media)
  41.  
  42.     
  43.     def createDocument(self, *args):
  44.         raise NotImplementedError
  45.  
  46.     
  47.     def createDocumentType(self, *args):
  48.         raise NotImplementedError
  49.  
  50.     
  51.     def hasFeature(self, feature, version):
  52.         return (feature.lower(), unicode(version)) in self._features
  53.  
  54.  
  55. xml.dom.registerDOMImplementation('cssutils', DOMImplementationCSS)
  56.  
  57. def parseString(*a, **k):
  58.     return CSSParser().parseString(*a, **k)
  59.  
  60. parseString.__doc__ = CSSParser.parseString.__doc__
  61.  
  62. def parseFile(*a, **k):
  63.     return CSSParser().parseFile(*a, **k)
  64.  
  65. parseFile.__doc__ = CSSParser.parseFile.__doc__
  66.  
  67. def parseUrl(*a, **k):
  68.     return CSSParser().parseUrl(*a, **k)
  69.  
  70. parseUrl.__doc__ = CSSParser.parseUrl.__doc__
  71.  
  72. def parse(*a, **k):
  73.     return parseFile(*a, **k)
  74.  
  75. parse = Deprecated('Use cssutils.parseFile() instead.')(parse)
  76. parse.__doc__ = CSSParser.parse.__doc__
  77.  
  78. def parseStyle(cssText, encoding = 'utf-8'):
  79.     if isinstance(cssText, str):
  80.         cssText = cssText.decode(encoding)
  81.     
  82.     style = css.CSSStyleDeclaration()
  83.     style.cssText = cssText
  84.     return style
  85.  
  86.  
  87. def setSerializer(serializer):
  88.     global ser
  89.     ser = serializer
  90.  
  91.  
  92. def getUrls(sheet):
  93.     for importrule in (lambda .0: for r in .0:
  94. if r.type == r.IMPORT_RULE:
  95. rcontinue)(sheet):
  96.         yield importrule.href
  97.     
  98.     
  99.     def getUrl(v):
  100.         if v.CSS_PRIMITIVE_VALUE == v.cssValueType and v.CSS_URI == v.primitiveType:
  101.             return v.getStringValue()
  102.  
  103.     
  104.     def styleDeclarations(base):
  105.         if hasattr(base, 'cssRules'):
  106.             for rule in base.cssRules:
  107.                 for s in styleDeclarations(rule):
  108.                     yield s
  109.                 
  110.             
  111.         elif hasattr(base, 'style'):
  112.             yield base.style
  113.         
  114.  
  115.     for style in styleDeclarations(sheet):
  116.         for p in style.getProperties(all = True):
  117.             v = p.cssValue
  118.             if v.CSS_VALUE_LIST == v.cssValueType:
  119.                 for item in v:
  120.                     u = getUrl(item)
  121.                     if u is not None:
  122.                         yield u
  123.                         (None,)
  124.                         continue
  125.                 
  126.             if v.CSS_PRIMITIVE_VALUE == v.cssValueType:
  127.                 u = getUrl(v)
  128.                 if u is not None:
  129.                     yield u
  130.                 
  131.             u is not None
  132.         
  133.     
  134.  
  135.  
  136. def replaceUrls(sheet, replacer, ignoreImportRules = False):
  137.     if not ignoreImportRules:
  138.         for importrule in (lambda .0: for r in .0:
  139. if r.type == r.IMPORT_RULE:
  140. rcontinue)(sheet):
  141.             importrule.href = replacer(importrule.href)
  142.         
  143.     
  144.     
  145.     def setProperty(v):
  146.         if v.CSS_PRIMITIVE_VALUE == v.cssValueType and v.CSS_URI == v.primitiveType:
  147.             v.setStringValue(v.CSS_URI, replacer(v.getStringValue()))
  148.         
  149.  
  150.     
  151.     def styleDeclarations(base):
  152.         if hasattr(base, 'cssRules'):
  153.             for rule in base.cssRules:
  154.                 for s in styleDeclarations(rule):
  155.                     yield s
  156.                 
  157.             
  158.         elif hasattr(base, 'style'):
  159.             yield base.style
  160.         
  161.  
  162.     for style in styleDeclarations(sheet):
  163.         for p in style.getProperties(all = True):
  164.             v = p.cssValue
  165.             if v.CSS_VALUE_LIST == v.cssValueType:
  166.                 for item in v:
  167.                     setProperty(item)
  168.                 
  169.             ((None,),)
  170.             if v.CSS_PRIMITIVE_VALUE == v.cssValueType:
  171.                 setProperty(v)
  172.                 continue
  173.         
  174.     
  175.  
  176.  
  177. def resolveImports(sheet, target = None):
  178.     if not target:
  179.         target = css.CSSStyleSheet(href = sheet.href, media = sheet.media, title = sheet.title)
  180.     
  181.     
  182.     def getReplacer(targetbase):
  183.         (basesch, baseloc, basepath, basequery, basefrag) = urlparse.urlsplit(targetbase)
  184.         (basepath, basepathfilename) = os.path.split(basepath)
  185.         
  186.         def replacer(url):
  187.             (scheme, location, path, query, fragment) = urlparse.urlsplit(url)
  188.             if not scheme and not location and not path.startswith(u'/'):
  189.                 (path, filename) = os.path.split(path)
  190.                 combined = os.path.normpath(os.path.join(basepath, path, filename))
  191.                 return urllib.pathname2url(combined)
  192.             return url
  193.  
  194.         return replacer
  195.  
  196.     for rule in sheet.cssRules:
  197.         if rule.type == rule.CHARSET_RULE:
  198.             continue
  199.         if rule.type == rule.IMPORT_RULE:
  200.             log.info(u'Processing @import %r' % rule.href, neverraise = True)
  201.             if rule.styleSheet:
  202.                 target.add(css.CSSComment(cssText = u'/* START @import "%s" */' % rule.href))
  203.                 
  204.                 try:
  205.                     importedSheet = resolveImports(rule.styleSheet)
  206.                 except xml.dom.HierarchyRequestErr:
  207.                     e = None
  208.                     log.warn(u'@import: Cannot resolve target, keeping rule: %s' % e, neverraise = True)
  209.                     target.add(rule)
  210.  
  211.                 log.info(u'@import: Adjusting paths for %r' % rule.href, neverraise = True)
  212.                 replaceUrls(importedSheet, getReplacer(rule.href), ignoreImportRules = True)
  213.                 if rule.media.mediaText == u'all':
  214.                     mediaproxy = None
  215.                 else:
  216.                     keepimport = False
  217.                     for r in importedSheet:
  218.                         if r.type not in (r.COMMENT, r.STYLE_RULE, r.IMPORT_RULE):
  219.                             keepimport = True
  220.                             break
  221.                             continue
  222.                     
  223.                     if keepimport:
  224.                         log.warn(u'Cannot combine imported sheet with given media as other rules then comments or stylerules found %r, keeping %r' % (r, rule.cssText), neverraise = True)
  225.                         target.add(rule)
  226.                         continue
  227.                     
  228.                     log.info(u'@import: Wrapping some rules in @media  to keep media: %s' % rule.media.mediaText, neverraise = True)
  229.                     mediaproxy = css.CSSMediaRule(rule.media.mediaText)
  230.                 for r in importedSheet:
  231.                     if mediaproxy:
  232.                         mediaproxy.add(r)
  233.                         continue
  234.                     target.add(r)
  235.                 
  236.                 if mediaproxy:
  237.                     target.add(mediaproxy)
  238.                 
  239.             else:
  240.                 log.error(u'Cannot get referenced stylesheet %r, keeping rule' % rule.href, neverraise = True)
  241.                 target.add(rule)
  242.         rule.styleSheet
  243.         target.add(rule)
  244.     
  245.     return target
  246.  
  247. if __name__ == '__main__':
  248.     print __doc__
  249.  
  250.