How to launch own 2to3 fixer?

梦想的初衷 提交于 2019-12-22 10:34:23

问题


I wrote own fixer, how can i run it? I don't find obvious way to do this.

Only this:

> cd /usr/lib/python2.7/lib2to3/fixes/
> ln -s path/to/my_fixer.py

And then run it:

> cd path/to/project
> 2to3 -f my_fixer .

回答1:


I got it! (file: my2to3)

#!/usr/bin/env python2.7
import sys
from lib2to3.main import main

sys.path.append('path/to/my_own_package')
sys.exit(main('my_own_package.contained_fixers'))

And then run it:

> ./my2to3 -f my_fixer -w project



回答2:


  • Create a directory /mypath/custom_fixers
  • Generate a file /mypath/custom_fixers/fix_custom_fixers.py with this content:

    from lib2to3.fixer_base import BaseFix
    from lib2to3.pgen2 import token
    
    class FixCustomFixers(BaseFix):
    
        _accept_type = token.NAME
    
        def match(self, node):
            if node.value == 'oldname':
                return True
            return False
    
        def transform(self, node, results):
            node.value = 'newname'
            node.changed()
    
  • Create a file /mypath/myfixer.py with this content:

    import sys
    from lib2to3.main import main
    sys.exit(main('custom_fixers'))
    
  • Run PYTHONPATH=/mypath python3 /mypath/myfixer.py -f custom_fixers python_file_to_fix.py



来源:https://stackoverflow.com/questions/24508357/how-to-launch-own-2to3-fixer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!