Completed
Push — master ( 3c108b...266306 )
by Oleksandr
03:35
created

CompileCommandExecutor._validate_locales()   A

Complexity

Conditions 5

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 19
rs 9.2333
c 0
b 0
f 0
cc 5
nop 2
1
import argparse
0 ignored issues
show
introduced by
Missing module docstring
Loading history...
2
3
from pathlib import Path
4
5
from typing import Callable
0 ignored issues
show
Unused Code introduced by
Unused Callable imported from typing
Loading history...
6
from typing import List
7
from typing import Optional
8
from typing import Text
9
from typing import Type
0 ignored issues
show
Unused Code introduced by
Unused Type imported from typing
Loading history...
10
11
from .command_base import BaseCommand
12
from .command_base import BaseCommandExecutor
13
14
from .encoding import has_bom
15
16
from .gettext_tools import compile_translations
17
from .gettext_tools import validate_gettext_tools_exist
18
19
from .paths import get_names_of_immediate_subdirectories
20
from .paths import make_messages_dir_path
21
from .paths import make_mo_file_path
22
23
from .text import flatten_comma_separated_values
24
from .text import stringify_path
25
26
from .utils import halt
27
from .utils import print_err
28
from .utils import print_out
29
from .utils import show_usage_error_and_halt
30
31
from . import defaults
32
33
34
class CompileCommandExecutor(BaseCommandExecutor):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
35
36
  def __init__(self, args=argparse.Namespace) -> None:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
Bug introduced by
The __init__ method of the super-class BaseCommandExecutor is not called.

It is generally advisable to initialize the super-class by calling its __init__ method:

class SomeParent:
    def __init__(self):
        self.x = 1

class SomeChild(SomeParent):
    def __init__(self):
        # Initialize the super class
        SomeParent.__init__(self)
Loading history...
37
    self._locales_dir_path = self._handle_locales_dir_path(args.locales_dir)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
38
    self._validate_locales_dir_path(self._locales_dir_path)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
39
40
    self._locales = self._handle_locales(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
41
      locales=args.locale,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
42
      locales_dir_path=self._locales_dir_path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
43
    )
44
    self._validate_locales(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
45
      locales=self._locales,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
46
      locales_dir_path=self._locales_dir_path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
47
    )
48
49
    self._exclude = set(flatten_comma_separated_values(args.exclude))
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
50
51
    self._fuzzy = args.fuzzy
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
52
53
    self._msgfmt_extra_args = flatten_comma_separated_values(args.msgfmt_extra_args)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
54
    self._verbose = args.verbose
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
55
56
  @staticmethod
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
57
  def _handle_locales_dir_path(path: Text) -> Path:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
58
    return Path(path).absolute()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
59
60
  @staticmethod
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
61
  def _validate_locales_dir_path(path: Path) -> None:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
62
    if not path.exists():
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
63
      print_err(f"locales dir does not exist (path={stringify_path(path)}")
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
64
      show_usage_error_and_halt()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
65
66
    if not path.is_dir():
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
67
      print_err(f"locales dir is not a directory (path={stringify_path(path)})")
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
68
      show_usage_error_and_halt()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
69
70
  @staticmethod
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
71
  def _handle_locales(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
72
    locales: Optional[List[Text]],
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block.
Loading history...
73
    locales_dir_path: Path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block.
Loading history...
74
  ) -> List[Text]:
75
76
    if locales:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
77
      return flatten_comma_separated_values(locales)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
78
79
    return get_names_of_immediate_subdirectories(locales_dir_path)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
80
81
  @staticmethod
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
82
  def _validate_locales(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
83
    locales: List[Text],
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block.
Loading history...
84
    locales_dir_path: Path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation before block.
Loading history...
85
  ) -> None:
86
    if not locales:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
87
      print_err(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
88
        "specify at least 1 locale or specify processing of all existing locales"
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
89
      )
90
      show_usage_error_and_halt()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
91
92
    for locale in locales:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
93
      path = make_messages_dir_path(locales_dir_path, locale)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
94
      if not (path.exists() and path.is_dir()):
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
95
        print_err(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 16 spaces were expected, but 8 were found.
Loading history...
96
          f"invalid locale '{locale}': "
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
97
          f"directory '{stringify_path(path)}' expected to exist"
98
        )
99
        show_usage_error_and_halt()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 16 spaces were expected, but 8 were found.
