/
plugin.py
243 lines (199 loc) · 6.99 KB
/
plugin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# -*- coding: utf-8 -*-
#
# Copyright © Spyder Project Contributors
# Licensed under the terms of the MIT License
# (see spyder/__init__.py for details)
"""Files and Directories Explorer Plugin"""
# pylint: disable=C0103
# pylint: disable=R0903
# pylint: disable=R0911
# pylint: disable=R0201
# Standard library imports
import os.path as osp
# Third party imports
from qtpy.QtCore import Signal
# Local imports
from spyder.api.translations import get_translation
from spyder.api.plugins import SpyderDockablePlugin, Plugins
from spyder.api.plugin_registration.decorators import on_plugin_available
from spyder.plugins.explorer.widgets.main_widget import ExplorerWidget
from spyder.plugins.explorer.confpage import ExplorerConfigPage
# Localization
_ = get_translation('spyder')
class Explorer(SpyderDockablePlugin):
"""File and Directories Explorer DockWidget."""
NAME = 'explorer'
REQUIRES = [Plugins.Preferences]
OPTIONAL = [Plugins.IPythonConsole, Plugins.Editor]
TABIFY = Plugins.VariableExplorer
WIDGET_CLASS = ExplorerWidget
CONF_SECTION = NAME
CONF_WIDGET_CLASS = ExplorerConfigPage
CONF_FILE = False
DISABLE_ACTIONS_WHEN_HIDDEN = False
# --- Signals
# ------------------------------------------------------------------------
sig_dir_opened = Signal(str)
"""
This signal is emitted to indicate a folder has been opened.
Parameters
----------
directory: str
The opened path directory.
Notes
-----
This will update the current working directory.
"""
sig_file_created = Signal(str)
"""
This signal is emitted to request creating a new file with Spyder.
Parameters
----------
path: str
File path to run.
"""
sig_file_removed = Signal(str)
"""
This signal is emitted when a file is removed.
Parameters
----------
path: str
File path removed.
"""
sig_file_renamed = Signal(str, str)
"""
This signal is emitted when a file is renamed.
Parameters
----------
old_path: str
Old path for renamed file.
new_path: str
New path for renamed file.
"""
sig_open_file_requested = Signal(str)
"""
This signal is emitted to request opening a new file with Spyder.
Parameters
----------
path: str
File path to run.
"""
sig_folder_removed = Signal(str)
"""
This signal is emitted when a folder is removed.
Parameters
----------
path: str
Folder to remove.
"""
sig_folder_renamed = Signal(str)
"""
This signal is emitted when a folder is renamed.
Parameters
----------
path: str
Folder to remove.
"""
sig_interpreter_opened = Signal(str)
"""
This signal is emitted to request opening an interpreter with the given
path as working directory.
Parameters
----------
path: str
Path to use as working directory of interpreter.
"""
sig_module_created = Signal(str)
"""
This signal is emitted to indicate a module has been created.
Parameters
----------
directory: str
The created path directory.
"""
sig_run_requested = Signal(str)
"""
This signal is emitted to request running a file.
Parameters
----------
path: str
File path to run.
"""
# ---- SpyderDockablePlugin API
# ------------------------------------------------------------------------
def get_name(self):
"""Return widget title"""
return _("Files")
def get_description(self):
"""Return the description of the explorer widget."""
return _("Explore files in the computer with a tree view.")
def get_icon(self):
"""Return the explorer icon."""
# TODO: Find a decent icon for the explorer
return self.create_icon('outline_explorer')
def on_initialize(self):
widget = self.get_widget()
# Expose widget signals on the plugin
widget.sig_dir_opened.connect(self.sig_dir_opened)
widget.sig_file_created.connect(self.sig_file_created)
widget.sig_open_file_requested.connect(self.sig_open_file_requested)
widget.sig_open_interpreter_requested.connect(
self.sig_interpreter_opened)
widget.sig_module_created.connect(self.sig_module_created)
widget.sig_removed.connect(self.sig_file_removed)
widget.sig_renamed.connect(self.sig_file_renamed)
widget.sig_run_requested.connect(self.sig_run_requested)
widget.sig_tree_removed.connect(self.sig_folder_removed)
widget.sig_tree_renamed.connect(self.sig_folder_renamed)
@on_plugin_available(plugin=Plugins.Editor)
def on_editor_available(self):
editor = self.get_plugin(Plugins.Editor)
editor.sig_dir_opened.connect(self.chdir)
self.sig_file_created.connect(lambda t: editor.new(text=t))
self.sig_file_removed.connect(editor.removed)
self.sig_file_renamed.connect(editor.renamed)
self.sig_folder_removed.connect(editor.removed_tree)
self.sig_folder_renamed.connect(editor.renamed_tree)
self.sig_module_created.connect(editor.new)
self.sig_open_file_requested.connect(editor.load)
@on_plugin_available(plugin=Plugins.Preferences)
def on_preferences_available(self):
# Add preference config page
preferences = self.get_plugin(Plugins.Preferences)
preferences.register_plugin_preferences(self)
@on_plugin_available(plugin=Plugins.IPythonConsole)
def on_ipython_console_available(self):
ipyconsole = self.get_plugin(Plugins.IPythonConsole)
self.sig_interpreter_opened.connect(
ipyconsole.create_client_from_path)
self.sig_run_requested.connect(
lambda fname:
ipyconsole.run_script(fname, osp.dirname(fname), '', False,
False, False, True, False))
# ---- Public API
# ------------------------------------------------------------------------
def chdir(self, directory, emit=True):
"""
Set working directory.
Parameters
----------
directory: str
The new working directory path.
emit: bool, optional
Emit a signal to indicate the working directory has changed.
Default is True.
"""
self.get_widget().chdir(directory, emit=emit)
def refresh(self, new_path=None, force_current=True):
"""
Refresh history.
Parameters
----------
new_path: str, optional
Path to add to history. Default is None.
force_current: bool, optional
Default is True.
"""
widget = self.get_widget()
widget.update_history(new_path)
widget.refresh(new_path, force_current=force_current)