~nhoad/pyopen

50116146fd7f47f72d9925cc270563eeaa648df7 — Nathan Hoad 1 year, 4 months ago 82fcd23
Run black.
1 files changed, 34 insertions(+), 21 deletions(-)

M bin/pyopen.py
M bin/pyopen.py => bin/pyopen.py +34 -21
@@ 10,7 10,7 @@ def error(message):


def get_object(path):
    parts = path.split('.')
    parts = path.split(".")

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


@@ 31,7 31,7 @@ def import_path_to_file_path(path):
    try:
        obj = get_object(path)
    except (AttributeError, ImportError) as e:
        error('error opening %r: %s' % (path, str(e)))
        error("error opening %r: %s" % (path, str(e)))

    try:
        _source, lineno = inspect.findsource(obj)


@@ 39,7 39,7 @@ def import_path_to_file_path(path):
        try:
            _source, lineno = inspect.findsource(obj.__class__)
        except TypeError as e:
            error('error opening %r: %s' % (path, str(e)))
            error("error opening %r: %s" % (path, str(e)))
    except IOError as e:
        # allow opening an empty module (e.g. __init__.py)
        if inspect.ismodule(obj):


@@ 49,7 49,7 @@ def import_path_to_file_path(path):
                e = None

        if e:
            error('error opening %r: %s' % (path, str(e)))
            error("error opening %r: %s" % (path, str(e)))

    try:
        path = inspect.getsourcefile(obj)


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

    if lineno:
        additional_args = ['+%d' % (lineno+1)]
        additional_args = ["+%d" % (lineno + 1)]
    else:
        additional_args = []



@@ 72,11 72,11 @@ def import_path_to_file_path(path):


def get_editor():
    editor = os.environ.get('EDITOR', None)
    editor = os.environ.get("EDITOR", None)
    if editor:
        return editor

    editor = '/usr/bin/xdg-open'
    editor = "/usr/bin/xdg-open"
    if os.path.isfile(editor):
        return editor



@@ 84,24 84,37 @@ def get_editor():


@click.command(context_settings=dict(ignore_unknown_options=True))
@click.option("--print-dir", "-d", is_flag=True, help="Print directory to standard out rather than opening the file.")
@click.option(
    "--print-path", "-l",
    "--print-dir",
    "-d",
    is_flag=True,
    help="Print absolute file path to standard out rather than opening the file.")

    help="Print directory to standard out rather than opening the file.",
)
@click.option(
    "--absolute-open","-a",
    "--print-path",
    "-l",
    is_flag=True,
    help="Open the file using the absolute path rather than changing to the parent directory.")

    help="Print absolute file path to standard out rather than opening the file.",
)
@click.option(
    "--verbose","-v",
    "--absolute-open",
    "-a",
    is_flag=True,
    help="Display commands before they're run.")
    help="Open the file using the absolute path rather than changing to the parent directory.",
)
@click.option(
    "--verbose", "-v", is_flag=True, help="Display commands before they're run."
)
@click.argument("import_path")
@click.argument("additional_args", nargs=-1, type=click.UNPROCESSED)
def main(print_dir: bool, print_path: bool, absolute_open: bool, verbose: bool, import_path: str, additional_args: tuple[str, ...]) -> None:
def main(
    print_dir: bool,
    print_path: bool,
    absolute_open: bool,
    verbose: bool,
    import_path: str,
    additional_args: tuple[str, ...],
) -> None:
    dirname, basename, additional_args = import_path_to_file_path(import_path)

    if print_dir:


@@ 123,16 136,16 @@ def main(print_dir: bool, print_path: bool, absolute_open: bool, verbose: bool, 
        path = os.path.join(dirname, basename)
    else:
        if verbose:
            print('cd %s' % dirname)
            print("cd %s" % dirname)
        os.chdir(dirname)
        path = basename

    command = [editor, path] + additional_args

    if verbose:
        print(' '.join(command))
        print(" ".join(command))
    os.execvp(editor, command)


if __name__ == '__main__':
if __name__ == "__main__":
    main()