doc: Migrating from cnetworkmanager to nmcli
[cnetworkmanager.git] / configparser_knm.py
blob4dcd1719a7d9de4ca13f027ec383bd40eed85a98
1 import ConfigParser # knm config
2 import xml.dom.minidom
3 import os
4 import dbus # maybe reduce deps and postprocess dbus.Byte?
6 class ConfigParserKNM:
7 "Parse ~/.kde/share/config/knetworkmanagerrc"
9 def __init__(self):
10 p = ConfigParser.RawConfigParser()
11 ok = p.read(os.getenv("HOME") + "/.kde/share/config/knetworkmanagerrc")
13 self.conmaps_d = {}
14 for s in p.sections():
15 path = s.split("_")
16 #print "##", path
17 if path[0] in ["ConnectionSetting", "ConnectionSecrets"]:
18 cid = path[1]
19 self.conmaps_d.setdefault(cid, {})
20 part = path[2]
22 values = {}
23 for (n, v) in p.items(s):
24 # WTF, Value_ is transformed to value_
25 if n[:6] == "value_":
26 n = n[6:]
27 v = self.ParseValue(v)
28 #print "# %s:%s" % (n, v)
29 # do not overwrite ConnectionSecrets
30 # with empty ConnectionSettings field
31 try:
32 vv = self.conmaps_d[cid][part][n]
33 except KeyError:
34 vv = ""
35 if vv == "":
36 values[n] = v
37 if len(values) != 0: # empty 802-1x confuses NM!?
38 self.conmaps_d[cid].setdefault(part, {})
39 self.conmaps_d[cid][part].update(**values)
40 #print "PARSED", cid, part, values
42 def ConMaps(self):
43 return self.conmaps_d.values()
45 def ParseValue(self, v):
46 v = eval('"%s"' % v) # unescape backslashes
47 dom = xml.dom.minidom.parseString(v)
48 return self.ParseNode(dom.documentElement)
50 def ParseNode(self, n):
51 t = n.localName
52 if t != "list":
53 v = self.NodeText(n)
55 if t == "string":
56 return v
57 elif t == "byte":
58 return dbus.Byte(int(v))
59 elif t == "bool":
60 return v == "true"
61 elif t == "int32" or t == "uint32":
62 return int(v)
63 elif t == "list":
64 v = []
65 c = n.firstChild
66 while c != None:
67 if c.localName != None: # whitespace
68 v.append(self.ParseNode(c))
69 c = c.nextSibling
70 return v
72 def NodeText(self, n):
73 if n.hasChildNodes():
74 return n.firstChild.wholeText
75 else:
76 return ""