craft_grammar package

Submodules

Module contents

Enhance part definitions with advanced grammar.

class craft_grammar.CompoundStatement(*, statements, body, processor, call_stack=None)[source]

Bases: craft_grammar._statement.Statement

Multiple statements that need to be treated as a group.

Parameters
  • statements (List[Statement]) –

  • body (Sequence[Union[str, Dict[str, Any]]]) –

  • processor (GrammarProcessor) –

  • call_stack (Optional[List[ForwardRef]]) –

check()[source]

Check if a statement main body should be processed.

Return type

bool

Returns

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

class craft_grammar.GrammarProcessor(*, checker, arch, target_arch, transformer=None)[source]

Bases: object

The GrammarProcessor extracts desired primitives from grammar.

Parameters
  • checker (Callable[[Any], bool]) –

  • arch (str) –

  • target_arch (str) –

  • transformer (Optional[Callable[[List[Statement], str, str], str]]) –

process(*, grammar, call_stack=None)[source]

Process grammar and extract desired primitives.

Parameters
  • grammar (Sequence[Union[str, Dict[str, Any]]]) – Unprocessed grammar.

  • call_stack (Optional[List[Statement]]) – Call stack of statements leading to now.

Return type

List[Any]

Returns

Primitives selected

class craft_grammar.OnStatement(*, on_statement, body, processor, call_stack=None)[source]

Bases: craft_grammar._statement.Statement

Process an ‘on’ statement in the grammar.

Parameters
  • on_statement (str) –

  • body (Sequence[Union[str, Dict[str, Any]]]) –

  • processor (GrammarProcessor) –

  • call_stack (Optional[List[ForwardRef]]) –

check()[source]

Check if a statement main body should be processed.

Return type

bool

Returns

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

class craft_grammar.Statement(*, body, processor, call_stack, check_primitives=False)[source]

Bases: object

Base class for all grammar statements.

Parameters
  • body (Sequence[Union[str, Dict[str, Any]]]) –

  • processor (GrammarProcessor) –

  • call_stack (Optional[List[ForwardRef]]) –

  • check_primitives (bool) –

add_else(else_body)[source]

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.

Parameters

else_body (Optional[Sequence[Union[str, Dict[str, Any]]]]) –

Return type

None

abstract check()[source]

Check if a statement main body should be processed.

Return type

bool

Returns

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

process()[source]

Process this statement.

Return type

List[str]

Returns

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

class craft_grammar.ToStatement(*, to_statement, body, processor, call_stack=None)[source]

Bases: craft_grammar._statement.Statement

Process a ‘to’ statement in the grammar.

Parameters
  • to_statement (str) –

  • body (Sequence[Union[str, Dict[str, Any]]]) –

  • processor (GrammarProcessor) –

  • call_stack (Optional[List[ForwardRef]]) –

check()[source]

Check if a statement main body should be processed.

Return type

bool

Returns

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

class craft_grammar.TryStatement(*, body, processor, call_stack=None)[source]

Bases: craft_grammar._statement.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’}

Parameters
  • body (Sequence[Union[str, Dict[str, Any]]]) –

  • processor (GrammarProcessor) –

  • call_stack (Optional[List[ForwardRef]]) –

check()[source]

Check if a statement main body should be processed.

Return type

bool

Returns

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