aboutsummaryrefslogtreecommitdiffstats
path: root/examples/iniparser.py
diff options
context:
space:
mode:
Diffstat (limited to 'examples/iniparser.py')
-rwxr-xr-xexamples/iniparser.py44
1 files changed, 44 insertions, 0 deletions
diff --git a/examples/iniparser.py b/examples/iniparser.py
new file mode 100755
index 00000000000..9b0cf2360d4
--- /dev/null
+++ b/examples/iniparser.py
@@ -0,0 +1,44 @@
+#!/usr/bin/python
+import os
+import sys
+import time
+
+class IniParser():
+
+ def getValue(self, inifile, section, var):
+ if os.path.isfile(inifile):
+ with open(inifile, "r") as cfgfile:
+ content = cfgfile.read()
+ content = content.split("\n")
+ for i in xrange (len(content)-1):
+ if "["+section+"]" in content[i]:
+ j = i+1
+ while not "[" in content[j]:
+ if var == content[j].split("=")[0]:
+ return content[j].split("=")[1].replace('"','').strip()
+ else:
+ j += 1
+ cfgfile.close()
+
+ def setValue(self, inifile, section, var, value):
+ if os.path.isfile(inifile):
+ with open(inifile, "r") as cfgfile:
+ content = cfgfile.read()
+ content = content.split("\n")
+ for i in xrange (len(content)-1):
+ if "["+section+"]" in content[i]:
+ j = i+1
+ while not "[" in content[j]:
+ if var == content[j].split("=")[0]:
+ content[j] = var + '="' + value +'"'
+ break
+ else:
+ j += 1
+ cfgfile.close()
+ with open(inifile, "w") as cfgfile:
+ cfgfile.write("\n".join(content))
+
+#inifile = IniParser()
+
+#print inifile.getValue("test.ini", "section3", "var3")
+ \ No newline at end of file