Best Python code snippet using ATX
generate.py
Source:generate.py
...251 def set_parameter(self, *args, **kwargs):252 self.schema.set_parameter(*args, **kwargs)253 def add_debug(self, *args, **kwargs):254 self.schema.add_debug(*args, **kwargs)255 def add_line(self, *args, **kwargs):256 self.schema.add_line(*args, **kwargs)257 def skip_line(self, *args, **kwargs):258 self.schema.skip_line(*args, **kwargs)259 def pop_line(self, *args, **kwargs):260 return self.schema.pop_line(*args, **kwargs)261 def start_block(self, *args, **kwargs):262 self.schema.start_block(*args, **kwargs)263 def end_block(self, *args, **kwargs):264 self.schema.end_block(*args, **kwargs)265 266 def c_allocate(self, *args, **kwargs):267 self.schema.c_allocate(*args, **kwargs)268 def serialize_py_varint(self, *args, **kwargs):269 self.schema.serialize_py_varint(*args, **kwargs)270 def serialize_c_varint(self, *args, **kwargs):271 self.schema.serialize_c_varint(*args, **kwargs)272 def deserialize_py_varint(self, *args, **kwargs):273 self.schema.deserialize_py_varint(*args, **kwargs)274 def deserialize_c_varint(self, *args, **kwargs):275 self.schema.deserialize_c_varint(*args, **kwargs)276 def deserialize_c_int(self, *args, **kwargs):277 self.schema.deserialize_c_int(*args, **kwargs)278@dataclass279class StructMember(SchemaElement):280 schema: Schema281 name: str282 type: Union[283 StructDefinition, 284 TableDefinition, 285 EnumDefinition, 286 Primitives.String, 287 Primitives.Boolean,288 Primitives.Int8289 ]290 default: Union[str, int, None] = None291 vector: bool = False292 vector_size: Optional[int] = None293 field_id: Optional[int] = None294 def resolve_type(self, schema):295 if self.type in BUILTIN_TYPES:296 self.type = BUILTIN_TYPES[self.type]297 else:298 self.type = schema.definitions[self.type]299 @property300 def pointer_type(self):301 if self.type.c_name.endswith('*'):302 separator = ""303 else:304 separator = " "305 if self.vector:306 return "{}{}**{}".format(self.type.c_name, separator, self.name)307 else:308 return "{}{}*{}".format(self.type.c_name, separator, self.name)309 @property310 def param_type(self):311 if self.type.c_name.endswith('*'):312 separator = ""313 else:314 separator = " "315 if self.vector:316 type_name = "{}{}*{}".format(self.type.c_name, separator, self.name)317 else:318 type_name = "{}{}{}".format(self.type.c_name, separator, self.name)319 return type_name320 @property321 def c_type(self):322 if self.type.c_name.endswith('*'):323 separator = ""324 else:325 separator = " "326 if self.vector:327 if self.vector_size:328 return "{}{}{}[{}]".format(self.type.c_name, separator, self.name, self.vector_size)329 else:330 return "{}{}*{}".format(self.type.c_name, separator, self.name)331 else:332 return "{}{}{}".format(self.type.c_name, separator, self.name)333 @property334 def cast(self):335 if self.type.c_name.endswith('*'):336 separator = ""337 else:338 separator = " "339 if self.vector:340 return "{}{}*".format(self.type.c_name, separator)341 else:342 return "{}".format(self.type.c_name)343 344 @property345 def python_type(self):346 if isinstance(self.type, (StructDefinition, TableDefinition, EnumDefinition)):347 s = self.type.name348 elif self.type is Primitives.String:349 s = 'str'350 elif self.type is Primitives.Boolean:351 s = 'bool'352 elif self.type in INTEGER_PRIMITIVES:353 s = 'int'354 if self.vector:355 s = "Optional[List[{}]]".format(s)356 else:357 s = "Optional[{}]".format(s)358 return s359 @property360 def python_parameter(self):361 return "{}: {} = None".format(self.name, self.python_type)362 def generate_typedef_field(self):363 self.add_line("{};".format(self.c_type))364 if self.vector and self.vector_size is None:365 self.add_line("size_t {}_length;".format(self.name))366 def generate_typedef_present(self):367 self.add_line("bool {}_present: 1;".format(self.name))368 def generate_signatures(self, parent: Union[StructDefinition, TableDefinition]):369 self.set_parameter("struct", parent.name)370 self.set_parameter("field", self.name)371 self.set_parameter("type_name", self.type.name)372 self.set_parameter(373 "default",374 '"{}"'.format(self.default)375 if isinstance(self.default, str) else376 self.default377 )378 params = {379 "s": """380 Any {struct}_t, returned from one of `{struct}_new()`, `{struct}_copy()`,381 or `{struct}_deserialize()`.382 """,383 }384 if self.vector:385 if self.vector_size is None:386 params[self.name] = "Array of `{field}_length` {type_name} to store in the {struct}."387 else:388 params[self.name] = "Array of " + str(self.vector_size) + " {type_name} to store in the {struct}."389 else:390 params[self.name] = "{type_name} to store in the {struct}."391 if self.vector and self.vector_size is None:392 if isinstance(self.type, StructDefinition) or self.type is Primitives.String:393 params[self.name] = "{type_name} to store in the {field} field of the given {struct}."394 self.add_c_comment(395 comment="Store a copy of the given {type_name} array in the given {struct}.",396 return_comment="True on success, false if memory allocation fails.",397 **params398 )399 self.add_line("bool {struct}_set_{field}({struct}_t *s, " + self.param_type + ", size_t {field}_length);")400 self.skip_line()401 if self.default is not None:402 self.add_c_comment(403 comment="""404 Retrieve the {field} field from the given {struct} if that field is present,405 or the default value ({default}) if the {field} field is not present.406 """,407 return_comment="Always returns true."408 )409 else:410 self.add_c_comment(411 comment="Retrieve the {field} field from the given {struct}.",412 return_comment="True if the field is present, false otherwise."413 )414 self.add_line("bool {struct}_get_{field}({struct}_t *s, " + self.pointer_type + ", size_t *{field}_length);")415 self.skip_line()416 else:417 if self.vector:418 self.add_c_comment(419 comment="Store a copy of the given {type_name} array in the {field} field of the given {struct}.",420 return_comment="True on success, false if memory allocation fails.",421 **params422 )423 elif isinstance(self.type, StructDefinition) or self.type is Primitives.String:424 self.add_c_comment(425 comment="Store a copy of the given {type_name} in the {field} field of the given {struct}.",426 return_comment="True on success, false if memory allocation fails.",427 **params428 )429 else:430 self.add_c_comment(431 comment="Store the given {type_name} in the {field} field of the given {struct}.",432 return_comment="True on success, false if memory allocation fails.",433 **params434 )435 self.add_line("bool {struct}_set_{field}({struct}_t *s, " + self.param_type + ");")436 self.skip_line()437 if self.default is not None:438 self.add_c_comment(439 comment="""440 Retrieve the {field} field from the given {struct} if that field is present,441 or the default value ({default}) if the {field} field is not present.442 """,443 return_comment="Always returns true."444 )445 else:446 self.add_c_comment(447 comment="Retrieve the {field} field from the given {struct}.",448 return_comment="True if the field is present, false otherwise."449 )450 self.add_line("bool {struct}_get_{field}({struct}_t *s, " + self.pointer_type + ");")451 self.skip_line()452 def generate_free(self, parent: Union[StructDefinition, TableDefinition]):453 self.push_parameters()454 self.set_parameter("name", self.name)455 self.set_parameter("type_name", self.type.name)456 if self.vector:457 self.start_block("if (s->{name}_present) {{")458 self.add_line("s->{name}_present = false;")459 if self.type is Primitives.String:460 if self.vector_size is None:461 self.start_block("for (size_t i=0; i < s->{name}_length; i++) {{")462 else:463 self.start_block("for (size_t i=0; i < " + str(self.vector_size) + "; i++) {{")464 self.add_line("free(s->{name}[i]);")465 self.end_block("}}")466 elif isinstance(self.type, (TableDefinition, StructDefinition)):467 if self.vector_size is None:468 self.start_block("for (size_t i=0; i < s->{name}_length; i++) {{")469 else:470 self.start_block("for (size_t i=0; i < " + str(self.vector_size) + "; i++) {{")471 self.add_line("{type_name}_free(s->{name}[i]);")472 self.end_block("}}")473 if self.vector_size is None:474 self.add_line("free(s->{name});")475 self.end_block("}}")476 elif self.type is Primitives.String:477 self.start_block("if (s->{name}_present) {{")478 self.add_line("s->{name}_present = false;")479 self.add_line("free(s->{name});")480 self.end_block("}}")481 elif isinstance(self.type, (TableDefinition, StructDefinition)):482 self.start_block("if (s->{name}_present) {{")483 self.add_line("s->{name}_present = false;")484 self.add_line("{type_name}_free(s->{name});")485 self.end_block("}}")486 self.pop_parameters()487 def generate_c_deserialize(self, parent: Union[StructDefinition, TableDefinition]):488 self.push_parameters()489 self.set_parameter("name", self.name)490 self.set_parameter("type_name", self.type.name)491 self.set_parameter("field_id", self.field_id)492 self.start_block("if (bitfield[{}] & {}) {{{{".format(493 self.field_id // 8,494 1 << (7 - self.field_id & 7)495 ))496 if self.vector:497 self.set_parameter("length", self.vector_size if self.vector_size is not None else "vector_size")498 # Pre for loop499 if self.vector_size is None:500 self.deserialize_c_varint("vector_size", declare=True)501 self.add_line("s->{name} = malloc(vector_size * sizeof(*s->{name}));")502 self.start_block("for (size_t i = 0; i < {length}; i++) {{")503 # Body of the for loop504 if isinstance(self.type, (TableDefinition, StructDefinition)):505 self.deserialize_c_varint("{name}_size", declare=True)506 self.start_block("if (bytes_read + {name}_size > buffer_size) {{")507 self.start_block("for (size_t j = 0; j < i; j++) {{")508 self.add_line("{type_name}_free(s->{name}[j]);")509 self.end_block("}}")510 self.add_debug('printf("Not enough bytes remaining to deserialize nested table: (%zu/%zu)\\n", buffer_size-bytes_read, {name}_size);')511 self.add_line("goto ERROR;")512 self.end_block("}}")513 self.add_line("{type_name}_t *{name} = {type_name}_deserialize(buffer + bytes_read, {name}_size);")514 self.start_block("if ({name} == NULL) {{")515 self.start_block("for (size_t j = 0; j < i; j++) {{")516 self.add_line("{type_name}_free(s->{name}[j]);")517 self.end_block("}}")518 self.add_debug('printf("Nested deserialize failed\\n");')519 self.add_line("goto ERROR;")520 self.end_block("}}")521 self.add_line("bytes_read += {name}_size;")522 self.add_line("s->{name}[i] = {name};")523 elif isinstance(self.type, EnumDefinition):524 self.deserialize_c_int("{name}", self.type.size.byte_width, declare=True)525 self.add_line("s->{name}[i] = ({type_name}_e){name};")526 elif self.type in INTEGER_PRIMITIVES:527 self.deserialize_c_int("{name}", self.type.byte_width, declare=True, signed=self.type.signed)528 self.add_line("s->{name}[i] = {name};")529 elif self.type is Primitives.String: 530 self.deserialize_c_varint("{name}_length", declare=True)531 self.start_block("if (bytes_read + {name}_length > buffer_size) {{")532 self.add_debug('printf("Not enough bytes remaining to deserialize string: (%zu/%zu)\\n", buffer_size-bytes_read, {name}_length);')533 self.add_line("goto ERROR;")534 self.end_block("}}")535 self.add_line("char *{name} = malloc({name}_length + 1);")536 self.start_block("if ({name} == NULL) {{")537 self.add_debug('printf("Malloc failure\\n");')538 self.add_line("goto ERROR;")539 self.end_block("}}")540 self.add_line("memcpy({name}, buffer + bytes_read, {name}_length);")541 self.add_line("{name}[{name}_length] = '\\0';")542 self.add_line("bytes_read += {name}_length;")543 self.add_line("s->{name}[i] = {name};")544 elif self.type is Primitives.Boolean:545 self.start_block("if (bytes_read + 1 > buffer_size) {{")546 self.add_debug('printf("Not enough bytes remaining to deserialize bool\\n");')547 self.add_line("goto ERROR;")548 self.end_block("}}")549 self.add_line("s->{name}[i] = (bool)(buffer[bytes_read + i / 8] & (1 << (7 - (i & 7))));")550 self.end_block("}}")551 # Post for loop552 if self.vector_size is None:553 self.add_line("s->{name}_length = vector_size;")554 self.add_line("s->{name}_present = true;")555 if self.type is Primitives.Boolean:556 self.add_line("bytes_read += (({length} - 1) / 8) + 1;")557 else:558 if isinstance(self.type, (TableDefinition, StructDefinition)):559 self.deserialize_c_varint("{name}_size", declare=True)560 self.start_block("if (bytes_read + {name}_size > buffer_size) {{")561 self.add_debug('printf("Not enough bytes remaining to deserialize nested table: (%zu/%zu)\\n", buffer_size-bytes_read, {name}_size);')562 self.add_line("goto ERROR;")563 self.end_block("}}")564 self.add_line("{type_name}_t *{name} = {type_name}_deserialize(buffer + bytes_read, {name}_size);")565 self.start_block("if ({name} == NULL) {{")566 self.add_debug('printf("Nested deserialize failed\\n");')567 self.add_line("goto ERROR;")568 self.end_block("}}")569 self.add_line("bytes_read += {name}_size;")570 self.add_line("s->{name} = {name};")571 self.add_line("s->{name}_present = true;")572 elif isinstance(self.type, EnumDefinition):573 self.deserialize_c_int("{name}", self.type.size.byte_width, declare=True)574 self.add_line("s->{name} = ({type_name}_e){name};")575 self.add_line("s->{name}_present = true;")576 elif self.type in INTEGER_PRIMITIVES:577 self.deserialize_c_int("{name}", self.type.byte_width, declare=True, signed=self.type.signed)578 self.add_line("s->{name} = {name};")579 self.add_line("s->{name}_present = true;")580 elif self.type is Primitives.String: 581 self.deserialize_c_varint("{name}_length", declare=True)582 self.start_block("if (bytes_read + {name}_length > buffer_size) {{")583 self.add_debug('printf("Not enough bytes remaining to deserialize string: (%zu/%zu)\\n", buffer_size-bytes_read, {name}_length);')584 self.add_line("goto ERROR;")585 self.end_block("}}")586 self.add_line("char *{name} = malloc({name}_length + 1);")587 self.start_block("if ({name} == NULL) {{")588 self.add_debug('printf("Malloc failed\\n");')589 self.add_line("goto ERROR;")590 self.end_block("}}")591 self.add_line("memcpy({name}, buffer + bytes_read, {name}_length);")592 self.add_line("{name}[{name}_length] = '\\0';")593 self.add_line("bytes_read += {name}_length;")594 self.add_line("s->{name} = {name};")595 self.add_line("s->{name}_present = true;")596 elif self.type is Primitives.Boolean:597 self.start_block("if (bytes_read + 1 > buffer_size) {{")598 self.add_debug('printf("Not enough bytes remaining to deserialize bool\\n");')599 self.add_line("goto ERROR;")600 self.end_block("}}")601 self.add_line("s->{name} = (bool)buffer[bytes_read];")602 self.add_line("bytes_read += 1;")603 self.add_line("s->{name}_present = true;")604 self.end_block("}}")605 self.pop_parameters()606 def generate_c_serialize(self, parent: Union[StructDefinition, TableDefinition]):607 self.push_parameters()608 self.set_parameter("name", self.name)609 self.set_parameter("type_name", self.type.name)610 self.set_parameter("field_id", self.field_id)611 self.start_block("if (s->{name}_present) {{")612 self.add_line("buffer[bitfield_index+{}] |= {};".format(613 self.field_id // 8,614 1 << (7 - self.field_id & 7)615 ))616 617 if self.vector:618 self.set_parameter("length", self.vector_size if self.vector_size is not None else "s->{}_length".format(self.name))619 if self.vector_size is None:620 self.serialize_c_varint("{length}")621 # Pre for-loop622 if isinstance(self.type, (TableDefinition, StructDefinition)):623 pass624 elif self.type is Primitives.String:625 pass626 elif isinstance(self.type, EnumDefinition):627 self.set_parameter("byte_width", self.type.size.byte_width)628 self.set_parameter("c_type", self.type.size.c_name)629 if self.type.size.byte_width in (2,4,8):630 converter = "htobe{}".format(self.type.size.byte_width * 8)631 else:632 converter = ""633 self.set_parameter("converter", converter)634 self.c_allocate("{byte_width} * {length}")635 elif self.type in INTEGER_PRIMITIVES:636 self.set_parameter("byte_width", self.type.byte_width)637 self.set_parameter("c_type", self.type.c_name)638 if self.type.byte_width in (2,4,8):639 converter = "htobe{}".format(self.type.byte_width * 8)640 else:641 converter = ""642 self.set_parameter("converter", converter)643 self.c_allocate("{byte_width} * {length}")644 elif self.type is Primitives.Boolean:645 self.add_line("size_t byte_count = (({length} - 1) / 8) + 1;")646 self.c_allocate("byte_count")647 self.add_line("bzero(buffer + bytes_written, byte_count);")648 self.start_block("for (size_t i = 0; i < {length}; i++) {{")649 # Mid for-loop650 if isinstance(self.type, (TableDefinition, StructDefinition)):651 self.add_line("uint8_t *child_buffer;")652 self.add_line("size_t child_buffer_size;")653 self.add_line("{type_name}_serialize(s->{name}[i], &child_buffer, &child_buffer_size);")654 self.serialize_c_varint("child_buffer_size")655 self.c_allocate("child_buffer_size")656 self.add_line("memcpy(buffer + bytes_written, child_buffer, child_buffer_size);")657 self.add_line("bytes_written += child_buffer_size;")658 self.add_line("free(child_buffer);")659 elif isinstance(self.type, EnumDefinition):660 self.add_line("*({c_type}*)(buffer + bytes_written) = {converter}(({c_type})s->{name}[i]);")661 self.add_line("bytes_written += {byte_width};")662 elif self.type in INTEGER_PRIMITIVES:663 self.add_line("(({c_type}*)(buffer + bytes_written))[0] = {converter}(({c_type})s->{name}[i]);")664 self.add_line("bytes_written += {byte_width};")665 elif self.type is Primitives.String:666 self.add_line("size_t string_size = strlen(s->{name}[i]);")667 self.serialize_c_varint("string_size")668 self.c_allocate("string_size")669 self.add_line("memcpy(buffer + bytes_written, s->{name}[i], string_size);")670 self.add_line("bytes_written += string_size;")671 elif self.type is Primitives.Boolean:672 self.start_block("if (s->{name}[i]) {{")673 self.add_line("(buffer + bytes_written + i / 8)[0] |= 1 << (7 - (i & 7));")674 self.end_block("}}")675 else:676 raise TypeError("Unrecognized member type '{}'".format(self.type))677 self.end_block("}}")678 # Post for-loop679 if self.type is Primitives.Boolean:680 self.add_line("bytes_written += byte_count;")681 else:682 if isinstance(self.type, (TableDefinition, StructDefinition)):683 self.add_line("uint8_t *child_buffer;")684 self.add_line("size_t child_buffer_size;")685 self.add_line("{type_name}_serialize(s->{name}, &child_buffer, &child_buffer_size);")686 self.serialize_c_varint("child_buffer_size")687 self.c_allocate("child_buffer_size")688 self.add_line("memcpy(buffer + bytes_written, child_buffer, child_buffer_size);")689 self.add_line("bytes_written += child_buffer_size;")690 self.add_line("free(child_buffer);")691 elif isinstance(self.type, EnumDefinition):692 if self.type.size.byte_width in (2,4,8):693 converter = "htobe{}".format(self.type.size.byte_width * 8)694 else:695 converter = ""696 self.set_parameter("converter", converter)697 self.set_parameter("byte_width", self.type.size.byte_width)698 self.set_parameter("c_type", self.type.size.c_name)699 self.c_allocate("{byte_width}")700 self.add_line("(({c_type}*)(buffer + bytes_written))[0] = {converter}(s->{name});")701 self.add_line("bytes_written += {byte_width};")702 elif self.type in INTEGER_PRIMITIVES:703 if self.type.byte_width in (2,4,8):704 converter = "htobe{}".format(self.type.byte_width * 8)705 else:706 converter = ""707 self.set_parameter("converter", converter)708 self.set_parameter("byte_width", self.type.byte_width)709 self.set_parameter("c_type", self.type.c_name)710 self.c_allocate("{byte_width}")711 self.add_line("(({c_type}*)(buffer + bytes_written))[0] = {converter}(s->{name});")712 self.add_line("bytes_written += {byte_width};")713 elif self.type is Primitives.String:714 self.add_line("size_t string_size = strlen(s->{name});")715 self.serialize_c_varint("string_size")716 self.c_allocate("string_size")717 self.add_line("memcpy(buffer + bytes_written, s->{name}, string_size);")718 self.add_line("bytes_written += string_size;")719 elif self.type is Primitives.Boolean:720 self.c_allocate(1)721 self.start_block("if (s->{name}) {{")722 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = 1;")723 self.end_block("}}")724 self.start_block("else {{")725 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = 0;")726 self.end_block("}}")727 self.add_line("bytes_written += 1;")728 else:729 raise TypeError("Unrecognized member type '{}'".format(self.type))730 self.end_block("}}")731 self.pop_parameters()732 def generate_copy_sets(self, parent: Union[StructDefinition, TableDefinition]):733 self.push_parameters()734 self.set_parameter("parent_name", parent.name)735 self.set_parameter("name", self.name)736 self.set_parameter("cast", self.cast)737 self.start_block("if (s->{name}_present) {{")738 if self.vector and not self.vector_size:739 self.start_block("if (!{parent_name}_set_{name}(new_s, ({cast})s->{name}, s->{name}_length)) {{")740 else:741 self.start_block("if (!{parent_name}_set_{name}(new_s, s->{name})) {{")742 self.add_line("{parent_name}_free(new_s);")743 self.add_line("return NULL;")744 self.end_block("}}")745 self.end_block("}}")746 self.pop_parameters()747 def generate_get_set(self, parent: Union[StructDefinition, TableDefinition]):748 self.push_parameters()749 self.set_parameter("parent_name", parent.name)750 self.set_parameter("name", self.name)751 self.set_parameter("param_type", self.param_type)752 self.set_parameter("pointer_type", self.pointer_type)753 self.set_parameter("type_name", self.type.name)754 self.set_parameter("vector_size", self.vector_size)755 self.set_parameter("default", self.default)756 if self.vector_size is None:757 self.set_parameter("length", "{}_length".format(self.name))758 else:759 self.set_parameter("length", str(self.vector_size))760 # Set761 if self.vector and self.vector_size is None:762 self.start_block("bool {parent_name}_set_{name}({parent_name}_t *s, {param_type}, size_t {name}_length) {{")763 else:764 self.start_block("bool {parent_name}_set_{name}({parent_name}_t *s, {param_type}) {{")765 if self.vector:766 self.generate_free(parent)767 # Allocate space for sizeless arrays768 if self.vector_size is None:769 self.add_line("s->{name}_length = {name}_length;")770 self.add_line("s->{name} = malloc(sizeof(*{name}) * {name}_length);")771 self.start_block("if (s->{name} == NULL) {{")772 self.add_line("return false;")773 self.end_block("}}")774 # Strdup() in all strings775 if self.type is Primitives.String:776 self.start_block("for (size_t i=0; i < {length}; i++) {{")777 self.add_line("s->{name}[i] = strdup({name}[i]);")778 self.start_block("if (s->{name}[i] == NULL) {{")779 self.start_block("for (size_t j=0; j < i; j++) {{")780 self.add_line("free(s->{name}[j]);")781 self.end_block("}}")782 self.add_line("return false;")783 self.end_block("}}")784 self.end_block("}}")785 # <name>_copy() in all structs786 elif isinstance(self.type, (TableDefinition, StructDefinition)):787 self.start_block("for (size_t i=0; i < {length}; i++) {{")788 self.add_line("s->{name}[i] = {type_name}_copy({name}[i]);")789 self.start_block("if (s->{name}[i] == NULL) {{")790 self.start_block("for (size_t j=0; j < i; j++) {{")791 self.add_line("{type_name}_free(s->{name}[j]);")792 self.end_block("}}")793 self.add_line("return false;")794 self.end_block("}}")795 self.end_block("}}")796 else:797 self.add_line("memcpy(s->{name}, {name}, sizeof(*{name}) * {length});")798 elif self.type is Primitives.String:799 self.generate_free(parent)800 self.add_line("s->{name} = strdup({name});")801 self.start_block("if (s->{name} == NULL) {{")802 self.add_line("return false;")803 self.end_block("}}")804 elif isinstance(self.type, (TableDefinition, StructDefinition)):805 self.generate_free(parent)806 self.add_line("s->{name} = {type_name}_copy({name});")807 self.start_block("if (s->{name} == NULL) {{")808 self.add_line("return false;")809 self.end_block("}}")810 else:811 self.add_line("s->{name} = {name};")812 self.add_line("s->{name}_present = true;")813 self.add_line("return true;")814 self.end_block("}}")815 self.skip_line()816 # Get817 if self.vector and self.vector_size is None:818 self.start_block("bool {parent_name}_get_{name}({parent_name}_t *s, {pointer_type}, size_t *{name}_length) {{")819 else:820 self.start_block("bool {parent_name}_get_{name}({parent_name}_t *s, {pointer_type}) {{")821 self.start_block("if (s->{name}_present) {{")822 if self.vector and self.vector_size is not None:823 self.add_line("*{name} = (void *)&s->{name};")824 else:825 self.add_line("*{name} = s->{name};")826 if self.vector and self.vector_size is None:827 self.add_line("*{name}_length = s->{name}_length;")828 self.end_block("}}")829 self.start_block("else {{")830 if isinstance(self.default, str):831 self.add_line('*{name} = (char *)"{default}";')832 elif isinstance(self.default, int):833 self.add_line("*{name} = {default};")834 else:835 self.add_line("return false;")836 self.end_block("}}")837 self.add_line("return true;")838 self.end_block("}}")839 self.skip_line()840 self.pop_parameters()841 def generate_print(self, parent: Union[StructDefinition, TableDefinition]):842 self.push_parameters()843 self.set_parameter("name", self.name)844 self.set_parameter("type_name", self.type.name)845 self.start_block("if (s->{name}_present) {{")846 if self.vector:847 self.set_parameter("length", self.vector_size if self.vector_size is not None else "s->{}_length".format(self.name))848 self.add_line('printf_indent(indent, "{name} [\\n");')849 self.add_line("indent += 1;")850 self.start_block("for (size_t i = 0; i < {length}; i++) {{")851 if isinstance(self.type, (TableDefinition, StructDefinition)):852 self.add_line("{type_name}_print_indent(s->{name}[i], indent);")853 elif isinstance(self.type, EnumDefinition):854 self.add_line('printf_indent(indent, "%u\\n", s->{name}[i]);')855 elif self.type in INTEGER_PRIMITIVES:856 self.add_line('printf_indent(indent, "%x\\n", s->{name}[i]);')857 elif self.type is Primitives.String: 858 self.add_line('printf_indent(indent, "\\"%s\\"\\n", s->{name}[i]);')859 elif self.type is Primitives.Boolean:860 self.add_line('printf_indent(indent, "%s\\n", (s->{name}[i] ? "True" : "False"));')861 self.end_block("}}")862 self.add_line("indent -= 1;")863 self.add_line('printf_indent(indent, "]\\n");')864 else:865 if isinstance(self.type, (TableDefinition, StructDefinition)):866 self.add_line("{type_name}_print_indent(s->{name}, indent);")867 elif isinstance(self.type, EnumDefinition):868 self.add_line('printf_indent(indent, "{name} = %u\\n", s->{name});')869 elif self.type in INTEGER_PRIMITIVES:870 self.add_line('printf_indent(indent, "{name} = %x\\n", s->{name});')871 elif self.type is Primitives.String: 872 self.add_line('printf_indent(indent, "{name} = \\"%s\\"\\n", s->{name});')873 elif self.type is Primitives.Boolean:874 self.add_line('printf_indent(indent, "{name} = %s\\n", (s->{name} ? "True" : "False"));')875 self.end_block("}}")876 self.start_block("else {{")877 self.add_line('printf_indent(indent, "{name} not present.\\n");')878 self.end_block("}}")879 self.pop_parameters()880 def generate_python_serialize(self):881 """882 Struct Member - Serialize883 """884 if self.vector:885 if self.vector_size is None:886 self.add_line("field_length = len(self.{field})")887 self.serialize_py_varint("field_length")888 else:889 self.set_parameter("vector_size", self.vector_size)890 self.add_line("field_length = {vector_size}")891 if self.type is Primitives.Boolean:892 self.add_line("data = bytearray(((field_length - 1) // 8) + 1)")893 self.start_block("for i in range(field_length):")894 if isinstance(self.type, (StructDefinition, TableDefinition)):895 self.add_line("child_buf = self.{field}[i].serialize()")896 self.add_line("child_length = len(child_buf)")897 self.serialize_py_varint("child_length")898 self.add_line("buf.extend(child_buf)")899 elif isinstance(self.type, EnumDefinition):900 self.set_parameter("bit_width", self.type.size.byte_width*8)901 self.add_line("buf.extend(uint{bit_width}(self.{field}[i]))")902 elif self.type is Primitives.String:903 self.add_line("string_length = len(self.{field}[i])")904 self.serialize_py_varint("string_length")905 self.add_line("buf.extend(self.{field}[i].encode('utf-8'))")906 elif self.type is Primitives.Boolean:907 self.start_block("if self.{field}[i]:")908 self.add_line("data[i//8] |= 1 << (7 - (i & 7))")909 self.end_block()910 elif self.type in INTEGER_PRIMITIVES:911 self.set_parameter("bit_width", self.type.byte_width*8)912 self.add_line("buf.extend(uint{bit_width}(self.{field}[i]))")913 else:914 raise TypeError("Unrecognized struct member type")915 self.end_block()916 if self.type is Primitives.Boolean:917 self.add_line("buf.extend(data)")918 else:919 if isinstance(self.type, (StructDefinition, TableDefinition)):920 self.add_line("child_buf = self.{field}.serialize()")921 self.add_line("child_length = len(child_buf)")922 self.serialize_py_varint("child_length")923 self.add_line("buf.extend(child_buf)")924 elif isinstance(self.type, EnumDefinition):925 self.set_parameter("bit_width", self.type.size.byte_width*8)926 self.add_line("buf.extend(uint{bit_width}(self.{field}))")927 elif self.type is Primitives.String:928 self.add_line("string_length = len(self.{field})")929 self.serialize_py_varint("string_length")930 self.add_line("buf.extend(self.{field}.encode('utf-8'))")931 elif self.type is Primitives.Boolean:932 self.start_block("if self.{field}:")933 self.add_line("buf.extend(uint8(1))")934 self.end_block()935 self.start_block("else:")936 self.add_line("buf.extend(uint8(0))")937 self.end_block()938 elif self.type in INTEGER_PRIMITIVES:939 self.set_parameter("bit_width", self.type.byte_width*8)940 self.add_line("buf.extend(uint{bit_width}(self.{field}))")941 else:942 raise TypeError("Unrecognized struct member type")943 def generate_python_deserialize(self):944 """945 Struct Member - Deserialize946 """947 self.start_block("if buf[bitfield_index + {}] & {}:".format(948 self.field_id // 8,949 1 << (7 - self.field_id & 7)950 ))951 if self.vector:952 # Before for loop953 if self.vector_size is None:954 self.deserialize_py_varint("field_length")955 else:956 self.set_parameter("vector_size", self.vector_size)957 self.add_line("field_length = {vector_size}")958 959 self.add_line("values = []")960 if self.type is Primitives.Boolean:961 self.add_line("data_size = ((field_length - 1) // 8) + 1")962 self.add_line("data = buf[buf_index:buf_index + data_size]")963 self.add_line("buf_index += data_size")964 965 # During for loop966 self.start_block("for i in range(field_length):")967 if isinstance(self.type, (StructDefinition, TableDefinition)):968 self.deserialize_py_varint("child_length")969 self.add_line("child_buf = buf[buf_index:buf_index+child_length]")970 self.add_line("buf_index += child_length")971 self.add_line("values.append({type_name}.deserialize(child_buf))")972 elif isinstance(self.type, EnumDefinition):973 self.set_parameter("byte_width", self.type.size.byte_width)974 self.add_line("values.append({type_name}(unsigned_int(buf[buf_index:buf_index+{byte_width}])))")975 self.add_line("buf_index += {byte_width}")976 elif self.type is Primitives.String:977 self.deserialize_py_varint("string_length")978 self.add_line("values.append(buf[buf_index:buf_index+string_length].decode('utf-8'))")979 self.add_line("buf_index += string_length")980 elif self.type is Primitives.Boolean:981 self.start_block("if data[i//8] & (1 << (7 - (i & 7))):")982 self.add_line("values.append(True)")983 self.end_block()984 self.start_block("else:")985 self.add_line("values.append(False)")986 self.end_block()987 elif self.type in INTEGER_PRIMITIVES:988 self.set_parameter("byte_width", self.type.byte_width)989 self.set_parameter("int_type", "signed_int" if self.type.signed else "unsigned_int")990 self.add_line("values.append({int_type}(buf[buf_index:buf_index+{byte_width}]))")991 self.add_line("buf_index += {byte_width}")992 else:993 raise TypeError("Unrecognized struct member type")994 self.end_block()995 # After for loop996 self.add_line("table.{field} = values")997 else:998 if isinstance(self.type, (StructDefinition, TableDefinition)):999 self.deserialize_py_varint("child_length")1000 self.add_line("child_buf = buf[buf_index:buf_index+child_length]")1001 self.add_line("buf_index += child_length")1002 self.add_line("table.{field} = {type_name}.deserialize(child_buf)")1003 elif isinstance(self.type, EnumDefinition):1004 self.set_parameter("byte_width", self.type.size.byte_width)1005 self.add_line("table.{field} = {type_name}(unsigned_int(buf[buf_index:buf_index+{byte_width}]))")1006 self.add_line("buf_index += {byte_width}")1007 elif self.type is Primitives.String:1008 self.deserialize_py_varint("string_length")1009 self.add_line("table.{field} = buf[buf_index:buf_index+string_length].decode('utf-8')")1010 self.add_line("buf_index += string_length")1011 elif self.type is Primitives.Boolean:1012 self.add_line("table.{field} = (buf[buf_index] != 0)")1013 self.add_line("buf_index += 1")1014 elif self.type in INTEGER_PRIMITIVES:1015 self.set_parameter("byte_width", self.type.byte_width)1016 self.set_parameter("int_type", "signed_int" if self.type.signed else "unsigned_int")1017 self.add_line("table.{field} = {int_type}(buf[buf_index:buf_index+{byte_width}])")1018 self.add_line("buf_index += {byte_width}")1019 else:1020 raise TypeError("Unrecognized struct member type")1021 self.end_block()1022 def generate_python_initialize(self):1023 """1024 Struct Member - Initialization1025 """1026 self.set_parameter("type_name", self.type.name)1027 if self.vector and self.vector_size is None:1028 self.start_block("if {field} is None:")1029 self.add_line("self.{field} = []")1030 self.end_block()1031 self.start_block("else:")1032 self.add_line("self.{field} = {field}")1033 self.end_block()1034 else:1035 self.add_line("self.{field} = {field}")1036@dataclass1037class EnumMember(SchemaElement):1038 schema: Schema1039 name: str1040 value: Optional[int] = None1041 def resolve_value(self, parent):1042 if self.value is None:1043 self.value = parent.next_value1044 parent.values.add(self.value)1045 parent.next_value = self.value + 11046@dataclass1047class StructDefinition(SchemaElement):1048 schema: Schema1049 name: str1050 members: List[StructMember] = field(default_factory=list)1051 table_id: Optional[int] = None1052 @property1053 def c_name(self):1054 return "{}_t *".format(self.name)1055 def resolve_types(self, schema):1056 for struct_member in self.members:1057 struct_member.resolve_type(schema)1058 def validate(self):1059 for struct_member in self.members:1060 # Below this are checks run only when a default is present1061 if struct_member.default is None:1062 continue1063 # If this type is a struct or table, verify no default is set1064 elif isinstance(struct_member.type, (StructDefinition, TableDefinition)):1065 raise TypeError("Struct member of type '{} (Struct)' {}.{} cannot have a default value.".format(1066 struct_member.type.name, self.name, struct_member.name))1067 # If this type is an enum, verify default is in the range of enum values1068 elif isinstance(struct_member.type, EnumDefinition):1069 if struct_member.default not in struct_member.type.values:1070 raise TypeError("Struct member of type '{} (Enum)' {}.{} cannot have value '{}'".format(1071 struct_member.type.name, self.name, struct_member.name, struct_member.default))1072 # If this type is an integer primitive, verify default is an int1073 elif struct_member.type in INTEGER_PRIMITIVES:1074 if not isinstance(struct_member.default, int):1075 raise TypeError("The default value of {}.{} must be an integer.".format(1076 self.name, struct_member.name))1077 # If this type is a string primitive, verify default is a string1078 elif struct_member.type is Primitives.String:1079 if not isinstance(struct_member.default, str):1080 raise TypeError("The default value of string {}.{} must be a string.".format(1081 self.name, struct_member.name))1082 # If this type is a boolean primitive, verify default is 0 or 11083 elif struct_member.type is Primitives.Boolean:1084 if struct_member.default != 0 and struct_member.default != 1:1085 raise TypeError("The default value of boolean {}.{} must be true (1) or false (0).".format(1086 self.name, struct_member.name))1087 # Unrecognized type1088 else:1089 raise TypeError("Struct member {}.{} has an unrecognized type: '{}'".format(1090 self.name, struct_member.name, struct_member.type))1091 def generate_typedefs(self):1092 self.set_parameter("name", self.name)1093 self.start_block("typedef struct {name}_t {{")1094 for member in self.members:1095 member.generate_typedef_field()1096 for member in self.members:1097 member.generate_typedef_present()1098 self.end_block("}} {name}_t;")1099 def generate_signatures(self):1100 self.set_parameter("name", self.name)1101 self.add_c_comment(1102 comment="Creates a new {name}_t on the heap.",1103 return_comment="A newly allocated {name}_t. Must be freed with `{name}_free()`."1104 )1105 self.add_line("{name}_t *{name}_new(void);")1106 self.skip_line()1107 self.add_c_comment(1108 comment="Creates a copy of an existing {name}_t.",1109 return_comment="A newly allocated {name}_t. Must be freed with `{name}_free()`."1110 )1111 self.add_line("{name}_t *{name}_copy({name}_t *s);")1112 self.skip_line()1113 self.add_c_comment(1114 comment="""1115 Deallocates an existing {name}_t that was previously allocated with `{name}_new()`,1116 `{name}_copy()`, or `{name}_deserialize()`.1117 """1118 )1119 self.add_line("void {name}_free({name}_t *s);")1120 self.skip_line()1121 self.add_c_comment(1122 comment="Serialize a {name}_t into a buffer. The {name}_t is unchanged.",1123 s="The {name}_t to serialize.",1124 buffer="Place to store the newly allocated buffer.",1125 buffer_size="Place to store the size of the newly allocated buffer.",1126 return_comment="True on success, false if memory allocation fails.",1127 )1128 self.add_line("bool {name}_serialize({name}_t *s, uint8_t **buffer, size_t *buffer_size);")1129 self.skip_line()1130 self.add_c_comment(1131 comment="Parse a buffer that was created by serializing a {name}_t and re-create the {name}_t.",1132 return_comment="A newly allocated {name}_t. Must be freed with `{name}_free()`."1133 )1134 self.add_line("{name}_t *{name}_deserialize(const uint8_t *buffer, size_t buffer_size);")1135 self.skip_line()1136 self.add_c_comment(1137 return_comment="True if the given buffer holds a valid serialized {name}_t, false otherwise.",1138 )1139 self.add_line("bool {name}_verify(const uint8_t *buffer, size_t buffer_size);")1140 self.skip_line()1141 self.add_line("void {name}_print({name}_t *s);")1142 self.skip_line()1143 for member in self.members:1144 member.generate_signatures(self)1145 def generate_c_source(self):1146 self.set_parameter("name", self.name)1147 self.set_parameter("table_id", self.table_id)1148 self.set_parameter("bitfield_byte_width", (len(self.members) - 1) // 8 + 1)1149 # Copy1150 self.start_block("{name}_t *{name}_copy({name}_t *s) {{")1151 self.add_line("{name}_t *new_s = {name}_new();")1152 self.start_block("if (new_s == NULL) {{")1153 self.add_line("return NULL;")1154 self.end_block("}}")1155 for member in self.members:1156 member.generate_copy_sets(self)1157 self.add_line("return new_s;")1158 self.end_block("}}")1159 self.skip_line()1160 # New1161 self.start_block("{name}_t *{name}_new(void) {{")1162 self.add_line("return calloc(1, sizeof({name}_t));")1163 self.end_block("}}")1164 self.skip_line()1165 # Free1166 self.start_block("void {name}_free({name}_t *s) {{")1167 for member in self.members:1168 member.generate_free(self)1169 self.add_line("free(s);")1170 self.end_block("}}")1171 self.skip_line()1172 1173 # Serialize1174 self.start_block("bool {name}_serialize({name}_t *s, uint8_t **out_buffer, size_t *out_buffer_size) {{")1175 self.add_line("size_t buffer_size = 32;")1176 self.add_line("uint8_t *buffer = malloc(buffer_size);")1177 self.add_line("size_t bytes_written = 0;")1178 if self.table_id > 0xFFFFFFFF:1179 self.c_allocate(9)1180 self.add_line("buffer[bytes_written] = 0xFF;")1181 self.add_line("bytes_written += 1;")1182 self.add_line("((uint64_t*)(buffer + bytes_written))[0] = TABLE_TYPE_{name};")1183 self.add_line("bytes_written += 8;")1184 elif self.table_id > 0xFFFF:1185 self.c_allocate(5)1186 self.add_line("buffer[bytes_written] = 0xFE;")1187 self.add_line("bytes_written += 1;")1188 self.add_line("((uint32_t*)(buffer + bytes_written))[0] = TABLE_TYPE_{name};")1189 self.add_line("bytes_written += 4;")1190 elif self.table_id >= 0xFD:1191 self.c_allocate(3)1192 self.add_line("buffer[bytes_written] = 0xFD;")1193 self.add_line("bytes_written += 1;")1194 self.add_line("((uint16_t*)(buffer + bytes_written))[0] = TABLE_TYPE_{name};")1195 self.add_line("bytes_written += 2;")1196 else:1197 self.c_allocate(1)1198 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = TABLE_TYPE_{name};")1199 self.add_line("bytes_written += 1;")1200 if len(self.members) > 0:1201 self.add_line("size_t bitfield_index = bytes_written;")1202 # Allocate room for the bitfield1203 self.c_allocate((len(self.members) - 1) // 8 + 1)1204 self.add_line("bzero(buffer + bytes_written, {bitfield_byte_width});")1205 self.add_line("bytes_written += {bitfield_byte_width};")1206 for member in self.members:1207 member.generate_c_serialize(self)1208 self.add_line("*out_buffer = buffer;")1209 self.add_line("*out_buffer_size = bytes_written;")1210 self.add_line("return true;")1211 self.end_block("}}")1212 self.skip_line()1213 # Deserialize1214 self.start_block("{name}_t *{name}_deserialize(const uint8_t *buffer, size_t buffer_size) {{")1215 self.add_line("{name}_t* s = {name}_new();")1216 self.start_block("if (s == NULL) {{")1217 self.add_line("return NULL;")1218 self.end_block("}}")1219 self.add_line("size_t bytes_read = 0;")1220 self.deserialize_c_varint("table_id", declare=True)1221 self.start_block("if (table_id != {table_id}) {{")1222 self.add_debug('printf("Table ID in buffer doesn\'t match: %zu instead of %i\\n", table_id, {table_id});')1223 self.add_line("goto ERROR;")1224 self.end_block("}}")1225 if len(self.members) > 0:1226 self.add_line("const uint8_t *bitfield = buffer + bytes_read;")1227 self.add_line("bytes_read += {bitfield_byte_width};")1228 self.skip_line()1229 for member in self.members:1230 member.generate_c_deserialize(self)1231 self.add_line("return s;")1232 self.skip_line()1233 self.start_block("ERROR: {{")1234 self.add_line("{name}_free(s);")1235 self.add_line("return NULL;")1236 self.end_block("}}")1237 self.end_block("}}")1238 self.skip_line()1239 # Verify1240 self.start_block("bool {name}_verify(const uint8_t *buffer, size_t buffer_size) {{")1241 self.add_line("size_t bytes_read = 0;")1242 self.deserialize_c_varint("table_id", declare=True)1243 self.start_block("if (table_id != {table_id}) {{")1244 self.add_line("goto ERROR;")1245 self.end_block("}}")1246 self.add_line("return true;")1247 self.start_block("ERROR: {{")1248 self.add_line("return false;")1249 self.end_block("}}")1250 self.end_block("}}")1251 self.skip_line()1252 # Get/Sets1253 for member in self.members:1254 member.generate_get_set(self)1255 # Static Print1256 self.start_block("static void {name}_print_indent({name}_t *s, size_t indent) {{")1257 self.add_line('printf_indent(indent, "{name} {{\\n");')1258 self.add_line("indent += 1;")1259 for member in self.members:1260 member.generate_print(self)1261 self.add_line("indent -= 1;")1262 self.add_line('printf_indent(indent, "}}\\n");')1263 self.end_block("}}")1264 self.skip_line()1265 # Wrapper Print1266 self.start_block("void {name}_print({name}_t *s) {{")1267 self.add_line("{name}_print_indent(s, 0);")1268 self.end_block("}}")1269 1270 def generate_python(self):1271 """1272 Struct Definition1273 """1274 self.set_parameter("cls", self.name)1275 self.set_parameter("table_id", self.table_id)1276 self.set_parameter("bitfield_byte_width", (len(self.members) - 1) // 8 + 1)1277 self.start_block("class {cls}:")1278 self.add_line("__slots__ = [{}]".format(", ".join('"_{}"'.format(m.name) for m in self.members)))1279 self.skip_line()1280 parameters = ['self']1281 for member in self.members:1282 parameters.append(member.python_parameter)1283 self.start_block("def __init__({}):".format(1284 ", ".join(parameters)1285 ))1286 if not self.members:1287 self.add_line("pass")1288 for member in self.members:1289 self.set_parameter("field", member.name)1290 member.generate_python_initialize()1291 self.end_block()1292 self.skip_line()1293 self.start_block("def __repr__(self):")1294 self.add_line("values = []")1295 1296 for member in self.members:1297 self.set_parameter("field", member.name)1298 if member.vector and member.vector_size is None:1299 self.start_block("if len(self.{field}) > 0:")1300 else:1301 self.start_block("if self.{field} is not None:")1302 self.add_line("values.append('{field}=' + repr(self.{field}))")1303 self.end_block()1304 self.add_line("return '{cls}(' + ', '.join(values) + ')'")1305 self.end_block()1306 self.skip_line()1307 for member in self.members:1308 self.set_parameter("field", member.name)1309 self.set_parameter("python_type", member.python_type)1310 self.set_parameter("type_name", member.type.name)1311 self.set_parameter("vector_size", member.vector_size)1312 self.add_line("@property")1313 self.start_block("def {field}(self):")1314 if member.default is not None:1315 if member.type is Primitives.Boolean:1316 self.set_parameter("default", repr(bool(member.default)))1317 else:1318 self.set_parameter("default", repr(member.default))1319 self.start_block("if self._{field} is None:")1320 if isinstance(member.type, EnumDefinition):1321 self.add_line("return {type_name}({default})")1322 else:1323 self.add_line("return {default}")1324 self.end_block()1325 self.start_block("else:")1326 self.add_line("return self._{field}")1327 self.end_block()1328 else:1329 self.add_line("return self._{field}")1330 self.end_block()1331 self.skip_line()1332 self.add_line("@{field}.setter")1333 self.start_block("def {field}(self, value: {python_type}):")1334 if member.vector and member.vector_size:1335 self.start_block("if value is not None and len(value) != {vector_size}:")1336 self.add_line("raise ValueError('{cls}.{field} must be a list of fixed size: {vector_size}')")1337 self.end_block()1338 if not member.vector and isinstance(member.type, EnumDefinition):1339 self.start_block("if value is None:")1340 self.add_line("self._{field} = None")1341 self.end_block()1342 self.start_block("else:")1343 self.add_line("self._{field} = {type_name}(value)")1344 self.end_block()1345 else:1346 self.add_line("self._{field} = value")1347 self.end_block()1348 self.skip_line()1349 1350 self.start_block("def __eq__(self, other):")1351 self.start_block("if type(self) != type(other):")1352 self.add_line("return False")1353 self.end_block()1354 if self.members:1355 self.start_block("try:")1356 for member in self.members:1357 self.set_parameter("field", member.name)1358 self.start_block("if self._{field} != other._{field}:")1359 self.add_line("return False")1360 self.end_block()1361 self.end_block()1362 self.start_block("except AttributeError:")1363 self.add_line("return False")1364 self.end_block()1365 1366 self.add_line("return True")1367 self.end_block()1368 self.skip_line()1369 self.start_block("def serialize(self) -> bytes:")1370 self.add_line("buf = bytearray()")1371 if self.table_id > 0xFFFFFFFF:1372 table_id_bytes = "b'0xFF' + uint64({})".format(self.table_id)1373 elif self.table_id > 0xFFFF:1374 table_id_bytes = "b'0xFE' + uint32({})".format(self.table_id)1375 elif self.table_id >= 0xFD:1376 table_id_bytes = "b'0xFD' + uint16({})".format(self.table_id)1377 else:1378 table_id_bytes = "uint8({})".format(self.table_id)1379 self.add_line("buf.extend({})".format(table_id_bytes))1380 self.add_line("bitfield_index = len(buf)")1381 self.add_line("buf.extend(bytearray({bitfield_byte_width}))")1382 for member in self.members:1383 self.set_parameter("field", member.name)1384 if member.vector and member.vector_size is None:1385 self.start_block("if len(self._{field}) > 0:")1386 else:1387 self.start_block("if self._{field} is not None:")1388 self.add_line("buf[bitfield_index+{}] |= {}".format(1389 member.field_id // 8,1390 1 << (7 - member.field_id & 7)1391 ))1392 member.generate_python_serialize()1393 self.end_block()1394 self.add_line("return bytes(buf)")1395 self.end_block()1396 self.skip_line()1397 self.add_line("@classmethod")1398 self.start_block("def deserialize(cls, buf: Union[bytes, bytearray]) -> {cls}:")1399 self.add_line("buf_index = 0")1400 self.deserialize_py_varint("table_id")1401 self.start_block("if table_id != {table_id}:")1402 self.add_line("raise ValueError('Invalid table ID {{}}, expected {table_id}'.format(table_id))")1403 self.end_block()1404 self.add_line("table = cls()")1405 self.add_line("bitfield_index = buf_index")1406 self.set_parameter("bitfield_byte_width", (len(self.members) - 1) // 8 + 1)1407 self.add_line("buf_index += {bitfield_byte_width}")1408 for member in self.members:1409 self.set_parameter("field", member.name)1410 self.set_parameter("type_name", member.type.name)1411 member.generate_python_deserialize()1412 self.add_line("return table")1413 self.end_block()1414 self.end_block()1415 self.skip_line()1416 self.skip_line()1417class TableDefinition(StructDefinition):1418 pass1419@dataclass1420class EnumDefinition(SchemaElement):1421 schema: Schema1422 name: str1423 members: List[EnumMember] = field(default_factory=list)1424 size: Union[Primitives.Int8, Primitives.UInt8] = 'uint16'1425 next_value: int = 01426 values: Set[int] = field(default_factory=set)1427 @property1428 def c_name(self):1429 return "{}_e".format(self.name)1430 def resolve_types(self, schema):1431 self.size = BUILTIN_TYPES.get(self.size, self.size)1432 if self.size not in INTEGER_PRIMITIVES:1433 raise TypeError("the type of an enum definition must be an integer primitive, not '{}'".format(self.size))1434 for enum_member in self.members:1435 enum_member.resolve_value(self)1436 def validate(self):1437 pass1438 def generate_typedefs(self):1439 self.set_parameter("name", self.name)1440 self.start_block("typedef enum {name}_e {{")1441 for member, end in join_iterate(self.members):1442 line = "{} = {}".format(member.name, member.value)1443 if not end:1444 line += ','1445 self.add_line(line)1446 self.end_block("}} {name}_e;")1447 def generate_python(self):1448 """1449 Enum Definition1450 """1451 self.set_parameter("cls", self.name)1452 self.start_block("class {cls}(IntEnum):")1453 for member in self.members:1454 self.add_line("{} = {}".format(member.name, member.value))1455 self.end_block()1456 self.skip_line()1457 self.skip_line()1458@dataclass1459class Schema:1460 definitions: Dict[str, Union[EnumDefinition, StructDefinition, TableDefinition]]1461 name: str = "ANONYMOUS_SCHEMA"1462 next_table_id: int = 11463 indentation: str = ' '1464 indentation_level: int = 01465 current_output: List[str] = field(default_factory=list)1466 format_parameters: Dict[str, str] = field(default_factory=dict)1467 format_parameters_stack: List[Dict[str, str]] = field(default_factory=list)1468 debug: bool = False1469 def add_c_comment(self, comment="", **kwargs):1470 self.add_comment(comment, opener="/**", line_start=" *", closer=" */", **kwargs)1471 def add_py_comment(self, comment="", **kwargs):1472 self.add_comment(comment, opener="\"\"\"", line_start=" ", closer="\"\"\"", **kwargs)1473 def add_comment(self, comment="", *, return_comment=None, opener, line_start, closer, **kwargs):1474 self.add_line(opener)1475 if comment:1476 for line in textwrap.wrap(re.sub('\s+', ' ', comment.strip()), width=80):1477 self.add_line(line_start + " " + line)1478 self.add_line(line_start)1479 for param, comment in kwargs.items():1480 padding_length = len(param) + len("@param ")1481 for i, line in enumerate(textwrap.wrap(re.sub('\s+', ' ', comment.strip()), width=80)):1482 if i == 0:1483 self.add_line("{} @param {} {}".format(line_start, param, line))1484 else:1485 self.add_line("{} {} {}".format(line_start, " " * padding_length, line))1486 self.add_line(line_start)1487 if return_comment:1488 for i, line in enumerate(textwrap.wrap(re.sub('\s+', ' ', return_comment.strip()), width=80)):1489 if i == 0:1490 self.add_line("{} @return {}".format(line_start, line))1491 else:1492 self.add_line("{} {}".format(line_start, line))1493 else:1494 self.pop_line()1495 self.add_line(closer)1496 1497 def serialize_py_varint(self, expr: str):1498 self.start_block("if {} > 0xFFFFFFFF:".format(expr))1499 self.add_line("buf.extend(uint8(0xFF))")1500 self.add_line("buf.extend(uint64({}))".format(expr))1501 self.end_block()1502 self.start_block("elif {} > 0xFFFF:".format(expr))1503 self.add_line("buf.extend(uint8(0xFE))")1504 self.add_line("buf.extend(uint32({}))".format(expr))1505 self.end_block()1506 self.start_block("elif {} >= 0xFD:".format(expr))1507 self.add_line("buf.extend(uint8(0xFD))")1508 self.add_line("buf.extend(uint16({}))".format(expr))1509 self.end_block()1510 self.start_block("else:")1511 self.add_line("buf.extend(uint8({}))".format(expr))1512 self.end_block()1513 def c_allocate(self, expr: Union[str, int]):1514 self.add_line("allocate_space(&buffer, &buffer_size, bytes_written, {});".format(expr))1515 def serialize_c_varint(self, expr: str):1516 self.add_line("serialize_varint(&buffer, buffer_size, &bytes_written, {});".format(expr))1517 def deserialize_py_varint(self, name: str):1518 self.add_line("specifier = buf[buf_index]")1519 self.add_line("buf_index += 1")1520 self.start_block("if specifier == 0xFF:")1521 self.add_line("{} = unsigned_int(buf[buf_index:buf_index+8])".format(name))1522 self.add_line("buf_index += 8")1523 self.end_block()1524 self.start_block("elif specifier == 0xFE:")1525 self.add_line("{} = unsigned_int(buf[buf_index:buf_index+4])".format(name))1526 self.add_line("buf_index += 4")1527 self.end_block()1528 self.start_block("elif specifier == 0xFD:")1529 self.add_line("{} = unsigned_int(buf[buf_index:buf_index+2])".format(name))1530 self.add_line("buf_index += 2")1531 self.end_block()1532 self.start_block("else:")1533 self.add_line("{} = specifier".format(name))1534 self.end_block()1535 def deserialize_c_int(self, name: str, size: int, *, declare: bool = False, signed: bool = False):1536 self.start_block("if (bytes_read + {} > buffer_size) {{{{".format(size))1537 self.add_line("goto ERROR;")1538 self.end_block("}}")1539 ctype = "{}int{}_t".format("" if signed else "u", size * 8)1540 if size in (2,4,8):1541 converter = "be{}toh".format(size * 8)1542 else:1543 converter = ""1544 if declare:1545 self.add_line("{} {} = {}(*({}*)(buffer+bytes_read));".format(ctype, name, converter, ctype))1546 else:1547 self.add_line("{} = {}(*({}*)(buffer+bytes_read));".format(name, converter, ctype))1548 self.add_line("bytes_read += {};".format(size))1549 def deserialize_c_varint(self, name: str, *, declare: bool = False):1550 if declare:1551 self.add_line("size_t {} = parse_varint(buffer, buffer_size, &bytes_read);".format(name))1552 else:1553 self.add_line("{} = parse_varint(buffer, buffer_size, &bytes_read);".format(name))1554 self.start_block("if ({} == SIZE_MAX) {{{{".format(name))1555 self.add_line("goto ERROR;")1556 self.end_block("}}")1557 1558 def set_parameter(self, key, value):1559 self.format_parameters[key] = value1560 def push_parameters(self):1561 self.format_parameters_stack.append(self.format_parameters.copy())1562 def pop_parameters(self):1563 self.format_parameters = self.format_parameters_stack.pop()1564 def add_debug(self, code=None):1565 if self.debug:1566 self.add_line(code)1567 def add_line(self, code=None):1568 if code:1569 self.current_output.append(1570 self.indentation * self.indentation_level + code.format(**self.format_parameters)1571 )1572 else:1573 self.current_output.append('')1574 def pop_line(self):1575 return self.current_output.pop()1576 def skip_line(self, count=1):1577 for i in range(count):1578 self.current_output.append('')1579 def start_block(self, code=""):1580 if code:1581 self.add_line(code)1582 self.indentation_level += 11583 def end_block(self, code=""):1584 self.indentation_level -= 11585 if code:1586 self.add_line(code)1587 def output(self):1588 result = "\n".join(self.current_output) + "\n"1589 self.current_output = []1590 return result1591 @property1592 def structs(self):1593 return (d for d in self.definitions.values() if not isinstance(d, EnumDefinition))1594 def resolve_types(self):1595 for definition in self.definitions.values():1596 definition.resolve_types(self)1597 1598 for definition in self.structs:1599 definition.table_id = self.next_table_id1600 self.next_table_id += 11601 next_field_id = 01602 for member in definition.members:1603 member.field_id = next_field_id1604 next_field_id += 11605 def validate(self):1606 for definition in self.definitions.values():1607 definition.validate()1608 def generate_c_header(self):1609 self.set_parameter("schema_name", self.name.lower())1610 self.add_line("#ifndef _SERIALIB_{schema_name}_H")1611 self.add_line("#define _SERIALIB_{schema_name}_H")1612 self.skip_line()1613 self.add_line("#include <stdint.h>")1614 self.add_line("#include <stdbool.h>")1615 self.add_line("#include <stdlib.h>")1616 self.skip_line()1617 self.start_block("typedef enum TableType_e {{")1618 self.add_line("TABLE_TYPE_INVALID = 0,")1619 for definition, end in join_iterate(self.structs):1620 self.set_parameter("table_name", definition.name)1621 self.set_parameter("table_id", definition.table_id)1622 if end:1623 self.add_line("TABLE_TYPE_{table_name} = {table_id}")1624 else:1625 self.add_line("TABLE_TYPE_{table_name} = {table_id},")1626 self.end_block("}} TableType_e;")1627 self.skip_line()1628 for definition in self.definitions.values():1629 definition.generate_typedefs()1630 self.skip_line()1631 try:1632 example_definition = next(iter(self.structs))1633 self.set_parameter("example", example_definition.name)1634 self.add_c_comment(1635 comment="""1636 Checks a buffer that has been serialized with one of the <table/struct>_serialize() functions1637 and returns the TableType_e that corresponds with the table used to create the buffer.1638 For example, if {example}_serialize() was used to create the buffer, this function will return1639 TABLE_TYPE_{example}. If the buffer cannot be any of the known tables, TABLE_TYPE_INVALID is1640 returned instead.1641 """1642 )1643 self.add_line("TableType_e determine_table_type(const uint8_t *buffer, size_t buffer_size);")1644 self.skip_line()1645 except StopIteration:1646 pass1647 for definition in self.structs:1648 definition.generate_signatures()1649 self.skip_line()1650 self.add_line("#endif")1651 return self.output()1652 def generate_c_source(self, header_path):1653 self.set_parameter("schema_name", self.name.lower())1654 self.set_parameter("header_path", header_path)1655 self.add_line("#include <stdint.h>")1656 self.add_line("#include <stdbool.h>")1657 self.add_line("#include <stdlib.h>")1658 self.add_line("#include <string.h>")1659 self.add_line("#include <endian.h>")1660 self.add_line("#include <stdarg.h>")1661 self.add_line("#include <stdio.h>")1662 self.add_line("#include \"{header_path}\"")1663 self.skip_line()1664 self.start_block("static void printf_indent(size_t indent, const char *fmt, ...) {{")1665 self.start_block("for (size_t i = 0; i < indent; i++) {{")1666 self.add_line('printf(" ");')1667 self.end_block("}}")1668 self.add_line("va_list va_args;")1669 self.add_line("va_start(va_args, fmt);")1670 self.add_line("vprintf(fmt, va_args);")1671 self.add_line("va_end(va_args);")1672 self.end_block("}}")1673 self.skip_line()1674 self.start_block("static void allocate_space(uint8_t **buffer, size_t *buffer_size, size_t bytes_written, size_t bytes_needed) {{")1675 self.start_block("while (bytes_written + bytes_needed > *buffer_size) {{")1676 self.add_line("*buffer_size *= 2;")1677 self.end_block("}}")1678 self.add_line("*buffer = realloc(*buffer, *buffer_size);")1679 self.end_block("}}")1680 self.skip_line()1681 self.start_block("static void serialize_varint(uint8_t **buffer_ptr, size_t buffer_size, size_t *bytes_written_ptr, size_t value) {{")1682 self.add_line("uint8_t *buffer = *buffer_ptr;")1683 self.add_line("size_t bytes_written = *bytes_written_ptr;")1684 self.start_block("if (value > 0xFFFFFFFF) {{")1685 self.c_allocate(9)1686 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = 0xFF;")1687 self.add_line("bytes_written += 1;")1688 self.add_line("((uint64_t*)(buffer + bytes_written))[0] = htobe64((uint64_t)(value));")1689 self.add_line("bytes_written += 8;")1690 self.end_block("}}")1691 self.start_block("else if (value > 0xFFFF) {{")1692 self.c_allocate(5)1693 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = 0xFE;")1694 self.add_line("bytes_written += 1;")1695 self.add_line("((uint32_t*)(buffer + bytes_written))[0] = htobe32((uint32_t)(value));")1696 self.add_line("bytes_written += 4;")1697 self.end_block("}}")1698 self.start_block("else if (value >= 0xFD) {{")1699 self.c_allocate(3)1700 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = 0xFD;")1701 self.add_line("bytes_written += 1;")1702 self.add_line("((uint16_t*)(buffer + bytes_written))[0] = htobe16((uint16_t)(value));")1703 self.add_line("bytes_written += 2;")1704 self.end_block("}}")1705 self.start_block("else {{")1706 self.c_allocate(1)1707 self.add_line("((uint8_t*)(buffer + bytes_written))[0] = (uint8_t)(value);")1708 self.add_line("bytes_written += 1;")1709 self.end_block("}}")1710 self.add_line("*bytes_written_ptr = bytes_written;")1711 self.add_line("*buffer_ptr = buffer;")1712 self.end_block("}}")1713 self.start_block("static size_t parse_varint(const uint8_t *buffer, size_t buffer_size, size_t *bytes_read_ptr) {{")1714 self.add_line("size_t bytes_read = *bytes_read_ptr;")1715 self.start_block("if (bytes_read + 1 > buffer_size) {{")1716 self.add_line("goto ERROR;")1717 self.end_block("}}")1718 self.add_line("size_t value;")1719 self.start_block("if (buffer[bytes_read] == 0xFF) {{")1720 self.add_line("bytes_read += 1;")1721 self.deserialize_c_int("value", 8)1722 self.end_block("}}")1723 self.start_block("else if (buffer[bytes_read] == 0xFE) {{")1724 self.add_line("bytes_read += 1;")1725 self.deserialize_c_int("value", 4)1726 self.end_block("}}")1727 self.start_block("else if (buffer[bytes_read] == 0xFD) {{")1728 self.add_line("bytes_read += 1;")1729 self.deserialize_c_int("value", 2)1730 self.end_block("}}")1731 self.start_block("else {{")1732 self.deserialize_c_int("value", 1)1733 self.end_block("}}")1734 self.add_line("*bytes_read_ptr = bytes_read;")1735 self.add_line("return value;")1736 self.add_line("ERROR:")1737 self.add_line("*bytes_read_ptr = bytes_read;")1738 self.add_line("return SIZE_MAX;")1739 self.end_block("}}")1740 self.skip_line()1741 self.set_parameter("NUM_TABLE_IDS", self.next_table_id)1742 self.start_block("TableType_e determine_table_type(const uint8_t *buffer, size_t buffer_size) {{")1743 self.add_line("size_t bytes_read = 0;")1744 self.deserialize_c_varint("table_id", declare=True)1745 self.start_block("if (table_id >= {NUM_TABLE_IDS}) {{")1746 self.add_line("goto ERROR;")1747 self.end_block("}}")1748 self.add_line("return (TableType_e)table_id;")1749 self.start_block("ERROR: {{")1750 self.add_line("return TABLE_TYPE_INVALID;")1751 self.end_block("}}")1752 self.end_block("}}")1753 self.skip_line()1754 for definition in self.structs:1755 definition.generate_c_source()1756 return self.output()1757 def generate_python(self):1758 """1759 Schema1760 """1761 self.add_line("from __future__ import annotations")1762 self.add_line("from dataclasses import dataclass, field")1763 self.add_line("from enum import IntEnum")1764 self.add_line("from typing import Union, List, Optional")1765 self.skip_line(2)1766 self.start_block("def uint8(value):")1767 self.add_line("return value.to_bytes(1, 'big', signed=False)")1768 self.end_block()1769 self.skip_line(2)1770 self.start_block("def uint16(value):")1771 self.add_line("return value.to_bytes(2, 'big', signed=False)")1772 self.end_block()1773 self.skip_line(2)1774 self.start_block("def uint32(value):")1775 self.add_line("return value.to_bytes(4, 'big', signed=False)")1776 self.end_block()1777 self.skip_line(2)1778 self.start_block("def uint64(value):")1779 self.add_line("return value.to_bytes(8, 'big', signed=False)")1780 self.end_block()1781 self.skip_line(2)1782 self.start_block("def int8(value):")1783 self.add_line("return value.to_bytes(1, 'big', signed=True)")1784 self.end_block()1785 self.skip_line(2)1786 self.start_block("def int16(value):")1787 self.add_line("return value.to_bytes(2, 'big', signed=True)")1788 self.end_block()1789 self.skip_line(2)1790 self.start_block("def int32(value):")1791 self.add_line("return value.to_bytes(4, 'big', signed=True)")1792 self.end_block()1793 self.skip_line(2)1794 self.start_block("def int64(value):")1795 self.add_line("return value.to_bytes(8, 'big', signed=True)")1796 self.end_block()1797 self.skip_line(2)1798 self.start_block("def unsigned_int(buf):")1799 self.add_line("return int.from_bytes(buf, 'big', signed=False)")1800 self.end_block()1801 self.skip_line(2)1802 self.start_block("def signed_int(buf):")1803 self.add_line("return int.from_bytes(buf, 'big', signed=True)")1804 self.end_block()1805 self.skip_line(2)1806 for definition in self.definitions.values():1807 definition.generate_python()1808 self.start_block("TABLE_ID_MAP = {{")1809 for definition in self.structs:1810 self.add_line("{}: {},".format(definition.table_id, definition.name))1811 self.end_block("}}")1812 self.skip_line(2)1813 self.start_block("def deserialize(buf: Union[bytes, bytearray]):")1814 self.add_line("buf_index = 0")1815 self.deserialize_py_varint("table_id")1816 self.start_block("try:")1817 self.add_line("cls = TABLE_ID_MAP[table_id]")1818 self.end_block()1819 self.start_block("except KeyError:")1820 self.add_line("raise ValueError('Unrecognized Table ID')")1821 self.end_block()1822 self.add_line("return cls.deserialize(buf)")1823 self.end_block()1824 return self.output()1825def main():1826 import argparse1827 parser = argparse.ArgumentParser()1828 parser.add_argument('schema', type=Path)1829 parser.add_argument('--python', type=Path, default=None)1830 parser.add_argument('--c-source', type=Path, default=None)1831 parser.add_argument('--c-header', type=Path, default=None)1832 args = parser.parse_args()1833 # Read schema file1834 with open(args.schema, "r") as f:1835 text_schema = f.read()1836 # Lex and parse schema...
banner.py
Source:banner.py
...307################################################################################308# Writing the lines corresponding to the cuts309################################################################################310 311 self.add_line('maxjetflavor', 'int', 4)312 self.add_line('auto_ptj_mjj', 'bool', True)313 self.add_line('cut_decays', 'bool', True)314 # minimum pt315 self.add_line('ptj', 'float', 20)316 self.add_line('ptb', 'float', 20)317 self.add_line('pta', 'float', 20)318 self.add_line('ptl', 'float', 20)319 self.add_line('misset', 'float', 0)320 self.add_line('ptonium', 'float', 0.0)321 # maximal pt322 self.add_line('ptjmax', 'float', -1)323 self.add_line('ptbmax', 'float', -1)324 self.add_line('ptamax', 'float', -1)325 self.add_line('ptlmax', 'float', -1)326 self.add_line('missetmax', 'float', -1)327 # maximal rapidity (absolute value)328 self.add_line('etaj', 'float', 4.0)329 self.add_line('etab', 'float', 4.0)330 self.add_line('etaa', 'float', 4.0)331 self.add_line('etal', 'float', 4.0)332 # minimal rapidity (absolute value)333 self.add_line('etajmin', 'float', 0.0)334 self.add_line('etabmin', 'float', 0.0)335 self.add_line('etaamin', 'float', 0.0)336 self.add_line('etalmin', 'float', 0.0)337 self.add_line('etaonium', 'float', 100.0)338 # Minimul E's339 self.add_line('ej', 'float', 0.0)340 self.add_line('eb', 'float', 0.0)341 self.add_line('ea', 'float', 0.0)342 self.add_line('el', 'float', 0.0)343 # Maximum E's344 self.add_line('ejmax', 'float', -1)345 self.add_line('ebmax', 'float', -1)346 self.add_line('eamax', 'float', -1)347 self.add_line('elmax', 'float', -1) 348 # minimum delta_r349 self.add_line('drjj', 'float', 0.4) 350 self.add_line('drbb', 'float', 0.4) 351 self.add_line('drll', 'float', 0.4) 352 self.add_line('draa', 'float', 0.4) 353 self.add_line('drbj', 'float', 0.4) 354 self.add_line('draj', 'float', 0.4) 355 self.add_line('drjl', 'float', 0.4) 356 self.add_line('drab', 'float', 0.4) 357 self.add_line('drbl', 'float', 0.4) 358 self.add_line('dral', 'float', 0.4) 359 # maximum delta_r360 self.add_line('drjjmax', 'float', -1)361 self.add_line('drbbmax', 'float', -1)362 self.add_line('drllmax', 'float', -1)363 self.add_line('draamax', 'float', -1)364 self.add_line('drbjmax', 'float', -1)365 self.add_line('drajmax', 'float', -1)366 self.add_line('drjlmax', 'float', -1)367 self.add_line('drabmax', 'float', -1)368 self.add_line('drblmax', 'float', -1)369 self.add_line('dralmax', 'float', -1)370 # minimum invariant mass for pairs371 self.add_line('mmjj', 'float', 0.0)372 self.add_line('mmbb', 'float', 0.0)373 self.add_line('mmaa', 'float', 0.0)374 self.add_line('mmll', 'float', 0.0)375 # maximum invariant mall for pairs376 self.add_line('mmjjmax', 'float', -1)377 self.add_line('mmbbmax', 'float', -1)378 self.add_line('mmaamax', 'float', -1)379 self.add_line('mmllmax', 'float', -1)380 #Min Maxi invariant mass for all leptons 381 self.add_line("mmnl", 'float', 0.0)382 self.add_line("mmnlmax", 'float', -1)383 #inclusive cuts384 self.add_line("xptj", 'float', 0.0)385 self.add_line("xptb", 'float', 0.0)386 self.add_line("xpta", 'float', 0.0)387 self.add_line("xptl", 'float', 0.0)388 self.add_line("xmtcentral", 'float', 0.0, fortran_name='xmtc', log=10)389 # WBT cuts390 self.add_line("xetamin", 'float', 0.0)391 self.add_line("deltaeta", 'float', 0.0)392 # Jet measure cuts 393 self.add_line("xqcut", 'float', 0.0)394 self.add_line("d", 'float', 1.0, log=10)395 # Set min pt of one heavy particle 396 self.add_line("ptheavy", 'float', 0.0)397 # Pt of pairs of leptons (CHARGED AND NEUTRALS)398 self.add_line("ptllmin", "float", 0.0)399 self.add_line("ptllmax", "float", -1)400 # Check the pt's of the jets sorted by pt401 self.add_line("ptj1min", "float", 0.0)402 self.add_line("ptj1max", "float", -1)403 self.add_line("ptj2min", "float", 0.0)404 self.add_line("ptj2max", "float", -1)405 self.add_line("ptj3min", "float", 0.0)406 self.add_line("ptj3max", "float", -1)407 self.add_line("ptj4min", "float", 0.0)408 self.add_line("ptj4max", "float", -1)409 self.add_line("cutuse", "float", 0.0)410 # Check the pt's of leptons sorted by pt411 self.add_line("ptl1min", "float", 0.0)412 self.add_line("ptl1max", "float", -1)413 self.add_line("ptl2min", "float", 0.0)414 self.add_line("ptl2max", "float", -1)415 self.add_line("ptl3min", "float", 0.0)416 self.add_line("ptl3max", "float", -1)417 self.add_line("ptl4min", "float", 0.0)418 self.add_line("ptl4max", "float", -1)419 # Check Ht420 self.add_line("ht2min", 'float', 0.0)421 self.add_line("ht3min", 'float', 0.0)422 self.add_line("ht4min", 'float', 0.0)423 self.add_line("ht2max", 'float', -1)424 self.add_line("ht3max", 'float', -1)425 self.add_line("ht4max", 'float', -1) 426 self.add_line("htjmin", 'float', 0.0)427 self.add_line("htjmax", 'float', -1) 428 self.add_line("ihtmin", 'float', 0.0)429 self.add_line("ihtmax", 'float', -1)430################################################################################431# Writing the lines corresponding to anything but cuts432################################################################################433 # seed434 self.add_line("gridpack","bool", False)435 self.add_line("gridrun",'bool', False, log=10)436 if str(self['gridrun']) in ['1','T','.true','True'] and \437 str(self['gridpack']) in ['1','T','.true','True']:438 self.add_line('gseed', 'int', 0, fortran_name='iseed')439 else:440 self.add_line('iseed', 'int', 0, fortran_name='iseed')441 # Renormalizrion and factorization scales442 self.add_line('fixed_ren_scale', 'bool', True)443 self.add_line('fixed_fac_scale', 'bool', True)444 self.add_line('scale', 'float', 'float', 91.188)445 self.add_line('dsqrt_q2fact1','float', 91.188, fortran_name='sf1')446 self.add_line('dsqrt_q2fact2', 'float', 91.188, fortran_name='sf2')447 self.add_line('scalefact', 'float', 1.0)448 self.add_line('fixed_couplings', 'bool', True, log=10)449 self.add_line('ickkw', 'int', 0)450 self.add_line('chcluster', 'bool', False)451 self.add_line('ktscheme', 'int', 1)452 self.add_line('asrwgtflavor', 'int', 5)453 if int(self['ickkw'])>0:454 self.add_line('alpsfact', 'float', 1.0)455 self.add_line('pdfwgt', 'bool', True)456 if int(self['ickkw'])==2:457 self.add_line('highestmult','int', 0, fortran_name='nhmult')458 self.add_line('issgridfile','str','issudgrid.dat')459 # Collider energy and type460 self.add_line('lpp1', 'int', 1, fortran_name='lpp(1)')461 self.add_line('lpp2', 'int', 1, fortran_name='lpp(2)')462 self.add_line('ebeam1', 'float', 7000, fortran_name='ebeam(1)')463 self.add_line('ebeam2', 'float', 7000, fortran_name='ebeam(2)')464 # Beam polarization465 self.add_line('polbeam1', 'float', 0.0, fortran_name='pb1')466 self.add_line('polbeam2', 'float', 0.0, fortran_name='pb2')467 # BW cutoff (M+/-bwcutoff*Gamma)468 self.add_line('bwcutoff', 'float', 15.0)469 # Collider pdf470 self.add_line('pdlabel','str','cteq6l1')471 if self['pdlabel'] == 'lhapdf':472 self.add_line('lhaid', 'int', 10042)473 else:474 self.add_line('lhaid', 'int', 10042, log=10)475 476 self.fsock.close()477 478 def add_line(self, card_name, type, default, log=30, fortran_name=None):479 """get the line for the .inc file""" 480 481 value = self.get_default(card_name, default, log)482 if not fortran_name:483 fortran_name = card_name484 self.fsock.writelines(' %s = %s \n' % (fortran_name, self.format(type, value)))...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!