zmc
2023-08-08 e792e9a60d958b93aef96050644f369feb25d61b
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
"""
 
    webencodings.mklabels
    ~~~~~~~~~~~~~~~~~~~~~
 
    Regenarate the webencodings.labels module.
 
    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.
 
"""
 
import json
try:
    from urllib import urlopen
except ImportError:
    from urllib.request import urlopen
 
 
def assert_lower(string):
    assert string == string.lower()
    return string
 
 
def generate(url):
    parts = ['''\
"""
 
    webencodings.labels
    ~~~~~~~~~~~~~~~~~~~
 
    Map encoding labels to their name.
 
    :copyright: Copyright 2012 by Simon Sapin
    :license: BSD, see LICENSE for details.
 
"""
 
# XXX Do not edit!
# This file is automatically generated by mklabels.py
 
LABELS = {
''']
    labels = [
        (repr(assert_lower(label)).lstrip('u'),
         repr(encoding['name']).lstrip('u'))
        for category in json.loads(urlopen(url).read().decode('ascii'))
        for encoding in category['encodings']
        for label in encoding['labels']]
    max_len = max(len(label) for label, name in labels)
    parts.extend(
        '    %s:%s %s,\n' % (label, ' ' * (max_len - len(label)), name)
        for label, name in labels)
    parts.append('}')
    return ''.join(parts)
 
 
if __name__ == '__main__':
    print(generate('http://encoding.spec.whatwg.org/encodings.json'))