or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

constants.mdexpressions.mdfactories.mdindex.mdlicensing.mdsymbols.md
tile.json

tessl/pypi-license-expression

A comprehensive utility library to parse, compare, simplify and normalize license expressions using boolean logic

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/license-expression@30.4.x

To install, run

npx @tessl/cli install tessl/pypi-license-expression@30.4.0

index.mddocs/

License Expression

A comprehensive utility library to parse, compare, simplify and normalize license expressions (such as SPDX license expressions) using boolean logic. This library supports complex boolean operations on license expressions, provides validation and normalization capabilities, and includes complete license databases with SPDX and ScanCode license identifiers.

Package Information

  • Package Name: license-expression
  • Language: Python
  • Installation: pip install license-expression
  • Python Version: >= 3.9
  • Dependencies: boolean.py >= 4.0

Core Imports

from license_expression import Licensing, LicenseSymbol

For SPDX-compliant licensing:

from license_expression import get_spdx_licensing

For ScanCode licensing database:

from license_expression import get_scancode_licensing

Basic Usage

from license_expression import get_spdx_licensing

# Create SPDX licensing instance
licensing = get_spdx_licensing()

# Parse a license expression
expression = 'GPL-2.0 or LGPL-2.1 and MIT'
parsed = licensing.parse(expression)

# Get normalized expression
print(str(parsed))  # 'GPL-2.0-only OR (LGPL-2.1-only AND MIT)'

# Validate expressions
result = licensing.validate('MIT and Apache-2.0')
print(result.errors)  # [] - no errors

# Check if expressions are equivalent
expr1 = licensing.parse('MIT or Apache-2.0')
expr2 = licensing.parse('Apache-2.0 or MIT')
print(licensing.is_equivalent(expr1, expr2))  # True

# Simplify complex expressions
complex_expr = 'MIT or (GPL-2.0 and MIT) or MIT'
simplified = licensing.parse(complex_expr).simplify()
print(str(simplified))  # 'MIT'

Architecture

The license-expression library is built around several key components:

  • Licensing: Main orchestrator providing parsing, validation, and comparison operations
  • LicenseSymbol: Represents individual licenses with keys, aliases, and metadata
  • LicenseExpression: Boolean expression trees representing parsed license expressions
  • Validation System: Comprehensive validation with detailed error reporting
  • License Databases: Built-in SPDX and ScanCode license databases for recognition

The library uses boolean algebra to enable sophisticated license expression analysis, including equivalence testing, containment analysis, and expression simplification.

Capabilities

Core Licensing Operations

Main API for parsing, validating, and comparing license expressions. Provides the primary interface for working with license expressions through the Licensing class.

class Licensing:
    def parse(self, expression: str, validate: bool = False, strict: bool = False) -> LicenseExpression: ...
    def validate(self, expression: str) -> ExpressionInfo: ...
    def is_equivalent(self, expr1, expr2) -> bool: ...
    def contains(self, expr1, expr2) -> bool: ...

Core Licensing Operations

License Symbol Management

Classes and utilities for working with individual license symbols, including standard licenses, license-like symbols, and complex WITH exception constructs.

class LicenseSymbol:
    def __init__(self, key: str, aliases: list = None, is_exception: bool = False): ...

class LicenseWithExceptionSymbol:
    def __init__(self, license_symbol: LicenseSymbol, exception_symbol: LicenseSymbol): ...

License Symbol Management

Expression Utilities

Utility functions for expression manipulation, validation, combination, and analysis. Includes functions for combining multiple expressions and validating symbol collections.

def combine_expressions(expressions, relation: str = "AND", unique: bool = True, licensing=None) -> LicenseExpression: ...
def validate_symbols(symbols, validate_keys: bool = False) -> tuple: ...

Expression Utilities

Factory Functions

Convenience functions for creating preconfigured Licensing instances with SPDX or ScanCode license databases.

def get_spdx_licensing(license_index_location=None) -> Licensing: ...
def get_scancode_licensing(license_index_location=None) -> Licensing: ...
def get_license_index(license_index_location=None) -> dict: ...

Factory Functions

Constants and Error Handling

Error codes, token constants, and exception classes for robust error handling and expression parsing.

class ExpressionError(Exception): ...
class ExpressionParseError(ParseError, ExpressionError): ...

# Parse error constants
PARSE_INVALID_EXCEPTION: int
PARSE_INVALID_SYMBOL_AS_EXCEPTION: int

Constants and Error Handling

Types

class ExpressionInfo:
    original_expression: str
    normalized_expression: str
    errors: list
    invalid_symbols: list

class LicenseExpression:
    def pretty(self) -> str: ...
    def render(self, template: str) -> str: ...
    def simplify(self) -> 'LicenseExpression': ...