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