本文整理汇总了Python中cssutils.CSSParser.parseString方法的典型用法代码示例。如果您正苦于以下问题:Python CSSParser.parseString方法的具体用法?Python CSSParser.parseString怎么用?Python CSSParser.parseString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cssutils.CSSParser
的用法示例。
在下文中一共展示了CSSParser.parseString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_finish
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def test_finish(self):
"""
L{StylesheetRewritingRequestWrapper.finish} causes all written bytes to
be translated with C{_replace} written to the wrapped request.
"""
stylesheetFormat = """
.foo {
background-image: url(%s)
}
"""
originalStylesheet = stylesheetFormat % ("/Foo/bar",)
expectedStylesheet = stylesheetFormat % ("/bar/Foo/bar",)
request = FakeRequest()
roots = {request: URL.fromString('/bar/')}
wrapper = website.StylesheetRewritingRequestWrapper(
request, [], roots.get)
wrapper.write(originalStylesheet)
wrapper.finish()
# Parse and serialize both versions to normalize whitespace so we can
# make a comparison.
parser = CSSParser()
self.assertEqual(
parser.parseString(request.accumulator).cssText,
parser.parseString(expectedStylesheet).cssText)
示例2: validate_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def validate_css(string):
p = CSSParser(raiseExceptions = True)
if not string or only_whitespace.match(string):
return ('',ValidationReport())
report = ValidationReport(string)
# avoid a very expensive parse
max_size_kb = 100;
if len(string) > max_size_kb * 1024:
report.append(ValidationError((msgs['too_big']
% dict (max_size = max_size_kb))))
return (string, report)
try:
parsed = p.parseString(string)
except DOMException,e:
# yuck; xml.dom.DOMException can't give us line-information
# directly, so we have to parse its error message string to
# get it
line = None
line_match = error_message_extract_re.match(e.message)
if line_match:
line = line_match.group(1)
if line:
line = int(line)
error_message= (msgs['syntax_error']
% dict(syntaxerror = e.message))
report.append(ValidationError(error_message,e,line))
return (None,report)
示例3: do_import
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def do_import():
page = Page.get(request.form.get('page_key', ''))
if not page or page.import_state != IMPORTING:
return 'NO_IMPORTER' # We're done
importer = Importer.gql('WHERE page=:1', page.key()).get()
if not importer:
# This requires a request to fetch the page and parse the URLs.
# It also enqueues the next run.
create_importer(page)
return 'CREATED'
if importer.urls:
url = importer.urls.pop(0)
parser = None
try:
resp = urlfetch.fetch(url, deadline=10)
if resp.status_code == 200:
parser = CSSParser()
sheet = parser.parseString(resp.content, href=url)
style = sheet.cssText
importer.style += '\n\n/* Imported from %s */\n%s' % (url, style)
else:
raise Exception('Error fetching %s' % url)
except Exception, e:
import traceback
importer.errors.append('Error importing %s' % url)
logging.error('Error importing for Page %s from %s:\n%s\n%s', page.key().id(), url, e, traceback.format_exc())
finally:
示例4: create_importer
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def create_importer(page):
importer = Importer(page=page, style='')
resp = urlfetch.fetch(page.url, deadline=10)
if resp.status_code == 200:
soup = BeautifulSoup(resp.content)
parser = CSSParser()
for tag in soup.findAll(re.compile(r'^(link|style)$')):
if tag.name == 'link':
if tag.get('href', None) and tag.get('rel', 'stylesheet').lower() == 'stylesheet':
url = urljoin(page.url, tag['href'])
if urlparse(url).netloc != urlparse(request.url).netloc:
importer.urls.append(url)
elif tag.name == 'style':
media = tag.get('media', None)
sheet = parser.parseString(''.join(tag.contents).strip('\n'), href=url)
style = sheet.cssText
if media:
style = '@media %s {\n%s\n}' % (media, style)
style = '/* Imported directly from %s */\n%s\n' % (page.url, style)
importer.style += style
# Patch around AppEngine's frame inspection
del parser
importer.put()
queue_import(page)
示例5: beautify_text
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def beautify_text(raw, syntax):
from lxml import etree
from calibre.ebooks.oeb.polish.parsing import parse
from calibre.ebooks.oeb.polish.pretty import pretty_xml_tree, pretty_html_tree
from calibre.ebooks.chardet import strip_encoding_declarations
if syntax == 'xml':
root = etree.fromstring(strip_encoding_declarations(raw))
pretty_xml_tree(root)
elif syntax == 'css':
import logging
from calibre.ebooks.oeb.base import serialize, _css_logger
from calibre.ebooks.oeb.polish.utils import setup_cssutils_serialization
from cssutils import CSSParser, log
setup_cssutils_serialization(tprefs['editor_tab_stop_width'])
log.setLevel(logging.WARN)
log.raiseExceptions = False
parser = CSSParser(loglevel=logging.WARNING,
# We dont care about @import rules
fetcher=lambda x: (None, None), log=_css_logger)
data = parser.parseString(raw, href='<string>', validate=False)
return serialize(data, 'text/css')
else:
root = parse(raw, line_numbers=False)
pretty_html_tree(None, root)
return etree.tostring(root, encoding=unicode)
示例6: validate_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def validate_css(string, generate_https_urls):
p = CSSParser(raiseExceptions=True)
if not string or only_whitespace.match(string):
return ("", ValidationReport())
report = ValidationReport(string)
# avoid a very expensive parse
max_size_kb = 100
if len(string) > max_size_kb * 1024:
report.append(ValidationError((msgs["too_big"] % dict(max_size=max_size_kb))))
return ("", report)
if "\\" in string:
report.append(ValidationError(_("if you need backslashes, you're doing it wrong")))
try:
parsed = p.parseString(string)
except DOMException, e:
# yuck; xml.dom.DOMException can't give us line-information
# directly, so we have to parse its error message string to
# get it
line = None
line_match = error_message_extract_re.match(e.message)
if line_match:
line = line_match.group(1)
if line:
line = int(line)
error_message = msgs["syntax_error"] % dict(syntaxerror=e.message)
report.append(ValidationError(error_message, e, line))
return (None, report)
示例7: main
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def main():
css = u'''
/* some umlauts äöü and EURO sign € */
a:before {
content: "ä";
}'''
p = CSSParser()
sheet = p.parseString(css)
print """cssText in different encodings, depending on the console some
chars may look broken but are actually not"""
print
sheet.encoding = 'ascii'
print sheet.cssText
print
sheet.encoding = 'iso-8859-1'
print sheet.cssText
print
sheet.encoding = 'iso-8859-15'
print sheet.cssText
print
sheet.encoding = 'utf-8'
print sheet.cssText
print
# results in default UTF-8 encoding without @charset rule
sheet.encoding = None
print sheet.cssText
示例8: parse_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def parse_css(self, data, fname):
from cssutils import CSSParser, log
log.setLevel(logging.WARN)
log.raiseExceptions = False
data = self.decode(data)
data = self.css_preprocessor(data)
parser = CSSParser(loglevel=logging.WARNING,
# We dont care about @import rules
fetcher=lambda x: (None, None), log=_css_logger)
data = parser.parseString(data, href=fname, validate=False)
return data
示例9: finish
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def finish(self):
"""
Parse the buffered response body, rewrite its URLs, write the result to
the wrapped request, and finish the wrapped request.
"""
stylesheet = ''.join(self._buffer)
parser = CSSParser()
css = parser.parseString(stylesheet)
css.replaceUrls(self._replace)
self.request.write(css.cssText)
return self.request.finish()
示例10: parse_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def parse_css(self, data, fname='<string>', is_declaration=False):
from cssutils import CSSParser, log
log.setLevel(logging.WARN)
log.raiseExceptions = False
if isinstance(data, bytes):
data = self.decode(data)
if not self.tweak_mode:
data = self.css_preprocessor(data)
parser = CSSParser(loglevel=logging.WARNING,
# We dont care about @import rules
fetcher=lambda x: (None, None), log=_css_logger)
if is_declaration:
data = parser.parseStyle(data, validate=False)
else:
data = parser.parseString(data, href=fname, validate=False)
return data
示例11: parse_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def parse_css(data, fname='<string>', is_declaration=False, decode=None, log_level=None, css_preprocessor=None):
if log_level is None:
import logging
log_level = logging.WARNING
from cssutils import CSSParser, log
from calibre.ebooks.oeb.base import _css_logger
log.setLevel(log_level)
log.raiseExceptions = False
if isinstance(data, bytes):
data = data.decode('utf-8') if decode is None else decode(data)
if css_preprocessor is not None:
data = css_preprocessor(data)
parser = CSSParser(loglevel=log_level,
# We dont care about @import rules
fetcher=lambda x: (None, None), log=_css_logger)
if is_declaration:
data = parser.parseStyle(data, validate=False)
else:
data = parser.parseString(data, href=fname, validate=False)
return data
示例12: extract_css
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def extract_css(self, root, log):
ans = []
for s in root.xpath('//*[local-name() = "style" and @type="text/css"]'):
ans.append(s.text)
s.getparent().remove(s)
head = root.xpath('//*[local-name() = "head"]')
if head:
head = head[0]
ns = head.nsmap.get(None, '')
if ns:
ns = '{%s}'%ns
etree.SubElement(head, ns+'link', {'type':'text/css',
'rel':'stylesheet', 'href':'odfpy.css'})
css = u'\n\n'.join(ans)
parser = CSSParser(loglevel=logging.WARNING,
log=_css_logger)
self.css = parser.parseString(css, validate=False)
with open('odfpy.css', 'wb') as f:
f.write(css.encode('utf-8'))
示例13: __init__
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def __init__(self, tree, path, oeb, opts, profile=None,
extra_css='', user_css='', base_css=''):
self.oeb, self.opts = oeb, opts
self.profile = profile
if self.profile is None:
# Use the default profile. This should really be using
# opts.output_profile, but I don't want to risk changing it, as
# doing so might well have hard to debug font size effects.
from calibre.customize.ui import output_profiles
for x in output_profiles():
if x.short_name == 'default':
self.profile = x
break
if self.profile is None:
# Just in case the default profile is removed in the future :)
self.profile = opts.output_profile
self.body_font_size = self.profile.fbase
self.logger = oeb.logger
item = oeb.manifest.hrefs[path]
basename = os.path.basename(path)
cssname = os.path.splitext(basename)[0] + '.css'
stylesheets = [html_css_stylesheet()]
if base_css:
stylesheets.append(parseString(base_css, validate=False))
style_tags = xpath(tree, '//*[local-name()="style" or local-name()="link"]')
# Add cssutils parsing profiles from output_profile
for profile in self.opts.output_profile.extra_css_modules:
cssprofiles.addProfile(profile['name'],
profile['props'],
profile['macros'])
parser = CSSParser(fetcher=self._fetch_css_file,
log=logging.getLogger('calibre.css'))
self.font_face_rules = []
for elem in style_tags:
if (elem.tag == XHTML('style') and elem.get('type', CSS_MIME) in OEB_STYLES and media_ok(elem.get('media'))):
text = elem.text if elem.text else u''
for x in elem:
t = getattr(x, 'text', None)
if t:
text += u'\n\n' + force_unicode(t, u'utf-8')
t = getattr(x, 'tail', None)
if t:
text += u'\n\n' + force_unicode(t, u'utf-8')
if text:
text = oeb.css_preprocessor(text)
# We handle @import rules separately
parser.setFetcher(lambda x: ('utf-8', b''))
stylesheet = parser.parseString(text, href=cssname,
validate=False)
parser.setFetcher(self._fetch_css_file)
for rule in stylesheet.cssRules:
if rule.type == rule.IMPORT_RULE:
ihref = item.abshref(rule.href)
if not media_ok(rule.media.mediaText):
continue
hrefs = self.oeb.manifest.hrefs
if ihref not in hrefs:
self.logger.warn('Ignoring missing stylesheet in @import rule:', rule.href)
continue
sitem = hrefs[ihref]
if sitem.media_type not in OEB_STYLES:
self.logger.warn('CSS @import of non-CSS file %r' % rule.href)
continue
stylesheets.append(sitem.data)
# Make links to resources absolute, since these rules will
# be folded into a stylesheet at the root
replaceUrls(stylesheet, item.abshref,
ignoreImportRules=True)
stylesheets.append(stylesheet)
elif (elem.tag == XHTML('link') and elem.get('href') and elem.get(
'rel', 'stylesheet').lower() == 'stylesheet' and elem.get(
'type', CSS_MIME).lower() in OEB_STYLES and media_ok(elem.get('media'))
):
href = urlnormalize(elem.attrib['href'])
path = item.abshref(href)
sitem = oeb.manifest.hrefs.get(path, None)
if sitem is None:
self.logger.warn(
'Stylesheet %r referenced by file %r not in manifest' %
(path, item.href))
continue
if not hasattr(sitem.data, 'cssRules'):
self.logger.warn(
'Stylesheet %r referenced by file %r is not CSS'%(path,
item.href))
continue
stylesheets.append(sitem.data)
csses = {'extra_css':extra_css, 'user_css':user_css}
for w, x in csses.items():
if x:
try:
text = x
stylesheet = parser.parseString(text, href=cssname,
validate=False)
stylesheets.append(stylesheet)
except:
self.logger.exception('Failed to parse %s, ignoring.'%w)
self.logger.debug('Bad css: ')
#.........这里部分代码省略.........
示例14: __init__
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def __init__(self, tree, path, oeb, opts, profile=None, extra_css="", user_css=""):
self.oeb, self.opts = oeb, opts
self.profile = profile
if self.profile is None:
# Use the default profile. This should really be using
# opts.output_profile, but I don't want to risk changing it, as
# doing so might well have hard to debug font size effects.
from calibre.customize.ui import output_profiles
for x in output_profiles():
if x.short_name == "default":
self.profile = x
break
if self.profile is None:
# Just in case the default profile is removed in the future :)
self.profile = opts.output_profile
self.body_font_size = self.profile.fbase
self.logger = oeb.logger
item = oeb.manifest.hrefs[path]
basename = os.path.basename(path)
cssname = os.path.splitext(basename)[0] + ".css"
stylesheets = [html_css_stylesheet()]
style_tags = xpath(tree, '//*[local-name()="style" or local-name()="link"]')
# Add cssutils parsing profiles from output_profile
for profile in self.opts.output_profile.extra_css_modules:
cssprofiles.addProfile(profile["name"], profile["props"], profile["macros"])
parser = CSSParser(fetcher=self._fetch_css_file, log=logging.getLogger("calibre.css"))
self.font_face_rules = []
for elem in style_tags:
if elem.tag == XHTML("style") and elem.get("type", CSS_MIME) in OEB_STYLES:
text = elem.text if elem.text else u""
for x in elem:
t = getattr(x, "text", None)
if t:
text += u"\n\n" + force_unicode(t, u"utf-8")
t = getattr(x, "tail", None)
if t:
text += u"\n\n" + force_unicode(t, u"utf-8")
if text:
text = oeb.css_preprocessor(text, add_namespace=True)
# We handle @import rules separately
parser.setFetcher(lambda x: ("utf-8", b""))
stylesheet = parser.parseString(text, href=cssname, validate=False)
parser.setFetcher(self._fetch_css_file)
stylesheet.namespaces["h"] = XHTML_NS
for rule in stylesheet.cssRules:
if rule.type == rule.IMPORT_RULE:
ihref = item.abshref(rule.href)
if rule.media.mediaText == "amzn-mobi":
continue
hrefs = self.oeb.manifest.hrefs
if ihref not in hrefs:
self.logger.warn("Ignoring missing stylesheet in @import rule:", rule.href)
continue
sitem = hrefs[ihref]
if sitem.media_type not in OEB_STYLES:
self.logger.warn("CSS @import of non-CSS file %r" % rule.href)
continue
stylesheets.append(sitem.data)
for rule in tuple(stylesheet.cssRules.rulesOfType(CSSRule.PAGE_RULE)):
stylesheet.cssRules.remove(rule)
# Make links to resources absolute, since these rules will
# be folded into a stylesheet at the root
replaceUrls(stylesheet, item.abshref, ignoreImportRules=True)
stylesheets.append(stylesheet)
elif (
elem.tag == XHTML("link")
and elem.get("href")
and elem.get("rel", "stylesheet").lower() == "stylesheet"
and elem.get("type", CSS_MIME).lower() in OEB_STYLES
):
href = urlnormalize(elem.attrib["href"])
path = item.abshref(href)
sitem = oeb.manifest.hrefs.get(path, None)
if sitem is None:
self.logger.warn("Stylesheet %r referenced by file %r not in manifest" % (path, item.href))
continue
if not hasattr(sitem.data, "cssRules"):
self.logger.warn("Stylesheet %r referenced by file %r is not CSS" % (path, item.href))
continue
stylesheets.append(sitem.data)
csses = {"extra_css": extra_css, "user_css": user_css}
for w, x in csses.items():
if x:
try:
text = XHTML_CSS_NAMESPACE + x
stylesheet = parser.parseString(text, href=cssname, validate=False)
stylesheet.namespaces["h"] = XHTML_NS
stylesheets.append(stylesheet)
except:
self.logger.exception("Failed to parse %s, ignoring." % w)
self.logger.debug("Bad css: ")
self.logger.debug(x)
rules = []
index = 0
self.stylesheets = set()
self.page_rule = {}
for sheet_index, stylesheet in enumerate(stylesheets):
#.........这里部分代码省略.........
示例15: __init__
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseString [as 别名]
def __init__(self, tree, path, oeb, profile, extra_css='', user_css='',
change_justification='left'):
assert profile is not None
# XXX str/bytes hackfix
if isinstance(path, bytes):
decoded_path = path.decode('utf-8')
else:
decoded_path = path
self.oeb = oeb
self.profile = profile
self.change_justification = change_justification
item = oeb.manifest.hrefs[path]
basename = os.path.basename(decoded_path)
cssname = os.path.splitext(basename)[0] + '.css'
stylesheets = [html_css_stylesheet()]
head = xpath(tree, '/h:html/h:head')
if head:
head = head[0]
else:
head = []
parser = CSSParser(fetcher=self._fetch_css_file,
log=logging.getLogger('calibre.css'))
self.font_face_rules = []
for elem in head:
if (elem.tag == XHTML('style') and
elem.get('type', CSS_MIME) in OEB_STYLES):
text = elem.text if elem.text else ''
for x in elem:
t = getattr(x, 'text', None)
if t:
text += '\n\n' + force_unicode(t, 'utf-8')
t = getattr(x, 'tail', None)
if t:
text += '\n\n' + force_unicode(t, 'utf-8')
if text:
text = XHTML_CSS_NAMESPACE + elem.text
text = oeb.css_preprocessor(text)
stylesheet = parser.parseString(text, href=cssname)
stylesheet.namespaces['h'] = XHTML_NS
stylesheets.append(stylesheet)
elif elem.tag == XHTML('link') and elem.get('href') \
and elem.get('rel', 'stylesheet').lower() == 'stylesheet' \
and elem.get('type', CSS_MIME).lower() in OEB_STYLES:
href = urlnormalize(elem.attrib['href'])
path = item.abshref(href)
sitem = oeb.manifest.hrefs.get(path, None)
if sitem is None:
logging.warn(
'Stylesheet %r referenced by file %r not in manifest' %
(path, item.href))
continue
if not hasattr(sitem.data, 'cssRules'):
logging.warn(
'Stylesheet %r referenced by file %r is not CSS'%(path,
item.href))
continue
stylesheets.append(sitem.data)
csses = {'extra_css':extra_css, 'user_css':user_css}
for w, x in list(csses.items()):
if x:
try:
text = XHTML_CSS_NAMESPACE + x
stylesheet = parser.parseString(text, href=cssname)
stylesheet.namespaces['h'] = XHTML_NS
stylesheets.append(stylesheet)
except:
logging.exception('Failed to parse %s, ignoring.'%w)
logging.debug('Bad css: ')
logging.debug(x)
rules = []
index = 0
self.stylesheets = set()
self.page_rule = {}
for stylesheet in stylesheets:
href = stylesheet.href
self.stylesheets.add(href)
for rule in stylesheet.cssRules:
rules.extend(self.flatten_rule(rule, href, index))
index = index + 1
# XXX had to fix crash about unsortable type, so that's why we only sort by first item of tuple
rules.sort(key=lambda tup: tup[:1])
self.rules = rules
self._styles = {}
class_sel_pat = re.compile(r'\.[a-z]+', re.IGNORECASE)
capital_sel_pat = re.compile(r'h|[A-Z]+')
for _, _, cssdict, text, _ in rules:
fl = ':first-letter' in text
if fl:
text = text.replace(':first-letter', '')
try:
selector = CSSSelector(text)
except (AssertionError, ExpressionError, etree.XPathSyntaxError,
NameError, # thrown on OS X instead of SelectorSyntaxError
SelectorSyntaxError):
continue
try:
matches = selector(tree)
except etree.XPathEvalError:
continue
#.........这里部分代码省略.........