1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2024 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Module containing the SPCsvLoader class.
10 The class allows to load phrases from a csv file.
12 from typing import Iterable
16 from ...errors import UsageError
17 from .special_phrase import SpecialPhrase
21 Handles loading of special phrases from external csv file.
23 def __init__(self, csv_path: str) -> None:
24 self.csv_path = csv_path
27 def generate_phrases(self) -> Iterable[SpecialPhrase]:
28 """ Open and parse the given csv file.
29 Create the corresponding SpecialPhrases.
31 self._check_csv_validity()
33 with open(self.csv_path, encoding='utf-8') as fd:
34 reader = csv.DictReader(fd, delimiter=',')
36 yield SpecialPhrase(row['phrase'], row['class'], row['type'], row['operator'])
39 def _check_csv_validity(self) -> None:
41 Check that the csv file has the right extension.
43 _, extension = os.path.splitext(self.csv_path)
45 if extension != '.csv':
46 raise UsageError(f'The file {self.csv_path} is not a csv file.')