Exemple #1
0
def python(editor, args):
    """Run the contents of the editor or selection in Python

    executable may be a python interpreter executable or a directory
    such as a virtualenv containing `bin/python`.
    """
    if args is None:
        from editxt.commands import show_command_bar
        show_command_bar(editor, "python ")
        return
    python = args.executable
    if not python:
        raise CommandError("please specify python executable")
    if os.path.isdir(python):
        bin = os.path.join(python, "bin", "python")
        if os.path.exists(bin):
            python = bin
        else:
            raise CommandError("not found: %s" % bin)
    if args.scope == "selection":
        code = editor.document.text_storage[editor.selection]
    else:
        code = editor.document.text
    code = print_last_line(dedent(code))
    cwd = editor.dirname()
    command = [python] + [o for o in args.options if o] + ["-c", code]
    env = dict(os.environ)
    env.pop("PYTHONHOME", None)
    env.pop("PYTHONPATH", None)
    env.pop("PYTHONDONTWRITEBYTECODE", None)
    env.pop("EXECUTABLEPATH", None)
    env.pop("RESOURCEPATH", None)
    result = exec_shell(command, cwd=cwd, env=env)
    if result.returncode == 0:
        msg_type = const.INFO
        message = str(result)
        if message.endswith("\n"):
            message = message[:-1]
    else:
        msg_type = const.ERROR
        message = result or result.err
    if message:
        editor.message(message, msg_type=msg_type)
    else:
        return "no output"
Exemple #2
0
def python(editor, args):
    """Run the contents of the editor or selection in Python

    executable may be a python interpreter executable or a directory
    such as a virtualenv containing `bin/python`.
    """
    if args is None:
        from editxt.commands import show_command_bar
        show_command_bar(editor, "python ")
        return
    if not args.executable:
        try:
            python = editor.app.config.for_command("python")["executable"]
        except KeyError:
            python = "python"
    else:
        python = args.executable
        if os.path.isdir(python):
            bin = os.path.join(python, "bin", "python")
            if os.path.exists(bin):
                python = bin
    if args.scope == "selection":
        code = editor.document.text_storage[editor.selection]
    else:
        code = editor.document.text
    cwd = editor.dirname()
    command = [python] + [o for o in args.options if o] + ["-c", code]
    env = dict(os.environ)
    env.pop("PYTHONHOME", None)
    env.pop("PYTHONPATH", None)
    env.pop("PYTHONDONTWRITEBYTECODE", None)
    env.pop("EXECUTABLEPATH", None)
    env.pop("RESOURCEPATH", None)
    result = exec_shell(command, cwd=cwd, env=env)
    if result.returncode == 0:
        msg_type = const.INFO
        message = str(result)
    else:
        msg_type = const.ERROR
        message = result or result.err
    if message:
        editor.message(message, msg_type=msg_type)
    else:
        return "no output"
Exemple #3
0
def is_ag_installed(ag_path="ag", recheck=False, result={}):
    if result.get(ag_path) is not None and not recheck:
        return result.get(ag_path)
    result[ag_path] = exec_shell([ag_path, "--version"]).returncode == 0
    return result[ag_path]