def get_completions(word, code, subcontext, cursor_indentation): """gets the completions for word after evaluating code""" global subprogram_globals parsed_subcontext = context.calculate_subcontext(subcontext, cursor_indentation) context.exec_code(code) keys = context.get_context() dots = word.split('.') # If it is a not completable python expression completions = an empty list pattern = "^[A-Za-z_][A-Za-z_0-9]*([.][A-Za-z_][A-Za-z_0-9]*)*\.?$" if not re.match(pattern, word): completions = [] elif word.rfind('.') == -1: # If the word is a simple statement not containing "." completions = # the global keys starting with the word completions = [i for i in keys if i.startswith(word)] else: if word.startswith('self'): dot_index = parsed_subcontext.rfind('.') parsed_subcontext = parsed_subcontext[:dot_index] word = word.replace('self', parsed_subcontext) # If word ends with a "." strip it and execute get_dir if word.endswith('.'): module = context.eval_code(word[:-1]) if module: completions = get_dir(module) else: completions = [] else: # If word does not ends with "." but it contains a dot # then eval word up to ".", split the remaining part, get # the attributes of the module when the attribute starts # with the remaining dot_index = word.rfind('.') module = context.eval_code(word[:dot_index]) if module: mword = word[dot_index + 1:] completions = [ i for i in get_dir(module) if i.startswith(mword) ] else: completions = [] return sorted(list(set(completions)))
def get_completions(word, code, subcontext, cursor_indentation): """gets the completions for word after evaluating code""" global subprogram_globals parsed_subcontext = context.calculate_subcontext(subcontext, cursor_indentation) context.exec_code(code) keys = context.get_context() dots = word.split('.') # If it is a not completable python expression completions = an empty list pattern = "^[A-Za-z_][A-Za-z_0-9]*([.][A-Za-z_][A-Za-z_0-9]*)*\.?$" if not re.match(pattern, word): completions = [] elif word.rfind('.') == -1: # If the word is a simple statement not containing "." completions = # the global keys starting with the word completions = [i for i in keys if i.startswith(word)] else: if word.startswith('self'): dot_index = parsed_subcontext.rfind('.') parsed_subcontext = parsed_subcontext[:dot_index] word = word.replace('self', parsed_subcontext) # If word ends with a "." strip it and execute get_dir if word.endswith('.'): module = context.eval_code(word[:-1]) if module: completions = get_dir(module) else: completions = [] else: # If word does not ends with "." but it contains a dot # then eval word up to ".", split the remaining part, get # the attributes of the module when the attribute starts # with the remaining dot_index = word.rfind('.') module = context.eval_code(word[:dot_index]) if module: mword = word[dot_index+1:] completions = [i for i in get_dir(module) if i.startswith(mword)] else: completions = [] return sorted(list(set(completions)))
def get_help(obj): """Returns the help of the given object. Inspired in the original pycomplete package """ paren = obj.rfind("(") if paren != -1: obj = obj[:paren] if obj.endswith("(") or obj.endswith("."): obj = obj[:-1] found = False pobj = None context_dict = 'subprogram_globals' if not obj in context.get_context(): context_dict = 'helper_globals' found = context.cimport(obj, context_dict) else: pobj = context.eval_code(obj) print pobj if obj not in context.subcontext_globals and found: pobj = context.eval_code(obj, context_dict) if not pobj: return "no help string for " + obj obj = context.eval_code(obj) return pydoc.getdoc(obj) if __name__=='__main__': print context.exec_code('import glob') print get_help('glob')