summaryrefslogtreecommitdiffstats
path: root/third_party/python/compare_locales/compare_locales/paths/project.py
blob: 1f18a9d2d582c3c10a16ab4e4f607de4d58199ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import re
from compare_locales import mozpath
from .matcher import Matcher


class ExcludeError(ValueError):
    pass


class ProjectConfig:
    '''Abstraction of l10n project configuration data.
    '''

    def __init__(self, path):
        self.filter_py = None  # legacy filter code
        # {
        #  'l10n': pattern,
        #  'reference': pattern,  # optional
        #  'locales': [],  # optional
        #  'test': [],  # optional
        # }
        self.path = path
        self.root = None
        self.paths = []
        self.rules = []
        self.locales = None
        # cache for all_locales, as that's not in `filter`
        self._all_locales = None
        self.environ = {}
        self.children = []
        self.excludes = []
        self._cache = None

    def same(self, other):
        '''Equality test, ignoring locales.
        '''
        if other.__class__ is not self.__class__:
            return False
        if len(self.children) != len(other.children):
            return False
        for prop in ('path', 'root', 'paths', 'rules', 'environ'):
            if getattr(self, prop) != getattr(other, prop):
                return False
        for this_child, other_child in zip(self.children, other.children):
            if not this_child.same(other_child):
                return False
        return True

    def set_root(self, basepath):
        if self.path is None:
            self.root = None
            return
        self.root = mozpath.abspath(
            mozpath.join(mozpath.dirname(self.path), basepath)
        )

    def add_environment(self, **kwargs):
        self.environ.update(kwargs)

    def add_paths(self, *paths):
        '''Add path dictionaries to this config.
        The dictionaries must have a `l10n` key. For monolingual files,
        `reference` is also required.
        An optional key `test` is allowed to enable additional tests for this
        path pattern.
        '''
        self._all_locales = None  # clear cache
        for d in paths:
            rv = {
                'l10n': Matcher(d['l10n'], env=self.environ, root=self.root),
                'module': d.get('module')
            }
            if 'reference' in d:
                rv['reference'] = Matcher(
                    d['reference'], env=self.environ, root=self.root
                )
            if 'test' in d:
                rv['test'] = d['test']
            if 'locales' in d:
                rv['locales'] = d['locales'][:]
            self.paths.append(rv)

    def set_filter_py(self, filter_function):
        '''Set legacy filter.py code.
        Assert that no rules are set.
        Also, normalize output already here.
        '''
        assert not self.rules

        def filter_(module, path, entity=None):
            try:
                rv = filter_function(module, path, entity=entity)
            except BaseException:  # we really want to handle EVERYTHING here
                return 'error'
            rv = {
                True: 'error',
                False: 'ignore',
                'report': 'warning'
            }.get(rv, rv)
            assert rv in ('error', 'ignore', 'warning', None)
            return rv
        self.filter_py = filter_

    def add_rules(self, *rules):
        '''Add rules to filter on.
        Assert that there's no legacy filter.py code hooked up.
        '''
        assert self.filter_py is None
        for rule in rules:
            self.rules.extend(self._compile_rule(rule))

    def add_child(self, child):
        self._all_locales = None  # clear cache
        if child.excludes:
            raise ExcludeError(
                'Included configs cannot declare their own excludes.'
            )
        self.children.append(child)

    def exclude(self, child):
        for config in child.configs:
            if config.excludes:
                raise ExcludeError(
                    'Excluded configs cannot declare their own excludes.'
                )
        self.excludes.append(child)

    def set_locales(self, locales, deep=False):
        self._all_locales = None  # clear cache
        self.locales = locales
        if not deep:
            return
        for child in self.children:
            child.set_locales(locales, deep=deep)

    @property
    def configs(self):
        'Recursively get all configs in this project and its children'
        yield self
        for child in self.children:
            yield from child.configs

    @property
    def all_locales(self):
        'Recursively get all locales in this project and its paths'
        if self._all_locales is None:
            all_locales = set()
            for config in self.configs:
                if config.locales is not None:
                    all_locales.update(config.locales)
                for paths in config.paths:
                    if 'locales' in paths:
                        all_locales.update(paths['locales'])
            self._all_locales = sorted(all_locales)
        return self._all_locales

    def filter(self, l10n_file, entity=None):
        '''Filter a localization file or entities within, according to
        this configuration file.'''
        if l10n_file.locale not in self.all_locales:
            return 'ignore'
        if self.filter_py is not None:
            return self.filter_py(l10n_file.module, l10n_file.file,
                                  entity=entity)
        rv = self._filter(l10n_file, entity=entity)
        if rv is None:
            return 'ignore'
        return rv

    class FilterCache:
        def __init__(self, locale):
            self.locale = locale
            self.rules = []
            self.l10n_paths = []

    def cache(self, locale):
        if self._cache and self._cache.locale == locale:
            return self._cache
        self._cache = self.FilterCache(locale)
        for paths in self.paths:
            if 'locales' in paths and locale not in paths['locales']:
                continue
            self._cache.l10n_paths.append(paths['l10n'].with_env({
                "locale": locale
            }))
        for rule in self.rules:
            cached_rule = rule.copy()
            cached_rule['path'] = rule['path'].with_env({
                "locale": locale
            })
            self._cache.rules.append(cached_rule)
        return self._cache

    def _filter(self, l10n_file, entity=None):
        if any(
            exclude.filter(l10n_file) == 'error'
            for exclude in self.excludes
        ):
            return
        actions = {
            child._filter(l10n_file, entity=entity)
            for child in self.children}
        if 'error' in actions:
            # return early if we know we'll error
            return 'error'

        cached = self.cache(l10n_file.locale)
        if any(p.match(l10n_file.fullpath) for p in cached.l10n_paths):
            action = 'error'
            for rule in reversed(cached.rules):
                if not rule['path'].match(l10n_file.fullpath):
                    continue
                if ('key' in rule) ^ (entity is not None):
                    # key/file mismatch, not a matching rule
                    continue
                if 'key' in rule and not rule['key'].match(entity):
                    continue
                action = rule['action']
                break
            actions.add(action)
        if 'error' in actions:
            return 'error'
        if 'warning' in actions:
            return 'warning'
        if 'ignore' in actions:
            return 'ignore'

    def _compile_rule(self, rule):
        assert 'path' in rule
        if isinstance(rule['path'], list):
            for path in rule['path']:
                _rule = rule.copy()
                _rule['path'] = Matcher(path, env=self.environ, root=self.root)
                yield from self._compile_rule(_rule)
            return
        if isinstance(rule['path'], str):
            rule['path'] = Matcher(
                rule['path'], env=self.environ, root=self.root
            )
        if 'key' not in rule:
            yield rule
            return
        if not isinstance(rule['key'], str):
            for key in rule['key']:
                _rule = rule.copy()
                _rule['key'] = key
                yield from self._compile_rule(_rule)
            return
        rule = rule.copy()
        key = rule['key']
        if key.startswith('re:'):
            key = key[3:]
        else:
            key = re.escape(key) + '$'
        rule['key'] = re.compile(key)
        yield rule