Wrapper class to automatically handle copyright headers and header guards for generated scripts. Meant to be invoked via `with` statement similar to creating a file. - `path`: The path of the file to be created. - `guard`: Optional bool to determine if `#pragma once` should be
(
path: str,
guard: Optional[bool] = None,
)
| 1561 | |
| 1562 | @contextlib.contextmanager |
| 1563 | def generated_wrapper( |
| 1564 | path: str, |
| 1565 | guard: Optional[bool] = None, |
| 1566 | ) -> Generator[TextIOBase, None, None]: |
| 1567 | """ |
| 1568 | Wrapper class to automatically handle copyright headers and header guards |
| 1569 | for generated scripts. Meant to be invoked via `with` statement similar to |
| 1570 | creating a file. |
| 1571 | |
| 1572 | - `path`: The path of the file to be created. |
| 1573 | - `guard`: Optional bool to determine if `#pragma once` should be added. If |
| 1574 | unassigned, the value is determined by file extension. |
| 1575 | """ |
| 1576 | |
| 1577 | with open(path, "wt", encoding="utf-8", newline="\n") as file: |
| 1578 | if not path.endswith(".out"): # For test output, we only care about the content. |
| 1579 | file.write(generate_copyright_header(path)) |
| 1580 | file.write("\n/* THIS FILE IS GENERATED. EDITS WILL BE LOST. */\n\n") |
| 1581 | |
| 1582 | if guard is None: |
| 1583 | guard = path.endswith((".h", ".hh", ".hpp", ".hxx", ".inc")) |
| 1584 | if guard: |
| 1585 | file.write("#pragma once\n\n") |
| 1586 | |
| 1587 | with StringIO(newline="\n") as str_io: |
| 1588 | yield str_io |
| 1589 | file.write(str_io.getvalue().strip() or "/* NO CONTENT */") |
| 1590 | |
| 1591 | file.write("\n") |
| 1592 | |
| 1593 | |
| 1594 | def get_buffer(path: str) -> bytes: |
no test coverage detected