?? inianalyse.py
字號:
#the CIni class which used to extract the section and the key infomation
#history : created by liuzhibin at 2009/03/10
class CIni(object):
__SECTION_HEAD = r'[' #the head of the section
__SECTION_REAR = r']' #the rear of the section
__COMMENT_HEAD = r';' #the head of the comment
__KEY_EQUAL = r'=' #the equal sign which assgin the value to the key
__EMPTY_STR = -1 #the symbol which defines the return value of Emptystr
__NOT_CHAR_MATCH = -2 #the symbol which defines the return val of not match case
def __init__(self):
self._data = {} #the dictionary which contains the data of ini
# Function parse(self, filename)
# Description: open and extract the infomation of ini file
# if not success: return False
# if success: return True
# Accepted Para: the file name of ini file
def parse(self, filename):
file = open(filename,'r')
if file == None:
return False #open file error return error
#the current section which the key belonged to
_currentSection =None
for eachLine in file:
PosLineBegin =self._getPosNoneSpace(eachLine) #get the pos of the first non-space char
if (eachLine[0] ==self.__COMMENT_HEAD or PosLineBegin==len(eachLine)-1):
pass #comment and the empty line is not deal
elif eachLine[0] ==self.__SECTION_HEAD: #the section part
PosEnd= self._getCharPos(self.__SECTION_REAR,eachLine) #get the pos of the end of section rear
if PosEnd == self.__EMPTY_STR or PosEnd ==self.__NOT_CHAR_MATCH:
continue #there are error in this line skip
SectionName = eachLine[1:PosEnd]
#try to get the section keys
_currentSection = self._data.get(SectionName)
#if failed create a new one
if _currentSection == None:
_currentSection = self._data.setdefault(SectionName,{})
else: #the key part
PosSplite = self._getCharPos(self.__KEY_EQUAL,eachLine) #get the pos of symbol "=" to separate the key and the value
_currentSection.setdefault(eachLine[0:PosSplite],eachLine[PosSplite+1:len(eachLine)-1]) #set the value to the key
return True
# Function read_string(self, section, ident, default = "")
# Description: Get the specified ident of specified sction
# if not found: return default
# if found: return the_string
def read_string(self, section, ident, default = ""):
Section = self._data.get(section)
if Section == None:
return default
else:
return Section.get(ident,default)
#the private function returns the specified char of certain string
def _getCharPos(self,char,str):
if str =="":
return self.__EMPTY_STR #if the str is empty return EMPTY_LINE
else:
pos = 0
for ch in str:
if char ==ch:
return pos #match, return current pos
else:
pos+=1
return self.__NOT_CHAR_MATCH #none of the char match , return NOT_CHAR_MATCH
#get the pos of first none space
def _getPosNoneSpace(self,str):
pos = 0
for char in str:
if char!=" ":
return pos
else:
pos +=1
return -1;
#the function which display all the sections and the keys and helps to judge the result
def _display(self):
for section in self._data.keys():
print "section name = ",section
for key in self._data[section].keys():
print " ",key,"=",self._data[section][key]
def main():
print "begin"
FileName =raw_input("Please inut the ini fileName:")
print FileName
print "the Ini Filename is: ",FileName
Analyser = CIni()
if Analyser.parse(FileName) == False:
print "analyse false"
else:
print "All the sections and the keys are as belowed:"
Analyser._display()
print "done"
if __name__== "__main__":
main()
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -