CodeManager.py 文件源码

python
阅读 21 收藏 0 点赞 0 评论 0

项目:ThinkingInPython 作者: BruceEckel 项目源码 文件源码
def check(language, verbose=True):
        """
        Ensure that external code files exist and check which external files
        have changed from what's in the .rst files. Generate files in the
        _deltas subdirectory showing what has changed.
        """
        class Result: # Messenger
            def __init__(self, **kwargs):
                self.__dict__ = kwargs
        result = Result(missing = [], deltas = [])
        listings = [Result(code = shift(code), file = f)
                    for f in restFiles for code in
                    language.listings.findall(open(f).read())]
        paths = [os.path.normpath(os.path.join("..", "code", path)) for path in
                    [listing.code[0].strip()[len(language.commentTag):].strip()
                     for listing in listings]]
        if os.path.exists("_deltas"):
            shutil.rmtree("_deltas")
        for path, listing in zip(paths, listings):
            if not os.path.exists(path):
                result.missing.append(path)
            else:
                code = open(path).read().splitlines()
                if difference(listing.code, code):
                    d = difflib.HtmlDiff()
                    if not os.path.exists("_deltas"):
                        os.makedirs("_deltas")
                    html = os.path.join("_deltas",
                        os.path.basename(path).split('.')[0] + ".html")
                    open(html, 'w').write(
                        "<html><h1>Left: %s<br>Right: %s</h1>" %
                        (listing.file, path) +
                        d.make_file(listing.code, code))
                    result.deltas.append(Result(file = listing.file,
                        path = path, html = html, code = code))
        if result.missing:
            print("Missing %s files:\n%s" %
                  (language.__name__, "\n".join(result.missing)))
        if verbose:
            for delta in result.deltas:
                print("%s <==> %s; see %s" %
                    (delta.file, delta.path, delta.html))
        return result
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号