##// 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 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 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 34 filename = os.path.splitext(filename)
19 35 if filename[1] == ".pyc":
20 36 filename = (filename[0], ".py")
@@ -29,7 +45,12 b' def findFilename(filename):'
29 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 54 line = ''
34 55 if asynchronous:
35 56 line += 'start '
@@ -46,15 +67,17 b' def runCommand(path, command, arguments, asynchronous = True):'
46 67
47 68
48 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 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 77 h = win32console.GetConsoleWindow()
54 78 console_window = win32ui.CreateWindowFromHandle(h)
55 79 console_window.SetForegroundWindow()
56 80
57
58 81
59 82 # This is the most simple example of hook:
60 83 class GVimHook:
@@ -63,21 +86,21 b' class GVimHook:'
63 86 self.wakeup_duration = wakeup_duration
64 87
65 88 def __call__(self, ip, filename, lineno, columnno):
66 filename = findFilename(filename)
89 filename = find_filename(filename)
67 90
68 91 if not filename:
69 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 96 sleep(self.wakeup_duration)
74 97
75 restoreConsoleFocus()
98 restore_console_focus()
76 99
77 100
78 101 def gvim(path = r"C:\Program Files\vim\vim71", wakeup_duration = 100):
79 102 synchronize_with_editor = GVimHook(path, wakeup_duration)
80 setHook(synchronize_with_editor)
103 set_hook(synchronize_with_editor)
81 104
82 105
83 106 class EmacsHook:
@@ -87,24 +110,24 b' class EmacsHook:'
87 110 self.start_duration = start_duration
88 111
89 112 def __call__(self, ip, filename, lineno, columnno):
90 filename = findFilename(filename)
113 filename = find_filename(filename)
91 114
92 115 if not filename:
93 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 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 121 sleep(self.start_duration)
99 122 else:
100 123 sleep(self.wakeup_duration)
101 124
102 restoreConsoleFocus()
125 restore_console_focus()
103 126
104 127
105 128 def emacs(path = r"C:\Program Files\emacs\bin", wakeup_duration = 100, start_duration = 2000):
106 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 133 class SciteHook:
@@ -114,17 +137,17 b' class SciteHook:'
114 137 self.start_duration = start_duration
115 138
116 139 def __call__(self, ip, filename, lineno, columnno):
117 filename = findFilename(filename)
140 filename = find_filename(filename)
118 141
119 142 if not filename:
120 143 return
121 144
122 145 scites = scitedirector.findWindows()
123 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 149 sleep(self.start_duration)
127 restoreConsoleFocus()
150 restore_console_focus()
128 151 else:
129 152 scite = scites[0]
130 153 scitedirector.sendCommand(scite, 'open:%s' % filename.replace("\\", "/"))
@@ -133,7 +156,7 b' class SciteHook:'
133 156
134 157 def scite(path = r"C:\Program Files\SciTE Source Code Editor", wakeup_duration = 100, start_duration = 500):
135 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 162 class NodePadPlusPlusHook:
@@ -142,21 +165,21 b' class NodePadPlusPlusHook:'
142 165 self.wakeup_duration = wakeup_duration
143 166
144 167 def __call__(self, ip, filename, lineno, columnno):
145 filename = findFilename(filename)
168 filename = find_filename(filename)
146 169
147 170 if not filename:
148 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 175 sleep(self.wakeup_duration)
153 176
154 restoreConsoleFocus()
177 restore_console_focus()
155 178
156 179
157 180 def notepadplusplus(path = r"C:\Program Files\Notepad++", wakeup_duration = 100):
158 181 synchronize_with_editor = NodePadPlusPlusHook(path, wakeup_duration)
159 setHook(synchronize_with_editor)
182 set_hook(synchronize_with_editor)
160 183
161 184
162 185 class PsPadHook:
@@ -165,21 +188,21 b' class PsPadHook:'
165 188 self.wakeup_duration = wakeup_duration
166 189
167 190 def __call__(self, ip, filename, lineno, columnno):
168 filename = findFilename(filename)
191 filename = find_filename(filename)
169 192
170 193 if not filename:
171 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 198 sleep(self.wakeup_duration)
176 199
177 restoreConsoleFocus()
200 restore_console_focus()
178 201
179 202
180 203 def pspad(path = r"C:\Program Files\PSPad editor", wakeup_duration = 100):
181 204 synchronize_with_editor = PsPadHook(path, wakeup_duration)
182 setHook(synchronize_with_editor)
205 set_hook(synchronize_with_editor)
183 206
184 207
185 208 # This is an example of DDE hook:
@@ -190,7 +213,7 b' class UltraEditHook:'
190 213 self.start_duration = start_duration
191 214
192 215 def __call__(self, ip, filename, lineno, columnno):
193 filename = findFilename(filename)
216 filename = find_filename(filename)
194 217
195 218 if not filename:
196 219 return
@@ -204,16 +227,16 b' class UltraEditHook:'
204 227
205 228 sleep(self.wakeup_duration)
206 229 except:
207 runCommand(self.path, 'uedit32', '"%s/%d"' % (filename, lineno))
230 run_command(self.path, 'uedit32', '"%s/%d"' % (filename, lineno))
208 231
209 232 sleep(self.start_duration)
210 233
211 234 server.Shutdown()
212 235
213 restoreConsoleFocus()
236 restore_console_focus()
214 237
215 238
216 239 def ultraedit(path = r"C:\Program Files\IDM Computer Solutions\UltraEdit-32", wakeup_duration = 10, start_duration = 2000):
217 240 synchronize_with_editor = UltraEditHook(path, wakeup_duration, start_duration)
218 setHook(synchronize_with_editor)
241 set_hook(synchronize_with_editor)
219 242 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now