Version 1.0
Copyright © 2023 Lowell D. Thomas
python-ini
 … powered by Python APG
help.py
Go to the documentation of this file.
1 ''' @file python_ini/help.py
2 @brief This is the INI help function.
3 <pre>
4 options:
5  -h, --help show this help message and exit
6  -v, --version display the version and copyright information
7 
8 </pre>
9 '''
10 # import os
11 # import sys
12 import argparse
13 # sys.path.append(os.getcwd())
14 # add the current working directory to the path
15 # DO NOT MOVE THE FOLLOWING STATEMENT
16 # if using autopep8 formatter, for example, set argument '--ignore=E402'
17 
18 
19 desc = '''
20 This is a Python INI file parser.
21 '''
22 ep = '''NOTES: ???
23 '''
24 
25 how_to = '''usage:
26  The parser is implemented as a Python class with member functions to parse and retrieve key values.
27  The writer is implemented as a Python class with member functions to create comments,
28  key/value pairs and section names and write the result to a file.
29  Additional functions exist to configure the format.
30  See the full documentation at https://sabnf.com/docs/python-ini/index.html for complete details.
31 
32 A simple Python program illustrating the parsing basics follows.
33 
34 from python_ini.ini_file import IniFile
35 ini = IniFile() # default is single-value mode
36 ini.parse(INI_FILE_NAME)
37 if(ini.errors):
38  print('INI FILE ERRORS')
39  print(ini.display_errors()) # display any errors found in the INI file
40 keys = ini.get_keys() # get a list of all keys in the global section
41 for key in keys:
42  values = ini.get_values(key) # get the value for each key
43  print(key, end=': ')
44  print(values)
45 sections = ini.get_sections() # get a list of all section names
46 for section in sections:
47  keys = ini.get_section_keys(section) # get a list of all keys in each section
48  for key in keys:
49  values = ini.get_section_values(section, key) # get the value for each section key
50  print('[' + section + ']:' + key, end=' ')
51  print(values)
52 
53 A simple Python program illustrating the writing basics follows.
54 
55 from python_ini.ini_file import IniFile
56 w = IniWriter() # instantiate the writer
57 w.delimiters('#', ':', ',') # set all configurable values
58 w.booleans('TRUE', 'OFF', 'void')
59 w.comment_tab(30)
60 w.comment() # generate a blank comment line
61 w.comment('global keys') # generate a comment line
62 w.key('Unicode', 'a\U0010ffffb', 'max Unicode character')
63 w.key('flags', [True, False, None], 'all "booleans"') # generate key/value pair
64 w.comment()
65 w.comment('first section')
66 w.section('__SECTION__', 'this is a section') # generate a section line
67 w.key('section_key', [1, 2, 3])
68 w.key(
69  'long-key',
70  ['abc\xffdef\ue000ghi\U0010ffffjkl'],
71  'hex and Unicode string characters')
72 w.write('output.ini) # write the INI file to 'output.ini'
73 
74 '''
75 
76 
77 def main():
78  parser = argparse.ArgumentParser(
79  prog='python-ini',
80  description=desc
81  )
82  parser.add_argument('-v', '--version',
83  help='display the version and copyright information and exit',
84  dest='version',
85  action='store_true')
86  parser.add_argument('-u', '--usage',
87  help='display INI file parser usage and exit',
88  dest='usage',
89  action='store_true')
90  args = parser.parse_args()
91 
92  if(args.version):
93  # handle the version request
94  text = 'python-ini version 1.1.0'
95  text += '\nA Python INI file parser and writer'
96  text += '\nCopyright (c) 2023 Lowell D. Thomas'
97  print(text)
98  exit()
99 
100  if(args.usage):
101  # input SABNF file name required
102  print(how_to)
103  exit()
104 
105  print('options [--help | --version | --usage]')
def main()
Definition: help.py:77
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.