preliminary support for writing configs direct to JSON
I'm hoping this will allow for editing the JSON configs directly with edit-config Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
parent
8ad9b10018
commit
58f2f38546
|
@ -10,6 +10,7 @@ import os
|
||||||
import re
|
import re
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from google.protobuf.json_format import MessageToJson
|
||||||
from google.protobuf.message import Message
|
from google.protobuf.message import Message
|
||||||
|
|
||||||
import gp2040ce_bintools.storage as storage
|
import gp2040ce_bintools.storage as storage
|
||||||
|
@ -248,15 +249,19 @@ def write_new_config_to_filename(config: Message, filename: str, inject: bool =
|
||||||
with open(filename, 'wb') as file:
|
with open(filename, 'wb') as file:
|
||||||
file.write(binary)
|
file.write(binary)
|
||||||
else:
|
else:
|
||||||
binary = storage.serialize_config_with_footer(config)
|
if filename[-5:] == '.json':
|
||||||
with open(filename, 'wb') as file:
|
with open(filename, 'w') as file:
|
||||||
if filename[-4:] == '.uf2':
|
file.write(MessageToJson(config))
|
||||||
# we must pad to storage start in order for the UF2 write addresses to make sense
|
else:
|
||||||
file.write(storage.convert_binary_to_uf2([
|
binary = storage.serialize_config_with_footer(config)
|
||||||
(storage.USER_CONFIG_BINARY_LOCATION, storage.pad_config_to_storage_size(binary)),
|
with open(filename, 'wb') as file:
|
||||||
]))
|
if filename[-4:] == '.uf2':
|
||||||
else:
|
# we must pad to storage start in order for the UF2 write addresses to make sense
|
||||||
file.write(binary)
|
file.write(storage.convert_binary_to_uf2([
|
||||||
|
(storage.USER_CONFIG_BINARY_LOCATION, storage.pad_config_to_storage_size(binary)),
|
||||||
|
]))
|
||||||
|
else:
|
||||||
|
file.write(binary)
|
||||||
|
|
||||||
|
|
||||||
def write_new_config_to_usb(config: Message, endpoint_out: object, endpoint_in: object):
|
def write_new_config_to_usb(config: Message, endpoint_out: object, endpoint_in: object):
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
SPDX-FileCopyrightText: © 2023 Brian S. Stephan <bss@incorporeal.org>
|
SPDX-FileCopyrightText: © 2023 Brian S. Stephan <bss@incorporeal.org>
|
||||||
SPDX-License-Identifier: GPL-3.0-or-later
|
SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
"""
|
"""
|
||||||
|
import logging
|
||||||
import math
|
import math
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
@ -14,10 +15,12 @@ from decorator import decorator
|
||||||
import gp2040ce_bintools.builder as builder
|
import gp2040ce_bintools.builder as builder
|
||||||
from gp2040ce_bintools import get_config_pb2
|
from gp2040ce_bintools import get_config_pb2
|
||||||
from gp2040ce_bintools.storage import (STORAGE_SIZE, get_board_storage_section, get_config, get_config_footer,
|
from gp2040ce_bintools.storage import (STORAGE_SIZE, get_board_storage_section, get_config, get_config_footer,
|
||||||
get_user_storage_section, serialize_config_with_footer)
|
get_config_from_json, get_user_storage_section, serialize_config_with_footer)
|
||||||
|
|
||||||
HERE = os.path.dirname(os.path.abspath(__file__))
|
HERE = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
@decorator
|
@decorator
|
||||||
def with_pb2s(test, *args, **kwargs):
|
def with_pb2s(test, *args, **kwargs):
|
||||||
|
@ -351,6 +354,21 @@ def test_write_new_config_to_config_uf2(firmware_binary, tmp_path):
|
||||||
assert len(config_dump) == STORAGE_SIZE * 2
|
assert len(config_dump) == STORAGE_SIZE * 2
|
||||||
|
|
||||||
|
|
||||||
|
@with_pb2s
|
||||||
|
def test_write_new_config_to_config_json(config_binary, tmp_path):
|
||||||
|
"""Test that the config can be written to a file."""
|
||||||
|
tmp_file = os.path.join(tmp_path, 'config.json')
|
||||||
|
config = get_config(config_binary)
|
||||||
|
builder.write_new_config_to_filename(config, tmp_file)
|
||||||
|
|
||||||
|
# read new file
|
||||||
|
with open(tmp_file, 'r') as file:
|
||||||
|
config_dump = file.read()
|
||||||
|
logger.debug(config_dump)
|
||||||
|
config = get_config_from_json(config_dump)
|
||||||
|
assert config.boardVersion == 'v0.7.5'
|
||||||
|
|
||||||
|
|
||||||
@with_pb2s
|
@with_pb2s
|
||||||
def test_write_new_config_to_usb(config_binary):
|
def test_write_new_config_to_usb(config_binary):
|
||||||
"""Test that the config can be written to USB at the proper alignment."""
|
"""Test that the config can be written to USB at the proper alignment."""
|
||||||
|
|
Loading…
Reference in New Issue