def test_attributes(self): attrs = { 'a': l20n.Attribute(l20n.Identifier('a'), l20n.String('foo')), 'b': l20n.Attribute(l20n.Identifier('b'), l20n.String('foo2')) } entity = l20n.Entity(id=l20n.Identifier('id'), value=l20n.String('foo'), attrs=attrs) lol = l20n.LOL((entity, )) prog = compile(lol) self.assertEqual(len(prog.body), 2) exp = prog.body[0].expression self.assertEqual(exp.operator.token, "=") self.assertTrue(isinstance(exp.left.obj, js.ThisExpression)) self.assertEqual(exp.left.prop.name, "id") self.assertEqual(exp.left.computed, False) self.assertTrue(isinstance(exp.right, js.NewExpression)) self.assertEqual(exp.right.callee.name, "String") self.assertEqual(exp.right.arguments[0].value, "foo") exp = prog.body[1].expression self.assertEqual(exp.operator.token, '=') self.assertTrue(isinstance(exp.left.obj, js.MemberExpression)) self.assertEqual(exp.left.obj.prop.name, 'id') self.assertEqual(exp.left.prop.name, "_attrs") self.assertEqual(exp.left.computed, False)
def test_computed_property_expression(self): string = l20n.ComplexString([ l20n.String("word1 "), l20n.PropertyExpression(l20n.Identifier('foo'), l20n.Identifier('a'), True), l20n.String(" word2") ]) entity = l20n.Entity(id=l20n.Identifier('id'), value=string) lol = l20n.LOL((entity, )) prog = compile(lol) self.assertEqual(len(prog.body), 1) entity = prog.body[0].expression value = entity.right val_body = value.body val_return = val_body.body[0] complex_string = val_return.argument first_part = complex_string.left getent_mexp = complex_string.right.left third_part = complex_string.right.right getent_id = getent_mexp.obj self.assertEqual(getent_id.name, "sys") getent_call = getent_mexp.prop getent_arguments = getent_call.arguments self.assertEqual(getent_call.callee.name, "getent") self.assertEqual(getent_arguments[0].name, "env") self.assertEqual(getent_arguments[2].value, "foo") self.assertEqual(len(getent_arguments[3].elements), 1) subget = getent_arguments[3].elements[0] self.assertEqual(subget.prop.callee.name, 'getent') self.assertEqual(subget.prop.arguments[0].name, 'env') self.assertEqual(subget.prop.arguments[2].value, 'a')
def extract(node): id = ast.Identifier(node['data-l10n-id']) attrs2 = filter(isloc, node.attrs) attrs = {} for i in attrs2: attrs[i[0]] = ast.Attribute(ast.Identifier(i[0]), ast.String(i[1])) for child in node.findAll(): child.attrs = filter(isloc, child.attrs) value = ast.String(node.renderContents()) return id, value, attrs
def test_array_value(self): lol = l20n.LOL((l20n.Entity(id=l20n.Identifier('id'), value=l20n.Array(( l20n.String('foo'), l20n.String('foo2'), ))), )) prog = compile(lol) self.assertEqual(len(prog.body), 1) exp = prog.body[0].expression self.assertEqual(exp.operator.token, "=") self.assertTrue(isinstance(exp.left.obj, js.ThisExpression)) self.assertEqual(exp.left.prop.name, "id") self.assertEqual(exp.left.computed, False) self.assertEqual(len(exp.right.elements), 2) self.assertEqual(exp.right.elements[0].value, 'foo') self.assertEqual(exp.right.elements[1].value, 'foo2')
def get_complex_string(self, quote): str_end = quote[0] literal = re.compile('^([^\\\{%s]+)' % str_end) obj = [] buffer = '' self.content = self.content[len(quote):] while not self.content[:len(quote)] == quote: if self.content[0] == str_end: buffer += self.content[0] self.content = self.content[1:] if self.content[0] == '\\': jump = 3 if self.content[1:3] == '{{' else 2 buffer += self.content[1:jump] self.content = self.content[jump:] if self.content[:2] == '{{': self.content = self.content[2:] if buffer: string = ast.String(buffer) string._template = "%(content)s" obj.append(string) buffer = '' ws_pre_exp = self.get_ws() expr = self.get_expression() expr._template = '{{%s%s%s}}' % (ws_pre_exp, expr._template, self.get_ws()) obj.append(expr) if self.content[:2] != '}}': raise ParserError() self.content = self.content[2:] m = literal.match(self.content) if m: buffer += m.group(1) self.content = self.content[m.end(0):] if buffer: string = ast.String(buffer) string._template = '%(content)s' obj.append(string) self.content = self.content[len(quote):] if len(obj) == 1 and isinstance(obj[0], ast.String): obj[0]._template = '%s%%(content)s%s' % (quote, quote) return obj[0] cs = ast.ComplexString(obj) cs._template = '%s%%(content)s%s' % (quote, quote) cs._template_content = [''] return cs
def get_entity(self, line): m = self.patterns['entity'].match(line) if m: id = m.group(1).replace('-', '_') val = m.group(2) #print("entity %s = %s" % (id, val)) id = ast.Identifier(id) entity = ast.Entity(id) entity.value = ast.String(val) self.lols[self._current_locale].body.append(entity)
def test_hash_value(self): entity = l20n.Entity(id=l20n.Identifier('id'), value=l20n.Hash( (l20n.HashItem(l20n.Identifier('foo'), l20n.String('foo value')), l20n.HashItem(l20n.Identifier('foo2'), l20n.String('foo2 value'))))) lol = l20n.LOL((entity, )) prog = compile(lol) s = serializer.Serializer() self.assertEqual(len(prog.body), 1) exp = prog.body[0].expression self.assertEqual(exp.operator.token, "=") self.assertTrue(isinstance(exp.left.obj, js.ThisExpression)) self.assertEqual(exp.left.prop.name, "id") self.assertEqual(exp.left.computed, False) self.assertEqual(len(exp.right.properties), 2) self.assertEqual(exp.right.properties[0].value.value, 'foo value') self.assertEqual(exp.right.properties[1].value.value, 'foo2 value')
def get_string(self): match = self.patterns['value'].match(self.content) if not match: raise ParserError() self.content = self.content[match.end(0):] return ast.String(match.group(2))