def completer(self, event): """ Completer for export. """ from glob import iglob from itertools import chain from os.path import isdir from IPython.core.completer import expand_user, compress_user data = event.line.split() if (len(event.symbol) == 0 and data[-1] == "--from") \ or (len(event.symbol) > 0 and data[-2] == "--from"): relpath, tilde_expand, tilde_val = expand_user(data[-1]) dirs = [f.replace('\\', '/') + "/" for f in iglob(relpath + '*') if isdir(f)] return [compress_user(p, tilde_expand, tilde_val) for p in dirs] if (len(event.symbol) == 0 and len(data) > 0 and data[-1] == "--with") \ or (len(event.symbol) > 0 and len(data) > 1 and data[-2] == "--with"): return [] data = set(data) - {"export", "%export"} result = {'--incar', '--doscar', '--poscar', '--chgcar', '--contcar', '--potcar', '--wavecar', '--procar', '--list', '--down', '--from', '--with'} if '--list' not in data: other = event.line.split() if '--from' in other: i = other.index('--from') if i + 1 < len(other): other.pop(i + 1) other = [u for u in (set(other) - result - {'export', '%export'}) if u[0] != '-'] if len(other) == 0: for file in chain(iglob('*.tar'), iglob('*.tar.gz'), iglob('*.tgz'), iglob('*.bz'), iglob('*.bz2')): result.add(file) relpath, tilde_expand, tilde_val = expand_user(data[-1]) result |= {f.replace('\\', '/') + "/" for f in iglob(relpath + '*') if isdir(f)} elif len(other) == 1 and len(event.symbol) != 0: result.discard('--list') other = event.symbol if '.' in other: other = other[:other.find('.')] string = "{0}*.tar {0}*.tar.gz {0}*.tgz {0}*.tar.bz" \ "{0}*.tar.bz2 {0}*/".format(other) result |= {u for u in iglob(string)} if isdir(other) and other[-1] != '/': string = "{0}/*.tar {0}/*.tar.gz {0}/*.tgz {0}/*.tar.bz " \ "{0}/*.tar.bz2 {0}*/".format(other) result |= {u for u in iglob(string)} result = result - data if '--down' in data: result.discard('--from') if '--from' in data: result.discard('--down') return list(result) + ['--with']
def jobfolder_file_completer(data): """ Returns list of potential job-folder and directories. """ from os.path import isdir from glob import iglob from IPython.core.completer import expand_user, compress_user from .. import jobfolder_glob if len(data) == 0: data = [''] relpath, tilde_expand, tilde_val = expand_user(data[-1]) dirs = [f.replace('\\','/') + "/" for f in iglob(relpath+'*') if isdir(f)] dicts = [ f.replace('\\','/') for u in jobfolder_glob for f in iglob(relpath+u)] if '.' in data[-1]: relpath, a, b = expand_user(data[-1][:data[-1].find('.')]) dicts.extend([ f.replace('\\','/') for u in jobfolder_glob for f in iglob(relpath+u)]) dummy = [compress_user(p, tilde_expand, tilde_val) for p in dirs+dicts] return [d for d in dummy if d not in data]
def magic_run_completer(self, event): """Complete files that end in .py or .ipy or .ipynb for the %run command. """ comps = arg_split(event.line, strict=False) # relpath should be the current token that we need to complete. if (len(comps) > 1) and (not event.line.endswith(' ')): relpath = comps[-1].strip("'\"") else: relpath = '' #print("\nev=", event) # dbg #print("rp=", relpath) # dbg #print('comps=', comps) # dbg lglob = glob.glob isdir = os.path.isdir relpath, tilde_expand, tilde_val = expand_user(relpath) # Find if the user has already typed the first filename, after which we # should complete on all files, since after the first one other files may # be arguments to the input script. if any(magic_run_re.match(c) for c in comps): matches = [f.replace('\\','/') + ('/' if isdir(f) else '') for f in lglob(relpath+'*')] else: dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)] pys = [f.replace('\\','/') for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + lglob(relpath+'*.ipynb') + lglob(relpath + '*.pyw')] matches = dirs + pys #print('run comp:', dirs+pys) # dbg return [compress_user(p, tilde_expand, tilde_val) for p in matches]
def magic_run_completer(self, event): """Complete files that end in .py or .ipy for the %run command. """ comps = arg_split(event.line, strict=False) relpath = (len(comps) > 1 and comps[-1] or '').strip("'\"") #print("\nev=", event) # dbg #print("rp=", relpath) # dbg #print('comps=', comps) # dbg lglob = glob.glob isdir = os.path.isdir relpath, tilde_expand, tilde_val = expand_user(relpath) dirs = [f.replace('\\','/') + "/" for f in lglob(relpath+'*') if isdir(f)] # Find if the user has already typed the first filename, after which we # should complete on all files, since after the first one other files may # be arguments to the input script. if filter(magic_run_re.match, comps): pys = [f.replace('\\','/') for f in lglob('*')] else: pys = [f.replace('\\','/') for f in lglob(relpath+'*.py') + lglob(relpath+'*.ipy') + lglob(relpath + '*.pyw')] #print('run comp:', dirs+pys) # dbg return [compress_user(p, tilde_expand, tilde_val) for p in dirs+pys]
def cd_completer(self, event): """Completer function for cd, which only returns directories.""" ip = get_ipython() relpath = event.symbol #print(event) # dbg if event.line.endswith('-b') or ' -b ' in event.line: # return only bookmark completions bkms = self.db.get('bookmarks', None) if bkms: return bkms.keys() else: return [] if event.symbol == '-': width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) # jump in directory history by number fmt = '-%0' + width_dh + 'd [%s]' ents = [fmt % (i, s) for i, s in enumerate(ip.user_ns['_dh'])] if len(ents) > 1: return ents return [] if event.symbol.startswith('--'): return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']] # Expand ~ in path and normalize directory separators. relpath, tilde_expand, tilde_val = expand_user(relpath) relpath = relpath.replace('\\', '/') found = [] for d in [ f.replace('\\', '/') + '/' for f in glob.glob(relpath + '*') if os.path.isdir(f) ]: if ' ' in d: # we don't want to deal with any of that, complex code # for this is elsewhere raise TryNext found.append(d) if not found: if os.path.isdir(relpath): return [compress_user(relpath, tilde_expand, tilde_val)] # if no completions so far, try bookmarks bks = self.db.get('bookmarks', {}) bkmatches = [s for s in bks if s.startswith(event.symbol)] if bkmatches: return bkmatches raise TryNext return [compress_user(p, tilde_expand, tilde_val) for p in found]
def cd_completer(self, event): """Completer function for cd, which only returns directories.""" ip = get_ipython() relpath = event.symbol #print(event) # dbg if event.line.endswith('-b') or ' -b ' in event.line: # return only bookmark completions bkms = self.db.get('bookmarks', None) if bkms: return bkms.keys() else: return [] if event.symbol == '-': width_dh = str(len(str(len(ip.user_ns['_dh']) + 1))) # jump in directory history by number fmt = '-%0' + width_dh +'d [%s]' ents = [ fmt % (i,s) for i,s in enumerate(ip.user_ns['_dh'])] if len(ents) > 1: return ents return [] if event.symbol.startswith('--'): return ["--" + os.path.basename(d) for d in ip.user_ns['_dh']] # Expand ~ in path and normalize directory separators. relpath, tilde_expand, tilde_val = expand_user(relpath) relpath = relpath.replace('\\','/') found = [] for d in [f.replace('\\','/') + '/' for f in glob.glob(relpath+'*') if os.path.isdir(f)]: if ' ' in d: # we don't want to deal with any of that, complex code # for this is elsewhere raise TryNext found.append(d) if not found: if os.path.isdir(relpath): return [compress_user(relpath, tilde_expand, tilde_val)] # if no completions so far, try bookmarks bks = self.db.get('bookmarks',{}) bkmatches = [s for s in bks if s.startswith(event.symbol)] if bkmatches: return bkmatches raise TryNext return [compress_user(p, tilde_expand, tilde_val) for p in found]
def jobfolder_file_completer(data): """ Returns list of potential job-folder and directories. """ from os.path import isdir from glob import iglob from IPython.core.completer import expand_user, compress_user from .. import jobfolder_glob if len(data) == 0: data = [''] relpath, tilde_expand, tilde_val = expand_user(data[-1]) dirs = [ f.replace('\\', '/') + "/" for f in iglob(relpath + '*') if isdir(f) ] dicts = [ f.replace('\\', '/') for u in jobfolder_glob for f in iglob(relpath + u) ] if '.' in data[-1]: relpath, a, b = expand_user(data[-1][:data[-1].find('.')]) dicts.extend([ f.replace('\\', '/') for u in jobfolder_glob for f in iglob(relpath + u) ]) dummy = [compress_user(p, tilde_expand, tilde_val) for p in dirs + dicts] return [d for d in dummy if d not in data]
def completer(self, event): """ Completer for export. """ from glob import iglob from itertools import chain from os.path import isdir from IPython.core.completer import expand_user, compress_user data = event.line.split() if (len(event.symbol) == 0 and data[-1] == "--from") \ or (len(event.symbol) > 0 and data[-2] == "--from"): relpath, tilde_expand, tilde_val = expand_user(data[-1]) dirs = [ f.replace('\\', '/') + "/" for f in iglob(relpath + '*') if isdir(f) ] return [compress_user(p, tilde_expand, tilde_val) for p in dirs] if (len(event.symbol) == 0 and len(data) > 0 and data[-1] == "--with") \ or (len(event.symbol) > 0 and len(data) > 1 and data[-2] == "--with"): return [] data = set(data) - {"export", "%export"} result = { '--incar', '--doscar', '--poscar', '--chgcar', '--contcar', '--potcar', '--wavecar', '--procar', '--list', '--down', '--from', '--with' } if '--list' not in data: other = event.line.split() if '--from' in other: i = other.index('--from') if i + 1 < len(other): other.pop(i + 1) other = [ u for u in (set(other) - result - {'export', '%export'}) if u[0] != '-' ] if len(other) == 0: for file in chain(iglob('*.tar'), iglob('*.tar.gz'), iglob('*.tgz'), iglob('*.bz'), iglob('*.bz2')): result.add(file) relpath, tilde_expand, tilde_val = expand_user(data[-1]) result |= { f.replace('\\', '/') + "/" for f in iglob(relpath + '*') if isdir(f) } elif len(other) == 1 and len(event.symbol) != 0: result.discard('--list') other = event.symbol if '.' in other: other = other[:other.find('.')] string = "{0}*.tar {0}*.tar.gz {0}*.tgz {0}*.tar.bz" \ "{0}*.tar.bz2 {0}*/".format(other) result |= {u for u in iglob(string)} if isdir(other) and other[-1] != '/': string = "{0}/*.tar {0}/*.tar.gz {0}/*.tgz {0}/*.tar.bz " \ "{0}/*.tar.bz2 {0}*/".format(other) result |= {u for u in iglob(string)} result = result - data if '--down' in data: result.discard('--from') if '--from' in data: result.discard('--down') return list(result) + ['--with']