python class encapsulates read ini configuration

This is based on the package, not much consideration exception handling

 

class

# Coding: UTF. 8- 
Import ConfigParser
 Import OS 

class IniCfg ():
     DEF  the __init__ (Self): 
        self.conf = configparser.ConfigParser () 
        self.cfgpath = '' 

    DEF checkSection (Self, sectionTop):
         the try : 
            self.conf. items (section) 
        the except Exception:
             Print ( " >> no such section, please check [% S] " % section)
             return None
         return True 

    # read ini, and get all of the section name
    DEF readSectionItems (Self, cfgpath):
         IF  Not The os.path.isfile (cfgpath):
             Print ( " >> no such file, check the path [% S] " % cfgpath)
             return None 
        self.cfgpath = cfgpath 
        self.conf. read (cfgpath, encoding = " UTF-. 8 " )
         return self.conf.sections () 

    # read a section, list objects which are ancestral 
    DEF readOneSection (Self, sectionTop):
         the try : 
            Item = self.conf.items (sectionTop )
         the exceptException:
             Print ( " >> no such section, please check [% S] " % section)
             return None
         return Item 

    # read a section to the dictionary 
    DEF prettySecToDic (Self, section):
         IF  not self.checkSection (section):
             return None 
        RES = {}
         for Key, Val in self.conf.items (section): 
            RES [Key] = Val
         return RES 

    # read all the dictionary section 
    defprettySecsToDic (Self): 
        RES_1 = {} 
        Res_2 = {} 
        Sections = self.conf.sections ()
         for sec in Sections:
             for Key, Val in self.conf.items (sec): 
                Res_2 [Key] = Val 
            RES_1 [sec ] = res_2.copy () 
            res_2.clear () 
        return RES_1 

    # delete a section of one item (as to identify key KEY) 
    DEF removeItem (Self, section, Key):
         IF  not self.checkSection (section):
            return 
        self.conf.remove_option (section, Key) 

    # delete the entire section this one 
    DEF removeSection (Self, section):
         IF  not self.checkSection (section):
             return 
        self.conf.remove_section (section) 

    # add a section 
    DEF Addsection (Self, section): 
        self.conf.add_section (section) 

    # to section add key and value 
    DEF addItem (Self, section, key, value):
         IF  not self.checkSection (section):
             return 
        self.conf.set (section , Key, value) 

    #Perform write write, remove and set method does not really modify the ini file contents only when performing conf.write () method, will modify the ini file contents 
    DEF actionOperate (Self, the MODE):
         IF the MODE == ' r + ' : 
            conf.write (Open (self.cfgpath, " R & lt + " , encoding = " UTF-. 8 " ))    # editing mode 
        elif mODE == ' W ' : 
            conf.write (Open (self.cfgpath, " W " ) )                       # delete the original file to re-write 
        elif the MODE == ' A ' :
            conf.write(open(self.cfgpath, "a"))                      # 追加模式写入




cfgpath = r'C:\Users\SXF\Desktop\config.ini'

inicfg = IniCfg()
sections = inicfg.readSectionItems(cfgpath)
print(sections)
content = inicfg.readOneSection('chaoji')
print(content)
dic = inicfg.prettySecToDic('chaoji')
print(dic)
dic = inicfg.prettySecsToDic()
print(dic)
inicfg.addSection('chaoji22')

content = inicfg.readOneSection('chaoji')
print(content)

 

 

Test ini

[chaoji]

chaoji_username = 123

chaoji_password = 456


[my]

soft_id         = 789

sleeptime         = asd

cnt_count         = zxc

 

Guess you like

Origin www.cnblogs.com/sxf1061700625/p/12154494.html