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_macro(self): m = l20n.Identifier('foo') args = [ l20n.VariableExpression(l20n.Identifier('n')), ] exp = l20n.VariableExpression(l20n.Identifier('n')) exp = l20n.BinaryExpression( l20n.BinaryOperator('+'), l20n.VariableExpression(l20n.Identifier('n')), l20n.Identifier('n')) macro = l20n.Macro(id=m, args=args, expression=exp) lol = l20n.LOL((macro, )) prog = compile(lol) macro = prog.body[0].expression value = macro.right val_body = value.body val_return = val_body.body[0] return_exp = val_return.argument data = return_exp.left self.assertEqual(data.obj.name, 'data') self.assertEqual(data.prop.value, 0) getent = return_exp.right self.assertEqual(getent.obj.name, 'sys') self.assertEqual(getent.prop.callee.name, 'getent') self.assertEqual(getent.prop.arguments[1].name, 'sys') self.assertEqual(getent.prop.arguments[0].name, 'env') self.assertEqual(getent.prop.arguments[2].value, 'n')
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 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 test_empty_entity(self): lol = l20n.LOL((l20n.Entity(l20n.Identifier('id')), )) 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(exp.right.value, None)
def get_identifier(self): if self.content[0] == '~': self.content = self.content[1:] te = ast.ThisExpression() te._template = '~' return te match = self.patterns['id'].match(self.content) if not match: raise ParserError() self.content = self.content[match.end(0):] identifier = ast.Identifier(match.group(1)) identifier._template = '%(name)s' return identifier
def _transform_macro_idrefs(cls, expression, ids): exp = deepcopy(expression) for field in exp._fields: attr = getattr(exp, field) if isinstance(attr, l20n.VariableExpression): if attr.id.name in ids: n = l20n.PropertyExpression( l20n.Identifier('data'), l20n.Literal(ids.index(attr.id.name)), False) setattr(exp, field, n) elif isinstance(attr, l20n.Expression): setattr(exp, field, cls._transform_macro_idrefs(attr, ids)) return exp
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 add_entity(lol, k, value): id = ast.Identifier(k) entity = ast.Entity(id) entity.value = value lol.body.append(entity)