home *** CD-ROM | disk | FTP | other *** search
Wrap
# Source Generated with Decompyle++ # File: in.pyc (Python 2.6) __all__ = [ 'css', 'stylesheets', 'CSSParser', 'CSSSerializer'] __docformat__ = 'restructuredtext' __author__ = 'Christof Hoeke with contributions by Walter Doerwald' __date__ = '$LastChangedDate:: 2009-12-30 22:26:29 +0100 #$:' VERSION = '0.9.7a2' __version__ = '%s $Id: __init__.py 1902 2009-12-30 21:26:29Z cthedot $' % VERSION import codec import os.path as os import urllib import urlparse import xml.dom as xml from helper import Deprecated import errorhandler log = errorhandler.ErrorHandler() import css import stylesheets import util from parse import CSSParser from serialize import CSSSerializer ser = CSSSerializer() from profiles import Profiles profile = Profiles(log = log) _ANYNS = -1 class DOMImplementationCSS(object): _features = [ ('css', '1.0'), ('css', '2.0'), ('stylesheets', '1.0'), ('stylesheets', '2.0')] def createCSSStyleSheet(self, title, media): return css.CSSStyleSheet(title = title, media = media) def createDocument(self, *args): raise NotImplementedError def createDocumentType(self, *args): raise NotImplementedError def hasFeature(self, feature, version): return (feature.lower(), unicode(version)) in self._features xml.dom.registerDOMImplementation('cssutils', DOMImplementationCSS) def parseString(*a, **k): return CSSParser().parseString(*a, **k) parseString.__doc__ = CSSParser.parseString.__doc__ def parseFile(*a, **k): return CSSParser().parseFile(*a, **k) parseFile.__doc__ = CSSParser.parseFile.__doc__ def parseUrl(*a, **k): return CSSParser().parseUrl(*a, **k) parseUrl.__doc__ = CSSParser.parseUrl.__doc__ def parse(*a, **k): return parseFile(*a, **k) parse = Deprecated('Use cssutils.parseFile() instead.')(parse) parse.__doc__ = CSSParser.parse.__doc__ def parseStyle(cssText, encoding = 'utf-8'): if isinstance(cssText, str): cssText = cssText.decode(encoding) style = css.CSSStyleDeclaration() style.cssText = cssText return style def setSerializer(serializer): global ser ser = serializer def getUrls(sheet): for importrule in (lambda .0: for r in .0: if r.type == r.IMPORT_RULE: rcontinue)(sheet): yield importrule.href def getUrl(v): if v.CSS_PRIMITIVE_VALUE == v.cssValueType and v.CSS_URI == v.primitiveType: return v.getStringValue() def styleDeclarations(base): if hasattr(base, 'cssRules'): for rule in base.cssRules: for s in styleDeclarations(rule): yield s elif hasattr(base, 'style'): yield base.style for style in styleDeclarations(sheet): for p in style.getProperties(all = True): v = p.cssValue if v.CSS_VALUE_LIST == v.cssValueType: for item in v: u = getUrl(item) if u is not None: yield u (None,) continue if v.CSS_PRIMITIVE_VALUE == v.cssValueType: u = getUrl(v) if u is not None: yield u u is not None def replaceUrls(sheet, replacer, ignoreImportRules = False): if not ignoreImportRules: for importrule in (lambda .0: for r in .0: if r.type == r.IMPORT_RULE: rcontinue)(sheet): importrule.href = replacer(importrule.href) def setProperty(v): if v.CSS_PRIMITIVE_VALUE == v.cssValueType and v.CSS_URI == v.primitiveType: v.setStringValue(v.CSS_URI, replacer(v.getStringValue())) def styleDeclarations(base): if hasattr(base, 'cssRules'): for rule in base.cssRules: for s in styleDeclarations(rule): yield s elif hasattr(base, 'style'): yield base.style for style in styleDeclarations(sheet): for p in style.getProperties(all = True): v = p.cssValue if v.CSS_VALUE_LIST == v.cssValueType: for item in v: setProperty(item) ((None,),) if v.CSS_PRIMITIVE_VALUE == v.cssValueType: setProperty(v) continue def resolveImports(sheet, target = None): if not target: target = css.CSSStyleSheet(href = sheet.href, media = sheet.media, title = sheet.title) def getReplacer(targetbase): (basesch, baseloc, basepath, basequery, basefrag) = urlparse.urlsplit(targetbase) (basepath, basepathfilename) = os.path.split(basepath) def replacer(url): (scheme, location, path, query, fragment) = urlparse.urlsplit(url) if not scheme and not location and not path.startswith(u'/'): (path, filename) = os.path.split(path) combined = os.path.normpath(os.path.join(basepath, path, filename)) return urllib.pathname2url(combined) return url return replacer for rule in sheet.cssRules: if rule.type == rule.CHARSET_RULE: continue if rule.type == rule.IMPORT_RULE: log.info(u'Processing @import %r' % rule.href, neverraise = True) if rule.styleSheet: target.add(css.CSSComment(cssText = u'/* START @import "%s" */' % rule.href)) try: importedSheet = resolveImports(rule.styleSheet) except xml.dom.HierarchyRequestErr: e = None log.warn(u'@import: Cannot resolve target, keeping rule: %s' % e, neverraise = True) target.add(rule) log.info(u'@import: Adjusting paths for %r' % rule.href, neverraise = True) replaceUrls(importedSheet, getReplacer(rule.href), ignoreImportRules = True) if rule.media.mediaText == u'all': mediaproxy = None else: keepimport = False for r in importedSheet: if r.type not in (r.COMMENT, r.STYLE_RULE, r.IMPORT_RULE): keepimport = True break continue if keepimport: 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) target.add(rule) continue log.info(u'@import: Wrapping some rules in @media to keep media: %s' % rule.media.mediaText, neverraise = True) mediaproxy = css.CSSMediaRule(rule.media.mediaText) for r in importedSheet: if mediaproxy: mediaproxy.add(r) continue target.add(r) if mediaproxy: target.add(mediaproxy) else: log.error(u'Cannot get referenced stylesheet %r, keeping rule' % rule.href, neverraise = True) target.add(rule) rule.styleSheet target.add(rule) return target if __name__ == '__main__': print __doc__