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/orfinalize()
: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 ofdispatch()
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()
.
- 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, callsfinalize()
, 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()
.
- 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/orfinalize_section()
.finalize_section()
is called with the previous section whenever a section ends.- 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 orparse_section()
otherwise. Callsis_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:
- 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.
- 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:
- Returns:
The result of calling
finalize_section()
, which is called if a section ends.- Return type:
- Raises:
KeyError – If the section header is unknown.
- 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.