| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2012 The Native Client Authors. All rights reserved. | |
| 2 # Use of this source code is governed by a BSD-style license that can be | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 import re | |
| 6 | |
| 7 | |
| 8 def ParseTest(lines): | |
| 9 r"""Parses section-based test. | |
| 10 | |
| 11 Args: | |
| 12 lines: list of \n-terminated strings. | |
| 13 | |
| 14 Returns: | |
| 15 List of string pairs (field name, field content) in order. Field content is | |
| 16 concatenation of \n-terminated lines, so it's either empty or ends with \n. | |
| 17 """ | |
| 18 fields = [] | |
| 19 field_data = {} | |
| 20 current_field = None | |
| 21 | |
| 22 for line in lines: | |
| 23 if line.startswith(' '): | |
| 24 assert current_field is not None, line | |
| 25 field_data[current_field].append(line[2:]) | |
| 26 else: | |
| 27 match = re.match('@(\S+):$', line) | |
| 28 if match is None: | |
| 29 raise Exception('Bad line: %r' % line) | |
| 30 current_field = match.group(1) | |
| 31 assert current_field not in field_data, current_field | |
| 32 field_data[current_field] = [] | |
| 33 fields.append(current_field) | |
| 34 | |
| 35 return [(field, ''.join(field_data[field])) for field in fields] | |
| 36 | |
| 37 | |
| 38 def SplitLines(lines, separator_regex): | |
| 39 """Split sequence of lines into sequence of list of lines. | |
| 40 | |
| 41 Args: | |
| 42 lines: sequence of strings. | |
| 43 separator_regex: separator regex. | |
| 44 | |
| 45 Yields: | |
| 46 Nonempty sequence of (possibly empty) lists of strings. Separator lines | |
| 47 are not included. | |
| 48 """ | |
| 49 part = [] | |
| 50 for line in lines: | |
| 51 if re.match(separator_regex, line): | |
| 52 yield part | |
| 53 part = [] | |
| 54 else: | |
| 55 part.append(line) | |
| 56 yield part | |
| 57 | |
| 58 | |
| 59 def LoadTestFile(filename): | |
| 60 r"""Loads and parses .test file. | |
| 61 | |
| 62 Args: | |
| 63 filename: filename. | |
| 64 | |
| 65 Returns: | |
| 66 List of tests (see ParseTest). | |
| 67 """ | |
| 68 with open(filename) as file_in: | |
| 69 return map(ParseTest, SplitLines(file_in, r'-{3,}\s*$')) | |
| 70 | |
| 71 | |
| 72 def UnparseTest(items_list): | |
| 73 """Convert test to sequence of \n-terminated strings | |
| 74 | |
| 75 Args: | |
| 76 items_list: list of string pairs (see ParseTest). | |
| 77 | |
| 78 Yields: | |
| 79 Sequence of \n-terminated strings. | |
| 80 """ | |
| 81 for field, content in items_list: | |
| 82 yield '@%s:\n' % field | |
| 83 if content == '': | |
| 84 continue | |
| 85 | |
| 86 assert content.endswith('\n') | |
| 87 content = content[:-1] | |
| 88 | |
| 89 for line in content.split('\n'): | |
| 90 yield ' %s\n' % line | |
| 91 | |
| 92 | |
| 93 def SaveTestFile(tests, filename): | |
| 94 r"""Saves .test file | |
| 95 | |
| 96 Args: | |
| 97 tests: list of tests (see ParseTest). | |
| 98 filename: filename. | |
| 99 Returns: | |
| 100 None. | |
| 101 """ | |
| 102 with open(filename, 'w') as file_out: | |
| 103 first = True | |
| 104 for test in tests: | |
| 105 if not first: | |
| 106 file_out.write('-' * 70 + '\n') | |
| 107 first = False | |
| 108 for line in UnparseTest(test): | |
| 109 file_out.write(line) | |
| OLD | NEW |