[docs]classCSVWriter(BaseWriter):""" Writer class with output to CSV files. For each table, a corresponding CSV file will be created. """name="csv"def__init__(self,workdir,tables,options,schema):""" :param workdir: Working directory :param tables: The table objects :param options: Flattening options """super().__init__(workdir,tables,options,schema=schema)self.writers={}self.fds=[]def__enter__(self):""" Write the headers to the output file. """forname,tableinself.tables.items():table_name,headers=self.init_sheet(name,table)try:path=self.workdir/f"{table_name}.csv"LOGGER.info(_("Dumping table '{}' to file '{}'").format(table_name,path))fd=open(path,"w")exceptOSErrorase:LOGGER.error(_("Failed to open file {} with error {}").format(path,e))# noqa: TRY400 # UXreturnNonewriter=csv.DictWriter(fd,headers)self.fds.append(fd)self.writers[name]=writerforname,writerinself.writers.items():headers=self.headers[name]try:writer.writerow(headers)exceptValueErroraserr:LOGGER.error(_("Failed to headers with error {}").format(err))# noqa: TRY400 # UXreturnselfdef__exit__(self,*args):""" Close the CSV files. """forfdinself.fds:fd.close()
[docs]defwriterow(self,table,row):""" Write a row to the output file. """try:self.writers[table].writerow(row)exceptValueErroraserr:LOGGER.error(# noqa: TRY400 # UX_("Operation produced invalid path. This a software bug, please send issue to developers"))LOGGER.error(# noqa: TRY400 # UX_("Failed to write row {} with error {}").format(row.get("rowID"),err))exceptKeyError:LOGGER.error(_("Invalid table {}").format(table))# noqa: TRY400 # UX