File: test.py

package info (click to toggle)
python-l20n 4.0.0~a1-7
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, trixie
  • size: 444 kB
  • sloc: python: 1,618; makefile: 18; sh: 8
file content (86 lines) | stat: -rwxr-xr-x 2,573 bytes parent folder | download | duplicates (4)
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
import os
import codecs
from collections import OrderedDict

import l20n.format.lol.parser as parser
import l20n.format.lol.serializer as serializer
import l20n.format.lol.ast as ast

import l10ndiff

def read_file(path):
    with codecs.open(path, 'r', encoding='utf-8') as file:
        text = file.read()
    return text

def get_entity_dict(lol):
    res = OrderedDict()
    for entry in lol.body:
        if isinstance(entry, ast.Entity):
            res[entry.id.name] = entry
    return res

def compare_resources(*paths):
    pass

def update_locale():
    source_locale = 'en-US'
    locale = 'pl'
    module = 'homescreen'
    mpath = '/Users/zbraniecki/projects/mozilla/gaia/apps/homescreen'
    orig_file = read_file(os.path.join('data', locale, '%s.lol.orig' % module))
    trans_file = read_file(os.path.join('data', locale, '%s.lol' % module))
    source_file = read_file(os.path.join(mpath, 'locale', '%s.lol' % source_locale))

    result = {
        'nottranslated': {},
        'outdated': {},
        'obsolete': {},
        'added': {},
        'uptodate': {},
    }

    p = parser.Parser()

    orig_lol = p.parse(orig_file)
    trans_lol = p.parse(trans_file)
    source_lol = p.parse(source_file)


    orig_dict = get_entity_dict(orig_lol)
    trans_dict = get_entity_dict(trans_lol)
    source_dict = get_entity_dict(source_lol)

    ldiff = l10ndiff.lists(orig_dict, trans_dict, values=False)
    for key in ldiff:
        print('%s: %s' % (key, ldiff[key]))

def test():
    e1 = ast.Entity(id=ast.Identifier('foo'))
    e1.value = ast.Array([ast.String('c'), ast.String('b')])
    #e1.value = ast.String('faa2')
    val = ast.Array(content=[ast.String('f'), ast.String('g')])
    attr = ast.Attribute(key=ast.Identifier('l'),
                         value=val)
    e1.attrs['l'] = attr
    e2 = ast.Entity(id=ast.Identifier('foo'))
    e2.value = ast.Array([ast.String('c'), ast.String('b')])
    #e2.value = ast.String('faa')
    val = ast.Array(content=[ast.String('p'), ast.String('g')])
    attr = ast.Attribute(key=ast.Identifier('l'),
                         value=val)
    e2.attrs['l'] = attr
    e3 = ast.Entity(id=ast.Identifier('foo'))
    e3.value = ast.Array([ast.String('c'), ast.String('b')])
    #e3.value = ast.String('faa')
    val = ast.Array(content=[ast.String('f'), ast.String('g')])
    attr = ast.Attribute(key=ast.Identifier('l'),
                         value=val)
    e3.attrs['l'] = attr
    
    ediff = l10ndiff.entities(e1, e2, e3)
    print(ediff)

if __name__ == '__main__':
    #test()
    update_locale()