def test_update_catalogs_all(minimal_i18n_settings, caplog, temp_builds_dir, fixtures_settings): """ Update every catalogs """ basepath = temp_builds_dir.join("i18n_update_catalogs_all") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) updated = manager.update_catalogs() assert updated == ["en_US", "fr_FR"] assert caplog.record_tuples == [ ( "optimus", logging.INFO, "Updating catalog (PO) for language 'en_US' to {}".format( manager.get_po_filepath("en_US")), ), ( "optimus", logging.INFO, "Updating catalog (PO) for language 'fr_FR' to {}".format( manager.get_po_filepath("fr_FR")), ), ]
def po_interface(settings, init=False, update=False, compile_opt=False): """ Manage project translation catalogs for all registred languages. You may enable all available modes. Modes are always processed in the same order: "init" then "update" and finally "compile". Arguments: settings (optimus.conf.model.SettingsModel): Settings object which define paths for locale directory and path for template sources to possibly scan. Keyword Arguments: init (boolean): Enable init mode to initialize POT file and "locale" directory. update (boolean): Enable update mode to refresh POT file and PO files for template changes. compile_opt (boolean): Enable compile mode to compile MO files from PO files. """ # Proceed to operations i18n = I18NManager(settings) if init or update or compile_opt: i18n.init_locales_dir() i18n.build_pot(force=update) i18n.init_catalogs() if update: i18n.update_catalogs() if compile_opt: i18n.compile_catalogs()
def test_update_catalogs_one(minimal_i18n_settings, caplog, temp_builds_dir, fixtures_settings): """ Update only default locale catalog """ basepath = temp_builds_dir.join("i18n_update_catalogs_one") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) updated = manager.update_catalogs([settings.LANGUAGE_CODE]) assert updated == [settings.LANGUAGE_CODE] assert os.path.exists(manager.get_po_filepath( settings.LANGUAGE_CODE)) is True assert caplog.record_tuples == [ ( "optimus", logging.INFO, "Updating catalog (PO) for language 'en_US' to {}".format( manager.get_po_filepath(settings.LANGUAGE_CODE)), ), ]
def test_pot_attribute_getter(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Reach the pot attribute that may trigger the build_pot when POT does not allready exists """ basepath = temp_builds_dir.join("i18n_pot_attribute_getter") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Remove locale dir so the POT doesnt exists anymore shutil.rmtree(settings.LOCALES_DIR) # Recreate just locale base dir os.makedirs(settings.LOCALES_DIR) # Access pot through pot attribute pot = manager.pot assert pot.header_comment == ("""# Translations template for """ """minimal_i18n project""" """\n# Created by Optimus""") assert ("Hello World!" in pot) is True assert pot["Hello World!"] == Message("Hello World!")
def test_build_pot(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Force to rebuild POT file """ basepath = temp_builds_dir.join("i18n_build_pot") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) pot = manager.build_pot(force=True) assert pot.header_comment == ("""# Translations template for """ """minimal_i18n project""" """\n# Created by Optimus""") assert ("Hello World!" in pot) is True assert pot["Hello World!"] == Message("Hello World!")
def test_init_locales_dir_success(minimal_i18n_settings, caplog, temp_builds_dir, fixtures_settings): """Check path resolutions and validations""" basepath = temp_builds_dir.join("i18n_init_locales_dir_success") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get settings settings = minimal_i18n_settings(destination) # Remove locale sample shutil.rmtree(settings.LOCALES_DIR) manager = I18NManager(settings) manager.init_locales_dir() assert manager.check_locales_dir() is True # Last log entry should say about creating locale dir assert caplog.record_tuples[-1] == ( "optimus", logging.WARNING, "Locale directory does not exists, creating it", )
def test_init_catalogs_one(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Init only default locale catalog """ basepath = temp_builds_dir.join("i18n_init_catalogs_one") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Empty locale dir from every enabled language for lang in manager.parse_languages(settings.LANGUAGES): shutil.rmtree(os.path.join(settings.LOCALES_DIR, lang)) created = manager.init_catalogs([settings.LANGUAGE_CODE]) assert created == [settings.LANGUAGE_CODE] assert os.path.exists(manager.get_po_filepath(settings.LANGUAGE_CODE)) is True
def test_overwrite_po_success(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ safe_write_po usage for overwritting file """ basepath = temp_builds_dir.join("i18n_overwrite_po_success") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) dummy_name = "dummy_pot.pot" dummy_pot = os.path.join(destination, dummy_name) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Create a dummy catalog to write catalog = Catalog(header_comment="# Foobar") catalog.add("foo %(name)s", locations=[("main.py", 1)], flags=("fuzzy", )) # Write it manager.safe_write_po(catalog, dummy_pot) # Check it with io.open(dummy_pot, "rb") as f: dummy_catalog = read_po(f) assert dummy_catalog.header_comment == "# Foobar" # New dummy catalog to overwrite previous one catalog = Catalog(header_comment="# Zob") catalog.add("ping", string="pong", locations=[("nope.py", 42)]) # Write it manager.safe_write_po(catalog, dummy_pot) # List existing pot file at root pots = [item for item in os.listdir(destination) if item.endswith(".pot")] # No other pot file assert pots == [dummy_name] # Check it again with io.open(dummy_pot, "rb") as f: dummy_catalog = read_po(f) assert dummy_catalog.header_comment == "# Zob" assert dummy_catalog["ping"] == Message("ping", string="pong")
def test_overwrite_po_fail(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ safe_write_po usage for overwritting file failing but left untouched initial file """ basepath = temp_builds_dir.join("i18n_overwrite_po_fail") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) dummy_name = "dummy_pot.pot" dummy_pot = os.path.join(destination, dummy_name) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Create a dummy catalog to write catalog = Catalog(header_comment="# Foobar") catalog.add("foo %(name)s", locations=[("main.py", 1)], flags=("fuzzy", )) # Write it manager.safe_write_po(catalog, dummy_pot) # Check it with io.open(dummy_pot, "rb") as f: dummy_catalog = read_po(f) assert dummy_catalog.header_comment == "# Foobar" # Try to overwrite with empty catalog to raise error with pytest.raises(TypeError): manager.safe_write_po(None, dummy_pot) # List existing pot file at root pots = [item for item in os.listdir(destination) if item.endswith(".pot")] # No other pot file assert pots == [dummy_name] # Check it again with io.open(dummy_pot, "rb") as f: dummy_catalog = read_po(f) # Initial has been left untouched assert dummy_catalog.header_comment == "# Foobar"
def test_compile_catalogs_invalid_catalog(minimal_i18n_settings, capsys, caplog, temp_builds_dir, fixtures_settings): """ Try compile an erroneous catalog Sadly we dont have any real error here, read 'I18NManager.compile_catalogs' for more details. """ basepath = temp_builds_dir.join("i18n_compile_catalogs_invalid_catalog") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Create erroneous catalog to compile erroneous_local = "bg" os.makedirs(manager.get_catalog_dir(erroneous_local)) with open(manager.get_po_filepath(erroneous_local), "w") as fp: fp.write(ERRONEOUS_PO) updated = manager.compile_catalogs([erroneous_local]) assert caplog.record_tuples == [ ( "optimus", logging.INFO, "Compiling catalog (MO) for language '{}' to {}".format( erroneous_local, manager.get_mo_filepath(erroneous_local)), ), ] # This should have been empty but sadly compiling does not thrown any # error from erroneous catalog assert updated == [erroneous_local] assert os.path.exists(manager.get_mo_filepath(erroneous_local)) is True out, err = capsys.readouterr() assert out == ("""WARNING: msg has more translations than num_plurals """ """of catalog\nWARNING: Problem on line 7: ''\n""")
def test_init_catalogs_all( minimal_i18n_settings, caplog, temp_builds_dir, fixtures_settings ): """ Init every enabled catalogs """ basepath = temp_builds_dir.join("i18n_init_catalogs_all") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) # Empty locale dir from every enabled language for lang in manager.parse_languages(settings.LANGUAGES): shutil.rmtree(os.path.join(settings.LOCALES_DIR, lang)) created = manager.init_catalogs() assert created == ["en_US", "fr_FR"] for lang in manager.parse_languages(settings.LANGUAGES): assert os.path.exists(manager.get_po_filepath(lang)) is True assert caplog.record_tuples == [ ("optimus", logging.DEBUG, "Opening template catalog (POT)"), ( "optimus", logging.DEBUG, "Init catalog (PO) for language 'en_US' to {}".format( manager.get_po_filepath("en_US") ), ), ( "optimus", logging.DEBUG, "Init catalog (PO) for language 'fr_FR' to {}".format( manager.get_po_filepath("fr_FR") ), ), ]
def test_init_catalogs_empty(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Nothing to do since sample allready contains every language catalogs """ basepath = temp_builds_dir.join("i18n_init_catalogs_empty") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) created = manager.init_catalogs() assert created == []
def test_path_helpers(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Check path resolutions and validations """ basepath = temp_builds_dir.join("i18n_path_helpers") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get settings settings = minimal_i18n_settings(destination) assert settings.SITE_NAME == "minimal_i18n" manager = I18NManager(settings) attempted_lang = "fr_FR" attempted_localedir = os.path.join(destination, "locale") attempted_pot = os.path.join(attempted_localedir, "messages.pot") attempted_catalogdir = os.path.join(attempted_localedir, attempted_lang, "LC_MESSAGES") attempted_po = os.path.join(attempted_catalogdir, "messages.po") attempted_mo = os.path.join(attempted_catalogdir, "messages.mo") assert manager.check_locales_dir() is True assert manager.check_catalog_path(attempted_lang) is True assert manager.check_catalog_path("zh_cn") is False assert manager.get_template_path() == attempted_pot assert manager.get_catalog_dir(attempted_lang) == attempted_catalogdir assert manager.get_po_filepath(attempted_lang) == attempted_po assert manager.get_mo_filepath(attempted_lang) == attempted_mo assert manager.get_mo_filepath(attempted_lang) == attempted_mo
def test_compile_catalogs_filenotfounderror(minimal_i18n_settings, caplog, temp_builds_dir, fixtures_settings): """ Try compile unexisting catalog This is not catched/managed from manager, but keep it for behavior history """ basepath = temp_builds_dir.join("i18n_compile_catalogs_filenotfounderror") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) erroneous_local = "idontexist" with pytest.raises(IOError): manager.compile_catalogs([erroneous_local])
def test_clone_pot(minimal_i18n_settings, temp_builds_dir, fixtures_settings): """ Check POT cloning """ basepath = temp_builds_dir.join("i18n_clone_pot") # Copy sample project to temporary dir samplename = "minimal_i18n" samplepath = os.path.join(fixtures_settings.fixtures_path, samplename) destination = os.path.join(basepath.strpath, samplename) shutil.copytree(samplepath, destination) # Get manager with settings settings = minimal_i18n_settings(destination) manager = I18NManager(settings) manager.build_pot(force=True) cloned = manager.clone_pot() assert cloned.header_comment == ("""# Translations template for """ """minimal_i18n project""" """\n# Created by Optimus""") assert ("Hello World!" in cloned) is True assert cloned["Hello World!"] == Message("Hello World!")
def test_parse_languages(languages, attempted): # We dont need real settings object for this test manager = I18NManager(None) assert list(manager.parse_languages(languages)) == attempted