ConfigParser
ConfigParser is a class used to read configuration files
First of all, we should understand two concepts:
- section: as shown in the figure below, [] brackets contain nodes (non repeatable)
- option: as shown in the figure below, the form of key = value pairs is options (can also be separated by:)
- Understanding: imagine the entire configuration file as a residential community. The node is the number of buildings in the community. The number of each building must be different. The key in the option is the house number and the value is the person living in it.
Configuration file example:
[ENV] environment = test [REPORT] title = Interface automation Report [DB] host = 127.0.01 user = test password = 123456 port = 3306 [EMAIL] username = louie password = 123456 # Third party client password
Example of reading configuration class:
# 1. Import read configuration class # 2. Construct configuration file path # 3. Instantiate the configuration file class object # 4. Read configuration file data from object # 5. Use the object method to obtain configuration information or write configuration data from configparser import ConfigParser import os class ReadConifg: # Project foundation path BASE_DIR = os.path.dirname(os.path.dirname( os.path.abspath(__file__))) # Profile path CONFIG_PATH = os.path.join(BASE_DIR, "config", "config.ini") def __init__(self): # Instantiate ConfigParser class object self.cf = ConfigParser() # Use the instance object to read conifg For the data in INI file, pay attention to the coding utf-8, otherwise it will be garbled in Chinese. self.cf.read(self.CONFIG_PATH, encoding="utf-8") def get_email(self, option): value = self.cf.get("EMAIL", option) return value def get_db(self, option): value = self.cf.get("DB", option) return value def get_env(self): value = self.cf.get("ENV", "environment") return str(value).lower() def get_report(self, option): value = self.cf.get("REPORT", option) return value def set_token(self, option, value): self.cf.set(section='TOKEN', option=option, value=value) with open(self.CONFIG_DIR, 'w+', encoding='utf-8') as f: self.cf.write(f) rc = ReadConifg()
- The core of the configuration file is to read the configuration information. Generally, the most commonly used function is get() function to obtain the configuration information.
Common functions
function | describe |
---|---|
cf.read(filenames, encoding=None) | When reading configuration file information, encoding is to set the encoding format, generally using utf-8. |
cf.sections() | Returns a list of nodes, excluding [DEFAULT] |
cf.add_section(section) | When adding a new node, if it already exists, it will report an exception DuplicateSectionError (the node already exists). If the name is DEFAULT, it will report an exception ValueError (invalid node). Therefore, it is generally necessary to judge whether the node exists before adding. |
cf.has_section(section) | Judge whether the node exists, return True if it exists, and return False if it does not exist. |
cf.options(section) | Returns the list of options under the specified node |
cf.get(section, option) | Returns the value of the specified option under the specified node |
cf.has_option(section, option) | Judge whether the option exists, return True if it exists, and return False if it does not exist. It should be noted that if the node is None or an empty string, it is assumed to be DEFAULT. |
cf.set(section, option, value=None) | Add a new option. The default value is empty |
cf.write(fp, space_around_delimiters=True) | Write the configuration file, = = key: = = if you modify the file (for example, add_section), you must use this function to re write it into the configuration file before it takes effect. |
cf.remove_section(section) | Remove node (less used) |
cf.remove_option(section, option) | Remove option (less used) |