2 Module containing the SPCsvLoader class.
4 The class allows to load phrases from a csv file.
8 from nominatim.tools.special_phrases.special_phrase import SpecialPhrase
9 from nominatim.tools.special_phrases.sp_loader import SPLoader
10 from nominatim.errors import UsageError
12 class SPCsvLoader(SPLoader):
14 Base class for special phrases loaders.
15 Handle the loading of special phrases from external sources.
17 def __init__(self, csv_path):
19 self.csv_path = csv_path
20 self.has_been_read = False
23 if self.has_been_read:
26 self.has_been_read = True
27 SPCsvLoader.check_csv_validity(self.csv_path)
28 return SPCsvLoader.parse_csv(self.csv_path)
31 def parse_csv(csv_path):
33 Open and parse the given csv file.
34 Create the corresponding SpecialPhrases.
38 with open(csv_path) as file:
39 reader = csv.DictReader(file, delimiter=',')
42 SpecialPhrase(row['phrase'], row['class'], row['type'], row['operator'])
47 def check_csv_validity(csv_path):
49 Check that the csv file has the right extension.
51 _, extension = os.path.splitext(csv_path)
53 if extension != '.csv':
54 raise UsageError('The file {} is not a csv file.'.format(csv_path))