Version 1.0
Copyright © 2022 Lowell D. Thomas
Python APG
 … an ABNF Parser Generator
main.py
Go to the documentation of this file.
1 ''' @file examples/ini_file/main.py
2 @brief The driver function for the ini file demonstration.
3 @dir examples/ini_file
4 @brief This director contains all the file for the ini file example.
5 
6 This is a demonstration of a substantial, "real life" application
7 using the Python APG parser generator and the parser
8 that it generates. A [standard ini file format](https://en.wikipedia.org/wiki/INI_file)
9 is defined in the file, examples/ini_file/grammar.abnf.
10 The corresponding grammar object is in the file examples/ini_file/grammar.py
11 and was generated with Python APG, apg.py, with the command
12 > python3 %apg.py --input examples/ini_file/grammar.abnf
13 The sample data parsed by this example is in the file examples/ini_file/data.
14 '''
15 import sys
16 import os
17 # add the current working directory to the path
18 # DO NOT MOVE THE FOLLOWING STATEMENT
19 # if using autopep8 formatter, for example, set argument '--ignore=E402'
20 sys.path.append(os.getcwd())
21 print(os.getcwd())
22 from apg_py.lib import utilities as utils
23 from pprint import pprint
24 from examples.ini_file.ini_file import IniFile
25 
26 title = '''This is a demonstration of building a substantial
27 "real life", parser for parsing the well-known ini file format
28 for key/value pairs in possibly named sections.
29 This parser recognizes an ini file format similar to the
30 one described here(https://en.wikipedia.org/wiki/INI_file).
31 '''
32 print()
33 print(title)
34 
35 ini = IniFile()
36 print('IniFile.help()')
37 print(ini.help())
38 
39 fname = 'examples/ini_file/data'
40 # fd = open(fname, 'r')
41 # data = fd.read()
42 # fd.close()
43 # print('\nThe ini file.')
44 # print(data)
45 
46 ini.parse_ini_file(fname)
47 print('\n1) display the anonymous key names')
48 keys = ini.get_keys()
49 pprint(keys)
50 
51 print('\n2) for each key display the values')
52 for key in keys:
53  values = ini.get_values(key)
54  # print(key, end=' - ')
55  print(key + ' -')
56  for value in values:
57  print(' - ', end='')
58  print(value)
59  # pprint(values)
60 
61 print('\n3) display the section names')
62 sections = ini.get_sections()
63 pprint(sections)
64 
65 print('\n3) display the section key names')
66 for section in sections:
67  keys = ini.get_section_keys(section)
68  print('[' + section + ']: ', end='')
69  pprint(keys)
70 
71 print('\n4) for each section and key, display the values')
72 for section in sections:
73  keys = ini.get_section_keys(section)
74  for key in keys:
75  values = ini.get_section_values(section, key)
76  print('[' + section + ']:' + key, end=' - ')
77  pprint(values)
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.