Srikrishna Iyer | ffac3b6 | 2021-09-24 16:50:46 -0700 | [diff] [blame] | 1 | # Copyright lowRISC contributors. |
| 2 | # Licensed under the Apache License, Version 2.0, see LICENSE for details. |
| 3 | # SPDX-License-Identifier: Apache-2.0 |
| 4 | |
| 5 | from textwrap import fill |
| 6 | |
| 7 | LICENSE_BANNER = ( |
| 8 | "Copyright lowRISC contributors.\n" |
| 9 | "Licensed under the Apache License, Version 2.0, see LICENSE for details.\n" |
| 10 | "SPDX-License-Identifier: Apache-2.0") |
| 11 | |
| 12 | AUTOGEN_BANNER = ( |
| 13 | "THIS FILE HAS BEEN GENERATED, DO NOT EDIT MANUALLY. COMMAND:\n{command}") |
| 14 | |
| 15 | MAX_LEN = 70 |
| 16 | |
| 17 | |
| 18 | def get_autogen_banner(command: str, comment: str = "") -> str: |
| 19 | """Returns a commented out auto-generated code warning banner. |
| 20 | |
| 21 | command is a fully formatted string representing what command was used |
| 22 | to auto-generate the source. |
| 23 | comment is the style of comment supported by the file type. |
| 24 | """ |
| 25 | command = fill(command.strip(), |
| 26 | width=MAX_LEN, |
| 27 | break_long_words=False, |
| 28 | break_on_hyphens=False) |
| 29 | text = AUTOGEN_BANNER.format(command=command) |
| 30 | return apply_comment(text, comment) |
| 31 | |
| 32 | |
| 33 | def get_license_banner(comment: str = "") -> str: |
| 34 | """Returns a commented out license banner. |
| 35 | |
| 36 | comment is the style of comment supported by the source file type. |
| 37 | """ |
| 38 | return apply_comment(LICENSE_BANNER, comment) |
| 39 | |
| 40 | |
| 41 | def apply_comment(text: str, comment: str) -> str: |
| 42 | """Applies comment to a text paragraph. |
| 43 | |
| 44 | The returned string terminates in a newline. |
| 45 | """ |
| 46 | if comment: |
| 47 | comment += " " |
| 48 | return "\n".join([f"{comment}{line}" for line in text.split("\n")]) + "\n" |