buildcommands: Move data dictionary generation to its own class

Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
This commit is contained in:
Kevin O'Connor 2018-11-18 02:57:18 -05:00
parent 95e4c2d617
commit b3e8429b54
1 changed files with 49 additions and 41 deletions

View File

@ -1,7 +1,7 @@
#!/usr/bin/env python2
# Script to handle build time requests embedded in C code.
#
# Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
# Copyright (C) 2016-2018 Kevin O'Connor <kevin@koconnor.net>
#
# This file may be distributed under the terms of the GNU GPLv3 license.
import sys, os, subprocess, optparse, logging, shlex, socket, time, traceback
@ -302,34 +302,6 @@ const uint8_t command_index_size PROGMEM = ARRAY_SIZE(command_index);
Handlers.append(HandleCommandGeneration())
######################################################################
# Identify data dictionary generation
######################################################################
def build_identify():
data = {}
for h in Handlers:
h.update_data_dictionary(data)
# Format compressed info into C code
data = json.dumps(data)
zdata = zlib.compress(data, 9)
out = []
for i in range(len(zdata)):
if i % 8 == 0:
out.append('\n ')
out.append(" 0x%02x," % (ord(zdata[i]),))
fmt = """
const uint8_t command_identify_data[] PROGMEM = {%s
};
// Identify size = %d (%d uncompressed)
const uint32_t command_identify_size PROGMEM
= ARRAY_SIZE(command_identify_data);
"""
return data, fmt % (''.join(out), len(zdata), len(data))
######################################################################
# Version generation
######################################################################
@ -419,6 +391,49 @@ class HandleVersions:
Handlers.append(HandleVersions())
######################################################################
# Identify data dictionary generation
######################################################################
# Automatically generate the wire protocol data dictionary
class HandleIdentify:
def __init__(self):
self.ctr_dispatch = {}
def update_data_dictionary(self, data):
pass
def generate_code(self, options):
# Generate data dictionary
data = {}
for h in Handlers:
h.update_data_dictionary(data)
datadict = json.dumps(data)
# Write data dictionary
if options.write_dictionary:
f = open(options.write_dictionary, 'wb')
f.write(datadict)
f.close()
# Format compressed info into C code
zdatadict = zlib.compress(datadict, 9)
out = []
for i in range(len(zdatadict)):
if i % 8 == 0:
out.append('\n ')
out.append(" 0x%02x," % (ord(zdatadict[i]),))
fmt = """
const uint8_t command_identify_data[] PROGMEM = {%s
};
// Identify size = %d (%d uncompressed)
const uint32_t command_identify_size PROGMEM
= ARRAY_SIZE(command_identify_data);
"""
return fmt % (''.join(out), len(zdatadict), len(datadict))
Handlers.append(HandleIdentify())
######################################################################
# Main code
######################################################################
@ -455,19 +470,12 @@ def main():
if cmd not in ctr_dispatch:
error("Unknown build time command '%s'" % cmd)
ctr_dispatch[cmd](req)
# Create identify information
code = "".join([h.generate_code(options) for h in Handlers])
datadict, icode = build_identify()
# Write output
f = open(outcfile, 'wb')
f.write(FILEHEADER + code + icode)
f.close()
# Write data dictionary
if options.write_dictionary:
f = open(options.write_dictionary, 'wb')
f.write(datadict)
f.close()
# Write output
code = "".join([FILEHEADER] + [h.generate_code(options) for h in Handlers])
f = open(outcfile, 'wb')
f.write(code)
f.close()
if __name__ == '__main__':
main()