Source

rope_py3k / rope / core.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
from threading import Thread
from Tkinter import *
import tkMessageBox
import tkFileDialog
import tkSimpleDialog

from rope.fileeditor import FileEditor
from rope.editor import GraphicalEditor
from rope.project import Project, FileFinder, PythonFileRunner

class Core(object):
    '''The main class for the IDE'''
    def __init__(self):
        self.root = Tk()
        self.root.title('Rope')
        self.menubar = Menu(self.root, relief=RAISED, borderwidth=1)
        self.root['menu'] = self.menubar
        self._create_menu()

        self.main = Frame(self.root, height='13c', width='26c', relief=RIDGE, bd=2)
        self.editor_list = Frame(self.main, borderwidth=0)
        self.editor_frame = Frame(self.main, borderwidth=0, relief=RIDGE)
        self.status_bar = Frame(self.main, borderwidth=1, relief=RIDGE)
        self.status_text = Label(self.status_bar, text='')
        self.status_text.pack(side=LEFT)

        self.editors = []
        self.active_file_path = StringVar('')
        self.active_editor = None

        self._set_key_binding(self.root)
        self.root.protocol('WM_DELETE_WINDOW', self.exit)
        self.runningThread = Thread(target=self.run)
        self.project = None
    
    def _create_menu(self):
        fileMenu = Menu(self.menubar, tearoff=0)
        self.menubar.add_cascade(label='File', menu=fileMenu, underline=1)
        fileMenu.add_command(label='Open Project ...', command=self._open_project_dialog, underline=6)
        fileMenu.add_command(label='Close Project', command=self.close_project, underline=3)
        fileMenu.add_separator()
        fileMenu.add_command(label='New File ...', command=self._create_new_file_dialog, underline=0)
        fileMenu.add_command(label='New Folder ...', command=self._create_new_folder_dialog, underline=1)
        fileMenu.add_command(label='New Module ...', command=self._create_module_dialog, underline=4)
        fileMenu.add_command(label='New Package ...', command=self._create_package_dialog, underline=4)
        fileMenu.add_separator()
        fileMenu.add_command(label='Open File ...', command=self._open_file_dialog, underline=0)
        fileMenu.add_command(label='Find File ...', command=self._find_file_dialog, underline=0)
        fileMenu.add_separator()
        fileMenu.add_command(label='Exit', command=self.exit, underline=1)

    def _set_key_binding(self, widget):
        widget.bind('<Control-x><Control-f>', self._open_file_dialog)
        widget.bind('<Control-x><Control-n>', self._create_new_file_dialog)
        def _save_active_editor(event):
            self.save_file()
            return 'break'
        widget.bind('<Control-x><Control-s>', _save_active_editor)
        widget.bind('<Control-x><Control-p>', self._open_project_dialog)
        def _exit(event):
            self.exit()
            return 'break'
        widget.bind('<Control-x><Control-c>', _exit)
        widget.bind('<Control-x><Control-d>', self._create_new_folder_dialog)
        widget.bind('<Control-R>', self._find_file_dialog)
        widget.bind('<Control-F11>', self._run_active_editor)
        def _close_active_editor(event):
            self.close_active_editor()
            return 'break'
        widget.bind('<Control-x><k>', _close_active_editor)
        widget.bind('<Control-x><b>', self._change_editor_dialog)
        def do_switch_active_editor(event):
            self.switch_active_editor()
            return 'break'
        widget.bind('<Control-F6>', do_switch_active_editor)


    def _find_file_dialog(self, event=None):
        if not self.project:
            tkMessageBox.showerror(parent=self.root, title='No Open Project',
                                   message='No project is open')
            return
        toplevel = Toplevel()
        toplevel.title('Find Project File')
        find_dialog = Frame(toplevel)
        name_label = Label(find_dialog, text='Name')
        name = Entry(find_dialog)
        found_label = Label(find_dialog, text='Matching Files')
        found = Listbox(find_dialog, selectmode=SINGLE, width=48, height=15)
        scrollbar = Scrollbar(find_dialog, orient=VERTICAL)
        scrollbar['command'] = found.yview
        found.config(yscrollcommand=scrollbar.set)
        file_finder = FileFinder(self.project)
        def name_changed(event):
            if name.get() == '':
                result = ()
            else:
                result = file_finder.find_files_starting_with(name.get())
            found.delete(0, END)
            for file in result:
                found.insert(END, file.get_path())
            if result:
                found.selection_set(0)
        def open_selected():
            selection = found.curselection()
            if selection:
                resource_name = found.get(selection[0])
                self.open_file(resource_name)
                toplevel.destroy()
        def cancel():
            toplevel.destroy()
        name.bind('<Any-KeyRelease>', name_changed)
        name.bind('<Return>', lambda event: open_selected())
        name.bind('<Escape>', lambda event: cancel())
        found.bind('<Return>', lambda event: open_selected())
        found.bind('<Escape>', lambda event: cancel())
        def select_prev(event):
            active = found.index(ACTIVE)
            if active - 1 >= 0:
                found.activate(active - 1)
                found.see(active - 1)
        found.bind('<Control-p>', select_prev)
        def select_next(event):
            active = found.index(ACTIVE)
            if active + 1 < found.size():
                found.activate(active + 1)
                found.see(active + 1)
        found.bind('<Control-n>', select_next)
        name_label.grid(row=0, column=0, columnspan=2)
        name.grid(row=1, column=0, columnspan=2)
        found_label.grid(row=2, column=0, columnspan=2)
        found.grid(row=3, column=0, columnspan=1)
        scrollbar.grid(row=3, column=1, columnspan=1, sticky=N+S)
        find_dialog.grid()
        name.focus_set()
        toplevel.grab_set()
        self.root.wait_window(toplevel)
        if event:
            return 'break'

    def _change_editor_dialog(self, event=None):
        if not self.project:
            tkMessageBox.showerror(parent=self.root, title='No Open Project',
                                   message='No project is open')
            return
        toplevel = Toplevel()
        toplevel.title('Change Editor')
        find_dialog = Frame(toplevel)
        name_label = Label(find_dialog, text='Name')
        name = Entry(find_dialog)
        found_label = Label(find_dialog, text='Editors')
        found = Listbox(find_dialog, selectmode=SINGLE, width=28, height=9)
        scrollbar = Scrollbar(find_dialog, orient=VERTICAL)
        scrollbar['command'] = found.yview
        found.config(yscrollcommand=scrollbar.set)
        for editor in self.editors:
            found.insert(END, editor.get_file().get_name())
        if len(self.editors) >= 2:
            found.selection_set(1)
        def name_changed(event):
            if name.get() == '':
                return
            found.select_clear(0, END)
            found_index = -1
            for index, editor in enumerate(self.editors):
                if editor.get_file().get_name().startswith(name.get()):
                    found_index = index
                    break
            if found_index != -1:
                found.selection_set(found_index)
        def open_selected():
            selection = found.curselection()
            if selection:
                editor = self.editors[int(selection[0])]
                self.activate_editor(editor)
                toplevel.destroy()
        def cancel():
            toplevel.destroy()
        name.bind('<Any-KeyRelease>', name_changed)
        name.bind('<Return>', lambda event: open_selected())
        name.bind('<Escape>', lambda event: cancel())
        found.bind('<Return>', lambda event: open_selected())
        found.bind('<Escape>', lambda event: cancel())
        def select_prev(event):
            active = found.index(ACTIVE)
            if active - 1 >= 0:
                found.activate(active - 1)
                found.see(active - 1)
        found.bind('<Control-p>', select_prev)
        def select_next(event):
            active = found.index(ACTIVE)
            if active + 1 < found.size():
                found.activate(active + 1)
                found.see(active + 1)
        found.bind('<Control-n>', select_next)
        name_label.grid(row=0, column=0, columnspan=2)
        name.grid(row=1, column=0, columnspan=2)
        found_label.grid(row=2, column=0, columnspan=2)
        found.grid(row=3, column=0, columnspan=1)
        scrollbar.grid(row=3, column=1, columnspan=1, sticky=N+S)
        find_dialog.grid()
        name.focus_set()
        toplevel.grab_set()
        self.root.wait_window(toplevel)
        if event:
            return 'break'

    def _create_resource_dialog(self, creation_callback,
                                resource_name='File', parent_name='Parent Folder'):
        '''Ask user about the parent folder and the name of the resource to be created
        
        creation_callback is a function accepting the parent and the name
        '''
        if not self.project:
            tkMessageBox.showerror(parent=self.root, title='No Open Project',
                                   message='No project is open')
            return
        toplevel = Toplevel()
        toplevel.title('New ' + resource_name)
        create_dialog = Frame(toplevel)
        parent_label = Label(create_dialog, text=parent_name)
        parent_entry = Entry(create_dialog)
        resource_label = Label(create_dialog, text=resource_name)
        resource_entry = Entry(create_dialog)
        
        def do_create_resource():
            parent_folder = self.project.get_resource(parent_entry.get())
            creation_callback(parent_folder, resource_entry.get())
            toplevel.destroy()
        def cancel():
            toplevel.destroy()
        parent_entry.bind('<Return>', lambda event: do_create_resource())
        parent_entry.bind('<Escape>', lambda event: cancel())
        resource_entry.bind('<Return>', lambda event: do_create_resource())
        resource_entry.bind('<Escape>', lambda event: cancel())
        parent_label.grid(row=0, column=0)
        parent_entry.grid(row=0, column=1)
        resource_label.grid(row=1, column=0)
        resource_entry.grid(row=1, column=1)
        create_dialog.grid()
        resource_entry.focus_set()
        toplevel.grab_set()
        self.root.wait_window(toplevel)

    def _create_module_dialog(self, event=None):
        def do_create_module(source_folder, module_name):
            new_module = self.project.create_module(source_folder,
                                                    module_name)
            self._open_file_resource(new_module)
        self._create_resource_dialog(do_create_module, 'Module', 'Source Folder')
        if event:
            return 'break'

    def _create_package_dialog(self, event=None):
        def do_create_package(source_folder, package_name):
            new_package = self.project.create_package(source_folder,
                                                      package_name)
            self._open_file_resource(new_package.get_child('__init__.py'))
        self._create_resource_dialog(do_create_package, 'Package', 'Source Folder')
        if event:
            return 'break'

    def _run_active_editor(self, event=None):
        if not self.get_active_editor():
            tkMessageBox.showerror(parent=self.root, title='No Open Editor',
                                   message='No Editor is open.')
            return
        self.run_active_editor()
        return 'break'

    def _open_file_dialog(self, event=None):
        if not self.project:
            tkMessageBox.showerror(parent=self.root, title='No Open Project',
                                   message='No project is open')
            return 'break'
        def doOpen(fileName):
                self.open_file(fileName)
        self._show_open_dialog(doOpen, 'Open File Dialog')
        return 'break'

    def _create_new_file_dialog(self, event=None):
        def do_create_file(parent_folder, file_name):
            new_file = parent_folder.create_file(file_name)
            self._open_file_resource(new_file)
        self._create_resource_dialog(do_create_file, 'File', 'Parent Folder')
        if event:
            return 'break'

    def _create_new_folder_dialog(self, event=None):
        def do_create_folder(parent_folder, folder_name):
            new_folder = parent_folder.create_folder(folder_name)
        self._create_resource_dialog(do_create_folder, 'Folder', 'Parent Folder')
        if event:
            return 'break'

    def _open_project_dialog(self, event=None):
        def doOpen(projectRoot):
            self.open_project(projectRoot)
        directory = tkFileDialog.askdirectory(parent=self.root, title='Open Project')
        if directory:
            doOpen(directory)
        return 'break'

    def _show_open_dialog(self, openCommand, title='Open Dialog'):
        input = tkSimpleDialog.askstring(title, 'Address :', parent=self.root)
        if input:
            try:
                openCommand(input)
            except Exception, e:
                tkMessageBox.showerror(parent=self.root, title='Failed',
                                       message=str(e))

    def start(self):
        self.runningThread.start()

    def run(self):
        self.root.rowconfigure(0, weight=1)
        self.root.columnconfigure(0, weight=1)
        self.main.rowconfigure(0, weight=1)
        self.main.columnconfigure(0, weight=1)
        self.editor_list.pack(fill=BOTH, side=TOP)
        self.editor_frame.pack(fill=BOTH, expand=1)
        self.status_bar.pack(fill=BOTH, side=BOTTOM)
        self.main.pack(fill=BOTH, expand=1)
        self.main.pack_propagate(0)
        self.root.mainloop()

    def open_file(self, fileName):
        if self.project is None:
            raise RopeException('No project is open')
        file = self.project.get_resource(fileName)
        return self._open_file_resource(file)

    def _open_file_resource(self, file):
        for editor in self.editors:
            if editor.get_file() == file:
                editor._rope_title.invoke()
                return editor
        editor = FileEditor(file, GraphicalEditor(self.editor_frame))
        self.editors.append(editor)
        title = Radiobutton(self.editor_list, text=file.get_name(), variable=self.active_file_path,
                            value=file.get_path(), indicatoron=0, bd=2,
                            command=lambda: self.activate_editor(editor),
                            selectcolor='#99A', relief=GROOVE)
        editor._rope_title = title
        title.select()
        title.pack(fill=BOTH, side=LEFT)
        self.activate_editor(editor)
        self._set_key_binding(editor.get_editor().getWidget())
        return editor

    def activate_editor(self, editor):
        if self.get_active_editor():
            self.get_active_editor().get_editor().getWidget().forget()
        editor.get_editor().getWidget().pack(fill=BOTH, expand=1)
        editor.get_editor().getWidget().focus_set()
        editor._rope_title.select()
        self.active_editor = editor
        self.editors.remove(editor)
        self.editors.insert(0, editor)

    def get_active_editor(self):
        return self.active_editor

    def close_active_editor(self):
        if self.active_editor is None:
            return
        self.active_editor.get_editor().getWidget().forget()
        self.editors.remove(self.active_editor)
        self.active_editor._rope_title.forget()
        self.active_editor = None
        if self.editors:
            self.editors[0]._rope_title.invoke()

    def save_file(self):
        activeEditor = self.get_active_editor()
        if activeEditor:
            activeEditor.save()

    def create_file(self, file_name):
        if self.project is None:
            raise RopeException('No project is open')
        try:
            last_slash = file_name.rindex('/')
            parent = project.get_resource(file_name[: last_slash])
            file_name = file_name[last_slash + 1:]
        except ValueError:
            parent = self.project.get_root_folder()
        parent.create_file(file_name)
        return self.open_file(file_name)

    def open_project(self, projectRoot):
        if self.project:
            self.close_project()
        self.project = Project(projectRoot)

    def close_project(self):
        while self.get_active_editor() is not None:
            self.close_active_editor()
        self.project = None

    def create_folder(self, folder_name):
        try:
            last_slash = folder_name.rindex('/')
            parent = project.get_resource(folder_name[: last_slash])
            folder_name = folder_name[last_slash + 1:]
        except ValueError:
            parent = self.project.get_root_folder()
        parent.create_folder(folder_name)

    def exit(self):
        self.root.quit()

    def get_open_project(self):
        return self.project

    def run_active_editor(self):
        activeEditor = self.get_active_editor()
        if activeEditor:
            runner = PythonFileRunner(activeEditor.get_file())
            return runner

    def switch_active_editor(self):
        if len(self.editors) >= 2:
            self.activate_editor(self.editors[1])

    @staticmethod
    def get_core():
        '''Get the singleton instance of Core'''
        if not hasattr(Core, '_core'):
            Core._core = Core()
        return Core._core


class RopeException(Exception):
    '''Base exception for rope'''
    pass