本文整理汇总了Python中MaKaC.common.contribPacker.ZIPFileHandler.hasFile方法的典型用法代码示例。如果您正苦于以下问题:Python ZIPFileHandler.hasFile方法的具体用法?Python ZIPFileHandler.hasFile怎么用?Python ZIPFileHandler.hasFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MaKaC.common.contribPacker.ZIPFileHandler
的用法示例。
在下文中一共展示了ZIPFileHandler.hasFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: OfflineEventCreator
# 需要导入模块: from MaKaC.common.contribPacker import ZIPFileHandler [as 别名]
# 或者: from MaKaC.common.contribPacker.ZIPFileHandler import hasFile [as 别名]
#.........这里部分代码省略.........
config = Config.getInstance()
soup = BeautifulSoup(html)
images = set(_fix_url_path(x['src']) for x in soup.select('img[src]'))
scripts = set(_fix_url_path(x['src']) for x in soup.select('script[src]'))
styles = set(_fix_url_path(x['href']) for x in soup.select('link[rel="stylesheet"]'))
for path in itertools.chain(images, scripts, styles):
src_path = re.sub(r'#.*$', '', os.path.join(config.getHtdocsDir(), path))
dst_path = os.path.join(self._staticPath, path)
if path in styles:
self._css_files.add(path)
if not os.path.isfile(src_path):
self._failed_paths.add(path)
elif path not in styles:
self._addFileFromSrc(dst_path, src_path)
for image in soup.select('img[src]'):
image['src'] = os.path.join('static', _fix_url_path(image['src']))
for script in soup.select('script[src]'):
script['src'] = os.path.join('static', _fix_url_path(script['src']))
for style in soup.select('link[rel="stylesheet"]'):
style['href'] = os.path.join('static', _fix_url_path(style['href']))
return str(soup)
def _get_failed_paths(self):
"""Downloads files that were not available in the fielystem via HTTP.
This is the only clean way to deal with static files from plugins since otherwise
we would have to emulate RHHtdocs.
"""
cfg = Config.getInstance()
# If we have the embedded webserver prefer its base url since the iptables hack does
# not work for connections from the same machine
base_url = cfg.getBaseURL()
for path in self._failed_paths:
dst_path = os.path.join(self._staticPath, path)
if not self._fileHandler.hasFile(dst_path):
response = requests.get(os.path.join(base_url, path), verify=False)
self._downloaded_files[dst_path] = response.content
self._fileHandler.addNewFile(dst_path, response.content)
def _get_css_refs(self):
"""Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
config = Config.getInstance()
for path in self._css_files:
src_path = os.path.join(config.getHtdocsDir(), path)
dst_path = os.path.join(self._staticPath, path)
if dst_path in self._downloaded_files and not os.path.exists(src_path):
css = self._downloaded_files[dst_path]
else:
with open(src_path, 'rb') as f:
css = f.read()
# Extract all paths inside url()
urls = set(m.group('url') for m in RE_CSS_URL.finditer(css) if m.group('url')[0] != '#')
for url in urls:
orig_url = url
url = _remove_qs(url) # get rid of cache busters
if url[0] == '/':
# make it relative and resolve '..' elements
url = os.path.normpath(url[1:])
# anything else is straightforward: the url is now relative to the htdocs folder
ref_src_path = os.path.join(config.getHtdocsDir(), url)
ref_dst_path = os.path.join(self._staticPath, url)
# the new url is relative to the css location
static_url = os.path.relpath(url, os.path.dirname(path))
else:
# make the relative path absolute (note: it's most likely NOT relative to htdocs!)
css_abs_path = os.path.join(config.getHtdocsDir(), path)
# now we can combine the relative url with that path to get the proper paths of the resource
示例2: OfflineEventCreator
# 需要导入模块: from MaKaC.common.contribPacker import ZIPFileHandler [as 别名]
# 或者: from MaKaC.common.contribPacker.ZIPFileHandler import hasFile [as 别名]
#.........这里部分代码省略.........
return self._outputFile
def _get_static_files(self, html):
config = Config.getInstance()
soup = BeautifulSoup(html)
scripts = set(_fix_url_path(x["src"]) for x in soup.select("script[src]"))
styles = set(_fix_url_path(x["href"]) for x in soup.select('link[rel="stylesheet"]'))
for path in itertools.chain(scripts, styles):
src_path = os.path.join(config.getHtdocsDir(), path)
dst_path = os.path.join(self._staticPath, path)
if path in styles:
self._css_files.add(path)
if not os.path.isfile(src_path):
self._failed_paths.add(path)
elif path not in styles:
self._addFileFromSrc(dst_path, src_path)
for script in soup.select("script[src]"):
script["src"] = os.path.join("static", _fix_url_path(script["src"]))
for style in soup.select('link[rel="stylesheet"]'):
style["href"] = os.path.join("static", _fix_url_path(style["href"]))
return str(soup)
def _get_failed_paths(self):
"""Downloads files that were not available in the fielystem via HTTP.
This is the only clean way to deal with static files from plugins since otherwise
we would have to emulate RHHtdocs.
"""
cfg = Config.getInstance()
# If we have the embedded webserver prefer its base url since the iptables hack does
# not work for connections from the same machine
base_url = cfg.getEmbeddedWebserverBaseURL() or cfg.getBaseURL()
for path in self._failed_paths:
dst_path = os.path.join(self._staticPath, path)
if not self._fileHandler.hasFile(dst_path):
response = requests.get(os.path.join(base_url, path), verify=False)
self._downloaded_files[dst_path] = response.content
self._fileHandler.addNewFile(dst_path, response.content)
def _get_css_refs(self):
"""Adds files referenced in stylesheets and rewrite the URLs inside those stylesheets"""
config = Config.getInstance()
for path in self._css_files:
src_path = os.path.join(config.getHtdocsDir(), path)
dst_path = os.path.join(self._staticPath, path)
if dst_path in self._downloaded_files and not os.path.exists(src_path):
css = self._downloaded_files[dst_path]
else:
with open(src_path, "rb") as f:
css = f.read()
# Extract all paths inside url()
urls = set(m.group("url") for m in RE_CSS_URL.finditer(css) if m.group("url")[0] != "#")
for url in urls:
orig_url = url
url = _remove_qs(url) # get rid of cache busters
if url[0] == "/":
# make it relative and resolve '..' elements
url = os.path.normpath(url[1:])
# anything else is straightforward: the url is now relative to the htdocs folder
ref_src_path = os.path.join(config.getHtdocsDir(), url)
ref_dst_path = os.path.join(self._staticPath, url)
# the new url is relative to the css location
static_url = os.path.relpath(url, os.path.dirname(path))
else:
# make the relative path absolute (note: it's most likely NOT relative to htdocs!)
css_abs_path = os.path.join(config.getHtdocsDir(), path)
# now we can combine the relative url with that path to get the proper paths of the resource