def collect(ip, arg): """ collect foo/a.txt rec:bar=*.py Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, likewise Without args, try to open ~/_ipython/collect dir (in win32 at least). """ from IPython.external.path import path basedir = path(ip.options.IPYTHONDIR + '/collect') try: fs = mglob.expand(arg.split(None, 1)[1]) except IndexError: os.startfile(basedir) return for f in fs: f = path(f) trg = basedir / f.splitdrive()[1].lstrip('/\\') if f.isdir(): print "mkdir", trg trg.makedirs() continue dname = trg.dirname() if not dname.isdir(): dname.makedirs() print f, "=>", trg shutil.copy2(f, trg)
def collect(ip,arg): """ collect foo/a.txt rec:bar=*.py Copies foo/a.txt to ~/_ipython/collect/foo/a.txt and *.py from bar, likewise Without args, try to open ~/_ipython/collect dir (in win32 at least). """ from IPython.external.path import path basedir = path(ip.ipython_dir + '/collect') try: fs = mglob.expand(arg.split(None,1)[1]) except IndexError: os.startfile(basedir) return for f in fs: f = path(f) trg = basedir / f.splitdrive()[1].lstrip('/\\') if f.isdir(): print "mkdir",trg trg.makedirs() continue dname = trg.dirname() if not dname.isdir(): dname.makedirs() print f,"=>",trg shutil.copy2(f,trg)
def lee_f(self, s): """ Open file(s)/objects in Leo - %lee hist -> open full session history in leo - Takes an object. l = [1,2,"hello"]; %lee l. Alt+I in leo pushes the object back - Takes an mglob pattern, e.g. '%lee *.cpp' or %lee 'rec:*.cpp' - Takes input history indices: %lee 4 6-8 10 12-47 """ # import os c = g_ipm.c ip = g_ipm.ip wb = g_ipm.wb try: if s == 'hist': if c: wb.ipython_history.b = g_ipm.get_history() wb.ipython_history.go() else: g.trace('no c') return if s and s[0].isdigit(): # numbers; push input slices to leo lines = self.extract_input_slices(s.strip().split(), True) v = add_var('stored_ipython_input') v.b = '\n'.join(lines) return # try editing the object directly obj = ip.user_ns.get(s, None) if obj is not None: edit_object_in_leo(obj, s) return if not c: # print('file not found: %s' % s) return # if it's not object, it's a file name / mglob pattern from IPython.external import mglob files = (os.path.abspath(f) for f in mglob.expand(s)) for fname in files: p = g.findNodeAnywhere(c, '@auto ' + fname) if not p: p = c.currentPosition().insertAfter() p.setHeadString('@auto ' + fname) if os.path.isfile(fname): c.setBodyString(p, open(fname).read()) c.selectPosition(p) print("Editing file(s), \ press ctrl+shift+w in Leo to write @auto nodes") finally: if c: c.redraw()
def parse_args(args): """ Given arg string 'CMD files... target', return ([files], target) """ tup = args.split(None, 1) if len(tup) == 1: raise UsageError("Expected arguments for " + tup[0]) tup2 = shlex.split(tup[1]) flist, trg = mglob.expand(tup2[0:-1]), tup2[-1] if not flist: raise UsageError("No files found:" + str(tup2[0:-1])) return flist, trg
def lee_f(self,s): """ Open file(s)/objects in Leo - %lee hist -> open full session history in leo - Takes an object. l = [1,2,"hello"]; %lee l. Alt+I in leo pushes the object back - Takes an mglob pattern, e.g. '%lee *.cpp' or %lee 'rec:*.cpp' - Takes input history indices: %lee 4 6-8 10 12-47 """ import os c.beginUpdate() try: if s == 'hist': wb.ipython_history.b = get_history() wb.ipython_history.go() return if s and s[0].isdigit(): # numbers; push input slices to leo lines = self.extract_input_slices(s.strip().split(), True) v = add_var('stored_ipython_input') v.b = '\n'.join(lines) return # try editing the object directly obj = ip.user_ns.get(s, None) if obj is not None: edit_object_in_leo(obj,s) return # if it's not object, it's a file name / mglob pattern from IPython.external import mglob files = (os.path.abspath(f) for f in mglob.expand(s)) for fname in files: p = g.findNodeAnywhere(c,'@auto ' + fname) if not p: p = c.currentPosition().insertAfter() p.setHeadString('@auto ' + fname) if os.path.isfile(fname): c.setBodyString(p,open(fname).read()) c.selectPosition(p) print "Editing file(s), press ctrl+shift+w in Leo to write @auto nodes" finally: c.endUpdate()
def irm(ip, arg): """ irm path[s]... Remove file[s] or dir[s] path. Dirs are deleted recursively. """ try: paths = mglob.expand(arg.split(None, 1)[1]) except IndexError: raise UsageError("%irm paths...") import distutils.dir_util for p in paths: print "rm", p if os.path.isdir(p): distutils.dir_util.remove_tree(p, verbose=1) else: os.remove(p)
def irm(ip,arg): """ irm path[s]... Remove file[s] or dir[s] path. Dirs are deleted recursively. """ try: paths = mglob.expand(arg.split(None,1)[1]) except IndexError: raise UsageError("%irm paths...") import distutils.dir_util for p in paths: print "rm",p if os.path.isdir(p): distutils.dir_util.remove_tree(p, verbose = 1) else: os.remove(p)
def leo_f(self, s): """ open file(s) in Leo Takes an mglob pattern, e.g. '%leo *.cpp' or %leo 'rec:*.cpp' """ import os from IPython.external import mglob files = mglob.expand(s) c.beginUpdate() try: for fname in files: p = g.findNodeAnywhere(c, '@auto ' + fname) if not p: p = c.currentPosition().insertAfter() p.setHeadString('@auto ' + fname) if os.path.isfile(fname): c.setBodyString(p, open(fname).read()) c.selectPosition(p) finally: c.endUpdate()
def leo_f(self,s): """ open file(s) in Leo Takes an mglob pattern, e.g. '%leo *.cpp' or %leo 'rec:*.cpp' """ import os from IPython.external import mglob files = mglob.expand(s) c.beginUpdate() try: for fname in files: p = g.findNodeAnywhere(c,'@auto ' + fname) if not p: p = c.currentPosition().insertAfter() p.setHeadString('@auto ' + fname) if os.path.isfile(fname): c.setBodyString(p,open(fname).read()) c.selectPosition(p) finally: c.endUpdate()
def igrep(ip, arg): """ igrep PAT files... Very dumb file scan, case-insensitive. e.g. igrep "test this" rec:*.py """ elems = shlex.split(arg) dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) res = [] for f in fs: found = False for l in open(f): if pat.lower() in l.lower(): if not found: print "[[", f, "]]" found = True res.append(f) print l.rstrip() return res
def igrep(ip,arg): """ igrep PAT files... Very dumb file scan, case-insensitive. e.g. igrep "test this" rec:*.py """ elems = shlex.split(arg) dummy, pat, fs = elems[0], elems[1], mglob.expand(elems[2:]) res = [] for f in fs: found = False for l in open(f): if pat.lower() in l.lower(): if not found: print "[[",f,"]]" found = True res.append(f) print l.rstrip() return res