2017-06-12 12 views
0
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") 

これにより、次のコンソール出力がトリガーされます。Python slimit minimizer不要な警告出力

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 

これらの警告は、アプリケーションコンソールの出力にあふれています。警告を生成せずにminifyを使用するにはどうすればよいですか?

私はPython 2.7.12を使用しています。現在、最新のライブラリバージョンslimit 0.8.1、ply 3.10が使用されています。

答えて

0

は、私が一緒に行った溶液でした。私は特別なerrorlog=ply.yacc.NullLogger()コールをply.yacc.yacc関数に渡す2つのスリム関数のカスタムバリアントを作った。

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 
1

Slimitは、stdlibのloggingを使用するフードでplyを使用します。 AFAICS slimitは、plylexyaccが期待するロギングパラメータを渡すことを許可していません。

あなたがそのことはできません(直接)アクセスplyのログイン時、あなたは私がグローバルログレベルを上げるそれらのメッセージを抑制することができるべきである:ここでは

import logging 
... 
logging.disable(logging.CRITICAL) 
minify("", mangle=True) 
logging.disable(logging.NOTSET) 
関連する問題