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