def attr_matches(self, text): """Compute matches when text contains a dot. Assuming the text is of the form NAME.NAME....[NAME], and is evaluatable in the globals of __main__, it will be evaluated and its attributes (as revealed by dir()) are used as possible completions. (For class instances, class members are are also considered.) WARNING: this can still invoke arbitrary C code, if an object with a __getattr__ hook is evaluated. """ import re brack_pattern = r"(?:\[.*\])?" m = re.match(r"(\w+" + brack_pattern + r"(\.\w+" + brack_pattern + \ r")*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) object = eval(expr, __main__.__dict__) words = dir(object) if hasattr(object,'__class__'): words.append('__class__') words = words + rlcompleter.get_class_members(object.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: try: object = eval(expr, session) except (NameError, AttributeError): return from scapy.packet import Packet, Packet_metaclass if isinstance(object, Packet) or isinstance(object, Packet_metaclass): words = filter(lambda x: x[0]!="_",dir(object)) words += [x.name for x in object.fields_desc] else: words = dir(object) if hasattr( object,"__class__" ): words = words + rlcompleter.get_class_members(object.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def create_sublist(self): """Function to create the dictionary of options for tab completion.""" # Split the input. list = split('\.', self.input) if len(list) == 0: return # Construct the module and get the corresponding object. module = list[0] for i in range(1, len(list)-1): module = module + '.' + list[i] object = eval(module, self.name_space) # Get the object attributes. self.list = dir(object) # If the object is a class, get all the class attributes as well. if hasattr(object, '__class__'): self.list.append('__class__') self.list = self.list + get_class_members(object.__class__) # Possible completions. self.options = [] for name in self.list: if match(list[-1], name): self.options.append(module + '.' + name) if self.verbosity: print("List: " + repr(list)) print("Module: " + repr(module)) print("self.list: " + repr(self.list)) print("self.options: " + repr(self.options))
def attr_matches(self, text): """ Derived from rlcompleter.Completer.attr_matches() """ m = self.PATTERN.match(text) if not m: return [] expr, attr = m.group(1, 3) try: thisobject = eval(expr, self.namespace) except Exception: return [] # get the content of the object, except __builtins__ words = dir(thisobject) if "__builtins__" in words: words.remove("__builtins__") if hasattr(thisobject, '__class__'): words.append('__class__') words.extend(rlcompleter.get_class_members(thisobject.__class__)) matches = [] n = len(attr) for word in words: if attr == '' and word[0] == '_': continue if word[:n] == attr and hasattr(thisobject, word): val = getattr(thisobject, word) word = self._callable_postfix(val, "%s.%s" % (expr, word)) matches.append(word) return matches
def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: object = eval(expr, session) if isinstance(object, Packet) or isinstance( object, Packet_metaclass): #words = filter(lambda x: x[0]!="_",dir(object)) words = [x for x in dir(object) if x[0] != "_"] words += [x.name for x in object.fields_desc] else: words = dir(object) if hasattr(object, "__class__"): words = words + rlcompleter.get_class_members( object.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def attr_matches(self, text): expr, attr = text.rsplit('.', 1) if '(' in expr or ')' in expr: # don't call functions return [] try: thisobject = eval(expr, self.namespace) except Exception: return [] # get the content of the object, except __builtins__ words = set(dir(thisobject)) words.discard("__builtins__") if hasattr(thisobject, '__class__'): words.add('__class__') words.update(rlcompleter.get_class_members(thisobject.__class__)) names = [] values = [] n = len(attr) if attr == '': noprefix = '_' elif attr == '_': noprefix = '__' else: noprefix = None words = sorted(words) while True: for word in words: if (word[:n] == attr and not (noprefix and word[:n+1] == noprefix)): try: val = getattr(thisobject, word) except Exception: val = None # Include even if attribute not set if not PY3K and isinstance(word, unicode): # this is needed because pyrepl doesn't like unicode # completions: as soon as it finds something which is not str, # it stops. word = word.encode('utf-8') names.append(word) values.append(val) if names or not noprefix: break if noprefix == '_': noprefix = '__' else: noprefix = None if len(names) == 1: return ['%s.%s' % (expr, names[0])] # only option, no coloring. prefix = commonprefix(names) if prefix and prefix != attr: return ['%s.%s' % (expr, prefix)] # autocomplete prefix return self.color_matches(names, values)
def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: object = eval(expr, session) words = dir(object) if hasattr(pm,"__class__" ): words = words + rlcompleter.get_class_members(pm.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr: matches.append("%s.%s" % (expr, word)) return matches
def attr_matches(self, text): m = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not m: return expr, attr = m.group(1, 3) try: object = eval(expr) except: object = eval(expr, session) words = dir(object) if hasattr(conobj, "__class__"): words = words + rlcompleter.get_class_members(conobj.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr: matches.append("%s.%s" % (expr, word)) return matches
def attr_lookup(self, obj, expr, attr): """Second half of attr_matches.""" words = self.list_attributes(obj) if inspection.hasattr_safe(obj, "__class__"): words.append("__class__") klass = inspection.getattr_safe(obj, "__class__") words = words + rlcompleter.get_class_members(klass) if not isinstance(klass, abc.ABCMeta): try: words.remove("__abstractmethods__") except ValueError: pass matches = [] n = len(attr) for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append(f"{expr}.{word}") return matches
def attr_matches(self, text): m = re.match(r"([\w\[\]]+(\.[\w\[\]]+)*)\.(\w*)", text) if m: expr, attr = m.group(1, 3) else: return try: thisobject = eval(expr) except: thisobject = eval(expr, abrupt.session.session_dict) words = dir(thisobject) if hasattr(thisobject, "__class__"): words = words + rlcompleter.get_class_members(thisobject.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("{}.{}".format(expr, word)) return matches
def attr_matches(self, text): """Compute matches when text contains a dot. Assuming the text is of the form NAME.NAME....[NAME], and is evaluatable in self.namespace, it will be evaluated and its attributes (as revealed by dir()) are used as possible completions. (For class instances, class members are also considered.) """ match = re.match(r"(\w+(\.\w+)*)\.(\w*)", text) if not match: return [] expr, attr = match.group(1, 3) try: thisobject = eval(expr, self.namespace) except Exception: return [] # get the content of the object, except __builtins__ words = dir(thisobject) if isinstance(thisobject, DeviceAlias): words.extend(dir(thisobject._obj)) if '__builtins__' in words: words.remove('__builtins__') if hasattr(thisobject, '__class__'): words.append('__class__') words.extend(rlcompleter.get_class_members(thisobject.__class__)) matches = [] n = len(attr) for word in words: if word[:n] == attr and hasattr(thisobject, word): val = getattr(thisobject, word) word = self._callable_postfix(val, '%s.%s' % (expr, word)) matches.append(word) textlen = len(text) return [ m for m in matches if not (m[textlen:].startswith( ('_', 'do')) or m[textlen:] in self.attr_hidden) ]
def attr_lookup(self, obj, expr, attr): """Second half of original attr_matches method factored out so it can be wrapped in a safe try/finally block in case anything bad happens to restore the original __getattribute__ method.""" words = dir(obj) if hasattr(obj, '__class__'): words.append('__class__') words = words + rlcompleter.get_class_members(obj.__class__) if has_abc and not isinstance(obj.__class__, abc.ABCMeta): try: words.remove('__abstractmethods__') except ValueError: pass matches = [] n = len(attr) for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def attr_matches(self, text): m = re.match(r"([\w\[\]\-]+(\.[\w\[\]]+)*)\.(\w*)", text) if m: expr, attr = m.group(1, 3) else: return try: thisobject = eval(expr) except: thisobject = eval(expr, abrupt.session.session_dict) words = dir(thisobject) if hasattr(thisobject, "__class__"): words = words + rlcompleter.get_class_members( thisobject.__class__) matches = [] n = len(attr) for word in words: if word[:n] == attr and word != "__builtins__": matches.append("{}.{}".format(expr, word)) return matches
def attr_lookup(self, obj, expr, attr): """Second half of attr_matches.""" words = self.list_attributes(obj) if inspection.hasattr_safe(obj, "__class__"): words.append("__class__") klass = inspection.getattr_safe(obj, "__class__") words = words + rlcompleter.get_class_members(klass) if not isinstance(klass, abc.ABCMeta): try: words.remove("__abstractmethods__") except ValueError: pass if not py3 and isinstance(obj, (InstanceType, ClassType)): # Account for the __dict__ in an old-style class. words.append("__dict__") matches = [] n = len(attr) for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def attr_lookup(self, obj, expr, attr): """Second half of original attr_matches method factored out so it can be wrapped in a safe try/finally block in case anything bad happens to restore the original __getattribute__ method.""" words = self.list_attributes(obj) if hasattr(obj, "__class__"): words.append("__class__") words = words + rlcompleter.get_class_members(obj.__class__) if not isinstance(obj.__class__, abc.ABCMeta): try: words.remove("__abstractmethods__") except ValueError: pass if not py3 and isinstance(obj, (InstanceType, ClassType)): # Account for the __dict__ in an old-style class. words.append("__dict__") matches = [] n = len(attr) for word in words: if self.method_match(word, n, attr) and word != "__builtins__": matches.append("%s.%s" % (expr, word)) return matches
def update_event(self, inp=-1): self.set_output_val(0, rlcompleter.get_class_members(self.input(0)))