2 Module containing the SPCsvLoader class.
4 The class allows to load phrases from a csv file.
8 from collections.abc import Iterator
9 from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
10 from nominatim.errors import UsageError
12 class SPCsvLoader(Iterator):
14 Handles loading of special phrases from external csv file.
16 def __init__(self, csv_path):
18 self.csv_path = csv_path
19 self.has_been_read = False
22 if self.has_been_read:
25 self.has_been_read = True
26 self.check_csv_validity()
27 return self.parse_csv()
31 Open and parse the given csv file.
32 Create the corresponding SpecialPhrases.
36 with open(self.csv_path) as file:
37 reader = csv.DictReader(file, delimiter=',')
40 SpecialPhrase(row['phrase'], row['class'], row['type'], row['operator'])
44 def check_csv_validity(self):
46 Check that the csv file has the right extension.
48 _, extension = os.path.splitext(self.csv_path)
50 if extension != '.csv':
51 raise UsageError('The file {} is not a csv file.'.format(self.csv_path))