def __init__(self, template_content, *args, **kwargs): """`template_content` is a string that contains directly the Jinja2 template code""" enforce_type(template_content, str) super(Jinja2StringTemplate, self).__init__(*args, **kwargs) self.__template_content = trimlws(template_content)
def __init__(self, filename, *args, **kwargs): """`filename` specified from which file the template code is loaded""" enforce_type(filename, str) super(Jinja2FileTemplate, self).__init__(*args, **kwargs) self.__filename = filename
def hasFieldByName(self, name): """Returns True if there is a field with the given name, otherwise False.""" enforce_type(name, str) return name in self.__fields_hashed.keys()
def requireTags(self, t, tags_names, inherit=True): """Checks whether the type t has instances of all tag classes specified as values in the dictionary `tags_names`. The return value is a dictionary with the same keys as tags_names, but where the values are the instances of the specified tag classes. """ enforce_type(t, Type) enforce_type_dict_values(tags_names, type) def_args = {} for tagname, tagclass in tags_names.items(): taginst = t.getTag(tagclass, inherit=inherit) if not taginst: raise Exception("Template '" + self.getName() + "' " "requires tag " + \ "'" + tagclass.__name__ + "', but no such tag " + "has been associated with type " + "'" + t.getName() + "'.") else: def_args[tagname] = taginst return def_args
def referencesType(self, t, types_checked=None): """Recursively checks whether this compound type contains a field whose type is `t` or if it references another compound type that contains such a field. `types_checked` is an optional list of types for which is has already been checked if they reference t. All of these types will be excluded when recursively invoking referencesType() for each field. """ enforce_type(t, Type) enforce_type(types_checked, [list, type(None)]) if types_checked is None: types_checked = [] types_checked.append(self) for f in self.getFields(): field_type = f.getType() if not f.isPointer() or f.isOwned(): if field_type == t: return True if field_type.isCompound() and (field_type not in types_checked): if field_type.referencesType(t, types_checked): return True return False
def __init__(self, num_elements_field_name, array_field_name, verbatim_field_names, function_name = None): """`Num_elements_field` is the name of the field that contains the number of elements to be read. `Array_field_name` is the name of the field that will point to the array's elements after reading the elements from disk. `verbatim_field_names` is a list of names of fields that are to be read verbatim from disk. Their order specifies the order in which they are read from disk. This list must contain num_elements_field_name.""" TemplatedGenerateFunctionTag.__init__( self, template_type = aftermath.templates.dsk.ArrayReadFunction) ReadFunction.__init__(self, function_name = function_name) enforce_type(num_elements_field_name, str) enforce_type(array_field_name, str) if not num_elements_field_name in verbatim_field_names: raise Exception("The field '" + num_elements_field_name + " with " + "the number of elements must appear in list of " + "fields to be read verbatim") self.__num_elements_field_name = num_elements_field_name self.__array_field_name = array_field_name self.__verbatim_field_names = verbatim_field_names
def getDependencies(self, exclude_pointers=False): """Returns a set with all types, for which a non-forward declaration is required for the definition of this type in C""" enforce_type(exclude_pointers, bool) return self.__fields.getDependencies(exclude_pointers=exclude_pointers)
def __init__(self, intval): enforce_type(intval, int) if intval < 0 or intval > 4: raise Exception("Illegal value for SelfMode") self.intval = intval
def hasTypeName(self, tname): """Returns true if the type list contains a type instance whose name is n""" enforce_type(tname, str) return tname in self.__types_hashed.keys()
def getType(self, tname): enforce_type(tname, str) if tname in self.__types_hashed.keys(): return self.__types_hashed[tname] else: return None
def renderJinjaTemplate(self, template, **kwargs): """Renders a Jinja2.Template with the given arguments""" enforce_type(template, jinja2.Template) args = self.getDefaultArgs() args.update(**kwargs) return template.render(args)
def __init__(self, signed, *args, **kwargs): """`signed` indicates whether the defined integer type can have negative values""" enforce_type(signed, bool) super(IntegerType, self).__init__(*args, **kwargs) self.__signed = signed
def __init__(self, num_bits, *args, **kwargs): """`num_bits` the maximum number of bits for values of the defined integer type""" enforce_type(num_bits, int) super(FixedWidthIntegerType, self).__init__(*args, **kwargs) self.__num_bits = num_bits
def filterByTag(self, c): """Returns a TypeList with all types that have a tag of class c""" enforce_type(c, type) return TypeList( list( filter(lambda field_type: field_type.hasTag(c), self.getTypes())))
def removeTag(self, c): """Removes a type tag of the given class c from the list of tags associated to this type.""" enforce_type(c, type) tag = self.__tags_hashed[c] self.__tags.remove(tag) del self.__tags_hashed[c]
def getFieldByName(self, name): """Returns the field in the list whose name is `name`. If no such field exists, an exception is thrown.""" enforce_type(name, str) if not name in self.__fields_hashed.keys(): raise Exception("Unknown field '" + name + "'") return self.__fields_hashed[name]
def setCompoundType(self, t): """Must be invoked if the field list is used for the definition of a compound type.""" enforce_type(t, CompoundType) self.__compound_type = t for f in self.__fields: f.setCompoundType(t)
def getTypeByIdent(self, ident): """Returns a type by its array identifier or None if no such type exists""" enforce_type(ident, str) if ident in self.__idents_hashed.keys(): return self.__idents_hashed[ident] else: return None
def addTarget(self, tgt): enforce_type(tgt, CollectTarget) if self.hasTarget(tgt): raise Exception("Collect target for field " + "'" + src.getArrayField().getName() + "' " + "of type " + "'" + src.getType().getName() + "' " + "already added") self.__targets.append(tgt) self.__targets_hashed[tgt] = tgt
def addSource(self, src): enforce_type(src, CollectSource) if self.hasSource(src): raise Exception("Collect source for field " + "'" + src.getPtrField().getName() + "' " + "of type " + "'" + src.getType().getName() + "' " + "already added") self.__sources.append(src) self.__sources_hashed[src] = src
def addRelation(self, r): """Adds the relation r to this type""" enforce_type(r, relations.Relation) if r.__class__ in self.__relations_hashed.keys(): self.__relations_hashed[r.__class__].append(r) else: self.__relations_hashed[r.__class__] = [r] self.__relations.append(r)
def addTarget(self, tgt): enforce_type(tgt, JoinTarget) if self.hasTarget(tgt): raise Exception("Target for on-disk field " + "'" + tgt.getDskField().getName() + "' " + "of type " + "'" + tgt.getDskField().getCompoundType().getName() + "' " + "already added") self.__targets.append(tgt) self.__targets_hashed[tgt] = tgt
def addSource(self, src): enforce_type(src, JoinSource) if self.hasSource(src): raise Exception("Source for on-disk field " + "'" + src.getDskField().getName() + "' " + "of type " + "'" + src.getDskField().getCompoundType().getName() + "' " + "already added") self.__sources.append(src) self.__sources_hashed[src] = src
def __init__(self, num_field, array_field): """`num_field` the field that counts the number of pointers in the array of pointers to the sources. `array_field` the field that collects the pointers to the sources. """ enforce_type(array_field, Field) enforce_type(num_field, Field) self.__array_field = array_field self.__num_field = num_field
def addTag(self, tag): """Adds a type tag to the list of tags of this type.""" enforce_type(tag, tags.Tag) if isinstance(tag, tags.MultiTag): self.__addMultiTag(tag) else: self.__addUniqueTag(tag) self.__tags.append(tag) tag.setType(self)
def __init__(self, dsk_type, mem_type, function_name=None): TemplatedGenerateFunctionTag.__init__( self, template_type=aftermath.templates.dsk.tomem. AddToAllMetaStructArraysFunction) AddToAllMetaStructArraysFunction.__init__(self, function_name=function_name) enforce_type(dsk_type, Type) enforce_type(mem_type, Type) self.__dsk_type = dsk_type self.__mem_type = mem_type
def leadingws(s): """Returns the leading whitespace of s""" enforce_type(s, str) ret = "" for c in s: if c == ' ' or c == '\t': ret += c else: return ret return ret
def __init__(self, default_args={}): """`default_args` is a dictionary, for which each key-value pair will define a variable in the template whose name is the key and whose value is the value. """ enforce_type(default_args, dict) self.__default_args = { "template": self, "aftermath": aftermath, "isinstance": isinstance } self.__default_args.update(default_args) self.__required_tags = {}
def __init__(self, fields, tags=None, *args, **kwargs): """`fields` must be an instance of FieldList defining the fields of the defined compound type.""" enforce_type(fields, FieldList) self.__fields = fields self.__fields.setCompoundType(self) if not tags: tags = [] tags.insert(0, aftermath.tags.Compound()) super(CompoundType, self).__init__(tags=tags, *args, **kwargs)
def addField(self, f, pos): """Adds a field at position pos (pos will be the 0-based index of the new field)""" enforce_type(f, Field) enforce_type(pos, int) if f.getName() in self.__fields_hashed.keys(): raise Exception("Duplicate field \"" + f.getName() + "\"") self.__fields_hashed[f.getName()] = f self.__fields.insert(pos, f) if self.__compound_type: f.setCompoundType(self.__compound_type)