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

Commit

Permalink
Reverting 2 functions called concurrently to again use pos arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
why-not-try-calmer committed Feb 20, 2023
1 parent 9e4b44a commit 9e142bf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
28 changes: 18 additions & 10 deletions pytransifex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def create_resource(

@ensure_login
def update_source_translation(
self, *, project_slug: str, resource_slug: str, path_to_file: str
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 @@ -186,16 +186,27 @@ def update_source_translation(
@ensure_login
def get_translation(
self,
*,
project_slug: str,
resource_slug: str,
language_code: str,
path_to_output_dir: str,
path_to_output_file: None | str = None,
path_to_output_dir: None | str = None,
) -> str:
"""Fetch the translation resource matching the given language"""
if path_to_output_dir and not path_to_output_file:
path_to_parent = Path(path_to_output_dir)
path_to_output_file = str(
path_to_parent.joinpath(f"{resource_slug}_{language_code}")
)
elif path_to_output_file and not path_to_output_dir:
path_to_parent = Path(path_to_output_file).parent
else:
raise ValueError(
f"get_translation needs exactly one between 'path_to_output_file' (str) or 'path_to_output_dir (str). "
)

Path.mkdir(path_to_parent, parents=True, exist_ok=True)
language = tx_api.Language.get(code=language_code)
path_to_output_dir_as_path = Path(path_to_output_dir)
Path.mkdir(path_to_output_dir_as_path, parents=True, exist_ok=True)

if project := self.get_project(project_slug=project_slug):
if resources := project.fetch("resources"):
Expand All @@ -204,16 +215,13 @@ def get_translation(
resource=resource, language=language
)
translated_content = requests.get(url).text
path_to_file = path_to_output_dir_as_path.joinpath(
f"{resource_slug}_{language_code}"
)
with open(path_to_file, "w") as fh:
with open(path_to_output_file, "w") as fh:
fh.write(translated_content)

logging.info(
f"Translations downloaded and written to file (resource: {resource_slug})"
)
return str(path_to_file)
return str(path_to_output_file)

else:
raise ValueError(
Expand Down
11 changes: 7 additions & 4 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def setUpClass(cls):
cls.path_to_input_dir = Path.cwd().joinpath("tests", "data", "resources")
cls.path_to_file = cls.path_to_input_dir.joinpath("test_resource_fr.po")
cls.output_dir = Path.cwd().joinpath("tests", "output")
cls.output_file = cls.output_dir.joinpath("test_resource_fr_DOWNLOADED.po")

cls.tx = client
cls.project_slug = test_config["project_slug"]
Expand Down Expand Up @@ -50,8 +51,9 @@ def test2_create_project(self):
pass

def test3_list_resources(self):
_ = self.tx.list_resources(project_slug=self.project_slug)
assert True
resources = self.tx.list_resources(project_slug=self.project_slug)
logging.info(f"Resources found: {resources}")
assert resources

def test4_create_resource(self):
self.tx.create_resource(
Expand All @@ -77,14 +79,15 @@ def test7_list_languages(self):
languages = self.tx.list_languages(
project_slug=self.project_slug, resource_slug=self.resource_slug
)
assert languages is not None
logging.info(f"Language founds: {languages}")
assert languages

def test8_get_translation(self):
path_to_output_file = self.tx.get_translation(
project_slug=self.project_slug,
resource_slug=self.resource_slug,
language_code="fr_CH",
path_to_output_dir=str(self.output_dir),
path_to_output_file=str(self.output_file),
)
assert Path.exists(Path(path_to_output_file))

Expand Down

0 comments on commit 9e142bf

Please sign in to comment.