Python slimit 最小化器不需要的警告输出

cla*_*lay 6 python python-2.7

from slimit import minify


if __name__ == "__main__":
    print("start")

    # Normally, I pass real JavaScript. For this issue, an empty string reproduces problem.
    minify("", mangle=True)

    print("exit")
Run Code Online (Sandbox Code Playgroud)

这会触发以下控制台输出。

start
WARNING: Couldn't write lextab module <module 'slimit.lextab' from '/Users/kurtostfeld/samba/wrapad/venv/lib/python2.7/site-packages/slimit/lextab.pyc'>. Won't overwrite existing lextab module
WARNING: yacc table file version is out of date
WARNING: Token 'IMPORT' defined, but not used
WARNING: Token 'BLOCK_COMMENT' defined, but not used
WARNING: Token 'ENUM' defined, but not used
WARNING: Token 'EXTENDS' defined, but not used
WARNING: Token 'LINE_COMMENT' defined, but not used
WARNING: Token 'LINE_TERMINATOR' defined, but not used
WARNING: Token 'CONST' defined, but not used
WARNING: Token 'EXPORT' defined, but not used
WARNING: Token 'CLASS' defined, but not used
WARNING: Token 'SUPER' defined, but not used
WARNING: There are 10 unused tokens
WARNING: Couldn't create <module 'slimit.yacctab' from '/Users/kurtostfeld/samba/wrapad/venv/lib/python2.7/site-packages/slimit/yacctab.pyc'>. Won't overwrite existing tabmodule
exit
Run Code Online (Sandbox Code Playgroud)

这些警告充斥着我的应用程序控制台输出。如何在不产生警告的情况下使用 minify?

我使用的是 Python 2.7.12,目前最新的库版本是什么:slimit 0.8.1,ply 3.10。

sno*_*ogg 5

根据Github 上的这个问题, slimit 取决于ply包。经过几次尝试,似乎这些警告自ply. . 您可以更新ply到 3.6,这是不带这些消息的最后一个版本:

pip uninstall ply -y && pip install ply==3.6
Run Code Online (Sandbox Code Playgroud)

它解决了我的问题。

更新 安装较早的版本ply确实是一个糟糕的解决方法,因为我的一些测试失败了。原始slimit版本似乎维护得不好,所以我建议更新到新版本,metatoaster 很好地改进了它并修复了警告消息的问题。我的解决方案是卸载slimit然后安装它的版本:

pip install git+https://github.com/metatoaster/slimit.git#egg=slimit
Run Code Online (Sandbox Code Playgroud)

最终更新事实上,slimit似乎不再维护,它的继任者被称为calmjs,几乎没有区别,但它确实更稳定,并且不会显示这些烦人的警告消息。参见:https : //github.com/calmjs/calmjs.parse


cla*_*lay 0

这是我采用的解决方案。我制作了两个 slimit 函数的自定义变体,它们errorlog=ply.yacc.NullLogger()向该ply.yacc.yacc函数传递了额外的调用。

class SlimitNoLoggingParser(Parser):
    """
    This is a simple customized variant to slimit.parser.Parser.

    The only difference is that this passes a errorlog=ply.yacc.NullLogger() to ply.yacc.yacc to suppress unwanted
    stderr logging output.
    """

    def __init__(self, lex_optimize=True, lextab=lextab,
                 yacc_optimize=True, yacctab=yacctab, yacc_debug=False):
        self.lex_optimize = lex_optimize
        self.lextab = lextab
        self.yacc_optimize = yacc_optimize
        self.yacctab = yacctab
        self.yacc_debug = yacc_debug

        self.lexer = Lexer()
        self.lexer.build(optimize=lex_optimize, lextab=lextab)
        self.tokens = self.lexer.tokens

        self.parser = ply.yacc.yacc(
            module=self, optimize=yacc_optimize,
            errorlog=ply.yacc.NullLogger(),
            debug=yacc_debug, tabmodule=yacctab, start='program')

        # https://github.com/rspivak/slimit/issues/29
        # lexer.auto_semi can cause a loop in a parser
        # when a parser error happens on a token right after
        # a newline.
        # We keep record of the tokens that caused p_error
        # and if the token has already been seen - we raise
        # a SyntaxError exception to avoid looping over and
        # over again.
        self._error_tokens = {}


# This is a simply variant of slimit.minify that suppresses unwanted noisy stderr logging output.
def warning_free_minify(text, mangle=False, mangle_toplevel=False):
    parser = SlimitNoLoggingParser(lex_optimize=False)
    tree = parser.parse(text)
    if mangle:
        mangler.mangle(tree, toplevel=mangle_toplevel)
    minified = ECMAMinifier().visit(tree)
    return minified
Run Code Online (Sandbox Code Playgroud)