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

Function _parse_expression

src/licensedcode/match_spdx_lid.py:226–268  ·  view source on GitHub ↗

Return a LicenseExpression object by parsing the `text` string using the ``licensing`` reference Licensing. Return None or raise an exception on errors. Use the ``expression_symbols`` mapping of {lowered key: LicenseSymbol} if provided. Otherwise use the standard SPDX license s

(text, licensing, expression_symbols, unknown_symbol)

Source from the content-addressed store, hash-verified

224
225
226def _parse_expression(text, licensing, expression_symbols, unknown_symbol):
227 """
228 Return a LicenseExpression object by parsing the `text` string using the
229 ``licensing`` reference Licensing. Return None or raise an exception on
230 errors.
231
232 Use the ``expression_symbols`` mapping of {lowered key: LicenseSymbol} if
233 provided. Otherwise use the standard SPDX license symbols.
234 """
235 if not text:
236 return
237 text = text.lower()
238 expression = licensing.parse(text, simple=True)
239
240 if expression is None:
241 return
242
243 # substitute old SPDX symbols with new ones if any
244 old_expressions_subs = get_old_expressions_subs_table(licensing)
245 updated = expression.subs(old_expressions_subs)
246
247 # collect known symbols and build substitution table: replace known symbols
248 # with a symbol wrapping a known license and unkown symbols with the
249 # unknown-spdx symbol
250 symbols_table = {}
251
252 def _get_matching_symbol(_symbol):
253 return expression_symbols.get(_symbol.key.lower(), unknown_symbol)
254
255 for symbol in licensing.license_symbols(updated, unique=True, decompose=False):
256 if isinstance(symbol, LicenseWithExceptionSymbol):
257 # we have two symbols:make a a new symbo, from that
258 new_with = LicenseWithExceptionSymbol(
259 license_symbol=_get_matching_symbol(symbol.license_symbol),
260 exception_symbol=_get_matching_symbol(symbol.exception_symbol)
261 )
262
263 symbols_table[symbol] = new_with
264 else:
265 symbols_table[symbol] = _get_matching_symbol(symbol)
266
267 symbolized = updated.subs(symbols_table)
268 return symbolized
269
270
271def _reparse_invalid_expression(

Calls 3

_get_matching_symbolFunction · 0.85
parseMethod · 0.45