Initial comment in ~/.pg_service.conf is not always marked with '#' which crashes the parser. This function takes a file object and "rewinds" it to the beginning of the first section, from where on it can be parsed safely :return: number of skipped lines
(f_stream: TextIO)
| 74 | |
| 75 | |
| 76 | def skip_initial_comment(f_stream: TextIO) -> int: |
| 77 | """ |
| 78 | Initial comment in ~/.pg_service.conf is not always marked with '#' |
| 79 | which crashes the parser. This function takes a file object and |
| 80 | "rewinds" it to the beginning of the first section, |
| 81 | from where on it can be parsed safely |
| 82 | |
| 83 | :return: number of skipped lines |
| 84 | """ |
| 85 | section_regex = r"\s*\[" |
| 86 | pos = f_stream.tell() |
| 87 | lines_skipped = 0 |
| 88 | while True: |
| 89 | line = f_stream.readline() |
| 90 | if line == "": |
| 91 | break |
| 92 | if re.match(section_regex, line) is not None: |
| 93 | f_stream.seek(pos) |
| 94 | break |
| 95 | else: |
| 96 | pos += len(line) |
| 97 | lines_skipped += 1 |
| 98 | return lines_skipped |
no outgoing calls