Skip to content

Commit

Permalink
Add importmagic script
Browse files Browse the repository at this point in the history
  • Loading branch information
razzius committed Mar 12, 2018
1 parent 45b0bdd commit 17a1d3e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
43 changes: 43 additions & 0 deletions bin/importmagic
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env python
"""
Update python imports using importmagic.
"""

import os
import argparse
import sys

import importmagic


parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('file_name')
parser.add_argument(
'--refresh',
action='store_true',
help='If set, forces a refresh of the importmagic index.'
)
parser.add_argument(
'--exclude-current-path',
action='store_true',
help='If set, will not automatically add the current directory'
' to the import path when building the index.'
)

args = parser.parse_args()

path = sys.path if args.exclude_current_path else sys.path + [os.getcwd()]

index = importmagic.SymbolIndex()
index.get_or_create_index(paths=path, refresh=args.refresh)

with open(args.file_name) as f:
python_source = f.read()

scope = importmagic.Scope.from_source(python_source)

unresolved, unreferenced = scope.find_unresolved_and_unreferenced_symbols()
python_source = importmagic.update_imports(python_source, index, unresolved, unreferenced)

with open(args.file_name, 'w') as f:
f.write(python_source)
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ def run(self):
'setuptools >= 0.6b1',
],
cmdclass={'test': PyTest},
scripts=['bin/importmagic'],
)

0 comments on commit 17a1d3e

Please sign in to comment.