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