blob: e0b8f87ed8bcead8988257ccef506fa036d49a47 [file] [log] [blame]
Srikrishna Iyerffac3b62021-09-24 16:50:46 -07001# Copyright lowRISC contributors.
2# Licensed under the Apache License, Version 2.0, see LICENSE for details.
3# SPDX-License-Identifier: Apache-2.0
4
5from textwrap import fill
6
7LICENSE_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
12AUTOGEN_BANNER = (
13 "THIS FILE HAS BEEN GENERATED, DO NOT EDIT MANUALLY. COMMAND:\n{command}")
14
15MAX_LEN = 70
16
17
18def 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
33def 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
41def 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"