craft_grammar package

Submodules

Module contents

Enhance part 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.GrammarProcessor(*, checker: Callable[[Any], bool], arch: str, target_arch: str, transformer: Callable[[list[Statement], str, str], str] | None = None)

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

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.

abstract 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.

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.