Get config information example

First, the experimental environment

1.Windows10x64

2.anaconda4.6.9 + python3.7.1 (anaconda integrated, without having to install separate)

Second, the sample code

# - * - Coding: UTF-. 8 - * - 
Import OS 
from ConfigParser Import the ConfigParser, NoSectionError 

Debug = False 


DEF debug_print (STR): 
    IF Debug: 
        Print (STR) 


class GetConfig (the ConfigParser): 
    DEF the __init __ (Self, Defaults = None ): 
        "" " 
        initialize function 
        : param Defaults: 
        " "" 
        ConfigParser .__ the init __ (Self, Defaults = None) 

    DEF optionxform (Self, optionstr): 
        "" " 
        solve ConfigParser the key to forcibly converted to lowercase problem 
        : param optionstr: 
        : return: 
        "" " 
        return optionstr 

    DEF OPEN_FILE (Self, file_name):
        """
        打开config文件夹
        :param file_name: config文件路径
        :return:
        """
        if os.path.exists(file_name):
            debug_print('%s exists' % (file_name))
            self.read(file_name)
            return True
        else:
            debug_print('%s do not exists' % (file_name))
            return False

    def get_sections(self):
        """
        获取config中所有section
        :return: section列表
        """
        section = self.sections()
        debug_print(section)
        return section

    def check_section(self, *args, **kwargs):
        def check_section_type(*args, **kwargs):
            if isinstance(args[0], str):
                print('The first parameter (section) must be a string')
                return
        return check_section_type

    @check_section
    def get_options(self, section):
        """
        返回section中所有option
        :param section: 指定section
        :return: option列表
        """
        try:
            options_list = self.options(section)
            return options_list
        except NoSectionError as e:
            debug_print(e)
            return

    @check_section
    get_dicts DEF (Self, section): 
        . "" "
        "" " 
        Returns to the key-in section 
        : param section: Specify section 
        : return: the key of 
        " "" 
        the try: 
            list_str = self.items (section) 
            dict_str = dict (list_str) 
            return dict_str 
        the except NoSectionError AS E: 
            debug_print (E ) 
            return 

    @check_section 
    DEF get_options_match_str (Self, section, match_str): 
        "" " 
        use the specified string matching option list 
        : param section: section specifies the 
        string to be matched:: param match_str 
        : return: a list of matches 
        options_list = self.get_options(section)
        match_list = []
        for each in options_list:
            if match_str in each:
                match_list.append(each)
        return match_list

    @check_section
    def get_para(self, section, key):
        """
        使用section和key索引value
        :param section: 指定section
        :param key: 指定key
        :return: 索引值
        """
        dicts = self.get_dicts(section)
        if dicts:
            result = dicts.get(key, None)
            return result
        else:
            return


if __name__ == '__main__':
    debug = True
    config = GetConfig()
    config.open_file(r'g:\gejunping\extract_bin\Tool\config_block.cfg')
    sections = config.get_sections()
    ret = config.get_para('AM620', 'AM620_128G')
    print(ret)
    options = config.get_options('AM620')
    print(options)
    match_list = config.get_options_match_str('AM620', 'PREFIX')
    print(match_list)
    for each_match in match_list:
        print(config.get_para('AM620', each_match))
    dicts = config.get_dicts('AM620')
    print(dicts)

  

Guess you like

Origin www.cnblogs.com/hester/p/12114692.html