Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ======================================================
- # iniFile.py
- # ------------------------------------------------------
- # Created for Full Circle Magazine Issue 145
- # Written by G.D. Walters
- # Copyright (c) 2019 by G.D. Walters
- # This source code is released under the MIT License
- # (See MIT_License.txt)
- # ======================================================
- import os
- import configparser
- global iniFileName
- iniFileName = "MyConfigFile.ini"
- # ======================================================
- # function read_ini()
- # ======================================================
- def read_ini():
- global ini, iniFileName
- global tree1, tree2, tree3, tree4
- global animal1, animal2, animal3
- global theanswer
- if os.path.isfile(iniFileName):
- ini.read(iniFileName)
- print(ini.sections())
- sections = ini.sections()
- for section in sections:
- print("Section: {0}".format(section))
- for key in ini[section]:
- print("Key = {0} - Value = {1}".format(key, ini[section][key]))
- # Now get and assign each variable in the ini file
- animals = ini['Animals']
- animal1 = animals['animal1']
- animal2 = animals['animal2']
- animal3 = animals['animal3']
- trees = ini['Trees']
- tree1 = trees['tree1']
- tree2 = trees['tree2']
- tree3 = trees['tree3']
- tree4 = trees['tree4']
- ans = ini['Answers']
- theanswer = ans.get('Life, The Universe and Everything')
- return(True)
- else:
- write_default_ini()
- return(False)
- # ======================================================
- # function write_ini()
- # ======================================================
- def write_ini():
- global ini, iniFileName
- global tree1, tree2, tree3, tree4
- global animal1, animal2, animal3
- ini.set('Trees', 'tree4', tree4)
- ini.write(open(iniFileName, 'w'))
- # ======================================================
- # function write_default_ini()
- # ======================================================
- def write_default_ini():
- global iniFileName
- config = configparser.RawConfigParser()
- config.add_section('Animals')
- config.set('Animals', 'Animal1', 'Frog')
- config.set('Animals', 'Animal2', 'Dog')
- config.set('Animals', 'Animal3', 'Hog')
- config.add_section('Trees')
- config.set('Trees', 'Tree1', 'The Larch')
- config.set('Trees', 'Tree2', 'Elm')
- config.set('Trees', 'Tree3', 'Ash')
- config.set('Trees', 'Tree4', '')
- config.add_section('Answers')
- config.set('Answers', 'life, the universe and everything', 42)
- # Writing our configuration file to 'example.cfg'
- with open(iniFileName, 'w') as configfile:
- config.write(configfile)
- # ======================================================
- # function show_ini_vars()
- # ======================================================
- def show_ini_vars():
- global tree1, tree2, tree3, tree4
- global animal1, animal2, animal3
- global theanswer
- print("animal1: {0}, animal2: {1}, animal3: {2}".format(
- animal1,
- animal2,
- animal3))
- print("tree1: {0}, tree2: {1}, tree3: {2}, tree4: {3}".format(
- tree1,
- tree2,
- tree3,
- tree4))
- print("What's the answer to Life, The Universe and Everything? {0}".format(
- theanswer))
- print("theanswer type is {0}".format(type(theanswer)))
- # ======================================================
- # function init()
- # ======================================================
- def init():
- # instantiate the ini object
- global ini
- ini = configparser.ConfigParser()
- # call the read_ini function
- isok = read_ini()
- if isok:
- # Call a function that prints out all our variables
- show_ini_vars()
- else:
- isok = read_ini()
- show_ini_vars()
- # Now, change a variable and write it back to the ini file...
- global tree4
- tree4 = 'Birch'
- write_ini()
- if __name__ == '__main__':
- # ======================================================
- # All code is run from the init() function
- # ======================================================
- init()
- # Notify the user that we are done
- print('Program End')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement