Advertisement
gregwa

FCM145 - iniFile.py

May 7th, 2019
409
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.68 KB | None | 0 0
  1. # ======================================================
  2. #     iniFile.py
  3. #  ------------------------------------------------------
  4. # Created for Full Circle Magazine Issue 145
  5. # Written by G.D. Walters
  6. # Copyright (c) 2019 by G.D. Walters
  7. # This source code is released under the MIT License
  8. # (See MIT_License.txt)
  9. # ======================================================
  10. import os
  11. import configparser
  12.  
  13. global iniFileName
  14. iniFileName = "MyConfigFile.ini"
  15.  
  16.  
  17. # ======================================================
  18. # function read_ini()
  19. # ======================================================
  20. def read_ini():
  21.     global ini, iniFileName
  22.     global tree1, tree2, tree3, tree4
  23.     global animal1, animal2, animal3
  24.     global theanswer
  25.  
  26.     if os.path.isfile(iniFileName):
  27.         ini.read(iniFileName)
  28.         print(ini.sections())
  29.         sections = ini.sections()
  30.         for section in sections:
  31.             print("Section: {0}".format(section))
  32.             for key in ini[section]:
  33.                 print("Key = {0} - Value = {1}".format(key, ini[section][key]))
  34.         # Now get and assign each variable in the ini file
  35.         animals = ini['Animals']
  36.         animal1 = animals['animal1']
  37.         animal2 = animals['animal2']
  38.         animal3 = animals['animal3']
  39.         trees = ini['Trees']
  40.         tree1 = trees['tree1']
  41.         tree2 = trees['tree2']
  42.         tree3 = trees['tree3']
  43.         tree4 = trees['tree4']
  44.         ans = ini['Answers']
  45.         theanswer = ans.get('Life, The Universe and Everything')
  46.         return(True)
  47.     else:
  48.         write_default_ini()
  49.         return(False)
  50.  
  51.  
  52. # ======================================================
  53. # function write_ini()
  54. # ======================================================
  55. def write_ini():
  56.     global ini, iniFileName
  57.     global tree1, tree2, tree3, tree4
  58.     global animal1, animal2, animal3
  59.     ini.set('Trees', 'tree4', tree4)
  60.     ini.write(open(iniFileName, 'w'))
  61.  
  62.  
  63. # ======================================================
  64. # function write_default_ini()
  65. # ======================================================
  66. def write_default_ini():
  67.     global iniFileName
  68.     config = configparser.RawConfigParser()
  69.     config.add_section('Animals')
  70.     config.set('Animals', 'Animal1', 'Frog')
  71.     config.set('Animals', 'Animal2', 'Dog')
  72.     config.set('Animals', 'Animal3', 'Hog')
  73.     config.add_section('Trees')
  74.     config.set('Trees', 'Tree1', 'The Larch')
  75.     config.set('Trees', 'Tree2', 'Elm')
  76.     config.set('Trees', 'Tree3', 'Ash')
  77.     config.set('Trees', 'Tree4', '')
  78.     config.add_section('Answers')
  79.     config.set('Answers', 'life, the universe and everything', 42)
  80.  
  81.     # Writing our configuration file to 'example.cfg'
  82.     with open(iniFileName, 'w') as configfile:
  83.         config.write(configfile)
  84.  
  85.  
  86. # ======================================================
  87. # function show_ini_vars()
  88. # ======================================================
  89. def show_ini_vars():
  90.     global tree1, tree2, tree3, tree4
  91.     global animal1, animal2, animal3
  92.     global theanswer
  93.  
  94.     print("animal1: {0}, animal2: {1}, animal3: {2}".format(
  95.                                                             animal1,
  96.                                                             animal2,
  97.                                                             animal3))
  98.     print("tree1: {0}, tree2: {1}, tree3: {2}, tree4: {3}".format(
  99.                                                                   tree1,
  100.                                                                   tree2,
  101.                                                                   tree3,
  102.                                                                   tree4))
  103.     print("What's the answer to Life, The Universe and Everything? {0}".format(
  104.         theanswer))
  105.     print("theanswer type is {0}".format(type(theanswer)))
  106.  
  107.  
  108. # ======================================================
  109. # function init()
  110. # ======================================================
  111. def init():
  112.     # instantiate the ini object
  113.     global ini
  114.     ini = configparser.ConfigParser()
  115.     # call the read_ini function
  116.     isok = read_ini()
  117.     if isok:
  118.         # Call a function that prints out all our variables
  119.         show_ini_vars()
  120.     else:
  121.         isok = read_ini()
  122.         show_ini_vars()
  123.     # Now, change a variable and write it back to the ini file...
  124.     global tree4
  125.     tree4 = 'Birch'
  126.     write_ini()
  127.  
  128.  
  129. if __name__ == '__main__':
  130.     # ======================================================
  131.     # All code is run from the init() function
  132.     # ======================================================
  133.     init()
  134.     # Notify the user that we are done
  135.     print('Program End')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement