vermouth.parser_utils module

Helper functions for parsers

class vermouth.parser_utils.LineParser[source]

Bases: object

Class that describes a parser object that parses a file line by line. Subclasses will probably want to override the methods dispatch(), parse_line(), and/or finalize():

  • dispatch() is called for every line and should return the function that should be used to parse that line.

  • parse_line() is called by the default implementation of dispatch() for every line.

  • finalize() is called at the end of the file.

COMMENT_CHAR = '#'
dispatch(line)[source]

Finds the correct method to parse line. Always returns parse_line().

finalize(lineno=0)[source]

Wraps up. Is called at the end of the file.

parse(file_handle)[source]

Reads lines from file_handle, and calls dispatch() to find which method to call to do the actual parsing. Yields the result of that call, if it’s not None. At the end, calls finalize(), and yields its results, iff it’s not None.

Parameters:

file_handle (collections.abc.Iterable[str]) – The data to parse. Should produce lines of data.

Yields:

object – The results of dispatching to parsing methods, and of finalize().

parse_line(line, lineno)[source]

Does nothing and should be overridden by subclasses.

class vermouth.parser_utils.SectionLineParser(*args, **kwargs)[source]

Bases: LineParser

Baseclass for all parsers that have to parse file formats that are based on sections. Parses the macros section. Subclasses will probably want to override finalize() and/or finalize_section().

finalize_section() is called with the previous section whenever a section ends.

section

The current section.

Type:

list[str]

macros

A set of subsitution rules as parsed from a macros section.

Type:

dict[str, str]

METH_DICT = {('macros',): (<function SectionLineParser._macros>, {})}

A dict of all known parser methods, mapping section names to the function to be called and the associated keyword arguments.

dispatch(line)[source]

Looks at line to see what kind of line it is, and returns either parse_header() if line is a section header or parse_section() otherwise. Calls is_section_header() to see whether line is a section header or not.

Parameters:

line (str) –

Returns:

The method that should be used to parse line.

Return type:

collections.abc.Callable

finalize(lineno=0)[source]

Called after the last line has been parsed to wrap up. Resets the instance and calls finalize_section().

Parameters:

lineno (int) – The line number.

finalize_section(previous_section, ended_section)[source]

Called once a section is finished. Currently does nothing.

Parameters:
  • previous_section (list[str]) – The last parsed section.

  • ended_section (list[str]) – The sections that have been ended.

static is_section_header(line)[source]
Parameters:

line (str) – A line of text.

Returns:

True iff line is a section header.

Return type:

bool

Raises:

IOError – The line starts like a section header but looks misformatted.

parse_header(line, lineno=0)[source]

Parses a section header with line number lineno. Sets section when applicable. Does not check whether line is a valid section header.

Parameters:
  • line (str) –

  • lineno (str) –

Returns:

The result of calling finalize_section(), which is called if a section ends.

Return type:

object

Raises:

KeyError – If the section header is unknown.

parse_section(line, lineno)[source]

Parse line with line number lineno by looking up the section in METH_DICT and calling that method.

Parameters:
  • line (str) –

  • lineno (int) –

Returns:

The result returned by calling the registered method.

Return type:

object

class vermouth.parser_utils.SectionParser(name, bases, attrs, **kwargs)[source]

Bases: type

Metaclass (!) that populates the METH_DICT attribute of new classes. The contents of METH_DICT are set by reading the _section_names attribute of all its attributes. You can conveniently set _section_names attributes using the section_parser() decorator.

static section_parser(*names, **kwargs)[source]
Parameters:
  • names (tuple[collections.abc.Hashable]) – The section names that should be associated with the decorated function.

  • kwargs (dict[str]) – The keyword arguments with which the decorated function should be called.

vermouth.parser_utils.split_comments(line, comment_char=';')[source]

Splits line at the first occurence of comment_char.

Parameters:
  • line (str) –

  • comment_char (str) –

Returns:

line before and after comment_char, respectively. If line does not contain comment_char, the second element will be an empty string.

Return type:

tuple[str, str]