From 94f6cc9dc5be7d6febc14a04f3138aacff94b423 Mon Sep 17 00:00:00 2001 From: why-not-try-calmer Date: Sat, 18 Feb 2023 11:03:04 +0100 Subject: [PATCH] Path moved to internals. Edges of program using str instead. --- pytransifex/api.py | 24 +++++++++++------------- pytransifex/cli.py | 10 +++++----- tests/test_api.py | 2 +- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/pytransifex/api.py b/pytransifex/api.py index 8efd524..c4fb8c4 100644 --- a/pytransifex/api.py +++ b/pytransifex/api.py @@ -1,5 +1,4 @@ import logging -from os import mkdir from pathlib import Path from typing import Any, Optional @@ -124,7 +123,7 @@ def create_resource( self, *, project_slug: str, - path_to_file: Path, + path_to_file: str, resource_slug: str | None = None, resource_name: str | None = None, **kwargs, @@ -155,7 +154,7 @@ def create_resource( @ensure_login def update_source_translation( - self, *, project_slug: str, resource_slug: str, path_to_file: Path + self, *, project_slug: str, resource_slug: str, path_to_file: str ): """ Update the translation strings for the given resource using the content of the file @@ -191,11 +190,10 @@ def get_translation( project_slug: str, resource_slug: str, language_code: str, - output_dir: Path, + path_to_file: str, ): """Fetch the translation resource matching the given language""" language = tx_api.Language.get(code=language_code) - file_name = Path.joinpath(output_dir, resource_slug) if project := self.get_project(project_slug=project_slug): if resources := project.fetch("resources"): @@ -205,10 +203,8 @@ def get_translation( ) translated_content = requests.get(url).text - if not Path.exists(output_dir): - mkdir(output_dir) - - with open(file_name, "w") as fh: + Path.mkdir(Path(path_to_file), parents=True, exist_ok=True) + with open(path_to_file, "w") as fh: fh.write(translated_content) logging.info( @@ -264,7 +260,9 @@ def create_language( if coordinators: project.add("coordinators", coordinators) - logging.info(f"Created language resource for {language_code}") + logging.info( + f"Created language resource for {language_code} and added these coordinators: {coordinators}" + ) @ensure_login def project_exists(self, project_slug: str) -> bool: @@ -298,7 +296,7 @@ def pull( project_slug: str, resource_slugs: list[str], language_codes: list[str], - output_dir: Path, + output_dir: str, ): """Pull resources from project.""" args = [] @@ -315,12 +313,12 @@ def pull( @ensure_login def push( - self, *, project_slug: str, resource_slugs: list[str], path_to_files: list[Path] + self, *, project_slug: str, resource_slugs: list[str], path_to_files: list[str] ): """Push resources with files under project.""" if len(resource_slugs) != len(path_to_files): raise ValueError( - f"Resources slugs ({len(resource_slugs)}) and Path to files ({len(path_to_files)}) must be equal in size!" + f"Resources slugs ({len(resource_slugs)}) and path to files ({len(path_to_files)}) must be equal in size!" ) resource_zipped_with_path = list(zip(resource_slugs, path_to_files)) diff --git a/pytransifex/cli.py b/pytransifex/cli.py index ca64746..b5ff07d 100644 --- a/pytransifex/cli.py +++ b/pytransifex/cli.py @@ -9,6 +9,7 @@ from pytransifex.config import CliSettings client = Transifex(defer_login=True) +assert client def path_to_slug(file_paths: list[Path]) -> list[str]: @@ -94,7 +95,7 @@ def push(input_directory: str | None): client.push( project_slug=settings.project_slug, resource_slugs=slugs, - path_to_files=files, + path_to_files=[str(f) for f in files], ) except Exception as error: reply += f"cli:push > Failed because of this error: {error}" @@ -107,16 +108,15 @@ def push(input_directory: str | None): @click.option("-l", "--only-lang", default="all") @click.option("-out", "--output-directory", is_flag=False) @cli.command("pull", help="Pull translation strings") -def pull(output_directory: str | Path | None, only_lang: str | None): +def pull(output_directory: str | None, only_lang: str | None): reply = "" settings = CliSettings.from_disk() language_codes = only_lang.split(",") if only_lang else [] if output_directory: - output_directory = Path(output_directory) - settings.output_directory = output_directory + settings.output_directory = Path(output_directory) else: - output_directory = settings.output_directory + output_directory = str(settings.output_directory) resource_slugs = [] try: diff --git a/tests/test_api.py b/tests/test_api.py index d6ee7f8..4aa7765 100644 --- a/tests/test_api.py +++ b/tests/test_api.py @@ -81,7 +81,7 @@ def test8_get_translation(self): project_slug=self.project_slug, resource_slug=self.resource_slug, language_code="fr_CH", - output_dir=self.output_dir, + path_to_file=self.output_dir, ) assert Path.exists(Path.joinpath(self.output_dir, self.resource_slug))