Skip to content

Commit

Permalink
fix: no comment by default
Browse files Browse the repository at this point in the history
  • Loading branch information
msto committed Oct 21, 2024
1 parent f4cef26 commit 7b21816
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions fgpyo/util/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ def read(
cls,
path: Path,
ignore_extra_fields: bool = True,
comment_prefix: str = "#",
comment_prefix: Optional[str] = None,
) -> Iterator["Metric[MetricType]"]:
"""Reads in zero or more metrics from the given path.
Expand Down Expand Up @@ -397,7 +397,7 @@ def fast_concat(*inputs: Path, output: Path) -> None:
def _read_header(
reader: TextIOWrapper,
delimiter: str = "\t",
comment_prefix: str = "#",
comment_prefix: Optional[str] = None,
) -> MetricFileHeader:
"""
Read the header from an open file.
Expand All @@ -422,12 +422,17 @@ def _read_header(
preamble: List[str] = []

for line in reader:
if line.strip().startswith(comment_prefix) or line.strip() == "":
# Skip any commented or empty lines before the header
preamble.append(line.strip())
line = line.strip()

if line == "":
# Skip any empty lines before the header
preamble.append(line)
elif comment_prefix is not None and line.startswith(comment_prefix):
# Skip any commented lines before the header
preamble.append(line)
else:
# The first line with any other content is assumed to be the header
fieldnames = line.strip().split(delimiter)
fieldnames = line.split(delimiter)
break
else:
# If the file was empty, kick back an empty header
Expand Down
2 changes: 1 addition & 1 deletion tests/fgpyo/util/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,4 @@ def test_read_validates_no_header(
metrics_file.writelines(lines)

with pytest.raises(ValueError, match="No header found"):
[m for m in data_and_classes.DummyMetric.read(metrics_path)]
[m for m in data_and_classes.DummyMetric.read(metrics_path, comment_prefix="#")]

0 comments on commit 7b21816

Please sign in to comment.