MCPcopy Index your code
hub / github.com/dbcli/pgcli / skip_initial_comment

Function skip_initial_comment

pgcli/config.py:76–98  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

74
75
76def 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

Callers 2

parse_service_infoFunction · 0.85

Calls

no outgoing calls

Tested by 1