logsnarf.snarf module

Log directory watcher and reader.

This class is responsible for watching a set of directories for updates to files matching a specific pattern. When those updates happen, it reads those changes and passes it to the consumer given to it at initialization time.

Updates are sent to the consumer as complete lines (including newlines). Infile progress is tracked via a persistent state object, which tracks the inode and file offset.

class logsnarf.snarf.LogSnarf(state_obj, consumer, reactor=None)[source]

Bases: object

Main logsnarf class.

implements twisted.internet.interfaces.IPushProducer

Initialize a LogSnarf object.

Parameters:
checkPattern(path)[source]

Check a path against our pattern.

Parameters:

path (twisted.python.filepath.FilePath) – The path to check

Returns bool:

true if the path matches.

cleanState()[source]

Remove invalid entries from the state file.

doRead(path)[source]

read from a file.

Parameters:

path (twisted.python.filepath.FilePath) – the file to read

pauseProducing()[source]

twisted.internet.interfaces.IPushProducer method

Pause producing.

resumeProducing()[source]

twisted.internet.interfaces.IPushProducer method

Resume producing.

setCallback(callback)[source]

Add a callback function.

The callback should have a signature of f(line), accepting one line (including newline) of text.

start()[source]

Start watching.

watch(path, pattern=None, recursive=True)[source]

Add a watch to a given directory.

Parameters:
  • path (string or twisted.python.filepath.FilePath) – directory to watch for changes in

  • pattern (re.RegexObject or None) – regular expression that files much match

  • recursive (bool) – If true, also watch subdirectories.