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

59 lines
2.2 KiB
Python
Executable file

#!/home/walkers/git/creeper-adventure/.venv/bin/python3
"""
collect-imports *.py
collect-imports < foo.py
Collect all imports from named files or stdin, and combine them into a single
block of import statements. Print the result to stdout.
"""
# pyflyby/collect-imports
# Copyright (C) 2011, 2014 Karl Chen.
# License: MIT http://opensource.org/licenses/MIT
from __future__ import absolute_import, division, with_statement
import re
import sys
from pyflyby._cmdline import filename_args, hfmt, parse_args
from pyflyby._importclns import ImportSet
from pyflyby._importdb import ImportDB
def main():
def addopts(parser):
parser.add_option("--ignore-known", default=False, action='store_true',
help=hfmt('''
Don't list imports already in the
known-imports database.'''))
parser.add_option("--no-ignore-known", dest="ignore_known",
action='store_false',
help=hfmt('''
(Default) List all imports, including those
already in the known-imports database.'''))
parser.add_option("--include",
default=[], action="append",
help=hfmt('''
Include only imports under the given package.'''))
options, args = parse_args(addopts, import_format_params=True)
filenames = filename_args(args)
importset = ImportSet(filenames, ignore_nonimports=True)
if options.include:
regexps = [
re.escape(prefix) if prefix.endswith(".") else
re.escape(prefix) + "([.]|$)"
for prefix in options.include
]
regexp = re.compile("|".join(regexps))
match = lambda imp: regexp.match(imp.fullname)
importset = ImportSet([imp for imp in importset if match(imp)])
if options.ignore_known:
db = ImportDB.get_default(".")
importset = importset.without_imports(db.known_imports)
sys.stdout.write(importset.pretty_print(
allow_conflicts=True, params=options.params))
if __name__ == '__main__':
main()