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 collections.abc import Iterator
15 from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
16 from nominatim.errors import UsageError
18 class SPCsvLoader(Iterator):
20 Handles loading of special phrases from external csv file.
22 def __init__(self, csv_path):
24 self.csv_path = csv_path
25 self.has_been_read = False
28 if self.has_been_read:
31 self.has_been_read = True
32 self.check_csv_validity()
33 return self.parse_csv()
37 Open and parse the given csv file.
38 Create the corresponding SpecialPhrases.
42 with open(self.csv_path) as file:
43 reader = csv.DictReader(file, delimiter=',')
46 SpecialPhrase(row['phrase'], row['class'], row['type'], row['operator'])
50 def check_csv_validity(self):
52 Check that the csv file has the right extension.
54 _, extension = os.path.splitext(self.csv_path)
56 if extension != '.csv':
57 raise UsageError(f'The file {self.csv_path} is not a csv file.')