From 68bb22b84fe3b6e1792ac25dd95f76e6eb038457 Mon Sep 17 00:00:00 2001 From: Simon Southwell Date: Wed, 7 Mar 2018 07:35:00 +0000 Subject: [PATCH] GIS-6846 Added gen_json.py to generate an amalgamated JSON file as an artifact of the build --- logic_memory_mapper/gen_json.py | 135 ++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 logic_memory_mapper/gen_json.py diff --git a/logic_memory_mapper/gen_json.py b/logic_memory_mapper/gen_json.py new file mode 100644 index 0000000..75c502a --- /dev/null +++ b/logic_memory_mapper/gen_json.py @@ -0,0 +1,135 @@ +# ----------------------------------------------------------------------------- +# 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 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()