Parsers

Classes for simple file processing

Keyword parser

class drewtils.parsers.KeywordParser(filePath, keys, separators=None, eof='')

Class for parsing a file for chunks separated by various keywords.

Parameters:
  • filePath (str) – Object to be read. Any object with read and close methods
  • keys (Iterable) – List of keywords/phrases that will indicate the start of a chunk
  • separators (Iterable or None) – List of additional phrases that can separate two chunks. If not given, will default to empty line '\n'.
  • eof (str) – String to indicate the end of the file
line

str – Most recently read line

parse()

Parse the file and return a list of keyword blocks.

Returns:List of key word argument chunks.
Return type:list
yieldChunks()

Return each chunk of text as a generator.

Yields:list – The next chunk in the file.

PatternReader

class drewtils.parsers.PatternReader(filePath)

Class that can read over a file looking for patterns.

Parameters:filePath (str) – path to the file that is to be read
line

str – Most recently read line

match

regular expression match or None – Match from the most recently read line

searchFor(pattern)

Return true if the pattern is found.

Parameters:pattern (str or compiled regular expression) –
Returns:bool
Return type:True if the pattern was found
yieldMatches(pattern)

Generator that returns all match groups that match pattern.

Parameters:pattern (str or compiled regular expression) – Seek through the file and yield all match groups for lines that contain this patten.
Yields:sequential match groups