MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / get_expression

Function get_expression

src/licensedcode/match_spdx_lid.py:154–193  ·  view source on GitHub ↗

Return an Expression object by parsing the `text` string using the ``licensing`` reference Licensing. Use the ``expression_symbols`` mapping of {lowered key: LicenseSymbol} if provided. Otherwise use the standard SPDX license symbols. Note that an expression is ALWAYS returned

(text, licensing, expression_symbols, unknown_symbol)

Source from the content-addressed store, hash-verified

152
153
154def get_expression(text, licensing, expression_symbols, unknown_symbol):
155 """
156 Return an Expression object by parsing the `text` string using the
157 ``licensing`` reference Licensing.
158
159 Use the ``expression_symbols`` mapping of {lowered key: LicenseSymbol} if
160 provided. Otherwise use the standard SPDX license symbols.
161
162 Note that an expression is ALWAYS returned: if the parsing fails or some
163 other error happens somehow, this function returns instead a bare
164 expression made of only "unknown-spdx" symbol.
165 """
166 _prefix, text = prepare_text(text)
167 if not text:
168 return
169 expression = None
170 try:
171 expression = _parse_expression(
172 text=text,
173 licensing=licensing,
174 expression_symbols=expression_symbols,
175 unknown_symbol=unknown_symbol,
176 )
177 except Exception:
178 try:
179 # Try to parse again using a lenient recovering parsing process such
180 # as for plain space or comma-separated list of licenses (e.g.
181 # UBoot)
182 expression = _reparse_invalid_expression(
183 text=text,
184 licensing=licensing,
185 expression_symbols=expression_symbols,
186 unknown_symbol=unknown_symbol,
187 )
188 except Exception:
189 pass
190
191 if expression is None:
192 expression = unknown_symbol
193 return expression
194
195
196# Some older SPDX ids are deprecated and therefore no longer referenced in

Calls 3

prepare_textFunction · 0.85
_parse_expressionFunction · 0.85