Coverage for app/venv/lib/python3.14/site-packages/weblate/machinery/management/commands/install_machinery.py: 0%
27 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
4from __future__ import annotations
6from typing import TYPE_CHECKING
8from django.core.management.base import CommandError
10from weblate.configuration.models import Setting, SettingCategory
11from weblate.machinery.models import validate_service_configuration
12from weblate.utils.management.base import BaseCommand
14if TYPE_CHECKING:
15 from django.core.management.base import CommandParser
18class Command(BaseCommand):
19 help = "installs site-wide automatic suggestion service"
21 def add_arguments(self, parser: CommandParser) -> None:
22 super().add_arguments(parser)
23 parser.add_argument("--service", required=True, help="Service name")
24 parser.add_argument(
25 "--configuration", default="{}", help="Service configuration in JSON"
26 )
27 parser.add_argument(
28 "--update",
29 action="store_true",
30 help="Update existing service configuration",
31 )
33 def handle(self, *args, **options) -> None:
34 service, configuration, errors = validate_service_configuration(
35 options["service"], options["configuration"]
36 )
38 if service is None or errors:
39 for error in errors:
40 self.stderr.write(error)
41 msg = "Invalid add-on configuration!"
42 raise CommandError(msg)
44 setting, created = Setting.objects.get_or_create(
45 category=SettingCategory.MT,
46 name=options["service"],
47 defaults={"value": configuration},
48 )
49 if not created and options["update"]:
50 setting.value = configuration
51 setting.save()
52 self.stdout.write(f"Service installed: {service.name}")