craft_grammar package

Submodules

Module contents

Enhance project definitions with advanced grammar.

class craft_grammar.CompoundStatement(*, statements: list[Statement], body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None = None)

Bases: Statement

Multiple statements that need to be treated as a group.

check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

class craft_grammar.ForStatement(*, for_statement: str, body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None = None)

Bases: Statement

Process a ‘for’ statement in the grammar.

check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

class craft_grammar.GrammarProcessor(*, checker: ~collections.abc.Callable[[~typing.Any], bool] = <function GrammarProcessor.<lambda>>, arch: str, target_arch: str | None = None, platforms: ~collections.abc.Collection[str] | None = None, transformer: ~collections.abc.Callable[[list[~craft_grammar._statement.Statement], str, str], str] | None = None, valid_platforms: ~collections.abc.Collection[str] | None = None, valid_architectures: ~collections.abc.Collection[str] | None = None, variant: ~craft_grammar._processor.Variant = Variant.UNKNOWN)

Bases: BaseProcessor

The GrammarProcessor extracts desired primitives from grammar.

process(*, grammar: Sequence[str | dict[str, Any]], call_stack: list[Statement] | None = None) list[Any]

Process grammar and extract desired primitives.

Parameters:
  • grammar – Unprocessed grammar.

  • call_stack – Call stack of statements leading to now.

Returns:

Primitives selected

property variant: Variant
class craft_grammar.OnStatement(*, on_statement: str, body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None = None)

Bases: Statement

Process an ‘on’ statement in the grammar.

check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

class craft_grammar.Statement(*, body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None, check_primitives: bool = False)

Bases: object

Base class for all grammar statements.

add_else(else_body: Sequence[str | dict[str, Any]] | None) None

Add an ‘else’ clause to the statement.

Parameters:

else_body (list) – The body of an ‘else’ clause.

The ‘else’ clauses will be processed in the order they are added.

abstractmethod check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

process() list[str]

Process this statement.

Returns:

Primitives as determined by evaluating the statement or its else clauses.

class craft_grammar.ToStatement(*, to_statement: str, body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None = None)

Bases: Statement

Process a ‘to’ statement in the grammar.

check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

class craft_grammar.TryStatement(*, body: Sequence[str | dict[str, Any]], processor: BaseProcessor, call_stack: list[Statement] | None = None)

Bases: Statement

Process a ‘try’ statement in the grammar.

For example: >>> from snapcraft_legacy import ProjectOptions >>> from ._processor import GrammarProcessor >>> def checker(primitive): … return “invalid” not in primitive >>> options = ProjectOptions() >>> processor = GrammarProcessor(None, options, checker) >>> clause = TryStatement(body=[“invalid”], processor=processor) >>> clause.add_else([“valid”]) >>> clause.process() {‘valid’}

check() bool

Check if a statement main body should be processed.

Returns:

True if main body should be processed, False if elses should be processed.

class craft_grammar.Variant(*values)

Bases: Enum

The grammar variant or syntax style.

FOR_VARIANT = "'for' variant"

The variant that uses ‘for <platform>’ statements.

TO_VARIANT = "'to' variant"

The variant that uses ‘to <arch>’, ‘on <arch> to <arch>’, ‘try’, ‘else’, and ‘else fail’ statements.

UNKNOWN = 'unknown'

The variant is unknown or there is no grammar present.

craft_grammar.create_grammar_model(model_class: type[BaseModel]) str

Create the code for a grammar-aware class compatible with model_class.

Parameters:

model_class – A pydantic.BaseModel subclass.