Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/validators.py: 72%

42 statements  

« 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 

4 

5from __future__ import annotations 

6 

7import re 

8 

9from django.core.exceptions import ValidationError 

10from django.utils.translation import gettext 

11from pyparsing import ParseException 

12 

13from weblate.checks.flags import FlagsValidator 

14from weblate.lang.models import Language 

15from weblate.trans.defines import LANGUAGE_CODE_LENGTH 

16 

17 

18def validate_filemask(val) -> None: 

19 """Validate that the filemask contains *.""" 

20 if "*" not in val: 

21 raise ValidationError( 

22 gettext("File mask does not contain * as a language placeholder!") 

23 ) 

24 

25 

26def validate_autoaccept(val) -> None: 

27 """Validate correct value for automatic acceptance.""" 

28 if val == 1: 

29 raise ValidationError( 

30 gettext( 

31 "A value of 1 is not allowed for automatic acceptance as " 

32 "it would permit users to vote on their own suggestions." 

33 ) 

34 ) 

35 

36 

37def validate_check_flags(val) -> None: 

38 """Validate check-influencing flags.""" 

39 try: 

40 flags = FlagsValidator(val) 

41 except (ParseException, re.error) as error: 

42 raise ValidationError(gettext("Could not parse flags: %s") % error) from error 

43 flags.validate() 

44 

45 

46def validate_language_code(code: str | None, filename: str, required: bool = False): 

47 if not code: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true

48 if not required: 

49 return None 

50 message = gettext( 

51 'The language code for "%(filename)s" is empty, please check the file mask.' 

52 ) % {"filename": filename} 

53 raise ValidationError({"filemask": message}) 

54 

55 if len(code) > LANGUAGE_CODE_LENGTH: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true

56 message = gettext( 

57 'The language code "%(code)s" for "%(filename)s" is too long,' 

58 " please check the file mask." 

59 ) % {"code": code, "filename": filename} 

60 raise ValidationError({"filemask": message}) 

61 

62 return Language.objects.auto_get_or_create(code=code, create=False) 

63 

64 

65def validate_file_format_parameters(value: dict | None) -> None: 

66 from weblate.trans.file_format_params import FILE_FORMATS_PARAMS 

67 

68 name_to_file_format_params = {param.name: param for param in FILE_FORMATS_PARAMS} 

69 

70 if value is None: 

71 return 

72 

73 if not isinstance(value, dict): 

74 raise ValidationError( 

75 gettext("File format parameters must be a dictionary of key-value pairs.") 

76 ) 

77 

78 for param_name, param_value in value.items(): 78 ↛ 79line 78 didn't jump to line 79 because the loop on line 78 never started

79 if param_name in name_to_file_format_params: 

80 param = name_to_file_format_params[param_name] 

81 param().get_field().clean(param_value) 

82 else: 

83 raise ValidationError( 

84 gettext('Unknown file format parameter: "%(param_name)s".') 

85 % {"param_name": param_name} 

86 )