Refactor util_char_map_gen.py to comply with PEP8

pull/13/head
nirenjan 2017-06-14 11:04:53 -07:00
parent 9070d88588
commit 262d125cd4
1 changed files with 50 additions and 27 deletions

View File

@ -1,11 +1,14 @@
#!/usr/bin/python
# Character map generator
"""
Generator script to parse character mapping
for the X52/X52 Pro MFD
"""
import sys
import re
import inspect
autogen_header = """
AUTOGEN_HEADER = """
/*
* Autogenerated character map file for Saitek X52 Pro
* Generated from %s
@ -15,7 +18,8 @@ autogen_header = """
"""
class MapTable():
class MapTable(object):
"""
Defines a MapTable entry, with each entry storing the value seen so far,
the type of the entry, and the value, if it's a value node.
@ -23,12 +27,15 @@ class MapTable():
# Empty list
root = [None] * 256
def __init__(self, value_so_far, map_value = None):
def __init__(self, value_so_far, map_value=None):
self.next_level = [None] * 256
self.value_so_far = value_so_far
self.map_value = map_value
def output_nodes(self):
"""
Output the individual nodes
"""
output_lines = []
output_count = 0
for node in self.next_level:
@ -37,14 +44,16 @@ class MapTable():
output_count += 1
if output_count != 0:
struct_header = 'static struct map_entry table_%x[64] = {' % self.value_so_far
struct_header = 'static struct map_entry table_%x[64] = {' % \
self.value_so_far
output_lines.append(struct_header)
for node_index in range(0,256):
for node_index in range(0, 256):
node = self.next_level[node_index]
if node is not None:
output_lines.append(self.dump_entry_line(0x80, node_index,
node.value_so_far, node.map_value))
node.value_so_far,
node.map_value))
output_lines.extend(['};', ''])
@ -52,6 +61,9 @@ class MapTable():
@staticmethod
def dump_entry_line(offset, node_index, value_so_far, map_value):
"""
Dump the array entry for the current node
"""
if map_value is None:
node_entry_line = '\t[0x%02x] = { table_%x, TYPE_POINTER, 0 },' % \
(node_index - offset, value_so_far)
@ -63,6 +75,9 @@ class MapTable():
@classmethod
def add_to_table(cls, input_val, map_val):
"""
Add a map value to the lookup table
"""
try:
uchr = unichr(input_val)
except NameError:
@ -78,22 +93,24 @@ class MapTable():
value_so_far = 0
level = cls.root
for i in range(len(utf8_vals)):
c = utf8_vals[i]
value_so_far = (value_so_far << 8) | c
if i < (len(utf8_vals) - 1):
node = level[c]
for index, char in enumerate(utf8_vals):
value_so_far = (value_so_far << 8) | char
if index < (len(utf8_vals) - 1):
node = level[char]
if node is None:
node = cls(value_so_far)
level[c] = node
level[char] = node
level = level[c].next_level
level = level[char].next_level
else:
node = cls(value_so_far, map_val)
level[c] = node
level[char] = node
@classmethod
def output_table_as_list(cls):
"""
Output the map table as a list of lines
"""
output_lines = []
for node in cls.root:
if node is not None:
@ -101,20 +118,26 @@ class MapTable():
output_lines.append('struct map_entry map_root[256] = {')
for node_index in range(0,256):
for node_index in range(0, 256):
node = cls.root[node_index]
if node is not None:
output_lines.append(cls.dump_entry_line(0x0, node_index,
node.value_so_far, node.map_value))
node.value_so_far,
node.map_value))
output_lines.extend(['};', ''])
return output_lines
class LineFormatError(ValueError):
"""
Error class for parser
"""
pass
def parse_line(line):
def parse_line(data):
"""
Parse a line containing a mapping descriptor. The mapping descriptor
must start with a hexadecimal unicode code point, followed by either a
@ -122,22 +145,22 @@ def parse_line(line):
value.
"""
# Strip off comments
line = re.sub(re.compile('#.*$'), '', line)
data = re.sub(re.compile('#.*$'), '', data)
# Strip off leading and trailing whitespace
line = line.strip()
data = data.strip()
# If the line is empty, it is a comment line
if len(line) == 0:
if len(data) == 0:
return None, None
# Find the code point and the target value
try:
code_point, target = line.strip().split()
code_point, target = data.strip().split()
except ValueError:
# Raised when there are either too many, or not enough values in
# the string
raise LineFormatError('Invalid descriptor format "%s"' % line)
raise LineFormatError('Invalid descriptor format "%s"' % data)
# Convert the string to its equivalent numeric value
try:
@ -159,18 +182,18 @@ def parse_line(line):
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write('Usage: %s <input-map> <output-c-file>\n' % sys.argv[0])
sys.stderr.write('Usage: %s <input-map> <output-c-file>\n' %
sys.argv[0])
sys.exit(1)
with open(sys.argv[1], 'r') as infile:
for line in infile:
src, dst = parse_line(line)
if src is not None:
MapTable.add_to_table(src,dst)
MapTable.add_to_table(src, dst)
with open(sys.argv[2], 'w') as outfile:
outfile.write(autogen_header % sys.argv[1])
outfile.write(AUTOGEN_HEADER % sys.argv[1])
for line in MapTable.output_table_as_list():
outfile.write(line + '\n')