Skip to content

Commit 8feec96

Browse files
authored
Fail on empty line in species list + trim (#790)
1 parent 66ca4f1 commit 8feec96

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

birdnet_analyzer/analyze/core.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ def _set_params(
257257
if os.path.isdir(cfg.SPECIES_LIST_FILE):
258258
cfg.SPECIES_LIST_FILE = os.path.join(cfg.SPECIES_LIST_FILE, "species_list.txt")
259259

260-
cfg.SPECIES_LIST = read_lines(cfg.SPECIES_LIST_FILE)
260+
cfg.SPECIES_LIST = read_lines(cfg.SPECIES_LIST_FILE, trim=True, fail_on_blank_lines=True)
261261
else:
262262
cfg.SPECIES_LIST_FILE = None
263263
cfg.SPECIES_LIST = get_species_list(cfg.LATITUDE, cfg.LONGITUDE, cfg.WEEK, cfg.LOCATION_FILTER_THRESHOLD)
@@ -274,17 +274,17 @@ def _set_params(
274274
cfg.LABELS_FILE = None
275275
cfg.LABELS = None
276276
else:
277-
cfg.LABELS = read_lines(cfg.LABELS_FILE)
277+
cfg.LABELS = read_lines(cfg.LABELS_FILE, fail_on_blank_lines=True)
278278
else:
279279
cfg.APPLY_SIGMOID = False
280280
# our output format
281281
cfg.LABELS_FILE = os.path.join(custom_classifier, "labels", "label_names.csv")
282282

283283
if not os.path.isfile(cfg.LABELS_FILE):
284284
cfg.LABELS_FILE = os.path.join(custom_classifier, "assets", "label.csv")
285-
cfg.LABELS = read_lines(cfg.LABELS_FILE)
285+
cfg.LABELS = read_lines(cfg.LABELS_FILE, fail_on_blank_lines=True)
286286
else:
287-
cfg.LABELS = [line.split(",")[1] for line in read_lines(cfg.LABELS_FILE)]
287+
cfg.LABELS = [line.split(",")[1] for line in read_lines(cfg.LABELS_FILE, fail_on_blank_lines=True)]
288288

289289
if cfg.LABELS_FILE:
290290
lfile = os.path.join(cfg.TRANSLATED_LABELS_PATH, os.path.basename(cfg.LABELS_FILE).replace(".txt", f"_{locale}.txt"))

birdnet_analyzer/utils.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def collect_all_files(path: str, filetypes: list[str], pattern: str = ""):
144144
return sorted(files)
145145

146146

147-
def read_lines(path: str | Path | None) -> list[str]:
147+
def read_lines(path: str | Path | None, trim: bool = False, fail_on_blank_lines: bool = False) -> list[str]:
148148
"""Reads the lines into a list.
149149
150150
Opens the file and reads its contents into a list.
@@ -156,7 +156,20 @@ def read_lines(path: str | Path | None) -> list[str]:
156156
Returns:
157157
A list of all species inside the file.
158158
"""
159-
return Path(path).read_text(encoding="utf-8").splitlines() if path else []
159+
160+
if not path:
161+
return []
162+
163+
lines = Path(path).read_text(encoding="utf-8").splitlines()
164+
cleaned_lines = []
165+
166+
for line in lines:
167+
if not line and fail_on_blank_lines:
168+
raise ValueError(f"Blank lines are not allowed in species list\nFile: {path}")
169+
170+
cleaned_lines.append(line.strip() if trim else line)
171+
172+
return cleaned_lines
160173

161174

162175
def list_subdirectories(path: str):

0 commit comments

Comments
 (0)