1 | import os |
||
2 | import subprocess |
||
3 | import sys |
||
4 | |||
5 | import json |
||
6 | |||
7 | import sublime |
||
8 | import sublime_plugin |
||
9 | |||
10 | |||
11 | class CondaCommand(sublime_plugin.WindowCommand): |
||
12 | """Contains all of the attributes that will be inherited by other commands.""" |
||
13 | |||
14 | @property |
||
15 | def settings(self): |
||
16 | """Load the plugin settings for commands to use.""" |
||
17 | return sublime.load_settings('conda.sublime-settings') |
||
18 | |||
19 | @property |
||
20 | def executable(self): |
||
21 | """Retrieve the python executable path from settings.""" |
||
22 | return os.path.expanduser(self.settings.get('executable')) |
||
23 | |||
24 | @property |
||
25 | def configuration(self): |
||
26 | """Retrieve the conda configuration file from settings.""" |
||
27 | return os.path.expanduser(self.settings.get('configuration')) |
||
28 | |||
29 | @property |
||
30 | def root_directory(self): |
||
31 | """Retrieve the directory of conda's root environment.""" |
||
32 | if sys.platform == 'win32': |
||
33 | root_directory = os.path.dirname(self.executable) |
||
34 | else: |
||
35 | root_directory = os.path.dirname(self.executable).replace('bin', '') |
||
36 | |||
37 | return root_directory |
||
38 | |||
39 | @property |
||
40 | def conda_environments(self): |
||
41 | """Find all conda environments in the specified directory.""" |
||
42 | try: |
||
43 | directory = os.path.expanduser(self.settings.get('environment_directory')) |
||
44 | |||
45 | environments = [['root', self.root_directory]] |
||
46 | environments.extend([[environment, os.path.join(directory, environment)] |
||
47 | for environment in os.listdir(directory)]) |
||
48 | |||
49 | return environments |
||
50 | |||
51 | except FileNotFoundError: |
||
52 | return [['root', self.root_directory]] |
||
53 | |||
54 | @property |
||
55 | def project_data(self): |
||
56 | """Retrieve the project data to be used in the current window.""" |
||
57 | if self.window.project_data() is None: |
||
58 | return {} |
||
59 | else: |
||
60 | return self.window.project_data() |
||
61 | |||
62 | @property |
||
63 | def startupinfo(self): |
||
64 | """Property used to hide command prompts when on Windows platforms.""" |
||
65 | startupinfo = None |
||
66 | |||
67 | if sys.platform == 'win32': |
||
68 | startupinfo = subprocess.STARTUPINFO() |
||
69 | |||
70 | if sys.version_info.major == 3: |
||
71 | startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW |
||
72 | else: |
||
73 | startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW |
||
74 | |||
75 | return startupinfo |
||
76 | |||
77 | def retrieve_environment_name(self, path): |
||
78 | """Retrieve the environment name from the active environment path. |
||
79 | |||
80 | If the active environment is the root environment, 'root' must be |
||
81 | returned instead of the basename from the environment path. |
||
82 | """ |
||
83 | if path == self.root_directory: |
||
84 | return 'root' |
||
85 | else: |
||
86 | return os.path.basename(path) |
||
87 | |||
88 | |||
89 | class CreateCondaEnvironmentCommand(CondaCommand): |
||
90 | """Contains the methods needed to create a conda environment.""" |
||
91 | |||
92 | def run(self): |
||
93 | """Display 'Conda: Create' in Sublime Text's command palette. |
||
94 | |||
95 | When 'Conda: Create' is clicked by the user, Sublime's text input |
||
96 | box will show allowing the user to input the name of environment. |
||
97 | This environment name is then passed to the create_environment |
||
98 | method. |
||
99 | """ |
||
100 | self.window.show_input_panel('Conda Environment Name:', '', |
||
101 | self.retrieve_python_version, None, None) |
||
102 | |||
103 | def retrieve_python_version(self, environment): |
||
104 | """Display a list of available Python versions for the environment. |
||
105 | |||
106 | Forcing the user to select the Python version allows conda to create |
||
107 | a new Python executable inside the environment directory. |
||
108 | """ |
||
109 | self.environment = environment |
||
110 | |||
111 | python_versions = ['Python 2.7', 'Python 3.5', 'Python 3.6'] |
||
112 | |||
113 | self.window.show_quick_panel(python_versions, self.create_environment) |
||
114 | |||
115 | def create_environment(self, index): |
||
116 | """Create a conda environment in the envs directory.""" |
||
117 | if index != -1: |
||
118 | if index == 0: |
||
119 | python_version = 'python=2.7' |
||
120 | elif index == 1: |
||
121 | python_version = 'python=3.5' |
||
122 | else: |
||
123 | python_version = 'python=3.6' |
||
124 | |||
125 | cmd = [self.executable, '-m', 'conda', 'create', |
||
126 | '--name', self.environment, python_version, '-y', '-q'] |
||
127 | |||
128 | self.window.run_command('exec', {'cmd': cmd}) |
||
129 | |||
130 | |||
131 | class RemoveCondaEnvironmentCommand(CondaCommand): |
||
132 | """Contains the methods needed to remove a conda environment.""" |
||
133 | |||
134 | def run(self): |
||
135 | """Display 'Conda: Remove' in Sublime Text's command palette. |
||
136 | |||
137 | When 'Conda: Removed' is clicked by the user, the command |
||
138 | palette whill show all conda environments available for removal. |
||
139 | The index of the selected environment is then passed to the |
||
140 | remove_environment method" |
||
141 | """ |
||
142 | self.window.show_quick_panel(self.conda_environments, |
||
143 | self.remove_environment) |
||
144 | |||
145 | def remove_environment(self, index): |
||
146 | """Remove a conda environment from the envs directory.""" |
||
147 | if index != -1: |
||
148 | environment = self.conda_environments[index][0] |
||
149 | |||
150 | cmd = [self.executable, '-m', 'conda', 'remove', |
||
151 | '--name', environment, '--all', '-y', '-q'] |
||
152 | |||
153 | self.window.run_command('exec', {'cmd': cmd}) |
||
154 | |||
155 | |||
156 | class ListCondaEnvironmentCommand(CondaCommand): |
||
157 | """Contains the methods needed to list available conda environments.""" |
||
158 | |||
159 | def run(self): |
||
160 | """Display 'Conda: List' in Sublime Text's command palette. |
||
161 | |||
162 | When 'Conda: List' is clicked by the user, the command |
||
163 | palette will show all available conda environments. |
||
164 | """ |
||
165 | self.window.show_quick_panel(self.conda_environments, |
||
166 | None) |
||
167 | |||
168 | |||
169 | class ActivateCondaEnvironmentCommand(CondaCommand): |
||
170 | """Contains the methods needed to activate a conda environment.""" |
||
171 | |||
172 | def run(self): |
||
173 | """Display 'Conda: Activate' in Sublime Text's command palette. |
||
174 | |||
175 | When 'Conda: Activate' is clicked by the user, the command |
||
176 | palette will show all available conda environments. The |
||
177 | clicked environment will be activated as the current environment. |
||
178 | """ |
||
179 | self.window.show_quick_panel(self.conda_environments, |
||
180 | self.activate_environment) |
||
181 | |||
182 | def activate_environment(self, index): |
||
183 | """Activate the environment selected from the command palette.""" |
||
184 | if index != -1: |
||
185 | project_data = self.project_data |
||
186 | |||
187 | project_data['conda_environment'] = self.conda_environments[index][1] |
||
188 | |||
189 | self.window.set_project_data(project_data) |
||
190 | |||
191 | sublime.status_message('Activated conda environment: {}' |
||
192 | .format(self.conda_environments[index][0])) |
||
193 | |||
194 | |||
195 | class DeactivateCondaEnvironmentCommand(CondaCommand): |
||
196 | """Contains the methods needed to deactivate a conda environment.""" |
||
197 | |||
198 | def run(self): |
||
199 | """Display 'Conda: Deactivate' in Sublime Text's command palette. |
||
200 | |||
201 | When 'Conda: Deactivate' is clicked by the user, the command |
||
202 | palette will show all available conda environments. The |
||
203 | clicked environment will be deactivated. |
||
204 | """ |
||
205 | self.window.show_quick_panel(self.active_environment, |
||
206 | self.deactivate_environment) |
||
207 | |||
208 | @property |
||
209 | def active_environment(self): |
||
210 | """Retrieve the active conda environment.""" |
||
211 | try: |
||
212 | environment_path = self.project_data['conda_environment'] |
||
213 | environment_name = self.retrieve_environment_name(environment_path) |
||
214 | |||
215 | return [[environment_name, os.path.dirname(environment_path)]] |
||
216 | |||
217 | except KeyError: |
||
218 | return ['No Active Conda Environment'] |
||
219 | |||
220 | def deactivate_environment(self, index): |
||
221 | """Deactivate the environment selected in the command palette.""" |
||
222 | if index != -1: |
||
223 | try: |
||
224 | project_data = self.project_data |
||
225 | |||
226 | del project_data['conda_environment'] |
||
227 | |||
228 | self.window.set_project_data(project_data) |
||
229 | |||
230 | sublime.status_message('Deactivated conda environment: {}' |
||
231 | .format(self.conda_environments[index][0])) |
||
232 | except KeyError: |
||
233 | sublime.status_message('No active conda environment') |
||
234 | |||
235 | |||
236 | class ListCondaPackageCommand(CondaCommand): |
||
237 | """Contains all of the methods needed to list all installed packages.""" |
||
238 | |||
239 | def run(self): |
||
240 | """Display 'Conda: List' in Sublime Text's command palette. |
||
241 | |||
242 | When 'Conda: List' is clicked by the user, the build output |
||
243 | displays all packages installed in the current environment. |
||
244 | """ |
||
245 | self.window.show_quick_panel(self.environment_packages, None) |
||
246 | |||
247 | View Code Duplication | @property |
|
0 ignored issues
–
show
Duplication
introduced
by
Loading history...
|
|||
248 | def environment_packages(self): |
||
249 | """List each package name and version installed in the environment.""" |
||
250 | try: |
||
251 | environment_path = self.project_data['conda_environment'] |
||
252 | environment = self.retrieve_environment_name(environment_path) |
||
253 | |||
254 | package_data = subprocess.check_output([self.executable, '-m', 'conda', 'list', |
||
255 | '--name', environment, '--json'], |
||
256 | startupinfo=self.startupinfo) |
||
257 | |||
258 | packages_info = json.loads(package_data.decode()) |
||
259 | packages = [[package['name'], package['dist_name']] |
||
260 | for package in packages_info] |
||
261 | |||
262 | return packages |
||
263 | except KeyError: |
||
264 | return ['No Active Conda Environment'] |
||
265 | |||
266 | |||
267 | class InstallCondaPackageCommand(CondaCommand): |
||
268 | """Contains all of the methods needed to install a conda package.""" |
||
269 | |||
270 | def run(self): |
||
271 | """Display an input box allowing the user to input a package name.""" |
||
272 | self.window.show_input_panel('Package Name:', '', self.install_package, |
||
273 | None, None) |
||
274 | |||
275 | def install_package(self, package): |
||
276 | """Install the given package name via conda.""" |
||
277 | try: |
||
278 | environment_path = self.project_data['conda_environment'] |
||
279 | environment = self.retrieve_environment_name(environment_path) |
||
280 | cmd = [self.executable, '-m', 'conda', 'install', package, |
||
281 | '--name', environment, '-y', '-q'] |
||
282 | self.window.run_command('exec', {'cmd': cmd}) |
||
283 | except KeyError: |
||
284 | sublime.status_message('No active conda environment.') |
||
285 | |||
286 | |||
287 | class RemoveCondaPackageCommand(CondaCommand): |
||
288 | """Contains all of the methods needed to remove a conda package.""" |
||
289 | |||
290 | def run(self): |
||
291 | """Display an input box allowing the user to pick a package to remove.""" |
||
292 | self.window.show_quick_panel(self.environment_packages, self.remove_package) |
||
293 | |||
294 | View Code Duplication | @property |
|
0 ignored issues
–
show
|
|||
295 | def environment_packages(self): |
||
296 | """List each package name and version installed in the environment. |
||
297 | |||
298 | This property had to be duplicated as the attribute from |
||
299 | ListCondaPackageCommand could not be inherited properly. |
||
300 | """ |
||
301 | try: |
||
302 | environment_path = self.project_data['conda_environment'] |
||
303 | environment = self.retrieve_environment_name(environment_path) |
||
304 | |||
305 | package_data = subprocess.check_output([self.executable, '-m', 'conda', 'list', |
||
306 | '--name', environment, '--json'], |
||
307 | startupinfo=self.startupinfo) |
||
308 | |||
309 | packages_info = json.loads(package_data.decode()) |
||
310 | packages = [[package['name'], package['dist_name']] |
||
311 | for package in packages_info] |
||
312 | |||
313 | return packages |
||
314 | except KeyError: |
||
315 | return ['No Active Conda Environment'] |
||
316 | |||
317 | def remove_package(self, index): |
||
318 | """Remove the given package name via conda.""" |
||
319 | if index != -1: |
||
320 | package_to_remove = self.environment_packages[index][0] |
||
321 | |||
322 | environment_path = self.project_data['conda_environment'] |
||
323 | |||
324 | environment = self.retrieve_environment_name(environment_path) |
||
325 | |||
326 | cmd = [self.executable, '-m', 'conda', 'remove', package_to_remove, |
||
327 | '--name', environment, '-y', '-q'] |
||
328 | |||
329 | self.window.run_command('exec', {'cmd': cmd}) |
||
330 | |||
331 | |||
332 | class ListCondaChannelsCommand(CondaCommand): |
||
333 | """Contains all of the methods needed to display conda's channel sources.""" |
||
334 | |||
335 | def run(self): |
||
336 | """Display 'Conda: List Channel Sources' in Sublime Text's command palette. |
||
337 | |||
338 | When 'Conda: List Channel Sources' is clicked by the user, |
||
339 | the command palette displays all of the channel sources found |
||
340 | in the condarc configuration file. |
||
341 | """ |
||
342 | self.window.show_quick_panel(self.channel_sources, None) |
||
343 | |||
344 | @property |
||
345 | def channel_sources(self): |
||
346 | """List each channel source found in the condarc configuration file.""" |
||
347 | sources = subprocess.check_output([self.executable, '-m', 'conda', 'config', |
||
348 | '--show-sources', '--json'], |
||
349 | startupinfo=self.startupinfo) |
||
350 | sources = json.loads(sources.decode()) |
||
351 | |||
352 | try: |
||
353 | return sources[self.configuration]['channels'] |
||
354 | except KeyError: |
||
355 | return ['No Channel Sources Available'] |
||
356 | |||
357 | |||
358 | class SearchCondaPackageCommand(CondaCommand): |
||
359 | """Contains all of the methods needed to search for a conda package.""" |
||
360 | |||
361 | def run(self): |
||
362 | """Display an input box allowing the user to input a package name.""" |
||
363 | self.window.show_input_panel('Package Name:', '', self.search_package, |
||
364 | None, None) |
||
365 | |||
366 | def search_package(self, package): |
||
367 | """Search for a package included in the defaults channel.""" |
||
368 | cmd = [self.executable, '-m', 'conda', 'search', package] |
||
369 | self.window.run_command('exec', {'cmd': cmd}) |
||
370 | |||
371 | |||
372 | class AddCondaChannelCommand(CondaCommand): |
||
373 | """Contains all of the methods needed to add a conda channel source.""" |
||
374 | |||
375 | def run(self): |
||
376 | """Display 'Conda: Add Channel Source' in Sublime Text's command palette. |
||
377 | |||
378 | When 'Conda: Add Channel Source' is clicked by the user, |
||
379 | an input box will show allowing the user to type the name |
||
380 | of the channel to add. |
||
381 | """ |
||
382 | self.window.show_input_panel('Conda Channel Name:', '', |
||
383 | self.add_channel, None, None) |
||
384 | |||
385 | def add_channel(self, channel): |
||
386 | """Add the given channel to the condarc configuration file.""" |
||
387 | cmd = [self.executable, '-m', 'conda', 'config', '--add', |
||
388 | 'channels', channel] |
||
389 | |||
390 | self.window.run_command('exec', {'cmd': cmd}) |
||
391 | |||
392 | |||
393 | class RemoveCondaChannelCommand(CondaCommand): |
||
394 | """Contains all of the methods needed to remove a conda channel source.""" |
||
395 | |||
396 | def run(self): |
||
397 | """Display 'Conda: Remove Channel Source' in Sublime Text's command palette. |
||
398 | |||
399 | When 'Conda: Remove Channel Source' is clicked by the user, |
||
400 | the command palette will show a list of channel sources |
||
401 | available to be removed by the user. |
||
402 | """ |
||
403 | self.window.show_quick_panel(self.channel_sources, self.remove_channel) |
||
404 | |||
405 | @property |
||
406 | def channel_sources(self): |
||
407 | """List each channel source found in the condarc configuration file. |
||
408 | |||
409 | This property had to be duplicated as the attribute from |
||
410 | ListCondaChannelCommand could not be inherited properly. |
||
411 | """ |
||
412 | sources = subprocess.check_output([self.executable, '-m', 'conda', 'config', |
||
413 | '--show-sources', '--json'], |
||
414 | startupinfo=self.startupinfo) |
||
415 | sources = json.loads(sources.decode()) |
||
416 | |||
417 | try: |
||
418 | return sources[self.configuration]['channels'] |
||
419 | except KeyError: |
||
420 | return ['No Channel Sources Available'] |
||
421 | |||
422 | def remove_channel(self, index): |
||
423 | """Remove a channel from the condarc configuration file.""" |
||
424 | if index != -1: |
||
425 | channel = self.channel_sources[index] |
||
426 | |||
427 | cmd = [self.executable, '-m', 'conda', 'config', '--remove', |
||
428 | 'channels', channel] |
||
429 | |||
430 | self.window.run_command('exec', {'cmd': cmd}) |
||
431 | |||
432 | |||
433 | class ExecuteCondaEnvironmentCommand(CondaCommand): |
||
434 | """Override Sublime Text's default ExecCommand with a targeted build.""" |
||
435 | |||
436 | def run(self, **kwargs): |
||
437 | """Run the current Python file with the conda environment's Python executable. |
||
438 | |||
439 | The activated conda environment is retrieved from the Sublime Text |
||
440 | window project data. The Python executable found in the conda |
||
441 | environment's bin directory is used to build the file. |
||
442 | """ |
||
443 | try: |
||
444 | environment = self.project_data['conda_environment'] |
||
445 | |||
446 | if sys.platform == 'win32': |
||
447 | executable_path = '{}\\python' .format(environment) |
||
448 | else: |
||
449 | executable_path = '{}/bin/python' .format(environment) |
||
450 | |||
451 | kwargs['cmd'][0] = os.path.normpath(executable_path) |
||
452 | except KeyError: |
||
453 | pass |
||
454 | |||
455 | self.window.run_command('exec', kwargs) |
||
456 |