Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Commit

Permalink
python interpreter selector
Browse files Browse the repository at this point in the history
  • Loading branch information
baverman committed Nov 2, 2011
1 parent b62f429 commit cdfe9ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
2 changes: 2 additions & 0 deletions snaked/plugins/external_tools/__init__.py
Expand Up @@ -4,6 +4,7 @@

import gtk
from uxie.utils import join_to_settings_dir
from snaked.plugins.python.utils import get_executable

tools = []

Expand Down Expand Up @@ -124,6 +125,7 @@ def run(editor, tool):
env.update(os.environ)
env['FILENAME'] = editor.uri
env['OFFSET'] = str(editor.cursor.get_offset())
env['PYTHON'] = get_executable(editor.conf)

def on_finish():
os.remove(filename)
Expand Down
27 changes: 22 additions & 5 deletions snaked/plugins/python/__init__.py
@@ -1,6 +1,6 @@
author = 'Anton Bobrov<bobrov@vl.ru>'
name = 'Python support'
desc = 'Autocompletion, definitions navigation and smart ident'
desc = 'Autocompletion, navigation and smart ident'

import weakref

Expand All @@ -19,7 +19,10 @@ def init(injector):
injector.bind('python-editor', 'goto-definition', '_Python#70/Goto _defenition', goto_definition)
injector.bind('python-editor', 'show-outline', 'Python/Show _outline', open_outline)

injector.bind('python-editor', 'show-calltip', 'Python/Show calltip', show_calltips)
injector.bind('python-editor', 'show-calltip', 'Python/Show call_tip', show_calltips)

injector.bind_menu('python-editor', 'select-interpreter', 'Python/_Executable',
generate_python_executable_menu, resolve_python_executable_menu_entry)

#injector.bind_accel('run-test', '<ctrl>F10', 'Tests', 'Run test in cursor scope', run_test)
#injector.bind_accel('run-all-tests', '<ctrl><shift>F10', 'Tests',
Expand All @@ -28,9 +31,11 @@ def init(injector):
#injector.bind_accel('toggle-test-panel', '<alt>1', 'Window',
# 'Toggle test panel', toggle_test_panel)

from snaked.core.prefs import add_option
add_option('PYTHON_EXECUTABLE', 'default',
'Path to python executable. Used by test runner and completion framework')
from snaked.core.prefs import add_option, add_internal_option

add_internal_option('PYTHON_EXECUTABLE', 'default')
add_option('PYTHON_EXECUTABLES', dict,
'Path to python executables. Used by test runner and completion framework')
add_option('PYTHON_EXECUTABLE_ENV', dict,
'Python interpreter environment. Used by test runner and completion framework')
add_option('PYTHON_SUPP_CONFIG', dict, 'Config for supplement')
Expand Down Expand Up @@ -237,3 +242,15 @@ def rerun_test(editor):
get_pytest_runner(editor).run(editor, *last_run_test[0])
else:
editor.message('You did not run any test yet', 'warn')

def set_python_executable(editor, name):
from .utils import get_executable
editor.conf['PYTHON_EXECUTABLE'] = name
editor.message('Python executable was set to:\n' + get_executable(editor.conf))

def generate_python_executable_menu(editor):
for t in sorted(set(('default', 'python2', 'python3')).union(editor.conf['PYTHON_EXECUTABLES'])):
yield t, t, (set_python_executable, (editor, t))

def resolve_python_executable_menu_entry(editor, entry_id):
return set_python_executable, (editor, entry_id), entry_id
7 changes: 3 additions & 4 deletions snaked/plugins/python/plugin.py
Expand Up @@ -6,6 +6,8 @@
from snaked.util import lazy_property
from snaked.signals import connect_external, connect_all

from .utils import get_executable

environments = {}
configured_projects = {}

Expand All @@ -22,10 +24,7 @@ def init_completion(self):

@property
def env(self):
executable = self.editor.conf['PYTHON_EXECUTABLE']
if executable == 'default':
executable = sys.executable

executable = get_executable(self.editor.conf)
env = self.editor.conf['PYTHON_EXECUTABLE_ENV']

try:
Expand Down
32 changes: 32 additions & 0 deletions snaked/plugins/python/utils.py
@@ -0,0 +1,32 @@
import sys
import os
from os.path import exists, join

def which(binary_name):
for p in os.environ.get('PATH', '').split(os.pathsep):
path = join(p, binary_name)
if exists(path):
return path

return None

def get_executable(conf):
name = conf['PYTHON_EXECUTABLE']

try:
return conf['PYTHON_EXECUTABLES'][name]
except KeyError:
pass

if name == 'default':
return sys.executable
elif name == 'python2':
path = which('python2')
if path:
return path
elif name == 'python3':
path = which('python3')
if path:
return path

return sys.executable

0 comments on commit cdfe9ca

Please sign in to comment.