This class contains serval methods to create an INI file.
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 | class Ini:
"Contains all that is needed to write an INI file"
def __init__(self):
"You can add here the begin of your INI file"
self.file = ""
def addsection(self, section):
"Add a section delimiter to the INI file"
self.file += "\n\n[" + section + "]"
def addkey(self, key, value):
"Writes a string to the INI file"
self.file += "\n" + key + "=" + value
#print key+"="+value ,"[OK - Ajouté]"
def comment(self, comm):
"Writes a comment to the INI file"
self.file += "\n ;" + comment
def verb(self, str):
"Write all that is given as argument to INI file"
self.file += str
def show(self, action="return"):
"""Prints or returns the contents of the INI file
Usage: action='return' returns the contents
action='print' prints the contents"""
if action == "return":
return self.file
elif action == "print"
print self.file
|
This can be used for a program that needs to write some configuration into an INI file. In fact, this generates an INI file in the file variable. The code is very simple, but very useful because the syntax of the file is made automatically. There's actually no INI parser/reader but it will come.
___Usage example___
w = Ini() w.comment("Hi Everybody") w.addsection("Adv Config Options") w.addkey("Number of rows","25") w.show(action="print")
Better and fully functional example. I wrote a fully functional example, yours does not even run :(
class Ini(object): def __init__(self): self.ini = []
if __name__ == "__main__": from ConfigParser import ConfigParser from StringIO import StringIO
rhymes@voodoo:~/downloads $ python iniforger.py ;Hi Everybody [Adv Config Options] Number of rows=25
['Adv Config Options'] ['number of rows']
Better and fully functional example. Ehm I forgot pre tags.