##// END OF EJS Templates
Add Doc string and conform to PEP 8
vds -
Show More
@@ -10,11 +10,27 b' import scitedirector'
10
10
11 # test to write.
11 # test to write.
12
12
13 def setHook(synchronize_with_editor):
13 def set_hook(synchronize_with_editor):
14 """Set the synchronize with editor hook with a callable object.
15
16 The callable object will be called with the following arguments when
17 IPython wants to synchronize with you favorite editor:
18
19 - ip: a running IPython instance.
20
21 - filename: the path of the file the editor is supposed to display.
22
23 - lineno : the line number of the line the editor is supposed to
24 highlight.
25
26 - columnno : the column number of the character the editor is supposed
27 to highlight.
28 """
14 ip.set_hook("synchronize_with_editor", synchronize_with_editor)
29 ip.set_hook("synchronize_with_editor", synchronize_with_editor)
15
30
16
31
17 def findFilename(filename):
32 def find_filename(filename):
33 """Return the filename to synchronize with based on """
18 filename = os.path.splitext(filename)
34 filename = os.path.splitext(filename)
19 if filename[1] == ".pyc":
35 if filename[1] == ".pyc":
20 filename = (filename[0], ".py")
36 filename = (filename[0], ".py")
@@ -29,7 +45,12 b' def findFilename(filename):'
29 return ""
45 return ""
30
46
31
47
32 def runCommand(path, command, arguments, asynchronous = True):
48 def run_command(path, command, arguments, asynchronous = True):
49 """Run a shell command and return the exit code of the command"""
50 # This is a thin wrapper around os.system that:
51 # - Let you run command asynchronously.
52 # - Accept spaces in command path.
53 # - Dont throw exception if the command don't exist.
33 line = ''
54 line = ''
34 if asynchronous:
55 if asynchronous:
35 line += 'start '
56 line += 'start '
@@ -46,15 +67,17 b' def runCommand(path, command, arguments, asynchronous = True):'
46
67
47
68
48 def sleep(milliseconds):
69 def sleep(milliseconds):
70 """Wait some milliseconds."""
71 # This is used to make sure the editor did its job before we reset the focus on the console.
49 win32api.Sleep(milliseconds)
72 win32api.Sleep(milliseconds)
50
73
51
74
52 def restoreConsoleFocus():
75 def restore_console_focus():
76 """Restore the focus to the IPython console."""
53 h = win32console.GetConsoleWindow()
77 h = win32console.GetConsoleWindow()
54 console_window = win32ui.CreateWindowFromHandle(h)
78 console_window = win32ui.CreateWindowFromHandle(h)
55 console_window.SetForegroundWindow()
79 console_window.SetForegroundWindow()
56
80
57
58
81
59 # This is the most simple example of hook:
82 # This is the most simple example of hook:
60 class GVimHook:
83 class GVimHook:
@@ -63,21 +86,21 b' class GVimHook:'
63 self.wakeup_duration = wakeup_duration
86 self.wakeup_duration = wakeup_duration
64
87
65 def __call__(self, ip, filename, lineno, columnno):
88 def __call__(self, ip, filename, lineno, columnno):
66 filename = findFilename(filename)
89 filename = find_filename(filename)
67
90
68 if not filename:
91 if not filename:
69 return
92 return
70
93
71 runCommand(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename))
94 run_command(self.path, 'gvim', '--remote-silent +%d "%s"' % (lineno, filename))
72
95
73 sleep(self.wakeup_duration)
96 sleep(self.wakeup_duration)
74
97
75 restoreConsoleFocus()
98 restore_console_focus()
76
99
77
100
78 def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100):
101 def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100):
79 synchronize_with_editor = GVimHook(path, wakeup_duration)
102 synchronize_with_editor = GVimHook(path, wakeup_duration)
80 setHook(synchronize_with_editor)
103 set_hook(synchronize_with_editor)
81
104
82
105
83 class EmacsHook:
106 class EmacsHook:
@@ -87,24 +110,24 b' class EmacsHook:'
87 self.start_duration = start_duration
110 self.start_duration = start_duration
88
111
89 def __call__(self, ip, filename, lineno, columnno):
112 def __call__(self, ip, filename, lineno, columnno):
90 filename = findFilename(filename)
113 filename = find_filename(filename)
91
114
92 if not filename:
115 if not filename:
93 return
116 return
94
117
95 r = runCommand(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False)
118 r = run_command(self.path, "emacsclient", '-n +%d:%d "%s" 2>nul' % (lineno, columnno, filename), False)
96 if r != 0:
119 if r != 0:
97 runCommand(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename))
120 run_command(self.path, 'runemacs', '--quick -f server-start +%d:%d "%s"' % (lineno, columnno, filename))
98 sleep(self.start_duration)
121 sleep(self.start_duration)
99 else:
122 else:
100 sleep(self.wakeup_duration)
123 sleep(self.wakeup_duration)
101
124
102 restoreConsoleFocus()
125 restore_console_focus()
103
126
104
127
105 def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000):
128 def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000):
106 synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration)
129 synchronize_with_editor = EmacsHook(path, wakeup_duration, start_duration)
107 setHook(synchronize_with_editor)
130 set_hook(synchronize_with_editor)
108
131
109
132
110 class SciteHook:
133 class SciteHook:
@@ -114,17 +137,17 b' class SciteHook:'
114 self.start_duration = start_duration
137 self.start_duration = start_duration
115
138
116 def __call__(self, ip, filename, lineno, columnno):
139 def __call__(self, ip, filename, lineno, columnno):
117 filename = findFilename(filename)
140 filename = find_filename(filename)
118
141
119 if not filename:
142 if not filename:
120 return
143 return
121
144
122 scites = scitedirector.findWindows()
145 scites = scitedirector.findWindows()
123 if not scites:
146 if not scites:
124 runCommand(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno))
147 run_command(self.path, "scite", '"-open:%s" -goto:%d' % (filename.replace("\\", "/"), lineno))
125
148
126 sleep(self.start_duration)
149 sleep(self.start_duration)
127 restoreConsoleFocus()
150 restore_console_focus()
128 else:
151 else:
129 scite = scites[0]
152 scite = scites[0]
130 scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/"))
153 scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/"))
@@ -133,7 +156,7 b' class SciteHook:'
133
156
134 def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500):
157 def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500):
135 synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration)
158 synchronize_with_editor = SciteHook(path, wakeup_duration, start_duration)
136 setHook(synchronize_with_editor)
159 set_hook(synchronize_with_editor)
137
160
138
161
139 class NodePadPlusPlusHook:
162 class NodePadPlusPlusHook:
@@ -142,21 +165,21 b' class NodePadPlusPlusHook:'
142 self.wakeup_duration = wakeup_duration
165 self.wakeup_duration = wakeup_duration
143
166
144 def __call__(self, ip, filename, lineno, columnno):
167 def __call__(self, ip, filename, lineno, columnno):
145 filename = findFilename(filename)
168 filename = find_filename(filename)
146
169
147 if not filename:
170 if not filename:
148 return
171 return
149
172
150 runCommand(self.path, "notepad++", '"%s" -n%d' % (filename, lineno))
173 run_command(self.path, "notepad++", '"%s" -n%d' % (filename, lineno))
151
174
152 sleep(self.wakeup_duration)
175 sleep(self.wakeup_duration)
153
176
154 restoreConsoleFocus()
177 restore_console_focus()
155
178
156
179
157 def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100):
180 def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100):
158 synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration)
181 synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration)
159 setHook(synchronize_with_editor)
182 set_hook(synchronize_with_editor)
160
183
161
184
162 class PsPadHook:
185 class PsPadHook:
@@ -165,21 +188,21 b' class PsPadHook:'
165 self.wakeup_duration = wakeup_duration
188 self.wakeup_duration = wakeup_duration
166
189
167 def __call__(self, ip, filename, lineno, columnno):
190 def __call__(self, ip, filename, lineno, columnno):
168 filename = findFilename(filename)
191 filename = find_filename(filename)
169
192
170 if not filename:
193 if not filename:
171 return
194 return
172
195
173 runCommand(self.path, "pspad", '"%s" -%d' % (filename, lineno))
196 run_command(self.path, "pspad", '"%s" -%d' % (filename, lineno))
174
197
175 sleep(self.wakeup_duration)
198 sleep(self.wakeup_duration)
176
199
177 restoreConsoleFocus()
200 restore_console_focus()
178
201
179
202
180 def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100):
203 def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100):
181 synchronize_with_editor = PsPadHook(path, wakeup_duration)
204 synchronize_with_editor = PsPadHook(path, wakeup_duration)
182 setHook(synchronize_with_editor)
205 set_hook(synchronize_with_editor)
183
206
184
207
185 # This is an example of DDE hook:
208 # This is an example of DDE hook:
@@ -190,7 +213,7 b' class UltraEditHook:'
190 self.start_duration = start_duration
213 self.start_duration = start_duration
191
214
192 def __call__(self, ip, filename, lineno, columnno):
215 def __call__(self, ip, filename, lineno, columnno):
193 filename = findFilename(filename)
216 filename = find_filename(filename)
194
217
195 if not filename:
218 if not filename:
196 return
219 return
@@ -204,16 +227,16 b' class UltraEditHook:'
204
227
205 sleep(self.wakeup_duration)
228 sleep(self.wakeup_duration)
206 except:
229 except:
207 runCommand(self.path, 'uedit32', '"%s/%d"' % (filename, lineno))
230 run_command(self.path, 'uedit32', '"%s/%d"' % (filename, lineno))
208
231
209 sleep(self.start_duration)
232 sleep(self.start_duration)
210
233
211 server.Shutdown()
234 server.Shutdown()
212
235
213 restoreConsoleFocus()
236 restore_console_focus()
214
237
215
238
216 def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000):
239 def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000):
217 synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration)
240 synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration)
218 setHook(synchronize_with_editor)
241 set_hook(synchronize_with_editor)
219 No newline at end of file
242
General Comments 0
You need to be logged in to leave comments. Login now