diff --git a/pytransifex/api_new.py b/pytransifex/api_new.py index 2443744..be09c1b 100644 --- a/pytransifex/api_new.py +++ b/pytransifex/api_new.py @@ -69,7 +69,7 @@ def create_project( logging.info("Project created!") return res except JsonApiException as error: - if "already exists" in error.detail: # type: ignore + if "already exists" in error.detail: # type: ignore return self.get_project(project_slug=project_slug) @ensure_login diff --git a/pytransifex/config-plugins/opengis-mkdocs/main.py b/pytransifex/config-plugins/opengis-mkdocs/main.py new file mode 100644 index 0000000..609a6b2 --- /dev/null +++ b/pytransifex/config-plugins/opengis-mkdocs/main.py @@ -0,0 +1,56 @@ +# © 2022 Mario Baranzini @ mario.baranzini@opengis.ch +import glob +import logging +import os +from typing import NamedTuple + +import frontmatter + + +class TxProjectConfig(NamedTuple): + TX_ORGANIZATION = "opengisch" + TX_PROJECT = "qfield-documentation" + TX_SOURCE_LANG = "en" + TX_TYPE = "GITHUBMARKDOWN" + + +def create_transifex_config(config: TxProjectConfig): + """Parse all source documentation files and add the ones with tx_slug metadata + defined to transifex config file. + """ + logging.info("Start creating transifex configuration") + + current_dir = os.path.dirname(os.path.abspath(__file__)) + config_file = os.path.join(current_dir, "..", ".tx", "config") + root = os.path.join(current_dir, "..") + count = 0 + + with open(config_file, "w") as f: + f.write("[main]\n") + f.write("host = https://www.transifex.com\n\n") + + for file in glob.iglob( + current_dir + "/../documentation/**/*.en.md", recursive=True + ): + + # Get relative path of file + relative_path = os.path.relpath(file, start=root) + + tx_slug = frontmatter.load(file).get("tx_slug", None) + + if tx_slug: + logging.info( + f"Found file with tx_slug defined: {relative_path}, {tx_slug}" + ) + f.write( + f"[o:{config.TX_ORGANIZATION}:p:{config.TX_PROJECT}:r:{tx_slug}]\n" + ) + f.write( + f"file_filter = {''.join(relative_path.split('.')[:-2])}..md\n" + ) + f.write(f"source_file = {relative_path}\n") + f.write(f"source_lang = {config.TX_SOURCE_LANG}\n") + f.write(f"type = {config.TX_TYPE}\n\n") + count += 1 + + logging.info(f"Transifex configuration created. {count} resources added.") diff --git a/pytransifex/config-plugins/opengis-mkdocs/requirements.txt b/pytransifex/config-plugins/opengis-mkdocs/requirements.txt new file mode 100644 index 0000000..aad499a --- /dev/null +++ b/pytransifex/config-plugins/opengis-mkdocs/requirements.txt @@ -0,0 +1 @@ +python-frontmatter==1.0.0 diff --git a/pytransifex/plugins_manager.py b/pytransifex/plugins_manager.py new file mode 100644 index 0000000..ef4054f --- /dev/null +++ b/pytransifex/plugins_manager.py @@ -0,0 +1,74 @@ +import sys +from functools import reduce +from importlib.util import module_from_spec, spec_from_file_location +from pathlib import Path +from typing import Any + + +class PluginManager: + # list of path_to_ subdir, path_to_ main.py + discovered_subdir_main: list[tuple[Path, Path]] = [] + # successfully imported modules + imported_modules: dict[str, Any] = {} + + @staticmethod + def discover(): + """ + Expecting directory structure: + pytransifex/ + config-plugins/{plugins} + main.py + """ + plugins_dir = Path.cwd().joinpath("pytransifex", "config-plugins") + subdirs = [f for f in plugins_dir.iterdir() if f.is_dir()] + + def collect_mains(acc: list[Path], sub: Path): + main = Path(sub).joinpath("main.py") + + if main.exists() and main.is_file(): + acc.append(main) + + return acc + + collected = reduce(collect_mains, subdirs, []) + PluginManager.discovered_subdir_main = list(zip(subdirs, collected)) + + @staticmethod + def load_plugin(target_plugin_dir: str): + conventional_main = "main.py" + + if name_main := next( + ( + (main_p, directory_p.name) + for directory_p, main_p in PluginManager.discovered_subdir_main + if directory_p.name == target_plugin_dir + ), + None, + ): + main, name = name_main + + if name in PluginManager.imported_modules: + print("Already imported! Nothing to do.") + return + + if spec := spec_from_file_location(conventional_main, main): + module = module_from_spec(spec) + sys.modules[conventional_main] = module + + if spec.loader: + spec.loader.exec_module(module) + PluginManager.imported_modules[name] = module + print( + f"Successfully imported and loaded {module}! Imported modules read: {PluginManager.imported_modules}" + ) + + else: + raise Exception(f"Failed to load module '{module}' at {main}") + + else: + raise Exception(f"Unable to find spec 'main.py' for {main}") + + else: + raise Exception( + f"Couldn't find the '{target_plugin_dir}' directory; it was expected to be a child of the 'pytransifex' directory." + ) diff --git a/tests/test_config-plugins.py b/tests/test_config-plugins.py new file mode 100644 index 0000000..720a7fc --- /dev/null +++ b/tests/test_config-plugins.py @@ -0,0 +1,16 @@ +import unittest + +from pytransifex.plugins_manager import PluginManager + + +class TestPluginsRegistrar(unittest.TestCase): + def test1_plugin_discover(self): + PluginManager.discover() + assert list(PluginManager.discovered_subdir_main) + + def test2_plugin_import(self): + PluginManager.load_plugin("opengis-mkdocs") + assert list(PluginManager.imported_modules) + + def test3_imported_plugin(self): + assert PluginManager.imported_modules["opengis-mkdocs"].TxProjectConfig