def run(cls, input_folder_name, output_filename, include_commented_messages=False, save=True): if openpyxl is None: raise ImportError( 'Please install "openpyxl" to use "PoFilesSpreadsheetExporter" class' ) po_filenames = get_po_filenames(input_folder_name) if not po_filenames: raise ValueError('No files were added. Please, try again.') wb, ws = cls._init_po_workbook(po_filenames) try: cls._fill_po_worksheet(po_filenames, ws, include_commented_messages) if save: wb.save(filename=output_filename) except Exception as e: logger.debug( 'Exception occurred while worksheet processing > {!r}'.format( e), exc_info=True) return wb
def _get_column_index(row, po_file_short_name): for cell in row: if po_file_short_name in cell.value: return cell.column else: logger.debug('Column [{}] doesn\'t have data for {}'.format( cell.value, po_file_short_name)) return None
def __setitem__(self, id, message): message.string = self.clean_string(message.string) if not self._messages_strings.has_value(message.string, self._fuzzy_score_threshold): self._messages_strings.add(message.string) super(UniqueMessagesCatalog, self).__setitem__(id, message) else: logger.debug('Message is not unique > {}'.format( text_type(message.string))) self._repeatable_messages[id] = message.string
def clean_string(self, msg_string): if self._clean_msg_funcs: for func in self._clean_msg_funcs: try: msg_string = func(msg_string) except Exception as e: logger.debug( 'message string cleaning error > {!r}'.format(e), exc_info=True) return msg_string
def has_value(self, value, score_threshold): try: v = self.get(value) res = v is not None and v[0][0] > score_threshold if res: logger.debug( 'Value in set > {}, provided value > {}'.format( v, value)) return res except ZeroDivisionError: # set is empty return False
def _process_catalog(cls, worksheet, catalog, column_idx): new_catalog = UpdatableCatalog() new_catalog.__dict__ = catalog.__dict__.copy() for row in worksheet.iter_rows(min_row=2): if row: msgid = row[0].value msgstr = row[column_idx - 1].value if msgid and msgstr: new_catalog.add(msgid, string=msgstr) logger.debug( u'\nMsgid: {}\nMsgstr: {}\nMsgid: {}\nMsgstr (catalog): {}\n' .format(msgid, msgstr, new_catalog[msgid], new_catalog[msgid].string)) return new_catalog
def _update_po_files(cls, input_spreadsheet, po_filenames, output_dir, save): updated_catalogs = [] if output_dir: try: os.makedirs(output_dir) except OSError as e: if e.errno != errno.EEXIST: output_dir = None wb = openpyxl.load_workbook(input_spreadsheet, read_only=True) ws = wb.active first_row = ws[1] for filename in po_filenames: with open(filename) as f: short_filename = get_po_filename_from_path(filename) logger.debug('\n--------- Processing [{}] ---------\n'.format( short_filename)) col_index = cls._get_column_index(first_row, short_filename) if col_index: catalog = read_po(f, charset=ENCODING) new_catalog = cls._process_catalog(ws, catalog, col_index) updated_catalogs.append(new_catalog) if output_dir: file_args = (os.path.join(output_dir, short_filename), 'w+') else: file_args = (filename, 'w+') if save: with open(*file_args) as nf: write_po(nf, new_catalog, width=None) return updated_catalogs
def run(cls, input_spreadsheet, input_folder_name, output_dir=None, save=True): if openpyxl is None: raise ImportError( 'Please install "openpyxl" to use "PoFilesSpreadsheetUpdater" class' ) po_filenames = get_po_filenames(input_folder_name) if not po_filenames: raise ValueError('No files were added. Please, try again.') try: updated_catalogs = cls._update_po_files(input_spreadsheet, po_filenames, output_dir, save) except Exception as e: logger.debug( 'Exception occurred while po files update > {!r}'.format(e), exc_info=True) else: return updated_catalogs