| 1518 | |
| 1519 | |
| 1520 | class AddSchemasTransformer(CstTransformerBase): |
| 1521 | def __init__(self, class_name: str, schemas: list[str]): |
| 1522 | super().__init__() |
| 1523 | self.class_name = class_name |
| 1524 | self.schemas = schemas |
| 1525 | self.schema_added = 0 |
| 1526 | |
| 1527 | def leave_ClassDef(self, original_node: cst.ClassDef, updated_node: cst.ClassDef): |
| 1528 | if self.current_class_name == self.class_name: |
| 1529 | docstring = get_class_docstring(updated_node) |
| 1530 | if not docstring: |
| 1531 | print("Class has no docstring") |
| 1532 | else: |
| 1533 | lines = docstring.splitlines() |
| 1534 | first_line = lines[1] |
| 1535 | indent = first_line[: len(first_line) - len(first_line.lstrip())] |
| 1536 | heading = len(lines) - 1 # if there is no heading, we place it before the last line (the closing """) |
| 1537 | empty_footing = lines[-2].strip() == "" |
| 1538 | schema_lines = [] |
| 1539 | for idx, line in enumerate(lines): |
| 1540 | if "The OpenAPI schema can be found at" in line: |
| 1541 | heading = idx |
| 1542 | schema_lines = lines[idx + 1 : -2 if empty_footing else -1] |
| 1543 | break |
| 1544 | schema_lines = {schema_line for schema_line in schema_lines if schema_line} |
| 1545 | before = len(schema_lines) |
| 1546 | schema_lines = sorted(list(schema_lines.union({f"{indent}- {schema}" for schema in self.schemas}))) |
| 1547 | after = len(schema_lines) |
| 1548 | lines = ( |
| 1549 | lines[:heading] |
| 1550 | + |
| 1551 | # we add an empty line before the schema lines title if there is none |
| 1552 | ([""] if lines[heading - 1].strip() else []) |
| 1553 | + [indent + "The OpenAPI schema can be found at"] |
| 1554 | # we add an empty line after the schema lines title to get a proper bullet list in the docs |
| 1555 | + [""] |
| 1556 | + schema_lines |
| 1557 | + [""] |
| 1558 | + lines[-1:] |
| 1559 | ) |
| 1560 | docstring = "\n".join(lines) |
| 1561 | stmt = cst.SimpleStatementLine([cst.Expr(cst.SimpleString(docstring))]) |
| 1562 | stmts = [stmt] + list(updated_node.body.body[1:]) |
| 1563 | updated_node = updated_node.with_changes(body=updated_node.body.with_changes(body=stmts)) |
| 1564 | self.schema_added += after - before |
| 1565 | |
| 1566 | return super().leave_ClassDef(original_node, updated_node) |
| 1567 | |
| 1568 | |
| 1569 | class CreateClassMethodTransformer(CstTransformerBase): |
no outgoing calls
no test coverage detected
searching dependent graphs…