def evaluate(self) -> None: """ Add the index to the entity """ entity_like = self.namespace.get_type(self.type) assert isinstance(entity_like, EntityLike), "%s is not an entity or default" % entity_like entity_type = entity_like.get_entity() allattributes = entity_type.get_all_attribute_names() for attribute in self.attributes: if attribute not in allattributes: raise NotFoundException( self, attribute, "Attribute '%s' referenced in index is not defined in entity %s" % (attribute, entity_type) ) else: rattribute = entity_type.get_attribute(attribute) assert rattribute is not None # Make mypy happy if rattribute.is_optional(): raise IndexException( self, "Index can not contain optional attributes, Attribute ' %s.%s' is optional" % (attribute, entity_type), ) if rattribute.is_multi(): raise IndexException( self, "Index can not contain list attributes, Attribute ' %s.%s' is a list" % (attribute, entity_type) ) entity_type.add_index(self.attributes)
def final(self, excns: List[Exception]) -> None: for key, indices in self.index_queue.items(): for _, stmt in indices: excns.append( NotFoundException( stmt, key, "No match in index on type %s with key %s" % (self.get_full_name(), key)))
def get_attribute(self, name: str) -> ResultVariable: try: return self.slots[name] except KeyError: raise NotFoundException( None, name, "could not find attribute with name: %s in type %s" % (name, self.type))
def set_attribute(self, name, value, location, recur=True): if name not in self.slots: raise NotFoundException( None, name, "cannot set attribute with name %s on type %s" % (name, str(self.type))) try: self.slots[name].set_value(value, location, recur) except RuntimeException as e: raise AttributeException(None, self, name, cause=e)
def evaluate(self): """ Add the index to the entity """ entity_type = self.namespace.get_type(self.type) allattributes = entity_type.get_all_attribute_names() for attribute in self.attributes: if attribute not in allattributes: raise NotFoundException(self, attribute, "Index defined on attribute that does not exist") entity_type.add_index(self.attributes)
def lookup_index( self, params: "List[Tuple[str,object]]", stmt: "Statement", target: "Optional[ResultVariable]" = None) -> "Optional[Instance]": """ Search an instance in the index. """ all_attributes: List[str] = [x[0] for x in params] attributes: Set[str] = set(()) for attr in all_attributes: if attr in attributes: raise RuntimeException( stmt, "Attribute %s provided twice in index lookup" % attr) attributes.add(attr) found_index = False for index_attributes in self.get_indices(): if set(index_attributes) == attributes: found_index = True if not found_index: raise NotFoundException( stmt, self.get_full_name(), "No index defined on %s for this lookup: " % self.get_full_name() + str(params)) key = ", ".join([ "%s=%s" % (k, repr(v)) for (k, v) in sorted(params, key=lambda x: x[0]) ]) if target is None: if key in self._index: return self._index[key] else: return None elif key in self._index: target.set_value(self._index[key], stmt.location) else: if key in self.index_queue: self.index_queue[key].append((target, stmt)) else: self.index_queue[key] = [(target, stmt)] return None
def template(ctx: Context, path: "string"): """ Execute the template in path in the current context. This function will generate a new statement that has dependencies on the used variables. """ jinja_env = _get_template_engine(ctx) template_path = _extend_path(ctx, path) if template_path in tcache: template = tcache[template_path] else: template = jinja_env.get_template(template_path) tcache[template_path] = template resolver = ctx.get_resolver() try: out = template.render({"{{resolver": resolver}) return out except UndefinedError as e: raise NotFoundException(ctx.owner, None, e.message)
def lookup_index( self, params: "Dict[str,object]", stmt: "Statement", target: "Optional[ResultVariable]" = None) -> "Optional[Instance]": """ Search an instance in the index. """ attributes = set([x[0] for x in params]) found_index = False for index_attributes in self._index_def: if set(index_attributes) == attributes: found_index = True if not found_index: raise NotFoundException( stmt, self.get_full_name(), "No index defined on %s for this lookup: " % self.get_full_name() + str(params)) key = ", ".join(["%s=%s" % (k, repr(v)) for (k, v) in params]) if target is None: if key in self._index: return self._index[key] else: return None elif key in self._index: target.set_value(self._index[key], stmt.location) else: if key in self.index_queue: self.index_queue[key].append((target, stmt)) else: self.index_queue[key] = [(target, stmt)] return None
def direct_lookup(self, name: str) -> ResultVariable: if name in self.slots: return self.slots[name] else: raise NotFoundException(None, name, "variable %s not found" % name)
def execute_direct(self, requires: Dict[object, object]) -> object: if self.name not in requires: raise NotFoundException(self, "Could not resolve the value %s in this static context" % self.name) return requires[self.name]