Skip to content

Instantly share code, notes, and snippets.

@evansde77
Last active May 26, 2016 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evansde77/b17932e092223cbc090d7637ff8fd18c to your computer and use it in GitHub Desktop.
Save evansde77/b17932e092223cbc090d7637ff8fd18c to your computer and use it in GitHub Desktop.
Util to convert filenames to python module names as a command line tool
#!/usr/bin/env python
"""
pyfile_to_module
Simple util to convert a python filename like
some_package/some_module/some_file.py to some_package.some_module.some_file
style module name.
"""
import argparse
import os
import re
import sys
def build_parser():
"""build command line parser and process CLI options"""
parser = argparse.ArgumentParser(description='CLI util for converting filenames to module names')
parser.add_argument(
'filename', metavar='filename', type=str, nargs=1,
help='filename to convert to modulename'
)
parser.add_argument(
'--prefix', '-p', dest='prefix', default=None, help='dir prefix to strip from the filename'
)
parser.add_argument(
'--walk-pythonpath', '-w',
default=False,
action='store_true',
help='walk python path entries to find matching prefix instead of supplying it',
dest='walk'
)
args = parser.parse_args()
return args
end_replace = lambda p, sub, s: re.sub('{0}$'.format(p), sub, s)
start_replace = lambda p, sub, s: re.sub('^{0}'.format(p), sub, s)
def find_prefix(filename):
"""
walk through pythonpath and use it to find the appropriate module prefix
string for the filename
"""
filename = os.path.abspath(filename)
# find all entries in the path that filename starts with
matches = (x for x in sys.path if filename.startswith(x))
# pick the deepest path
best_match = max(matches)
return best_match
def convert_name(filename, prefix=None):
"""
convert_name
Given filename:
if prefix is provided, remove prefix from start of string
remove .py from end of string
swap / for .
return the output
"""
full_filename = os.path.abspath(filename)
if prefix:
full_filename = start_replace(prefix, '', full_filename)
if full_filename.startswith('/'):
full_filename = start_replace('/', '', full_filename)
full_filename = end_replace('.py', '', full_filename)
full_filename = full_filename.replace('/', '.')
return full_filename
def main():
"""
main entry point for CLI
"""
args = build_parser()
filename = args.filename[0]
prefix = args.prefix
if args.walk:
prefix = find_prefix(filename)
result = convert_name(filename, prefix)
sys.stdout.write(result)
sys.stdout.write('\n')
sys.exit(0)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment