File: compare-prop-lol.py

package info (click to toggle)
python-l20n 4.0.0~a1-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 412 kB
  • sloc: python: 1,618; makefile: 16; sh: 16
file content (88 lines) | stat: -rw-r--r-- 2,532 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
87
88
#!/usr/bin/python 
import sys
import codecs
import mozilla.format.properties.parser
import l20n.format.lol.parser
import l20n.format.lol.ast
from collections import OrderedDict

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

def update_id(id):
    return id.replace('-','_')

def update_prop(prop):
    newprop = {}
    for i in prop.keys():
        id = update_id(i)
        val = prop[i]['value']
        if id.find('.') != -1:
            id, attr = id.split('.')
            if id not in newprop:
                newprop[id] = {'id': id, 'value': None, 'attrs': {}}
            newprop[id]['attrs'][attr] = val
        else:
            if id not in newprop:
                newprop[id] = {'id': id, 'value': None, 'attrs': {}}
            newprop[id]['value'] = val
    return newprop

def compare(path1, path2):
    diff = {
      'obsolete': [],
      'missing': [],
      'modified': []
    }
    prop_source = read_file(path1)
    lol_source = read_file(path2)

    prop_parser = mozilla.format.properties.parser.Parser()
    prop = prop_parser.parse_to_entitylist(prop_source)

    prop = update_prop(prop)

    lol_parser = l20n.format.lol.parser.Parser()
    lol = lol_parser.parse(lol_source)
    
    lol_entities = OrderedDict()
    for i in lol.body:
        if isinstance(i, l20n.format.lol.ast.Entity):
            lol_entities[i.id.name] = i

    for i in prop.keys():
        if i not in lol_entities:
            diff['missing'].append(prop[i])
        else:
            val = lol_entities[i].value
            if val is not None:
                val = str(val)
            if prop[i]['value'] != val:
                ediff = {
                    'id': i,
                    'value': [prop[i]['value'], val],
                    'attrs': OrderedDict()
                }
                diff['modified'].append(ediff)
    for i in lol_entities.keys():
        if i not in prop:
            diff['obsolete'].append(lol_entities[i])
    return diff

if __name__ == "__main__":
    diff = compare(sys.argv[1], sys.argv[2])

    if diff['missing']:
        print('missing:')
        for i in diff['missing']:
            print('  %s' % i['id'])
    if diff['obsolete']:
        print('obsolete:')
        for i in diff['obsolete']:
            print('  %s' % i.id)
    if diff['modified']:
        print('modified:')
        for i in diff['modified']:
            print('  %s - {"%s" -> "%s"}' % (i['id'], i['value'][0], i['value'][1]))