~nhoad/pyopen

14f24a3d6dce694de39e6319136ce16a1a43a764 — Nathan Hoad 1 year, 4 months ago d6db0d4
Support things like pathlib.Path.open.

I'm confident this has broken lazily imported submodules, but I'll fix that when I encounter it next.
1 files changed, 17 insertions(+), 14 deletions(-)

M bin/pyopen.py
M bin/pyopen.py => bin/pyopen.py +17 -14
@@ 1,4 1,4 @@
#!/usr/bin/python2
#!/usr/bin/python3

import inspect
import os


@@ 13,18 13,20 @@ def error(message, exit_code=1):


def get_object(path):
    try:
        module, obj = path.rsplit('.', 1)
    except ValueError:
        module = path
        obj = None

    if obj is not None:
        module = __import__(module, fromlist=[obj])
        obj = getattr(module, obj)
    else:
        obj = __import__(module)
    parts = path.split('.')

    module = parts[0]
    remaining = parts[1:]

    if not remaining:
        return __import__(module)

    module = __import__(module, fromlist=[remaining[0]])

    obj = getattr(module, remaining.pop(0))

    while remaining:
        obj = getattr(obj, remaining.pop(0))
    return obj




@@ 57,9 59,9 @@ def import_path_to_file_path(path):
    except TypeError:
        try:
            path = inspect.getsourcefile(obj.__class__)
        except TypeError:
        except TypeError as e:
            error('error opening %r: %s' % (path, str(e)))
    except IOError as e:
    except IOError:
        path = obj.__file__

    if lineno:


@@ 146,5 148,6 @@ def main():
        print(' '.join(command))
    os.execvp(editor, command)


if __name__ == '__main__':
    main()