-
Notifications
You must be signed in to change notification settings - Fork 0
/
inf_parser.py
executable file
·68 lines (53 loc) · 1.96 KB
/
inf_parser.py
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2015 Richard Hughes <richard@hughsie.com>
# Licensed under the GNU General Public License Version 2
import sys
import ConfigParser
import StringIO
class InfParser(ConfigParser.RawConfigParser):
def __init__(self):
ConfigParser.RawConfigParser.__init__(self, allow_no_value=True)
def get(self, group, key):
val = ConfigParser.RawConfigParser.get(self, group, key)
# handle things in localised 'Strings'
if val.endswith('%') and val.startswith('%'):
val = ConfigParser.RawConfigParser.get(self, 'Strings', val[1:-1])
# format multiline comments
fixed = []
for ln in val.split('\n'):
# microsoftism
if ln.endswith('|'):
ln = ln[:-1].strip()
# strip double quotes
if ln.endswith('"') and ln.startswith('"'):
ln = ln[1:-1]
fixed.append(ln)
return '\n'.join(fixed)
def read(self, fn):
fd = open(fn)
self.read_data(fd.read())
def read_data(self, contents):
# fix registry keys to have a sane key=value structure
contents_new = []
for ln in contents.split('\n'):
sect = ln.split(',')
if len(sect) == 5:
ln = '%s->%s=%s' % (sect[0].strip(), sect[2].strip(), sect[4].strip())
contents_new.append(ln)
buf = StringIO.StringIO('\n'.join(contents_new))
self.readfp(buf)
def main():
cfg = InfParser()
for fn in sys.argv[1:]:
cfg.read(fn)
for section in cfg.sections():
print cfg.items(section)
print cfg.get("Version", "CatalogFile")
print cfg.get("Version", "Provider")
print cfg.get("Firmware_AddReg", "HKR->FirmwareId")
print cfg.get("Firmware_AddReg", "HKR->FirmwareVersion")
print cfg.get("Firmware_AddReg", "HKR->FirmwareFilename")
if __name__ == "__main__":
main()