Debian Codegraph @

scripts/nvchecker-ini2toml

raw symbols SCIP
1 #!/usr/bin/python3
2 # MIT licensed
3 # Copyright (c) 2020 lilydjwg <[email protected]>, et al.
4
5 import argparse
6
7 import configparser
8 import toml
9
10 _handler_precedence = (
11 'github', 'aur', 'pypi', 'archpkg', 'debianpkg', 'ubuntupkg',
12 'gems', 'pacman',
13 'cmd', 'bitbucket', 'regex', 'manual', 'vcs',
14 'cratesio', 'npm', 'hackage', 'cpan', 'gitlab', 'packagist',
15 'repology', 'anitya', 'android_sdk', 'sparkle', 'gitea'
16 )
17
18 BOOL_KEYS = [
19 'strip_release', 'use_last_modified',
20 'use_latest_release', 'use_latest_tag',
21 'use_max_release', 'use_max_tag', 'use_pre_release',
22 ]
23
24 INT_KEYS = [
25 'max_page',
26 ]
27
28 def main():
29 parser = argparse.ArgumentParser(description='convert 1.x ini file to 2.x toml file')
30 parser.add_argument('ini', type=argparse.FileType(),
31 help='the old ini file')
32 parser.add_argument('toml', type=argparse.FileType(mode='w'),
33 help='the new ini file')
34 args = parser.parse_args()
35
36 old = configparser.ConfigParser(
37 dict_type=dict, allow_no_value=True, interpolation=None,
38 )
39 old.read_file(args.ini)
40
41 if '__config__' in old:
42 c = old['__config__']
43 newconf = dict(c)
44 x = newconf.pop('max_concurrent', None)
45 if x is not None:
46 newconf['max_concurrency'] = x
47 confs = {'__config__': newconf}
48 else:
49 confs = {}
50
51 for section in old.sections():
52 if section == '__config__':
53 continue
54
55 conf = old[section]
56 newconf = {}
57
58 for key in _handler_precedence:
59 if key not in conf:
60 continue
61 newconf['source'] = key
62 if conf.get(key):
63 newconf[key] = conf.get(key)
64 break
65
66 dconf = dict(conf)
67
68 for k, v in dconf.items():
69 if '-' in k:
70 k = k.replace('-', '_')
71
72 if k in BOOL_KEYS:
73 newconf[k] = conf.getboolean(k)
74 elif k in INT_KEYS:
75 newconf[k] = conf.getint(k)
76 elif v != '':
77 newconf[k] = v
78
79 confs[section] = newconf
80
81 toml.dump(confs, args.toml)
82 args.toml.flush()
83
84 if __name__ == '__main__':
85 main()