def solve_select(expr, vars): """Use IAssociative.select to get key (rhs) from the data (lhs). This operation supports both scalars and repeated values on the LHS - selecting from a repeated value implies a map-like operation and returns a new repeated value. """ data = __solve_for_repeated(expr.lhs, vars) key = solve(expr.rhs, vars).value try: results = [associative.select(d, key) for d in repeated.getvalues(data)] except (KeyError, AttributeError): # Raise a better exception for accessing a non-existent key. raise errors.EfilterKeyError(root=expr, key=key, query=expr.source) except (TypeError, ValueError): # Raise a better exception for what is probably a null pointer error. if vars.locals is None: raise errors.EfilterNoneError( root=expr, query=expr.source, message="Cannot select key %r from a null." % key) else: raise except NotImplementedError: raise errors.EfilterError( root=expr, query=expr.source, message="Cannot select keys from a non-associative value.") return Result(repeated.meld(*results), ())
def solve_select(expr, vars): """Use IAssociative.select to get key (rhs) from the data (lhs). This operation supports both scalars and repeated values on the LHS - selecting from a repeated value implies a map-like operation and returns a new repeated value. """ data = solve(expr.lhs, vars).value key = solve(expr.rhs, vars).value try: results = [associative.select(d, key) for d in repeated.getvalues(data)] except (KeyError, AttributeError): # Raise a better exception for accessing a non-existent key. raise errors.EfilterKeyError(root=expr, key=key, query=expr.source) except (TypeError, ValueError): # Raise a better exception for what is probably a null pointer error. if vars.locals is None: raise errors.EfilterNoneError( root=expr, query=expr.source, message="Cannot select key %r from a null." % key) else: raise except NotImplementedError: raise errors.EfilterError( root=expr, query=expr.source, message="Cannot select keys from a non-associative value.") return Result(repeated.meld(*results), ())
def testInterfaces(self): rt = row_tuple.RowTuple(values=dict(foo="Foo", bar="Bar", car="Car"), ordered_columns=["car", "bar", "foo"]) self.assertEqual(len(rt), 3) self.assertEqual(counted.count(rt), 3) self.assertEqual(list(rt), ["Car", "Bar", "Foo"]) self.assertEqual(associative.select(rt, 2), "Foo") with self.assertRaises(KeyError): associative.select(rt, "foo") with self.assertRaises(KeyError): structured.resolve(rt, 2) self.assertEqual(structured.resolve(rt, "foo"), "Foo")
def select_Pointer(ptr, key): """Delegate to target of the pointer, if any.""" target_obj = ptr.deref() if not target_obj: ptr.session.logging.warn( "Attempting to access key %r of a void pointer %r.", key, ptr) if target_obj: return associative.select(target_obj, key)
def select_Pointer(ptr, key): """Delegate to target of the pointer, if any.""" obj = ptr.deref() if not obj: ptr.session.logging.warn( "Attempting to access key %r of a void pointer %r.", key, ptr) if obj: return associative.select(obj, key)
def solve_select(expr, vars): """Use IAssociative.select to get key (rhs) from the data (lhs).""" data = __within_lhs_as_repeated(expr.lhs, vars) key = solve(expr.rhs, vars).value try: results = [associative.select(d, key) for d in repeated.getvalues(data)] except (KeyError, AttributeError): # Raise a better exception for accessing a non-existent key. raise errors.EfilterKeyError(root=expr, key=key, query=expr.source) except (TypeError, ValueError): # Raise a better exception for what is probably a null pointer error. if vars is None: raise errors.EfilterNoneError( root=expr, query=expr.source, message="Cannot select key %r from a null." % key) else: raise except NotImplementedError: raise errors.EfilterError( root=expr, query=expr.source, message="Cannot select keys from a non-associative value.") return Result(repeated.meld(*results), ())
def visit_Binding(self, expr, **_): return associative.select(self.bindings, expr.value)
associative.IAssociative.implement( for_type=obj.Array, implementations={ associative.select: lambda obj, key: obj[key], associative.resolve: getattr } ) associative.IAssociative.implement( for_type=obj.Pointer, implementations={ associative.select: lambda ptr, key: associative.select(ptr.deref(), key), associative.resolve: lambda ptr, key: associative.resolve(ptr.deref(), key) } ) # What this implementation SHOULD do is run the plugin, select the column from # each row and then return a repeated value. However, that'll be implemented # once we've converted most plugins to typed plugins. Right now, this never # actually gets called and exists mainly to get infer_type to shut up about # Command not implementing IAssociative. associative.IAssociative.implement( for_type=plugin.Command, implementations={ associative.select: lambda x, idx: list(x)[idx],
(name for name, _ in s.getproperties())) }) # This lets us get indices out of Arrays. associative.IAssociative.implement(for_type=obj.Array, implementations={ associative.select: lambda obj, key: obj[key], }) # This lets us do some_array.some_member. structured.IStructured.implement(for_type=obj.Array, implementations={structured.resolve: getattr}) # Pointer[key] is implemented as Pointer.dereference()[key]. associative.IAssociative.implement( for_type=obj.Pointer, implementations={ associative.select: lambda ptr, key: associative.select(ptr.deref(), key), }) # Pointer.member is implemented as Pointer.dereference().member. structured.IStructured.implement( for_type=obj.Pointer, implementations={ structured.resolve: lambda ptr, key: structured.resolve(ptr.deref(), key) })