コード例 #1
0
    def download_and_replace(self, match: typing.Match):
        img_url = match.group('url').split('!')[0]
        img_path = urlparse(img_url).path[1:]
        asset_path = self.output_dir / img_path
        replace_url = self.real_output_path / img_path

        if not asset_path.exists():  # 不存在则尝试下载
            response = self.session.get(img_url)
            if response.status_code == 200:
                asset_path.parent.mkdir(parents=True, exist_ok=True)  # 尝试创建目录
                with asset_path.open('wb') as img_fp:  # 保存图片
                    img_fp.write(response.content)
                return match.expand(r'\g<start>%s\g<end>' % replace_url)
            else:
                logging.error("Can not download images from url %s, code: %s",
                              img_url, response.status_code)
                logging.debug(response.content)
                return match.group()
        else:
            return match.expand(r'\g<start>%s\g<end>' % replace_url)
コード例 #2
0
ファイル: engrave.py プロジェクト: ankostis/polyvers
    def _graft_match(
        self,
        graft: pvproject.Graft,
        fbytes: bytes,
        match: Match,
        offset: int,
        project: 'pvproject.Project',
    ) -> Tuple[bytes, int]:
        """
        :param graft:
            a graft with a non-null :attr:`pvproject.Graft.subst`
        :return:
            the substituted fbytes
        """
        subst = graft.subst_resolved(project)
        if subst is not None:
            mstart, mend = match.span()
            new_text = match.expand(subst)
            head = fbytes[:mstart + offset]
            tail = fbytes[mend + offset:]
            fbytes = head + new_text + tail
            offset += len(new_text) - (mend - mstart)

        return fbytes, offset
コード例 #3
0
ファイル: ietf.py プロジェクト: kellerza/data-playbook
def _re_rfc(match: Match) -> Tuple[str, Optional[str]]:
    template = r"RFC\1"
    if len(match.group(1)) < 4:
        template = r"RFC" + r"\1".zfill(6 - len(match.group(1)))
    return match.expand(template), None