From bcbc0dcfc8dcddcf129f54a1186483659a3fa729 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Tue, 14 Feb 2023 08:53:26 +0100 Subject: [PATCH] Cleanup --- pytransifex/{api_new.py => api.py} | 0 pytransifex/cli.py | 2 +- .../config-plugins/opengis-mkdocs/main.py | 56 -------------- .../opengis-mkdocs/requirements.txt | 1 - pytransifex/plugins_manager.py | 74 ------------------- tests/test_api_new.py | 2 +- tests/test_cli.py | 2 +- tests/test_config-plugins.py | 16 ---- 8 files changed, 3 insertions(+), 150 deletions(-) rename pytransifex/{api_new.py => api.py} (100%) delete mode 100644 pytransifex/config-plugins/opengis-mkdocs/main.py delete mode 100644 pytransifex/config-plugins/opengis-mkdocs/requirements.txt delete mode 100644 pytransifex/plugins_manager.py delete mode 100644 tests/test_config-plugins.py diff --git a/pytransifex/api_new.py b/pytransifex/api.py similarity index 100% rename from pytransifex/api_new.py rename to pytransifex/api.py diff --git a/pytransifex/cli.py b/pytransifex/cli.py index fd616e2..d8db4e3 100644 --- a/pytransifex/cli.py +++ b/pytransifex/cli.py @@ -3,7 +3,7 @@ import click -from pytransifex.api_new import Transifex +from pytransifex.api import Transifex from pytransifex.config import CliSettings client = Transifex(defer_login=True) diff --git a/pytransifex/config-plugins/opengis-mkdocs/main.py b/pytransifex/config-plugins/opengis-mkdocs/main.py deleted file mode 100644 index 609a6b2..0000000 --- a/pytransifex/config-plugins/opengis-mkdocs/main.py +++ /dev/null @@ -1,56 +0,0 @@ -# © 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 deleted file mode 100644 index aad499a..0000000 --- a/pytransifex/config-plugins/opengis-mkdocs/requirements.txt +++ /dev/null @@ -1 +0,0 @@ -python-frontmatter==1.0.0 diff --git a/pytransifex/plugins_manager.py b/pytransifex/plugins_manager.py deleted file mode 100644 index ef4054f..0000000 --- a/pytransifex/plugins_manager.py +++ /dev/null @@ -1,74 +0,0 @@ -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_api_new.py b/tests/test_api_new.py index 155ed63..e493ee6 100644 --- a/tests/test_api_new.py +++ b/tests/test_api_new.py @@ -2,7 +2,7 @@ from pathlib import Path from shutil import rmtree -from pytransifex.api_new import Transifex +from pytransifex.api import Transifex from pytransifex.interfaces import Tx diff --git a/tests/test_cli.py b/tests/test_cli.py index 6cd7c88..9a81a9c 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -4,7 +4,7 @@ from click.testing import CliRunner -from pytransifex.api_new import Transifex +from pytransifex.api import Transifex from pytransifex.cli import cli diff --git a/tests/test_config-plugins.py b/tests/test_config-plugins.py deleted file mode 100644 index 720a7fc..0000000 --- a/tests/test_config-plugins.py +++ /dev/null @@ -1,16 +0,0 @@ -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