-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathresolver.py
More file actions
428 lines (319 loc) · 13.2 KB
/
Copy pathresolver.py
File metadata and controls
428 lines (319 loc) · 13.2 KB
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
from __future__ import absolute_import, unicode_literals
import contextlib
from datetime import date, datetime
from decimal import Decimal
import attr
import six
from fluent.syntax.ast import (Attribute, AttributeExpression, CallExpression, Identifier, Message, MessageReference,
NumberLiteral, Pattern, Placeable, SelectExpression, StringLiteral, Term, TermReference,
TextElement, VariableReference, VariantExpression, VariantList)
from .errors import FluentCyclicReferenceError, FluentFormatError, FluentReferenceError
from .types import FluentDateType, FluentNone, FluentNumber, fluent_date, fluent_number
from .utils import (args_match, inspect_function_args, numeric_to_native,
reference_to_id, unknown_reference_error_obj)
try:
from functools import singledispatch
except ImportError:
# Python < 3.4
from singledispatch import singledispatch
text_type = six.text_type
# Prevent expansion of too long placeables, for memory DOS protection
MAX_PART_LENGTH = 2500
# Prevent messages with too many sub parts, for CPI DOS protection
MAX_PARTS = 1000
# Unicode bidi isolation characters.
FSI = "\u2068"
PDI = "\u2069"
@attr.s
class CurrentEnvironment(object):
# The parts of ResolverEnvironment that we want to mutate (and restore)
# temporarily for some parts of a call chain.
# The values of attributes here must not be mutated, they must only be
# swapped out for different objects using `modified` (see below).
# For Messages, VariableReference nodes are interpreted as external args,
# but for Terms they are the values explicitly passed using CallExpression
# syntax. So we have to be able to change 'args' for this purpose.
args = attr.ib()
# This controls whether we need to report an error if a VariableReference
# refers to an arg that is not present in the args dict.
error_for_missing_arg = attr.ib(default=True)
@attr.s
class ResolverEnvironment(object):
context = attr.ib()
errors = attr.ib()
dirty = attr.ib(factory=set)
part_count = attr.ib(default=0)
current = attr.ib(factory=CurrentEnvironment)
@contextlib.contextmanager
def modified(self, **replacements):
"""
Context manager that modifies the 'current' attribute of the
environment, restoring the old data at the end.
"""
# CurrentEnvironment only has args that we never mutate, so the shallow
# copy returned by attr.evolve is fine (at least for now).
old_current = self.current
self.current = attr.evolve(old_current, **replacements)
yield self
self.current = old_current
def modified_for_term_reference(self, args=None):
return self.modified(args=args if args is not None else {},
error_for_missing_arg=False)
def resolve(context, message, args):
"""
Given a FluentBundle, a Message instance and some arguments,
resolve the message to a string.
This is the normal entry point for this module.
"""
errors = []
env = ResolverEnvironment(context=context,
current=CurrentEnvironment(args=args),
errors=errors)
return fully_resolve(message, env), errors
def fully_resolve(expr, env):
"""
Fully resolve an expression to a string
"""
# This differs from 'handle' in that 'handle' will often return non-string
# objects, even if a string could have been returned, to allow for further
# handling of that object e.g. attributes of messages. fully_resolve is
# only used when we must have a string.
retval = handle(expr, env)
if isinstance(retval, text_type):
return retval
return fully_resolve(retval, env)
@singledispatch
def handle(expr, env):
raise TypeError("Cannot handle object {0} of type {1}"
.format(expr, type(expr).__name__))
@handle.register(Message)
def handle_message(message, env):
return handle(message.value, env)
@handle.register(Term)
def handle_term(term, env):
return handle(term.value, env)
@handle.register(Pattern)
def handle_pattern(pattern, env):
if pattern in env.dirty:
env.errors.append(FluentCyclicReferenceError("Cyclic reference"))
return FluentNone()
env.dirty.add(pattern)
parts = []
use_isolating = env.context._use_isolating and len(pattern.elements) > 1
for element in pattern.elements:
env.part_count += 1
if env.part_count > MAX_PARTS:
if env.part_count == MAX_PARTS + 1:
# Only append an error once.
env.errors.append(ValueError("Too many parts in message (> {0}), "
"aborting.".format(MAX_PARTS)))
parts.append(fully_resolve(FluentNone(), env))
break
if isinstance(element, TextElement):
# shortcut deliberately omits the FSI/PDI chars here.
parts.append(element.value)
continue
part = fully_resolve(element, env)
if use_isolating:
parts.append(FSI)
if len(part) > MAX_PART_LENGTH:
env.errors.append(ValueError(
"Too many characters in part, "
"({0}, max allowed is {1})".format(len(part),
MAX_PART_LENGTH)))
part = part[:MAX_PART_LENGTH]
parts.append(part)
if use_isolating:
parts.append(PDI)
retval = "".join(parts)
env.dirty.remove(pattern)
return retval
@handle.register(TextElement)
def handle_text_element(text_element, env):
return text_element.value
@handle.register(Placeable)
def handle_placeable(placeable, env):
return handle(placeable.expression, env)
@handle.register(StringLiteral)
def handle_string_expression(string_expression, env):
return string_expression.value
@handle.register(NumberLiteral)
def handle_number_expression(number_expression, env):
return numeric_to_native(number_expression.value)
@handle.register(MessageReference)
def handle_message_reference(message_reference, env):
return handle(lookup_reference(message_reference, env), env)
@handle.register(TermReference)
def handle_term_reference(term_reference, env):
with env.modified_for_term_reference():
return handle(lookup_reference(term_reference, env), env)
def lookup_reference(ref, env):
"""
Given a MessageReference, TermReference or AttributeExpression, returns the
AST node, or FluentNone if not found, including fallback logic
"""
ref_id = reference_to_id(ref)
try:
return env.context._messages_and_terms[ref_id]
except LookupError:
env.errors.append(unknown_reference_error_obj(ref_id))
if isinstance(ref, AttributeExpression):
# Fallback
parent_id = reference_to_id(ref.ref)
try:
return env.context._messages_and_terms[parent_id]
except LookupError:
# Don't add error here, because we already added error for the
# actual thing we were looking for.
pass
return FluentNone(ref_id)
@handle.register(FluentNone)
def handle_fluent_none(none, env):
return none.format(env.context._babel_locale)
@handle.register(type(None))
def handle_none(none, env):
# We raise the same error type here as when a message is completely missing.
raise LookupError("Message body not defined")
@handle.register(VariableReference)
def handle_variable_reference(argument, env):
name = argument.id.name
try:
arg_val = env.current.args[name]
except LookupError:
if env.current.error_for_missing_arg:
env.errors.append(
FluentReferenceError("Unknown external: {0}".format(name)))
return FluentNone(name)
# The code below should be synced with fluent.runtime.runtime.handle_argument
if isinstance(arg_val,
(int, float, Decimal,
date, datetime,
text_type)):
return arg_val
env.errors.append(TypeError("Unsupported external type: {0}, {1}"
.format(name, type(arg_val))))
return FluentNone(name)
@handle.register(AttributeExpression)
def handle_attribute_expression(attribute_ref, env):
return handle(lookup_reference(attribute_ref, env), env)
@handle.register(Attribute)
def handle_attribute(attribute, env):
return handle(attribute.value, env)
@handle.register(VariantList)
def handle_variant_list(variant_list, env):
return select_from_variant_list(variant_list, env, None)
def select_from_variant_list(variant_list, env, key):
found = None
for variant in variant_list.variants:
if variant.default:
default = variant
if key is None:
# We only want the default
break
compare_value = handle(variant.key, env)
if match(key, compare_value, env):
found = variant
break
if found is None:
if (key is not None and not isinstance(key, FluentNone)):
env.errors.append(FluentReferenceError("Unknown variant: {0}"
.format(key)))
found = default
if found is None:
return FluentNone()
return handle(found.value, env)
@handle.register(SelectExpression)
def handle_select_expression(expression, env):
key = handle(expression.selector, env)
return select_from_select_expression(expression, env,
key=key)
def select_from_select_expression(expression, env, key):
default = None
found = None
for variant in expression.variants:
if variant.default:
default = variant
compare_value = handle(variant.key, env)
if match(key, compare_value, env):
found = variant
break
if found is None:
found = default
if found is None:
return FluentNone()
return handle(found.value, env)
def is_number(val):
return isinstance(val, (int, float))
def match(val1, val2, env):
if val1 is None or isinstance(val1, FluentNone):
return False
if val2 is None or isinstance(val2, FluentNone):
return False
if is_number(val1):
if not is_number(val2):
# Could be plural rule match
return env.context._plural_form(val1) == val2
elif is_number(val2):
return match(val2, val1, env)
return val1 == val2
@handle.register(Identifier)
def handle_indentifier(identifier, env):
return identifier.name
@handle.register(VariantExpression)
def handle_variant_expression(expression, env):
message = lookup_reference(expression.ref, env)
if isinstance(message, FluentNone):
return message
# TODO What to do if message is not a VariantList?
# Need test at least.
assert isinstance(message.value, VariantList)
variant_name = expression.key.name
return select_from_variant_list(message.value,
env,
variant_name)
@handle.register(CallExpression)
def handle_call_expression(expression, env):
args = [handle(arg, env) for arg in expression.positional]
kwargs = {kwarg.name.name: handle(kwarg.value, env) for kwarg in expression.named}
if isinstance(expression.callee, (TermReference, AttributeExpression)):
term = lookup_reference(expression.callee, env)
if args:
env.errors.append(FluentFormatError("Ignored positional arguments passed to term '{0}'"
.format(reference_to_id(expression.callee))))
with env.modified_for_term_reference(args=kwargs):
return handle(term, env)
# builtin or custom function call
function_name = expression.callee.id.name
try:
function = env.context._functions[function_name]
except LookupError:
env.errors.append(FluentReferenceError("Unknown function: {0}"
.format(function_name)))
return FluentNone(function_name + "()")
arg_spec = inspect_function_args(function, function_name, env.errors)
match, sanitized_args, sanitized_kwargs, errors = args_match(function_name, args, kwargs, arg_spec)
env.errors.extend(errors)
if match:
return function(*sanitized_args, **sanitized_kwargs)
return FluentNone(function_name + "()")
@handle.register(FluentNumber)
def handle_fluent_number(number, env):
return number.format(env.context._babel_locale)
@handle.register(int)
def handle_int(integer, env):
return fluent_number(integer).format(env.context._babel_locale)
@handle.register(float)
def handle_float(f, env):
return fluent_number(f).format(env.context._babel_locale)
@handle.register(Decimal)
def handle_decimal(d, env):
return fluent_number(d).format(env.context._babel_locale)
@handle.register(FluentDateType)
def handle_fluent_date_type(d, env):
return d.format(env.context._babel_locale)
@handle.register(date)
def handle_date(d, env):
return fluent_date(d).format(env.context._babel_locale)
@handle.register(datetime)
def handle_datetime(d, env):
return fluent_date(d).format(env.context._babel_locale)