本文整理汇总了Python中cssutils.CSSParser.parseFile方法的典型用法代码示例。如果您正苦于以下问题:Python CSSParser.parseFile方法的具体用法?Python CSSParser.parseFile怎么用?Python CSSParser.parseFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cssutils.CSSParser
的用法示例。
在下文中一共展示了CSSParser.parseFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseFile [as 别名]
def run(self):
# Step 0: ensure that the document_root and base_path variables are
# set. If the file that's being processed was inside a source that has
# either one or both not set, then this processor can't run.
if self.document_root is None or self.base_path is None:
raise DocumentRootAndBasePathRequiredException
# We don't rename the file, so we can use the default output file.
parser = CSSParser(log=None, loglevel=logging.CRITICAL)
sheet = parser.parseFile(self.input_file)
# Step 1: ensure the file has URLs. If it doesn't, we can stop the
# processing.
url_count = 0
for url in getUrls(sheet):
url_count += 1
break
if url_count == 0:
return self.input_file
# Step 2: resolve the relative URLs to absolute paths.
replaceUrls(sheet, self.resolveToAbsolutePath)
# Step 3: verify that each of these files has been synced.
synced_files_db = urljoin(sys.path[0] + os.sep, SYNCED_FILES_DB)
self.dbcon = sqlite3.connect(synced_files_db)
self.dbcon.text_factory = unicode # This is the default, but we set it explicitly, just to be sure.
self.dbcur = self.dbcon.cursor()
all_synced = True
for urlstring in getUrls(sheet):
# Skip absolute URLs.
if urlstring.startswith("http://") or urlstring.startswith("https://"):
continue
# Skip broken references in the CSS file. This would otherwise
# prevent this CSS file from ever passing through this processor.
if not os.path.exists(urlstring):
continue
# Get the CDN URL for the given absolute path.
self.dbcur.execute("SELECT url FROM synced_files WHERE input_file=?", (urlstring,))
result = self.dbcur.fetchone()
if result == None:
raise RequestToRequeueException(
"The file '%s' has not yet been synced to the server '%s'" % (urlstring, self.process_for_server)
)
else:
cdn_url = result[0]
# Step 4: resolve the absolute paths to CDN URLs.
replaceUrls(sheet, self.resolveToCDNURL)
# Step 5: write the updated CSS to the output file.
f = open(self.output_file, "w")
f.write(sheet.cssText)
f.close()
return self.output_file
示例2: __init__
# 需要导入模块: from cssutils import CSSParser [as 别名]
# 或者: from cssutils.CSSParser import parseFile [as 别名]
class Parser:
def __init__(self):
self.css_parser = CSSParser()
def get_colors_from_file(self, f):
sheet = self.css_parser.parseFile(f, 'utf-8')
my_dict = {}
for rule in sheet:
if rule.type == rule.STYLE_RULE:
for property in rule.style:
if property.name == 'color':
key = property.value
if key in my_dict:
my_dict[key] += 1
else:
my_dict[key] = 1
return my_dict
def read_all_css_files_in_dir(self):
l = []
for filename in glob.glob('*.css'):
d = self.get_colors_from_file(filename)
l.append(d)
return l