Show More
@@ -177,3 +177,70 b' def inote(ip,arg):' | |||||
177 | ip.IP.hooks.editor(fname) |
|
177 | ip.IP.hooks.editor(fname) | |
178 |
|
178 | |||
179 | ip.defalias("inote",inote) |
|
179 | ip.defalias("inote",inote) | |
|
180 | ||||
|
181 | def pathobj_mangle(p): | |||
|
182 | return p.replace(' ', '__').replace('.','DOT') | |||
|
183 | def pathobj_unmangle(s): | |||
|
184 | return s.replace('__',' ').replace('DOT','.') | |||
|
185 | ||||
|
186 | ||||
|
187 | class FileObj: | |||
|
188 | def __init__(self,p): | |||
|
189 | self.path = p | |||
|
190 | def __call__(self): | |||
|
191 | os.startfile(self.path) | |||
|
192 | def __str__(self): | |||
|
193 | return self.path | |||
|
194 | ||||
|
195 | class PathObj: | |||
|
196 | def __init__(self,p): | |||
|
197 | self.path = p | |||
|
198 | if os.path.isdir(p): | |||
|
199 | self.ents = None | |||
|
200 | self.ents = [pathobj_mangle(ent) for ent in os.listdir(p)] | |||
|
201 | def __complete__(self): | |||
|
202 | if self.ents: | |||
|
203 | return self.ents | |||
|
204 | return None | |||
|
205 | def __getattr__(self,name): | |||
|
206 | if name in self.ents: | |||
|
207 | if self.path.endswith('/'): | |||
|
208 | sep = '' | |||
|
209 | else: | |||
|
210 | sep = '/' | |||
|
211 | ||||
|
212 | tgt = self.path + sep + pathobj_unmangle(name) | |||
|
213 | #print "tgt",tgt | |||
|
214 | if os.path.isdir(tgt): | |||
|
215 | return PathObj(tgt) | |||
|
216 | if os.path.isfile(tgt): | |||
|
217 | return FileObj(tgt) | |||
|
218 | ||||
|
219 | raise AttributeError, name # <<< DON'T FORGET THIS LINE !! | |||
|
220 | def __str__(self): | |||
|
221 | return self.path | |||
|
222 | ||||
|
223 | def __repr__(self): | |||
|
224 | return "<PathObj to %s>" % self.path | |||
|
225 | ||||
|
226 | def __call__(self): | |||
|
227 | print "cd:",self.path | |||
|
228 | os.chdir(self.path) | |||
|
229 | ||||
|
230 | def complete_pathobj(obj, prev_completions): | |||
|
231 | ||||
|
232 | if hasattr(obj,'__complete__'): | |||
|
233 | return obj.__complete__() | |||
|
234 | raise TryNext | |||
|
235 | ||||
|
236 | complete_pathobj = IPython.generics.complete_object.when_type(PathObj)(complete_pathobj) | |||
|
237 | ||||
|
238 | def test_pathobj(): | |||
|
239 | #p = PathObj('c:/prj') | |||
|
240 | #p2 = p.cgi | |||
|
241 | #print p,p2 | |||
|
242 | croot = PathObj("c:/") | |||
|
243 | startmenu = PathObj("d:/Documents and Settings/All Users/Start Menu/Programs") | |||
|
244 | ip.to_user_ns("croot startmenu") | |||
|
245 | ||||
|
246 | #test_pathobj() No newline at end of file |
@@ -6,6 +6,10 b'' | |||||
6 | * Debugger.py: Change to PSF license |
|
6 | * Debugger.py: Change to PSF license | |
7 |
|
7 | |||
8 | * simplegeneric.py: Add license & author notes. |
|
8 | * simplegeneric.py: Add license & author notes. | |
|
9 | ||||
|
10 | * ipy_fsops.py: Added PathObj and FileObj, an object-oriented way | |||
|
11 | to navigate file system with a custom completer. Run | |||
|
12 | ipy_fsops.test_pathobj() to play with it. | |||
9 |
|
13 | |||
10 | 2007-12-28 Fernando Perez <Fernando.Perez@colorado.edu> |
|
14 | 2007-12-28 Fernando Perez <Fernando.Perez@colorado.edu> | |
11 |
|
15 |
General Comments 0
You need to be logged in to leave comments.
Login now