Version 1.0
Copyright © 2023 Lowell D. Thomas
python-ini
 … powered by Python APG
ast_callbacks.py
Go to the documentation of this file.
1 ''' @file python_ini/ast_callbacks.py
2 @brief The AST call back functions for translation of the IniFile parser result.
3 '''
4 # import sys
5 # import os
6 # add the current working directory to the path
7 # DO NOT MOVE THE FOLLOWING STATEMENT
8 # if using autopep8 formatter, for example, set argument '--ignore=E402'
9 # sys.path.append(os.getcwd())
10 from apg_py.lib import identifiers as id
11 from apg_py.lib import utilities as utils
12 
13 
14 def section_name(state, input, index, length, data):
15  if(state == id.SEM_PRE):
16  name = utils.tuple_to_string(input[index:index + length])
17  if(name != data['current_section']):
18  data['current_section'] = name
19  if(not data['sections'].get(name)):
20  data['sections'][name] = {}
21 
22 
23 def key_name(state, input, index, length, data):
24  if(state == id.SEM_PRE):
25  name = utils.tuple_to_string(input[index:index + length])
26  if(data['current_section']):
27  section = data['sections'][data['current_section']]
28  else:
29  section = data['global']
30  key = section.get(name)
31  if(not key):
32  section[name] = []
33  data['current_key'] = name
34 
35 
36 def value(state, input, index, length, data):
37  if(state == id.SEM_POST):
38  if(data['current_section']):
39  section = data['sections'][data['current_section']]
40  else:
41  section = data['global']
42  section[data['current_key']].append(data['value'])
43 
44 
45 def hex_digit(d):
46  if(d >= 48 and d <= 57):
47  return d - 48
48  if(d >= 65 and d <= 72):
49  return d - 55
50  if(d >= 97 and d <= 102):
51  return d - 87
52  raise Exception('bad hex digit', d)
53 
54 
55 def string_eval(input):
56  value = ''
57  skip = 0
58  for i in range(len(input)):
59  if(skip):
60  skip -= 1
61  else:
62  if(input[i] == 92):
63  if(input[i + 1] == 120):
64  c = hex_digit(input[i + 2])
65  c = 16 * c + hex_digit(input[i + 3])
66  value += chr(c)
67  skip = 3
68  elif(input[i + 1] == 117):
69  c = hex_digit(input[i + 2])
70  c = 16 * c + hex_digit(input[i + 3])
71  c = 16 * c + hex_digit(input[i + 4])
72  c = 16 * c + hex_digit(input[i + 5])
73  value += chr(c)
74  skip = 5
75  elif(input[i + 1] == 85):
76  c = hex_digit(input[i + 2])
77  c = 16 * c + hex_digit(input[i + 3])
78  c = 16 * c + hex_digit(input[i + 4])
79  c = 16 * c + hex_digit(input[i + 5])
80  c = 16 * c + hex_digit(input[i + 6])
81  c = 16 * c + hex_digit(input[i + 7])
82  c = 16 * c + hex_digit(input[i + 8])
83  c = 16 * c + hex_digit(input[i + 9])
84  value += chr(c)
85  skip = 9
86  else:
87  if(input[i + 1] == 116):
88  value += chr(0x09)
89  elif(input[i + 1] == 114):
90  value += chr(0x0D)
91  elif(input[i + 1] == 110):
92  value += chr(0x0A)
93  elif(input[i + 1] == 98):
94  value += chr(0x20)
95  else:
96  value += chr(input[i + 1])
97  skip = 1
98  else:
99  value += chr(input[i])
100  return value
101 
102 
103 def d_value(state, input, index, length, data):
104  if(state == id.SEM_PRE):
105  data['value'] = string_eval(input[index:index + length])
106 
107 
108 def s_value(state, input, index, length, data):
109  if(state == id.SEM_PRE):
110  data['value'] = string_eval(input[index:index + length])
111 
112 
113 def string_value(state, input, index, length, data):
114  if(state == id.SEM_PRE):
115  data['value'] = string_eval(input[index:index + length])
116  a = data['value']
117  b = a
118 
119 
120 def float_value(state, input, index, length, data):
121  if(state == id.SEM_PRE):
122  value = utils.tuple_to_string(input[index:index + length])
123  data['value'] = float(value)
124  a = data['value']
125  b = a
126 
127 
128 def int_value(state, input, index, length, data):
129  if(state == id.SEM_PRE):
130  value = utils.tuple_to_string(input[index:index + length])
131  data['value'] = int(value)
132 
133 
134 def true_value(state, input, index, length, data):
135  if(state == id.SEM_PRE):
136  data['value'] = True
137 
138 
139 def false_value(state, input, index, length, data):
140  if(state == id.SEM_PRE):
141  data['value'] = False
142 
143 
144 def null_value(state, input, index, length, data):
145  if(state == id.SEM_PRE):
146  data['value'] = None
def true_value(state, input, index, length, data)
def string_value(state, input, index, length, data)
def false_value(state, input, index, length, data)
def s_value(state, input, index, length, data)
def d_value(state, input, index, length, data)
def section_name(state, input, index, length, data)
def float_value(state, input, index, length, data)
def null_value(state, input, index, length, data)
def key_name(state, input, index, length, data)
def value(state, input, index, length, data)
def int_value(state, input, index, length, data)
Python APG, Version 1.0, is licensed under the 2-Clause BSD License,
an Open Source Initiative Approved License.