def _merge_source_catalog(js_catalog, python_catalog, old_source_catalog): """ Read the message catalogs extracted from js and python for the default locale and merge them. Return - the new merged catalog for the default locale - a catalog with the new messages extracted from python code - a set with the unique identifiers (in the sense of `message_unique_identifier`) of fuzzy messages """ for message in python_catalog: if message.id: for comment in message.auto_comments: match = _default_message_re.match(comment) if match: default_message = match.group(1).strip() message.auto_comments.remove(comment) message.string = default_message fill_catalog(js_catalog, python_catalog, python_catalog) return ( js_catalog, python_catalog, find_fuzzy_messages(new_catalog=js_catalog, old_catalog=old_source_catalog), )
def extract_module_messages(directory: pathlib.Path): module_files = ModuleFiles.load_from_dirpath(directory) # raise ValueError source_catalog = _build_source_catalog( ModuleSpec.load_from_path(module_files.spec), pathlib.Path(module_files.code), directory, ) po_path = _po_path(directory, default_locale) try: old_source_catalog = read_po_catalog(po_path) except FileNotFoundError: old_source_catalog = Catalog(default_locale) if not catalogs_are_same(source_catalog, old_source_catalog): write_po_catalog(po_path, source_catalog) fuzzy = find_fuzzy_messages(old_catalog=old_source_catalog, new_catalog=source_catalog) for locale_id in supported_locales: if locale_id != default_locale: po_path = _po_path(directory, locale_id) try: old_catalog = read_po_catalog(po_path) except FileNotFoundError: old_catalog = Catalog(locale_id) catalog = _merge_nonsource_catalog(locale_id, old_catalog, source_catalog, fuzzy) if not catalogs_are_same(catalog, old_catalog): write_po_catalog(po_path, catalog)
def test_find_fuzzy_messages_fuzzy_with_context(self): old_catalog = Catalog() old_catalog.add("id1", string="Text1", context="ctxt") new_catalog = Catalog() new_catalog.add("id1", string="Text1new", context="ctxt") self.assertEqual( find_fuzzy_messages(old_catalog=old_catalog, new_catalog=new_catalog), frozenset([("id1", "ctxt")]), )
def test_find_fuzzy_messages_fuzzy(self): old_catalog = Catalog() old_catalog.add("id1", string="Text1") new_catalog = Catalog() new_catalog.add("id1", string="Text1new") self.assertEqual( find_fuzzy_messages(old_catalog=old_catalog, new_catalog=new_catalog), frozenset(["id1"]), )
def test_find_fuzzy_messages_ignore_empty_old(self): old_catalog = Catalog() old_catalog.add("id1", string="") new_catalog = Catalog() old_catalog.add("id1", string="Text1") self.assertEqual( find_fuzzy_messages(old_catalog=old_catalog, new_catalog=new_catalog), frozenset(), )
def test_find_fuzzy_messages_ignore_already_fuzzy(self): old_catalog = Catalog() old_catalog.add("id1", string="Text1", flags=["fuzzy"]) new_catalog = Catalog() new_catalog.add("id1", string="Text1", flags=["fuzzy"]) self.assertEqual( find_fuzzy_messages(old_catalog=old_catalog, new_catalog=new_catalog), frozenset(), )
def extract_module_messages(directory: pathlib.Path): with directory_loaded_as_zipfile_path(directory) as zip_path: module_zipfile = ModuleZipfile(zip_path) # may be invalid source_catalog = _build_source_catalog(module_zipfile) po_path = _po_path(directory, default_locale) try: old_source_catalog = read_po_catalog(po_path) except FileNotFoundError: old_source_catalog = Catalog(default_locale) # Update file for default locale if not catalogs_are_same(source_catalog, old_source_catalog): write_po_catalog(po_path, source_catalog) # Update template catalog # We will have no specific locale in the template catalog template_catalog = copy_catalog(source_catalog, locale=None) move_strings_to_comments(template_catalog, comment_tag="default-message") pot_path = _pot_path(directory) try: old_template_catalog = read_po_catalog(pot_path) except FileNotFoundError: old_template_catalog = Catalog() if not catalogs_are_same(template_catalog, old_template_catalog): write_po_catalog( pot_path, template_catalog, ignore_obsolete=True, width= 10000000, # we set a huge value for width, so that special comments do not wrap omit_header= True, # removes locale and other info from the output file ) fuzzy = find_fuzzy_messages(old_catalog=old_source_catalog, new_catalog=source_catalog) for locale_id in supported_locales: if locale_id != default_locale: po_path = _po_path(directory, locale_id) try: old_catalog = read_po_catalog(po_path) except FileNotFoundError: old_catalog = Catalog(locale_id) catalog = _merge_nonsource_catalog(locale_id, old_catalog, source_catalog, fuzzy) if not catalogs_are_same(catalog, old_catalog): write_po_catalog(po_path, catalog)
def test_find_fuzzy_messages_fuzzy_multiple(self): old_catalog = Catalog() old_catalog.add("id0", string="Text1") old_catalog.add("id1", string="Text1") old_catalog.add("id2", string="Text1", context="ctxt1") old_catalog.add("id2", string="Text1", context="ctxt2") new_catalog = Catalog() new_catalog.add("id0", string="Text1") new_catalog.add("id1", string="Text1new") new_catalog.add("id2", string="Text1new", context="ctxt1") new_catalog.add("id2", string="Text1new", context="ctxt2") self.assertEqual( find_fuzzy_messages(old_catalog=old_catalog, new_catalog=new_catalog), frozenset(["id1", ("id2", "ctxt1"), ("id2", "ctxt2")]), )