File size: 2,417 Bytes
ae4627d | 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 69 70 71 72 73 74 75 76 | # -*- coding:utf-8 -*_
import ConfigParser, sys
class INIFILE:
"""
a class which can process *.ini file
with read and write function.
"""
def __init__(self, fileName):
self.fileName = fileName
# print self.fileName
self.initflag = False
self.cfg = None
self.readhandle = None
self.writehandle = None
self.is_read = False
self.is_write = False
#is_read :is read enable? is_write: is write enable?
#must write something if you set is_write to true. otherwise your file become empty.
def Init(self, is_read, is_write):
self.cfg = ConfigParser.ConfigParser()
try:
if is_read:
self.is_read = True
self.readhandle = open(self.fileName, 'r')
self.cfg.readfp(self.readhandle)
if is_write:
self.is_write = True
self.writehandle = open(self.fileName, 'w')
self.initflag = True
except Exception, e:
self.initflag = False
print e
return self.initflag
#close the file
def UnInit(self):
if self.initflag:
if self.is_read:
self.readhandle.close()
#print '================close the read handle================'
if self.is_write:
self.writehandle.close()
#print '================close the write handle================'
def GetValue(self, Section, Key, Default = '0'):
try:
value = self.cfg.get(Section, Key)
except Exception, e:
#print e
value = Default
return value
def SetValue(self, Section, Key, Value):
if not self.is_write:
print 'can not SetValue, switch the write function to true first.'
return
if not self.cfg.has_section(Section):
print 'Section [%s] does not exists. Going to add it first' %(Section)
self.cfg.add_section(Section)
self.cfg.set(Section, Key, Value)
self.cfg.write(self.writehandle)
# file = INIFILE('totalpage.ini')
# file.Init()
# num = file.GetValue('Main', 'totalpage', '')
# print num
#
# file.SetValue('Main', 'totalpage', '805')
# print file.GetValue('Main', 'TotalPage', '0')
#
# file.SetValue('DEMO', 'whoami', 'Admin')
# print file.GetValue('DEMO', 'whoami', 'Admin') |