Skip to content

Commit

Permalink
feat: support inline comments in deb822 source files
Browse files Browse the repository at this point in the history
  • Loading branch information
james-garner-canonical committed Nov 21, 2024
1 parent 674bb64 commit 9c1394e
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/charms/operator_libs_linux/v0/apt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,16 +1378,21 @@ def _parse_deb822_lines(

@staticmethod
def _iter_deb822_paragraphs(lines: Iterable[str]) -> Iterator[List[Tuple[int, str]]]:
"""Given lines from a deb822 format file, yield paragraphs.
A paragraph is a list of numbered lines that make up a source entry,
with comments stripped out (but accounted for in line numbering).
"""
current_paragraph: List[Tuple[int, str]] = []
for n, line in enumerate(lines): # 0 indexed line numbers, following `load`
if line.startswith("#"):
continue
if not line: # blank lines separate paragraphs
if current_paragraph:
yield current_paragraph
current_paragraph = []
continue
current_paragraph.append((n, line))
content, _delim, _comment = line.partition("#")
if content.strip(): # skip (potentially indented) comment line
current_paragraph.append((n, content.rstrip())) # preserve indent
if current_paragraph:
yield current_paragraph

Expand Down

0 comments on commit 9c1394e

Please sign in to comment.