pysys.utils.filegrep

Regular expression grep matching in text files.

getmatches

pysys.utils.filegrep.getmatches(file, regexpr, ignores=None, encoding=None, flags=0)[source]

Look for matches on a regular expression in an input file, return a sequence of the matches.

Parameters
  • file – The full path to the input file

  • regexpr – The regular expression used to search for matches

  • ignores – A list of regexes which will cause matches to be discarded

  • encoding – Specifies the encoding to be used for opening the file, or None for default.

Returns

A list of the match objects

Return type

list

Raises

FileNotFoundException – Raised if the input file does not exist

filegrep

pysys.utils.filegrep.filegrep(file, expr, ignores=None, returnMatch=False, encoding=None, flags=0)[source]

Search for matches to a regular expression in an input file, returning true if a match occurs.

Parameters
  • file – The full path to the input file

  • expr – The regular expression (uncompiled) to search for in the input file

  • ignores – Optional list of regular expression strings to ignore when searching file.

  • returnMatch – return the regex match object instead of a simple boolean

  • encoding – Specifies the encoding to be used for opening the file, or None for default.

Returns

success (True / False), unless returnMatch=True in which case it returns the regex match object (or None if not matched)

Return type

integer

Raises

FileNotFoundException – Raised if the input file does not exist

lastgrep

pysys.utils.filegrep.lastgrep(file, expr, ignore=[], include=[], encoding=None, returnMatch=False, flags=0)[source]

Search for matches to a regular expression in the last line of an input file, returning true if a match occurs.

Parameters
  • file – The full path to the input file

  • expr – The regular expression (uncompiled) to search for in the last line of the input file

  • ignore – A list of regular expressions which remove entries in the input file contents before making the grep

  • include – A list of regular expressions used to select lines from the input file contents to use in the grep

  • encoding – Specifies the encoding to be used for opening the file, or None for default.

Returns

success (True / False)

Return type

integer

Raises

FileNotFoundException – Raised if the input file does not exist

orderedgrep

pysys.utils.filegrep.orderedgrep(file, exprList, encoding=None, flags=0)[source]

Seach for ordered matches to a set of regular expressions in an input file, returning true if the matches occur in the correct order.

The ordered grep method will only return true if matches to the set of regular expression in the expression list occur in the input file in the order they appear in the expression list. Matches to the regular expressions do not have to be across sequential lines in the input file, only in the correct order. For example, for a file with contents

A is for apple 
B is for book
C is for cat
D is for dog

an expression list of [“^A.*$”, “^C.*$”, “^D.*$”] will return true, whilst an expression list of [“^A.*$”, “^C.$”, “^B.$”] will return false.

Parameters
  • file – The full path to the input file

  • exprList – A list of regular expressions (uncompiled) to search for in the input file

  • encoding – Specifies the encoding to be used for opening the file, or None for default.

Returns

None on success, or on failure the string expression that was not found.

Return type

string

Raises

FileNotFoundException – Raised if the input file does not exist

logContents

pysys.utils.filegrep.logContents(message, list)[source]

Log a list of strings, prepending the line number to each line in the log output.

Parameters

list – The list of strings to log