From 1243ab3c81fc33ddacb3c15d18b6c63a45e6989e Mon Sep 17 00:00:00 2001 From: Arnaud Durand Date: Mon, 24 May 2021 01:21:35 +0200 Subject: [PATCH] software/dump: add JSON dump --- litescope/software/driver/analyzer.py | 2 ++ litescope/software/dump/__init__.py | 1 + litescope/software/dump/json.py | 25 +++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 litescope/software/dump/json.py diff --git a/litescope/software/driver/analyzer.py b/litescope/software/driver/analyzer.py index e432cb0..4ce8743 100644 --- a/litescope/software/driver/analyzer.py +++ b/litescope/software/driver/analyzer.py @@ -168,6 +168,8 @@ class LiteScopeAnalyzerDriver: dump = CSVDump() elif ext == ".py": dump = PythonDump() + elif ext == ".json": + dump = JSONDump() elif ext == ".sr": dump = SigrokDump(samplerate=samplerate) else: diff --git a/litescope/software/dump/__init__.py b/litescope/software/dump/__init__.py index 0183987..5062041 100644 --- a/litescope/software/dump/__init__.py +++ b/litescope/software/dump/__init__.py @@ -1,5 +1,6 @@ from litescope.software.dump.common import DumpData, DumpVariable, Dump from litescope.software.dump.csv import CSVDump from litescope.software.dump.python import PythonDump +from litescope.software.dump.json import JSONDump from litescope.software.dump.sigrok import SigrokDump from litescope.software.dump.vcd import VCDDump diff --git a/litescope/software/dump/json.py b/litescope/software/dump/json.py new file mode 100644 index 0000000..d83bc72 --- /dev/null +++ b/litescope/software/dump/json.py @@ -0,0 +1,25 @@ +# +# This file is part of LiteScope. +# +# Copyright (c) 2021 Arnaud Durand +# SPDX-License-Identifier: BSD-2-Clause + +import json + +from litescope.software.dump.common import Dump + + +class JSONDump(Dump): + def __init__(self, dump=None): + Dump.__init__(self) + self.variables = [] if dump is None else dump.variables + + def generate_data(self): + return {v.name: v.values for v in self.variables} + + def write(self, filename): + with open(filename, "w") as f: + json.dump(self.generate_data(), f) + + def read(self, filename): + raise NotImplementedError("Python files can not (yet) be read, please contribute!")