Example of python's method for adding deleting modifying and checking the configuration file.ini

  • 2020-06-12 09:57:46
  • OfStack

preface

This article is mainly about the python configuration file.ini add, delete, change and check the relevant content, share for your reference and study, the following words are not enough, let's have a look at the detailed introduction:

1. Lead into configobj library file

Can be installed directly with pip


#!/usr/bin/python
# -*- coding: utf-8 -*-

import json

from configobj import ConfigObj

2. Add section

Here is an example of the front and back end separation, where json data is received from the front end and then written to the configuration file


def add(self, false=None):
 self.log.debug("list")
 try:
  conf_ini = CONFIG_INI_PATH+"users.ini.bak"
  config = ConfigObj(conf_ini, encoding='UTF8')
  req = self.input["input"]
  data = req["data"]
  userName = data["userName"]
  disc = data["disc"]
  ip = data["ip"]
  expMonth = int(float(data["expDate"]) * 12)
  for user in config.items():
   if userName == user[0]:
    self.out = '{"status": 1,"msg":" User name already exists! "}'
    return false
   else:
    pass
  config[userName] = {}
  config[userName]['user'] = userName
  config[userName]['disc'] = disc
  config[userName]['ip'] = ip
  config[userName]['validity_date'] = data["expDate"]
  config[userName]['cert_expired'] = get_today_month(expMonth)
  config[userName]['enable'] = 0
  config[userName]['path'] = USER_KEY_PATH + userName
  config.write()
  self.out = '{"status": 0,"msg":" Operation successful! "}'
 except Exception, e:
  self.out = '{"status":1, "msg":"'+str(e)+'"}'

3. Modify section


def modify(self):
 self.log.debug("modify")
 try:
  conf_ini = CONFIG_INI_PATH + "users.ini.bak"
  config = ConfigObj(conf_ini, encoding='UTF8')
  req = self.input["input"]
  data = req["data"]
  userName = data["userName"]
  disc = data["disc"]
  ip = data["ip"]
  config[userName]['disc'] = disc
  config[userName]['ip'] = ip
  config.write()
  self.out = '{"status": 0,"msg":" Operation successful! "}'
 except Exception, e:
  self.out = '{"status":1, "msg":"'+str(e)+'"}'

4. Delete section

Find the corresponding section by section to perform the del operation


def delete(self, false=None):
 self.log.debug("delete")
 try:
  conf_ini = CONFIG_INI_PATH + "users.ini.bak"
  config = ConfigObj(conf_ini, encoding='UTF8')
  req = self.input["input"]
  data = req["data"]
  userName = data["userName"]
  for user in config.items():
   if userName == user[0]:
    del config[userName]
    config.write()
    self.out = '{"status": 0,"msg":" Operation successful! "}'
    return false
   else:
    pass
  self.out = '{"status": 1,"msg":" The user does not exist! "}'
 except Exception, e:
  self.out = '{"status":1, "msg":"config err!"}'

5. Query section

The python dictionary is used to output the contents of the configuration file as a whole, with query and paging capabilities in the code


def list(self):
 self.log.debug("list")
 try:
  req = self.input["input"]
  data = req["data"]
  pageSize = req["pageSize"]
  pageIndex = req["pageIndex"]
  userName = data["userName"]
  conf_ini = CONFIG_INI_PATH + "users.ini.bak"
  config = ConfigObj(conf_ini, encoding='UTF8')
  users = []
  n = 0
  if userName == '':
   for user in config.items():
    n = n + 1
    if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):
     users.append(user[1])
    else:
     pass
  else:
   for user in config.items():
    if userName == user[0]:
     n = n + 1
     if pageSize * pageIndex + 1 <= n <= pageSize * (pageIndex + 1):
      users.append(user[1])
     else:
      pass
    else:
     pass

  utext = json.dumps(users)
  self.out = '{"status": 0,"total":'+str(n)+',"data":' + utext + '}'
 except Exception, e:
  self.out = '{"status":1, "msg":"' + str(e) + '"}'
 self.log.debug("list in.")

conclusion


Related articles: