1import traceback
2
3from autotest_lib.tko import status_lib, utils as tko_utils
4
5
6class parser(object):
7    """
8    Abstract parser base class. Provides a generic implementation of the
9    standard parser interfaction functions. The derived classes must
10    implement a state_iterator method for this class to be useful.
11    """
12    def start(self, job):
13        """ Initialize the parser for processing the results of
14        'job'."""
15        # initialize all the basic parser parameters
16        self.job = job
17        self.finished = False
18        self.line_buffer = status_lib.line_buffer()
19        # create and prime the parser state machine
20        self.state = self.state_iterator(self.line_buffer)
21        next(self.state)
22
23    def end(self, lines=[]):
24        """ Feed 'lines' into the parser state machine, signal to the
25        state machine that no more lines are forthcoming, and then
26        return a list of all the new test results produced."""
27        self.line_buffer.put_multiple(lines)
28        # run the state machine to clear out the buffer
29        self.finished = True
30        try:
31            return next(self.state)
32        except StopIteration:
33            msg = ("WARNING: parser was end()ed multiple times\n"
34                   "Current traceback:\n" +
35                   traceback.format_exc() +
36                   "\nCurrent stack:\n" +
37                   "".join(traceback.format_stack()))
38            tko_utils.dprint(msg)
39            return []
40
41
42    @staticmethod
43    def make_job(dir):
44        """ Create a new instance of the job model used by the
45        parser, given a results directory."""
46        raise NotImplementedError
47
48
49    def state_iterator(self, buffer):
50        """ A generator method that implements the actual parser
51        state machine. """
52        raise NotImplementedError
53