Skip to content

Commit

Permalink
fix(download): correctly download objects specified in configuration
Browse files Browse the repository at this point in the history
- add tests for configuration based download
- reuse download_components for single component download
- fixed download of categorized components
- consistent test data for categorization of components
- make Component always have category even if it None

Fixes #653
  • Loading branch information
nijel committed Sep 18, 2024
1 parent ec0ab39 commit 62f11db
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 14 deletions.
4 changes: 3 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
1.15
1.16
----

* Not yet released.
* Fixed downloading components based on configration.
* Improved handling of categorized components.

1.15
----
Expand Down
26 changes: 25 additions & 1 deletion wlc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ class LazyObject(dict):

PARAMS: ClassVar[tuple[str, ...]] = ()
OPTIONALS: ClassVar[set[str]] = set()
NULLS: ClassVar[set[str]] = set()
MAPPINGS: ClassVar[dict[str, Any]] = {}
ID: ClassVar[str] = "url"

Expand Down Expand Up @@ -431,6 +432,8 @@ def __getattr__(self, name):
try:
return self._data[name]
except KeyError as error:
if name in self.NULLS:
return None
raise AttributeError(name) from error

def setattrvalue(self, name, value):
Expand All @@ -451,7 +454,11 @@ def keys(self):
if len(self._data) <= 1:
self.refresh()
for param in self.PARAMS:
if param not in self.OPTIONALS or param in self._data:
if (
param not in self.OPTIONALS
or param in self._data
or param in self.NULLS
):
yield param

def items(self):
Expand Down Expand Up @@ -604,13 +611,24 @@ def delete(self):
def create_component(self, **kwargs):
return self.weblate.create_component(self.slug, **kwargs)

def full_slug(self):
return self.slug


class Category(LazyObject):
"""Category object."""

PARAMS: ClassVar[tuple[str, ...]] = ("category", "name", "project", "slug", "url")
MAPPINGS: ClassVar[dict[str, Any]] = {"project": Project}

def full_slug(self):
current = self
slugs = [self.project.slug, self.slug]
while current.category:
current = current.category
slugs.insert(1, current.slug)
return "/".join(slugs)


class Component(LazyObject, RepoObjectMixin):
"""Component object."""
Expand Down Expand Up @@ -645,6 +663,7 @@ class Component(LazyObject, RepoObjectMixin):
"category",
"linked_component",
}
NULLS: ClassVar[set[str]] = {"category"}
ID: ClassVar[str] = "slug"
MAPPINGS: ClassVar[dict[str, Any]] = {
"category": Category,
Expand All @@ -653,6 +672,11 @@ class Component(LazyObject, RepoObjectMixin):
}
REPOSITORY_CLASS = Repository

def full_slug(self):
if self.category:
return f"{self.category.full_slug()}/{self.slug}"
return f"{self.project.full_slug()}/{self.slug}"

def list(self):
"""List translations in the component."""
self.ensure_loaded("translations_url")
Expand Down
23 changes: 12 additions & 11 deletions wlc/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,9 @@ def download_components(self, iterable):
if getattr(component, "is_glossary", False) and self.args.no_glossary:
continue
self.download_component(component)
item = f"{component.project.slug}/{component.slug}"
self.println(f"downloaded translations for component: {item}")
self.println(
f"downloaded translations for component: {component.full_slug()}"
)

def run(self):
"""Executor."""
Expand All @@ -687,20 +688,20 @@ def run(self):

# All translations for a component
if isinstance(obj, wlc.Component):
for component in self.wlc.list_components():
# Only download for the component we scoped
if obj.slug == component.slug:
self.download_component(component)
self.println(
f"downloaded translations for component: {self.args.object[0]}"
)

# Only download for the component we scoped
self.download_components(
[
component
for component in self.wlc.list_components()
if obj.full_slug() == component.full_slug()
]
)
return

# All translations for a project
if isinstance(obj, wlc.Project):
self.download_components(obj.list())
self.println(f"downloaded translations for project: {self.args.object[0]}")
self.println(f"downloaded translations for project: {obj.full_slug()}")
return

self.download_components(self.wlc.list_components())
Expand Down
1 change: 1 addition & 0 deletions wlc/test_data/api/components-hello-android
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"branch": "main",
"file_format": "aresource",
"filemask": "android/values-*/strings.xml",
"category": "http://127.0.0.1:8000/api/categories/1/",
"git_export": "",
"license": "",
"license_url": "",
Expand Down
1 change: 0 additions & 1 deletion wlc/test_data/api/components-hello-weblate
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"agreement": "",
"branch": "main",
"category": "http://127.0.0.1:8000/api/categories/1/",
"file_format": "po",
"filemask": "po/*.po",
"git_export": "",
Expand Down
8 changes: 8 additions & 0 deletions wlc/test_data/wlc
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,11 @@ timeout = 30
[withkey]
url = http://127.0.0.1:8000/api/
key = KEY

[withcomponent]
url = http://127.0.0.1:8000/api/
translation = hello/weblate

[withproject]
url = http://127.0.0.1:8000/api/
translation = hello
32 changes: 32 additions & 0 deletions wlc/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,38 @@ def test_download(self):
)
self.assertEqual(os.listdir(tmpdirname), ["output"])

def test_download_config(self):
with TemporaryDirectory() as tmpdirname:
execute(
[
"--config",
TEST_CONFIG,
"--config-section",
"withcomponent",
"download",
"--output",
tmpdirname,
],
settings=False,
)
self.assertEqual(os.listdir(tmpdirname), ["hello-weblate.zip"])
with TemporaryDirectory() as tmpdirname:
execute(
[
"--config",
TEST_CONFIG,
"--config-section",
"withproject",
"download",
"--output",
tmpdirname,
],
settings=False,
)
self.assertEqual(
os.listdir(tmpdirname), ["hello-weblate.zip", "hello-android.zip"]
)

def test_upload(self):
"""Translation file uploads."""
msg = "Error: Failed to upload translations!\nNot found.\n"
Expand Down
1 change: 1 addition & 0 deletions wlc/test_wlc.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ def test_keys(self):
obj.keys(),
[
"agreement",
"category",
"branch",
"file_format",
"filemask",
Expand Down

0 comments on commit 62f11db

Please sign in to comment.