creeper-adventure/.venv/bin/transform-imports
2022-03-31 20:20:07 -05:00

48 lines
1.5 KiB
Python
Executable file

#!/home/walkers/git/creeper-adventure/.venv/bin/python3
"""
transform-imports --transform aa.bb=xx.yy *.py
transform-imports --transform aa.bb=xx.yy < foo.py
Transforms::
from aa.bb.cc import dd, ee
from aa import bb
to::
from xx.yy.cc import dd, ee
from xx import yy as bb
If filenames are given on the command line, rewrites them. Otherwise, if
stdin is not a tty, read from stdin and write to stdout.
"""
# pyflyby/transform-imports
# Copyright (C) 2014 Karl Chen.
# License: MIT http://opensource.org/licenses/MIT
from __future__ import absolute_import, division, with_statement
from pyflyby._cmdline import hfmt, parse_args, process_actions
from pyflyby._imports2s import transform_imports
def main():
transformations = {}
def addopts(parser):
def callback(option, opt_str, value, group):
k, v = value.split("=", 1)
transformations[k] = v
parser.add_option("--transform", action='callback',
type="string", callback=callback,
metavar="OLD=NEW",
help=hfmt('''
Replace OLD with NEW in imports.
May be specified multiple times.'''))
options, args = parse_args(
addopts, import_format_params=True, modify_action_params=True)
def modify(x):
return transform_imports(x, transformations, params=options.params)
process_actions(args, options.actions, modify)
if __name__ == '__main__':
main()