1# Copyright (C) 2014 The Android Open Source Project
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#   http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15def SplitStream(stream, fnProcessLine, fnLineOutsideChunk):
16  """ Reads the given input stream and splits it into chunks based on
17      information extracted from individual lines.
18
19  Arguments:
20   - fnProcessLine: Called on each line with the text and line number. Must
21     return a triplet, composed of the name of the chunk started on this line,
22     the data extracted, and the name of the architecture this test applies to
23     (or None to indicate that all architectures should run this test).
24   - fnLineOutsideChunk: Called on attempt to attach data prior to creating
25     a chunk.
26  """
27  lineNo = 0
28  allChunks = []
29  currentChunk = None
30
31  for line in stream:
32    lineNo += 1
33    line = line.strip()
34    if not line:
35      continue
36
37    # Let the child class process the line and return information about it.
38    # The _processLine method can modify the content of the line (or delete it
39    # entirely) and specify whether it starts a new group.
40    processedLine, newChunkName, testArch = fnProcessLine(line, lineNo)
41    # Currently, only a full chunk can be specified as architecture-specific.
42    assert testArch is None or newChunkName is not None
43    if newChunkName is not None:
44      currentChunk = (newChunkName, [], lineNo, testArch)
45      allChunks.append(currentChunk)
46    if processedLine is not None:
47      if currentChunk is not None:
48        currentChunk[1].append(processedLine)
49      else:
50        fnLineOutsideChunk(line, lineNo)
51  return allChunks
52