Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| from preprocess.utils.common.utils import get_delimiter | |
| import shutil | |
| import os | |
| def update_products_csv(new_csv_path, prods_file, overwrite_existing): | |
| if os.path.isfile(prods_file) and not overwrite_existing: | |
| main_sep=get_delimiter(prods_file) | |
| main_csv=pd.read_csv(prods_file, sep=main_sep, on_bad_lines="warn") | |
| new_sep=get_delimiter(new_csv_path) | |
| new_csv=pd.read_csv(new_csv_path, sep=new_sep, on_bad_lines="warn") | |
| if 'attrs' in new_csv.columns.values: | |
| raise Exception("Uploaded Products CSV does not seem to be valid") | |
| result=pd.concat([main_csv, new_csv]).drop_duplicates(subset='id', keep='last').reset_index(drop=True) | |
| result.to_csv(prods_file, sep=main_sep, index=False) | |
| else: | |
| new_sep=get_delimiter(new_csv_path) | |
| new_csv=pd.read_csv(new_csv_path, sep=new_sep, on_bad_lines="warn") | |
| new_csv.to_csv(prods_file, sep=new_sep, index=False) | |
| return prods_file | |
| '''def is_csv_exist(path): | |
| file_list=glob(path+'/*.csv') | |
| if len(file_list)>0: | |
| return file_list[0] | |
| else: | |
| None | |
| def uploader(new_path, main_dir='/home/user/app/tmp/prod.csv'): | |
| main_path=is_csv_exist(main_dir) | |
| if main_path==None: | |
| new_path = shutil.move(new_path, main_dir) | |
| return new_path | |
| else: | |
| update_products_csv(main_path, new_path) | |
| return main_path | |
| def remover(data_path): | |
| #path=is_csv_exist('/home/user/app/tmp/prod.csv') | |
| #if path!=None: | |
| os.remove(os.getcwd()+'/tmp/prod.csv') | |
| shutil.copy2('/home/user/app/tmp/service/prod.csv', '/home/user/app/tmp/prod.csv')''' | |