| @@ 33-69 (lines=37) @@ | ||
| 30 | _f.close() |
|
| 31 | ||
| 32 | ||
| 33 | def find_section_lines(file_contents, sec): |
|
| 34 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 35 | # All indented lines until the next global key are included in the range. |
|
| 36 | # For example: |
|
| 37 | # |
|
| 38 | # 0: not_it: |
|
| 39 | # 1: - value |
|
| 40 | # 2: this_one: |
|
| 41 | # 3: - 2 |
|
| 42 | # 4: - 5 |
|
| 43 | # 5: |
|
| 44 | # 6: nor_this: |
|
| 45 | # |
|
| 46 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 47 | # Note that multiple sections may exist in a file and each will be |
|
| 48 | # identified and returned. |
|
| 49 | sec_ranges = [] |
|
| 50 | ||
| 51 | sec_id = sec + ":" |
|
| 52 | sec_len = len(sec_id) |
|
| 53 | end_num = len(file_contents) |
|
| 54 | line_num = 0 |
|
| 55 | ||
| 56 | while line_num < end_num: |
|
| 57 | if len(file_contents[line_num]) >= sec_len: |
|
| 58 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 59 | begin = line_num |
|
| 60 | line_num += 1 |
|
| 61 | while line_num < end_num: |
|
| 62 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 63 | break |
|
| 64 | line_num += 1 |
|
| 65 | ||
| 66 | end = line_num - 1 |
|
| 67 | sec_ranges.append((begin, end)) |
|
| 68 | line_num += 1 |
|
| 69 | return sec_ranges |
|
| 70 | ||
| 71 | ||
| 72 | def update_key_value(contents, key, old_value, new_value): |
|
| @@ 57-93 (lines=37) @@ | ||
| 54 | _f.close() |
|
| 55 | ||
| 56 | ||
| 57 | def find_section_lines(file_contents, sec): |
|
| 58 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 59 | # All indented lines until the next global key are included in the range. |
|
| 60 | # For example: |
|
| 61 | # |
|
| 62 | # 0: not_it: |
|
| 63 | # 1: - value |
|
| 64 | # 2: this_one: |
|
| 65 | # 3: - 2 |
|
| 66 | # 4: - 5 |
|
| 67 | # 5: |
|
| 68 | # 6: nor_this: |
|
| 69 | # |
|
| 70 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 71 | # Note that multiple sections may exist in a file and each will be |
|
| 72 | # identified and returned. |
|
| 73 | sec_ranges = [] |
|
| 74 | ||
| 75 | sec_id = sec + ":" |
|
| 76 | sec_len = len(sec_id) |
|
| 77 | end_num = len(file_contents) |
|
| 78 | line_num = 0 |
|
| 79 | ||
| 80 | while line_num < end_num: |
|
| 81 | if len(file_contents[line_num]) >= sec_len: |
|
| 82 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 83 | begin = line_num |
|
| 84 | line_num += 1 |
|
| 85 | while line_num < end_num: |
|
| 86 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 87 | break |
|
| 88 | line_num += 1 |
|
| 89 | ||
| 90 | end = line_num - 1 |
|
| 91 | sec_ranges.append((begin, end)) |
|
| 92 | line_num += 1 |
|
| 93 | return sec_ranges |
|
| 94 | ||
| 95 | ||
| 96 | def update_key_value(contents, key, old_value, new_value): |
|
| @@ 57-93 (lines=37) @@ | ||
| 54 | _f.close() |
|
| 55 | ||
| 56 | ||
| 57 | def find_section_lines(file_contents, sec): |
|
| 58 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 59 | # All indented lines until the next global key are included in the range. |
|
| 60 | # For example: |
|
| 61 | # |
|
| 62 | # 0: not_it: |
|
| 63 | # 1: - value |
|
| 64 | # 2: this_one: |
|
| 65 | # 3: - 2 |
|
| 66 | # 4: - 5 |
|
| 67 | # 5: |
|
| 68 | # 6: nor_this: |
|
| 69 | # |
|
| 70 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 71 | # Note that multiple sections may exist in a file and each will be |
|
| 72 | # identified and returned. |
|
| 73 | sec_ranges = [] |
|
| 74 | ||
| 75 | sec_id = sec + ":" |
|
| 76 | sec_len = len(sec_id) |
|
| 77 | end_num = len(file_contents) |
|
| 78 | line_num = 0 |
|
| 79 | ||
| 80 | while line_num < end_num: |
|
| 81 | if len(file_contents[line_num]) >= sec_len: |
|
| 82 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 83 | begin = line_num |
|
| 84 | line_num += 1 |
|
| 85 | while line_num < end_num: |
|
| 86 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 87 | break |
|
| 88 | line_num += 1 |
|
| 89 | ||
| 90 | end = line_num - 1 |
|
| 91 | sec_ranges.append((begin, end)) |
|
| 92 | line_num += 1 |
|
| 93 | return sec_ranges |
|
| 94 | ||
| 95 | ||
| 96 | def update_key_value(contents, key, old_value, new_value): |
|
| @@ 141-177 (lines=37) @@ | ||
| 138 | print("%d: %s" % (line_num, file_contents[line_num])) |
|
| 139 | ||
| 140 | ||
| 141 | def find_section_lines(file_contents, sec): |
|
| 142 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 143 | # All indented lines until the next global key are included in the range. |
|
| 144 | # For example: |
|
| 145 | # |
|
| 146 | # 0: not_it: |
|
| 147 | # 1: - value |
|
| 148 | # 2: this_one: |
|
| 149 | # 3: - 2 |
|
| 150 | # 4: - 5 |
|
| 151 | # 5: |
|
| 152 | # 6: nor_this: |
|
| 153 | # |
|
| 154 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 155 | # Note that multiple sections may exist in a file and each will be |
|
| 156 | # identified and returned. |
|
| 157 | sec_ranges = [] |
|
| 158 | ||
| 159 | sec_id = sec + ":" |
|
| 160 | sec_len = len(sec_id) |
|
| 161 | end_num = len(file_contents) |
|
| 162 | line_num = 0 |
|
| 163 | ||
| 164 | while line_num < end_num: |
|
| 165 | if len(file_contents[line_num]) >= sec_len: |
|
| 166 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 167 | begin = line_num |
|
| 168 | line_num += 1 |
|
| 169 | while line_num < end_num: |
|
| 170 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 171 | break |
|
| 172 | line_num += 1 |
|
| 173 | ||
| 174 | end = line_num - 1 |
|
| 175 | sec_ranges.append((begin, end)) |
|
| 176 | line_num += 1 |
|
| 177 | return sec_ranges |
|
| 178 | ||
| 179 | ||
| 180 | def remove_lines(file_contents, lines): |
|