firmware_tools/logic_memory_mapper/gen_json.py

136 lines
4.3 KiB
Python

# -----------------------------------------------------------------------------
# Title : Amalgamated JSON generator
# Project : Generic FW tools
# -----------------------------------------------------------------------------
# File : gen_json.py
# Author : Simon Southwell
# Company : Global Inkjet Systems
# Created : 2018-03-06
# Last update: 2018-03-06
# Platform :
# Standard : Python 3.x
# -----------------------------------------------------------------------------
# Description:
# Definition of class to read JSON register definitions and combine in
# a single amalgamated JSON output.
# -----------------------------------------------------------------------------
# Copyright (c) 2018 Global Inkjet Systems
# -----------------------------------------------------------------------------
import os
import sys
import argparse
import json
import collections
# Import base class to locate and read JSON files, and present a model
# to derived child class.
import memMapperBase
from memMapperBase import MemMapperBase
# ---------------------------------------------------------
# ---------------------------------------------------------
class MemMapperJson(MemMapperBase):
def __init__(self):
print('MemMapperJson')
MemMapperBase.__init__(self)
self.__bigDict = {}
self.__suffix = ''
self.__opDir = ''
# ---------------------------------------------------------
# Return the amalgamated dictionary
# ---------------------------------------------------------
def getBigDict(self):
return self.__bigDict
# ---------------------------------------------------------
# Return the suffix
# ---------------------------------------------------------
def getSuffix(self):
return self.__suffix
# ---------------------------------------------------------
# Get output directory
# ---------------------------------------------------------
def getOpDir(self):
return self.__opDir
# ---------------------------------------------------------
# Overload virtual function with specific output generation,
# and combine each IP's JSON dictionary into a single
# combined object
# ---------------------------------------------------------
def genOutput(self, ip, opDir='', opSuffix=''):
# Overwrite internal members
self.__suffix = opSuffix
self.__opDir = opDir
# Fetch the JSON path and the dictionary with the parsed data
jsonPath, jsonDict = self._getModel(ip)
for jsonBusDict in jsonDict :
bus = jsonBusDict['bus']
if bus in self.busList :
if bus not in self.__bigDict :
self.__bigDict[bus] = {}
self.__bigDict[bus][ip] = jsonBusDict
return
# --------------------------------------------------------------
# Parse the command line arguments
#
def processCmdLine():
# Create a parser object
parser = argparse.ArgumentParser(description='Process command line options.')
# Process common command line options with inherited static base class method
MemMapperJson.processCmdLine(parser)
return parser.parse_args()
# ###############################################################
# Only run if not imported (to test)
#
if __name__ == '__main__':
memMap = MemMapperJson()
# Process the command line options
cmdArgs = processCmdLine()
# Run the program for C++ class generation
memMap.run(cmdArgs.topBlock, cmdArgs.busList, cmdArgs.repoDir, cmdArgs.opDir, cmdArgs.opFileSuffix, 'cpp', cmdArgs.jsonFile)
bigDict = memMap.getBigDict()
suffix = memMap.getSuffix()
opdir = memMap.getOpDir()
del memMap
# Generate specific output. Assumes doc/ exists (or will be created) in level above JSON file location.
if opdir == '':
destDir = '.'
else:
destDir = opdir
# Check destination directory exists, and create it if not
if not os.path.exists(destDir):
os.makedirs(destDir)
# Open the main class definition output file
writeFileName = destDir + os.sep + 'combined' + suffix + '.json'
wfp = open(writeFileName, 'w')
# Dump the dictionary as a JSON file, with pretty-printing
json.dump(bigDict, wfp, indent=4, separators=(',', ': '))
wfp.close()