| @@ 59-95 (lines=37) @@ | ||
| 56 | _f.close() |
|
| 57 | ||
| 58 | ||
| 59 | def find_section_lines(file_contents, sec): |
|
| 60 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 61 | # All indented lines until the next global key are included in the range. |
|
| 62 | # For example: |
|
| 63 | # |
|
| 64 | # 0: not_it: |
|
| 65 | # 1: - value |
|
| 66 | # 2: this_one: |
|
| 67 | # 3: - 2 |
|
| 68 | # 4: - 5 |
|
| 69 | # 5: |
|
| 70 | # 6: nor_this: |
|
| 71 | # |
|
| 72 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 73 | # Note that multiple sections may exist in a file and each will be |
|
| 74 | # identified and returned. |
|
| 75 | sec_ranges = [] |
|
| 76 | ||
| 77 | sec_id = sec + ":" |
|
| 78 | sec_len = len(sec_id) |
|
| 79 | end_num = len(file_contents) |
|
| 80 | line_num = 0 |
|
| 81 | ||
| 82 | while line_num < end_num: |
|
| 83 | if len(file_contents[line_num]) >= sec_len: |
|
| 84 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 85 | begin = line_num |
|
| 86 | line_num += 1 |
|
| 87 | while line_num < end_num: |
|
| 88 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 89 | break |
|
| 90 | line_num += 1 |
|
| 91 | ||
| 92 | end = line_num - 1 |
|
| 93 | sec_ranges.append((begin, end)) |
|
| 94 | line_num += 1 |
|
| 95 | return sec_ranges |
|
| 96 | ||
| 97 | ||
| 98 | def update_key_value(contents, key, old_value, new_value): |
|
| @@ 59-95 (lines=37) @@ | ||
| 56 | _f.close() |
|
| 57 | ||
| 58 | ||
| 59 | def find_section_lines(file_contents, sec): |
|
| 60 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 61 | # All indented lines until the next global key are included in the range. |
|
| 62 | # For example: |
|
| 63 | # |
|
| 64 | # 0: not_it: |
|
| 65 | # 1: - value |
|
| 66 | # 2: this_one: |
|
| 67 | # 3: - 2 |
|
| 68 | # 4: - 5 |
|
| 69 | # 5: |
|
| 70 | # 6: nor_this: |
|
| 71 | # |
|
| 72 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 73 | # Note that multiple sections may exist in a file and each will be |
|
| 74 | # identified and returned. |
|
| 75 | sec_ranges = [] |
|
| 76 | ||
| 77 | sec_id = sec + ":" |
|
| 78 | sec_len = len(sec_id) |
|
| 79 | end_num = len(file_contents) |
|
| 80 | line_num = 0 |
|
| 81 | ||
| 82 | while line_num < end_num: |
|
| 83 | if len(file_contents[line_num]) >= sec_len: |
|
| 84 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 85 | begin = line_num |
|
| 86 | line_num += 1 |
|
| 87 | while line_num < end_num: |
|
| 88 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 89 | break |
|
| 90 | line_num += 1 |
|
| 91 | ||
| 92 | end = line_num - 1 |
|
| 93 | sec_ranges.append((begin, end)) |
|
| 94 | line_num += 1 |
|
| 95 | return sec_ranges |
|
| 96 | ||
| 97 | ||
| 98 | def update_key_value(contents, key, old_value, new_value): |
|
| @@ 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): |
|
| @@ 183-219 (lines=37) @@ | ||
| 180 | print("%d: %s" % (line_num, file_contents[line_num])) |
|
| 181 | ||
| 182 | ||
| 183 | def find_section_lines(file_contents, sec): |
|
| 184 | # Hack to find a global key ("section"/sec) in a YAML-like file. |
|
| 185 | # All indented lines until the next global key are included in the range. |
|
| 186 | # For example: |
|
| 187 | # |
|
| 188 | # 0: not_it: |
|
| 189 | # 1: - value |
|
| 190 | # 2: this_one: |
|
| 191 | # 3: - 2 |
|
| 192 | # 4: - 5 |
|
| 193 | # 5: |
|
| 194 | # 6: nor_this: |
|
| 195 | # |
|
| 196 | # for the section "this_one", the result [(2, 5)] will be returned. |
|
| 197 | # Note that multiple sections may exist in a file and each will be |
|
| 198 | # identified and returned. |
|
| 199 | sec_ranges = [] |
|
| 200 | ||
| 201 | sec_id = sec + ":" |
|
| 202 | sec_len = len(sec_id) |
|
| 203 | end_num = len(file_contents) |
|
| 204 | line_num = 0 |
|
| 205 | ||
| 206 | while line_num < end_num: |
|
| 207 | if len(file_contents[line_num]) >= sec_len: |
|
| 208 | if file_contents[line_num][0:sec_len] == sec_id: |
|
| 209 | begin = line_num |
|
| 210 | line_num += 1 |
|
| 211 | while line_num < end_num: |
|
| 212 | if len(file_contents[line_num]) > 0 and file_contents[line_num][0] != ' ': |
|
| 213 | break |
|
| 214 | line_num += 1 |
|
| 215 | ||
| 216 | end = line_num - 1 |
|
| 217 | sec_ranges.append((begin, end)) |
|
| 218 | line_num += 1 |
|
| 219 | return sec_ranges |
|
| 220 | ||
| 221 | ||
| 222 | def remove_lines(file_contents, lines): |
|