support loading JSON files as configs to edit
Signed-off-by: Brian S. Stephan <bss@incorporeal.org>
This commit is contained in:
parent
56eb65dd55
commit
99504bfde6
|
@ -241,21 +241,24 @@ def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file
|
||||||
the parsed configuration
|
the parsed configuration
|
||||||
"""
|
"""
|
||||||
try:
|
try:
|
||||||
content = get_binary_from_file(filename)
|
if filename[-5:] == '.json':
|
||||||
|
with open(filename) as file_:
|
||||||
|
return get_config_from_json(file_.read())
|
||||||
|
else:
|
||||||
|
content = get_binary_from_file(filename)
|
||||||
|
if whole_board:
|
||||||
|
if board_config:
|
||||||
|
return get_config(get_board_storage_section(content))
|
||||||
|
else:
|
||||||
|
return get_config(get_user_storage_section(content))
|
||||||
|
else:
|
||||||
|
return get_config(content)
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
if not allow_no_file:
|
if not allow_no_file:
|
||||||
raise
|
raise
|
||||||
config_pb2 = get_config_pb2()
|
config_pb2 = get_config_pb2()
|
||||||
return config_pb2.Config()
|
return config_pb2.Config()
|
||||||
|
|
||||||
if whole_board:
|
|
||||||
if board_config:
|
|
||||||
return get_config(get_board_storage_section(content))
|
|
||||||
else:
|
|
||||||
return get_config(get_user_storage_section(content))
|
|
||||||
else:
|
|
||||||
return get_config(content)
|
|
||||||
|
|
||||||
|
|
||||||
def get_config_from_usb(address: int) -> tuple[Message, object, object]:
|
def get_config_from_usb(address: int) -> tuple[Message, object, object]:
|
||||||
"""Read a config section from a USB device and provide the protobuf Message.
|
"""Read a config section from a USB device and provide the protobuf Message.
|
||||||
|
|
|
@ -99,6 +99,15 @@ def test_get_board_config_from_file_whole_board_dump():
|
||||||
assert config.addonOptions.bootselButtonOptions.enabled is False
|
assert config.addonOptions.bootselButtonOptions.enabled is False
|
||||||
|
|
||||||
|
|
||||||
|
@with_pb2s
|
||||||
|
def test_get_board_config_from_json_file():
|
||||||
|
"""Test that we can open a JSON file and parse the config."""
|
||||||
|
filename = os.path.join(HERE, 'test-files', 'test-config.json')
|
||||||
|
config = storage.get_config_from_file(filename, whole_board=True, board_config=True)
|
||||||
|
assert config.boardVersion == 'v0.7.6-15-g71f4512'
|
||||||
|
assert config.addonOptions.bootselButtonOptions.enabled is False
|
||||||
|
|
||||||
|
|
||||||
@with_pb2s
|
@with_pb2s
|
||||||
def test_get_config_from_file_file_not_fonud_ok():
|
def test_get_config_from_file_file_not_fonud_ok():
|
||||||
"""If we allow opening a file that doesn't exist (e.g. for the editor), check we get an empty config."""
|
"""If we allow opening a file that doesn't exist (e.g. for the editor), check we get an empty config."""
|
||||||
|
|
Loading…
Reference in New Issue