MakroParser#

Qualified name: makros.MakroParser

class makros.MakroParser(file_path: Path, global_controller: makros.makros.Makros)#

Bases: object

Provides an api for parsing a single file. You should avoid constructing this class directly and instead use Makros.get(). This provides the following methods for parsing a file:

  • parse: Parses the file at the path that is passed into the constructor and writes content do disk

  • parse_path: Parses the file at the provided path and writes content to disk

  • parse_string: Parses the string provided to the method and returns the output as a string

  • parse_tokens: Parses the tokens provided to the method and returns the output as a string

Internally, the following state is maintained, it is generally good to avoid changing it:

  • available_macros

  • current_indentation

Because internal state is maintained, it is generally a good idea to create a new parser instance for each file that is being parsed.

Methods

parse

Reads the file provided in the Parser constructor, expands any of the containing macros and outputs them back to the disk.

parse_path

Parses the provided path and writes the contents to disk

parse_string

Expand any macros used in the inputted string

parse_tokens

This is the base parsing method, which will convert a number of tokens into a valid python file.

Attributes

available_macros

The macros that have been imported into the file

current_indentation

The current indentation level of the macro file

available_macros: List[MacroDef] = []#

The macros that have been imported into the file

current_indentation: str = ''#

The current indentation level of the macro file

parse() None#

Reads the file provided in the Parser constructor, expands any of the containing macros and outputs them back to the disk.

parse_path(path: Path) None#

Parses the provided path and writes the contents to disk

Args:

path (Path): The path you wish to parse

parse_string(string: str) str#

Expand any macros used in the inputted string

Args:

string (str): The string that contains macros to be expanded

Returns:

str: The python generated from expanding the containing macros.

parse_tokens(raw_tokens: Generator[TokenInfo, None, None]) str#

This is the base parsing method, which will convert a number of tokens into a valid python file.

Args:

raw_tokens (Generator[tokenize.TokenInfo, None, None]): The tokens that will be used by the parser

Returns:

str: The python file generated from expanding any containing macros