Loading history...
100
101
  def __call__(self) -> None:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
102
    validate_gettext_tools_exist()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
103
104
    if self._verbose:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
105
      self._print_input_args(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
106
        locales_dir_path=stringify_path(self._locales_dir_path),
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
107
        locales=self._locales,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
108
        fuzzy=self._fuzzy,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
109
        msgfmt_extra_args=self._msgfmt_extra_args,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
110
        verbose=self._verbose,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
111
      )
112
113
    final_locales = sorted(set(self._locales) - self._exclude)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
114
115
    for locale in final_locales:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
116
      self._process_locale(locale=locale)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
117
118
  def _process_locale(self, locale: Text) -> None:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
119
    if self._verbose:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
120
      print_out(f"processing locale '{locale}'")
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
121
122
    messages_dir_path = make_messages_dir_path(self._locales_dir_path, locale)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
123
124
    for path in messages_dir_path.iterdir():
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
125
      if path.is_file() and path.suffix == ".po":
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
126
        self._process_translations_file(file_path=path)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 16 spaces were expected, but 8 were found.
Loading history...
127
128
  def _process_translations_file(self, file_path: Path) -> None:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
129
    if self._verbose:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
130
      print_out(f"processing file '{stringify_path(file_path)}'")
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
131
132
    if has_bom(file_path):
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
133
      print_err(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
134
        f"the file '{stringify_path(file_path)}' file has a BOM (Byte Order Mark). "
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
135
        f"Verboselib supports only '.po' files encoded in UTF-8 and without any BOM."
136
      )
137
      halt()
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 12 spaces were expected, but 6 were found.
Loading history...
138
139
    mo_file_path = make_mo_file_path(file_path)
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
140
141
    compile_translations(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
142
      mo_file_path=mo_file_path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
143
      po_file_path=file_path,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
144
      fuzzy=self._fuzzy,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
145
      msgfmt_extra_args=self._msgfmt_extra_args,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
146
    )
147
148
149
class CompileCommand(BaseCommand):
0 ignored issues
show
introduced by
Missing class docstring
Loading history...
150
  name = "compile"
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
151
  aliases = ["c", ]
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
152
  executor_class = CompileCommandExecutor
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
153
154
  @classmethod
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
155
  def make_parser(cls, factory=argparse.ArgumentParser) -> argparse.ArgumentParser:
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 4 spaces were expected, but 2 were found.
Loading history...
156
    description = "compile '.po' text files into '.mo' binaries"
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
157
    parser = factory(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
158
      prog=cls.name,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
159
      description=description,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
160
      add_help=True,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
161
      help=description,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
162
      formatter_class=argparse.ArgumentDefaultsHelpFormatter,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
163
    )
164
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
165
      "-d", "--locale-dir",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
166
      dest="locales_dir",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
167
      default=defaults.DEFAULT_LOCALE_DIR_NAME,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
168
      help="path to the directory where locales are stored",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
169
    )
170
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
171
      "-l", "--locale",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
172
      dest="locale",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
173
      action="append",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
174
      help=(
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
175
        "locale(s) to process, ex: 'en_US'; "
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
176
        "can be specified multiple times; "
177
        "all locales are processed if not specified"
178
      ),
179
    )
180
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
181
      "-e", "--exclude",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
182
      dest="exclude",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
183
      action="append",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
184
      help="locale(s) to exclude, ex: 'en_US'; can be specified multiple times",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
185
    )
186
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
187
      "-f", "--use-fuzzy",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
188
      action="store_true",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
189
      dest="fuzzy",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
190
      default=False,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
191
      help="use fuzzy translations",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
192
    )
193
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
194
      "--msgfmt-extra-args",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
195
      action="append",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
196
      dest="msgfmt_extra_args",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
197
      help=(
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
198
        "extra arguments for 'msgfmt' utility; "
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
199
        "can be comma-separated or specified multiple times"
200
      ),
201
    )
202
    parser.add_argument(
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
203
      "-v", "--verbose",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
204
      action="store_true",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
205
      dest="verbose",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
206
      default=False,
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
207
      help="use verbose output",
0 ignored issues
show
Coding Style introduced by
Wrong hanging indentation (add 2 spaces).
Loading history...
208
    )
209
    return parser
0 ignored issues
show
Coding Style introduced by
The indentation here looks off. 8 spaces were expected, but 4 were found.
Loading history...
210