Python Forum
Configparser Modules
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Configparser Modules
#1
The module I have developed for the project I am dealing with in Python, which you can simply use on configuration files. I am waiting for your suggestions and corrections for the code.



Github Repository Link ( Configparser_Modules )

from configparser import ConfigParser , DuplicateSectionError , DuplicateOptionError , NoSectionError , NoOptionError

cfg = ConfigParser()

def Add_Section( filename , section , **kwargs ):
	"""
	example :
	Add_Section( database.ini , "Settings" , username = qweqwe23 , password = 123456 , setting_2 = True ,  ..... )
	kwarg : option = value
    	"""
	try:
		with open(filename , 'r+') as file:
			cfg.read_file(file)
			cfg.add_section(section)
			for option , value in kwargs.items():
				cfg.set(section , option , value)
		
			file.seek(0) 
			cfg.write(file)
			file.truncate() 
	except DuplicateSectionError:
		return "That section is already exist."
	except DuplicateOptionError :
		return "That option is already exist."

def Delete_Section( filename , section ):
	"""
	example :
	Delete_Section( "database.ini" , "Settings" )
    	"""
	try:
		with open(filename , 'r+') as file:
			cfg.read_file(file)
			cfg.remove_section(section)
		
			file.seek(0) 
			cfg.write(file)
			file.truncate() 
	except NoSectionError:
		return "There is no section to change."

def Upgrade_Option( filename , section , **kwargs ) :
	"""
	example :
	Upgrade_Section( database.ini , "Settings" , username = qweqwe23 , password = 123456 , setting_2 = True ,  ..... )
	kwarg : option = value
    	"""
	try:
		with open(filename , 'r+') as file:
			cfg.read_file(file)
			for option , value in kwargs.items():
				cfg.set(section , option , value)
		
			file.seek(0) 
			cfg.write(file)
			file.truncate() 
	except NoOptionError:
		return "There is no option to change."
	except NoSectionError:
		return "There is no section to change."

def Rename_Section( filename , section_from, section_to):
	"""
	example :
	Rename_Section( database.ini , "Settings" , "new_Settings" )
	kwarg : option = value
	"""
	try:
		with open(filename , 'r+') as file:
			cfg.read_file(file)
			items = cfg.items(section_from)

			cfg.add_section(section_to)

			for item in items:
				cfg.set(section_to, item[0], item[1])

			cfg.remove_section(section_from)
			file.seek(0) 
			cfg.write(file)
			file.truncate() 
	except NoSectionError:
		return "There is no section to change."
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020