Skip to content

Commit 028601f

Browse files
committed
Support multiple python executables
autocomplete-python#66
1 parent 0f20f66 commit 028601f

6 files changed

Lines changed: 165 additions & 152 deletions

File tree

‎lib/hyperclickProvider.coffee‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
provider = require './provider'
2+
log = require './log'
23
{selectorsMatchScopeChain} = require './scope-helpers'
34
{Selector} = require 'selector-kit'
45

@@ -25,8 +26,8 @@ module.exports =
2526
return
2627

2728
if atom.config.get('autocomplete-python.outputDebug')
28-
provider._log range.start, @_getScopes(editor, range.start)
29-
provider._log range.end, @_getScopes(editor, range.end)
29+
log.debug range.start, @_getScopes(editor, range.start)
30+
log.debug range.end, @_getScopes(editor, range.end)
3031
callback = ->
3132
provider.goToDefinition(editor, bufferPosition)
3233
return {range, callback}

‎lib/interpreters-lookup.coffee‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
fs = require 'fs'
2+
path = require 'path'
3+
log = require './log'
4+
5+
module.exports =
6+
pythonExecutableRe: /^python(\d+(.\d+)?)?$/
7+
8+
possibleGlobalPythonPaths: ->
9+
if /^win/.test process.platform
10+
return ['C:\\Python2.7',
11+
'C:\\Python3.4',
12+
'C:\\Python3.5',
13+
'C:\\Program Files (x86)\\Python 2.7',
14+
'C:\\Program Files (x86)\\Python 3.4',
15+
'C:\\Program Files (x86)\\Python 3.5',
16+
'C:\\Program Files (x64)\\Python 2.7',
17+
'C:\\Program Files (x64)\\Python 3.4',
18+
'C:\\Program Files (x64)\\Python 3.5',
19+
'C:\\Program Files\\Python 2.7',
20+
'C:\\Program Files\\Python 3.4',
21+
'C:\\Program Files\\Python 3.5']
22+
else
23+
return ['/usr/local/bin', '/usr/bin', '/bin', '/usr/sbin', '/sbin']
24+
25+
readDir: (dirPath) ->
26+
try
27+
return fs.readdirSync dirPath
28+
catch
29+
return []
30+
31+
isBinary: (filePath) ->
32+
try
33+
fs.accessSync filePath, fs.X_OK
34+
return true
35+
catch
36+
return false
37+
38+
lookupInterpreters: (dirPath) ->
39+
interpreters = new Set()
40+
files = @readDir(dirPath)
41+
matches = (f for f in files when @pythonExecutableRe.test(f))
42+
for fileName in matches
43+
potentialInterpreter = path.join(dirPath, fileName)
44+
if @isBinary(potentialInterpreter)
45+
interpreters.add(potentialInterpreter)
46+
return interpreters
47+
48+
getInterpreter: ->
49+
userDefinedPythonPaths = []
50+
for p in atom.config.get('autocomplete-python.pythonPaths').split(';')
51+
for project in atom.project.getPaths()
52+
modified = p.replace(/\$PROJECT/i, project)
53+
if modified not in userDefinedPythonPaths
54+
userDefinedPythonPaths.push(modified)
55+
interpreters = (p for p in userDefinedPythonPaths when @isBinary(p))
56+
if interpreters.length > 0
57+
log.debug 'User defined interpreters found', interpreters
58+
return interpreters[0]
59+
60+
log.debug 'No user defined interpreter found, trying automatic lookup'
61+
interpreters = new Set()
62+
63+
for project in atom.project.getPaths()
64+
for f in @readDir(project)
65+
@lookupInterpreters(path.join(project, f, 'bin')).forEach (i) ->
66+
interpreters.add(i)
67+
log.debug 'Project level interpreters found', interpreters
68+
envPath = (process.env.PATH or '').split path.delimiter
69+
envPath = new Set(envPath.concat(@possibleGlobalPythonPaths()))
70+
envPath.forEach (potentialPath) =>
71+
@lookupInterpreters(potentialPath).forEach (i) ->
72+
interpreters.add(i)
73+
log.debug 'Total automatically found interpreters', interpreters
74+
if interpreters.length > 0
75+
return interpreters[0]

‎lib/interpreters-view.coffee‎

Lines changed: 0 additions & 28 deletions
This file was deleted.

‎lib/log.coffee‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports =
2+
prefix: 'autocomplete-python:'
3+
debug: (msg...) ->
4+
if atom.config.get('autocomplete-python.outputDebug')
5+
return console.debug @prefix, msg...
6+
7+
warning: (msg...) ->
8+
return console.warn @prefix, msg...

‎lib/main.coffee‎

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,77 @@ hyperclickProvider = require './hyperclickProvider'
33

44
module.exports =
55
config:
6-
caseInsensitiveCompletion:
7-
type: 'boolean'
8-
default: true
9-
title: 'Case Insensitive Completion'
10-
description: 'The completion is by default case insensitive.'
116
showDescriptions:
127
type: 'boolean'
138
default: true
9+
order: 1
1410
title: 'Show descriptions'
1511
description: 'Show doc strings from functions, classes, etc.'
16-
outputProviderErrors:
17-
type: 'boolean'
18-
default: false
19-
title: 'Output Provider Errors'
20-
description: 'Select if you would like to see the provider errors when they happen. By default they are hidden. Note that critical errors are always shown.'
21-
outputDebug:
22-
type: 'boolean'
23-
default: false
24-
title: 'Output Debug Logs'
25-
description: 'Select if you would like to see debug information in developer tools logs. May slow down your editor.'
2612
useSnippets:
2713
type: 'string'
2814
default: 'none'
15+
order: 2
2916
enum: ['none', 'all', 'required']
3017
title: 'Autocomplete Function Parameters'
31-
description: 'Automatically complete function arguments after typing left parenthesis character. Use completion key to jump between arguments.'
32-
pythonPath:
18+
description: '''Automatically complete function arguments after typing
19+
left parenthesis character. Use completion key to jump between
20+
arguments.'''
21+
pythonPaths:
3322
type: 'string'
3423
default: ''
35-
title: 'Path to python directory'
36-
description: 'Optional. Set it if default values are not working for you or you want to use specific python version. For example: `/usr/local/Cellar/python/2.7.3/bin` or `E:\\Python2.7`'
37-
pythonExecutable:
38-
type: 'string'
39-
default: 'python'
40-
enum: ['python', 'python2', 'python3']
41-
title: 'Python executable name'
42-
description: 'Set it if default values are not working for you or you want to use specific python version.'
24+
order: 3
25+
title: 'Python executable paths'
26+
description: '''Optional semicolon separated list of paths to python
27+
executables (including executable names), where the first one will take
28+
higher priority over the last one. By default autocomplete-python will
29+
automatically look for venvs inside of your project and try to use them
30+
as well as try to find global python executable.
31+
Use $PROJECT variable for project specific paths to point on executables
32+
in virtual environments.
33+
For example: `$PROJECT/env/bin/python3;/usr/bin/python`.
34+
Such config will fall back on `/usr/bin/python` for projects without venv.
35+
If you are using python3 executable while coding for python2 you will get
36+
python2 completions for some built-ins.'''
4337
extraPaths:
4438
type: 'string'
4539
default: ''
40+
order: 4
4641
title: 'Extra PATH'
47-
description: '''Semicolon separated list of modules to additionally include for autocomplete.
48-
You can use $PROJECT variable here to include project specific folders like virtual environment.
42+
description: '''Semicolon separated list of modules to additionally
43+
include for autocomplete. You can use $PROJECT variable here to include
44+
project specific folders like virtual environment.
4945
Note that it still should be valid python package.
5046
For example: $PROJECT/env/lib/python2.7/site-packages.
51-
'''
47+
You don't need to specify extra paths for libraries installed with python
48+
executable you use.'''
49+
caseInsensitiveCompletion:
50+
type: 'boolean'
51+
default: true
52+
order: 5
53+
title: 'Case Insensitive Completion'
54+
description: 'The completion is by default case insensitive.'
5255
fuzzyMatcher:
5356
type: 'boolean'
5457
default: false
58+
order: 6
5559
title: 'Use fuzzy matcher for completions'
56-
description: 'Typing `stdr` will match `stderr`. May significantly slow down completions on slow machines.'
60+
description: '''Typing `stdr` will match `stderr`. May significantly slow
61+
down completions on slow machines.'''
62+
outputProviderErrors:
63+
type: 'boolean'
64+
default: false
65+
order: 6
66+
title: 'Output Provider Errors'
67+
description: '''Select if you would like to see the provider errors when
68+
they happen. By default they are hidden. Note that critical errors are
69+
always shown.'''
70+
outputDebug:
71+
type: 'boolean'
72+
default: false
73+
order: 7
74+
title: 'Output Debug Logs'
75+
description: '''Select if you would like to see debug information in
76+
developer tools logs. May slow down your editor.'''
5777

5878
activate: (state) -> provider.constructor()
5979

0 commit comments

Comments
 (0)