whatstyle.py 文件源码

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

项目:whatstyle 作者: mikr 项目源码 文件源码
def show_diffs(formatter,          # type: CodeFormatter
               filenames,          # type: List[str]
               style,              # type: Style
               ansi,               # type: bool
               ansihtml,           # type: bool
               html,               # type: bool
               nopager,            # type: bool
               numlines,           # type: int
               wrapcolumn=0,       # type: int
               linenumbers=False,  # type: bool
               enc='utf-8'         # type: str
               ):
    # type: (...) -> None
    """Show the differences between the current and reformatted sources.
    """
    if not ansi and not html:
        if supports_color():
            ansi = True
        else:
            html = True
    pairs = []
    for filename in filenames:
        sourcedata = get_cached_file(filename)
        content = formatter.formatcode(style, sourcedata, filename=filename)
        pairs.append((sourcedata, content))

    unifilenames = '\n'.join([unifilename(f) for f in filenames]) + '\n'
    htmldiffer = HtmlMultiDiff(tabsize=4, wrapcolumn=wrapcolumn)
    table = htmldiffer.table_from_pairs(pairs,
                                        enc,
                                        fromdesc='',
                                        todesc='',
                                        context=True,
                                        numlines=numlines)
    headerhtml = '<pre>\n' + unifilenames + '</pre>'
    customhtml = make_custom_html(htmldiffer, headerhtml, [table], enc=enc)
    htmldata = unescape_ill_surrencode(customhtml, enc=enc)
    htmldata = translate_non_sgml_chars(htmldata, enc=enc)
    headerdata = yellow(unifilenames)
    if html or ansihtml:
        if ansihtml:
            htmldata = html2ansihtml(
                surrdecode(htmldata, enc=enc),
                header=headerdata,
                enc=enc,
                linenumbers=linenumbers)
        fd, tempname = tempfile.mkstemp(suffix='.html', prefix='whatstyle_')
        os.write(fd, htmldata)
        os.close(fd)
        url = urljoin('file:', pathname2url(tempname))
        webbrowser.open(url)
    elif ansi:
        with pagercontext(not nopager) as fp:
            outline(headerdata, fp=fp)
            htmldiff2ansi(customhtml, enc, linenumbers=linenumbers, fp=fp)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号