from localconfig import STORESFILE import csv class Stores(): def __init__(self): ''' Download the business unit CSV from the GK reporting server or pick it up from disk :param self: Description ''' # open csv file to read in all the stores to invoice self.stores = {} with open(STORESFILE, newline="", encoding="utf-8") as f_in: reader = csv.DictReader(f_in) for row in reader: if row["invoice"] == "1": if row['country'] not in self.stores: self.stores[row["country"]] = [] self.stores[row["country"]].append([row["store"], row['name']]) # self.stores = {} # filename = 'businessunitdetails.csv' # with open(filename, 'r', encoding='utf-8') as f: # while line := f.readline(): # store, name, dummy = line.split(',',2) # self.stores[store] = name # print(f" STORES {len(self.stores.keys())}") def all_countries(self): return self.stores.keys() def stores_for(self, country): result = [] if country in self.stores.keys(): for stores in self.stores[country]: result.append(stores[0]) return result def get_store_name(self, store): result = '[ store name ]' for country in self.stores.keys(): for s in self.stores[country]: if store == s[0]: result = s[1] return result def report_stores(self): result = '' for country in self.stores.keys(): ss = '' for s in self.stores[country]: ss += ' ' + s[0] result += f"{country}:{ss}\n" return result def country_for(self, store): result = 'XX' for country in self.stores.keys(): for s in self.stores[country]: if store == s[0]: result = country return result