Skip to content
This repository has been archived by the owner on Dec 2, 2024. It is now read-only.

Commit

Permalink
Path moved to internals. Edges of program using str instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Feb 18, 2023
1 parent 93a9178 commit 94f6cc9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
24 changes: 11 additions & 13 deletions pytransifex/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
from os import mkdir
from pathlib import Path
from typing import Any, Optional

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"):
Expand All @@ -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(
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 = []
Expand All @@ -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))
Expand Down
10 changes: 5 additions & 5 deletions pytransifex/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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}"
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down

0 comments on commit 94f6cc9

Please sign in to comment.