1 # SPDX-License-Identifier: GPL-2.0-only
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2022 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.
14 from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
15 from nominatim.errors import UsageError
19 Handles loading of special phrases from external csv file.
21 def __init__(self, csv_path):
23 self.csv_path = csv_path
26 def generate_phrases(self):
27 """ Open and parse the given csv file.
28 Create the corresponding SpecialPhrases.
30 self._check_csv_validity()
32 with open(self.csv_path, encoding='utf-8') as fd:
33 reader = csv.DictReader(fd, delimiter=',')
35 yield SpecialPhrase(row['phrase'], row['class'], row['type'], row['operator'])
38 def _check_csv_validity(self):
40 Check that the csv file has the right extension.
42 _, extension = os.path.splitext(self.csv_path)
44 if extension != '.csv':
45 raise UsageError(f'The file {self.csv_path} is not a csv file.')