Coverage for lice2/constants.py: 100%
15 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-13 11:11 +0000
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-13 11:11 +0000
1"""Define constants for the LICE2 package."""
3import re
4from importlib import resources
6# To extend language formatting sopport with a new language, add an item in
7# LANGS dict:
8# "language_suffix":"comment_name"
9# where "language_suffix" is the suffix of your language and "comment_name" is
10# one of the comment types supported and listed in LANG_CMT:
11# text : no comment
12# c : /* * */
13# unix : #
14# lua : --- --
16# if you want add a new comment type just add an item to LANG_CMT:
17# "comment_name":['string', 'string', 'string']
18# where the first string open multiline comment, second string comment every
19# license's line and the last string close multiline comment,
20# associate your language and source file suffix with your new comment type
21# how explained above.
22# EXAMPLE:
23# LANG_CMT = {"c":['/*', '*', '*/']} # noqa: ERA001
24# LANGS = {"cpp":"c"} # noqa: ERA001
25# (for more examples see LANG_CMT and langs dicts below)
27LANGS = {
28 "ada": "ada",
29 "adb": "ada",
30 "ads": "ada",
31 "agda": "haskell",
32 "bash": "unix",
33 "c": "c",
34 "cc": "c",
35 "clj": "lisp",
36 "cpp": "c",
37 "cs": "c",
38 "css": "c",
39 "dart": "c",
40 "el": "lisp",
41 "erl": "erlang",
42 "f": "fortran",
43 "f90": "fortran90",
44 "go": "c",
45 "h": "c",
46 "hpp": "c",
47 "hs": "haskell",
48 "html": "html",
49 "idr": "haskell",
50 "java": "java",
51 "js": "c",
52 "kt": "java",
53 "lisp": "lisp",
54 "lua": "lua",
55 "m": "c",
56 "md": "html",
57 "ml": "ml",
58 "php": "c",
59 "pl": "perl",
60 "ps": "powershell",
61 "py": "unix",
62 "rb": "ruby",
63 "r": "unix",
64 "rs": "rust",
65 "scala": "java",
66 "scm": "lisp",
67 "sh": "unix",
68 "sql": "c",
69 "swift": "c",
70 "toml": "unix",
71 "ts": "c",
72 "txt": "text",
73 "v": "c",
74 "vhdl": "ada",
75 "xml": "html",
76 "yaml": "unix",
77}
79LANG_CMT = {
80 "ada": ["", "--", ""],
81 "c": ["/*", " *", " */"],
82 "erlang": ["%%", "%", "%%"],
83 "fortran": ["C", "C", "C"],
84 "fortran90": ["!*", "!*", "!*"],
85 "haskell": ["{-", "", "-}"],
86 "html": ["<!--", "", "-->"],
87 "java": ["/**", " *", " */"],
88 "lisp": ["", ";;", ""],
89 "lua": ["--[[", "", "--]]"],
90 "ml": ["(*", "", "*)"],
91 "perl": ["=item", "", "=cut"],
92 "powershell": ["<#", "#", "#>"],
93 "ruby": ["=begin", "", "=end"],
94 "rust": ["", "//", ""],
95 "text": ["", "", ""],
96 "unix": ["", "#", ""],
97}
100def get_available_licenses() -> list[str]:
101 """Get a sorted list of available license names from template files.
103 Searches for templates in the current package's 'templates' directory
104 with pattern 'template-{name}.txt'.
106 Returns:
107 List of license names sorted alphabetically
108 """
109 # Get the current package name
110 package_name = __package__ if __package__ else __name__.split(".")[0]
112 template_path = resources.files(package_name).joinpath("templates")
113 licenses = []
115 for file in template_path.iterdir():
116 if file.is_file():
117 match = re.match(r"template-([a-z0-9_]+)\.txt", file.name)
118 if match:
119 licenses.append(match.groups()[0])
121 return sorted(licenses)
124LICENSES = get_available_licenses()