allow for "opening" an empty config from file
This commit is contained in:
parent
7d5052e811
commit
7aee99ef4f
@ -89,17 +89,24 @@ def get_config_footer(content: bytes) -> tuple[int, int, str]:
|
|||||||
return config_size, config_crc, config_magic
|
return config_size, config_crc, config_magic
|
||||||
|
|
||||||
|
|
||||||
def get_config_from_file(filename: str, whole_board: bool = False) -> Message:
|
def get_config_from_file(filename: str, whole_board: bool = False, allow_no_file: bool = False) -> Message:
|
||||||
"""Read the specified file (memory dump or whole board dump) and get back its config section.
|
"""Read the specified file (memory dump or whole board dump) and get back its config section.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
filename: the filename of the file to open and read
|
filename: the filename of the file to open and read
|
||||||
whole_board: optional, if true, attempt to find the storage section from its normal location on a board
|
whole_board: optional, if true, attempt to find the storage section from its normal location on a board
|
||||||
|
allow_no_file: if true, attempting to open a nonexistent file returns an empty config, else it errors
|
||||||
Returns:
|
Returns:
|
||||||
the parsed configuration
|
the parsed configuration
|
||||||
"""
|
"""
|
||||||
with open(filename, 'rb') as dump:
|
try:
|
||||||
content = dump.read()
|
with open(filename, 'rb') as dump:
|
||||||
|
content = dump.read()
|
||||||
|
except FileNotFoundError:
|
||||||
|
if not allow_no_file:
|
||||||
|
raise
|
||||||
|
config_pb2 = get_config_pb2()
|
||||||
|
return config_pb2.Config()
|
||||||
|
|
||||||
if whole_board:
|
if whole_board:
|
||||||
return get_config(get_storage_section(content))
|
return get_config(get_storage_section(content))
|
||||||
|
@ -89,6 +89,21 @@ def test_get_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_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."""
|
||||||
|
filename = os.path.join(HERE, 'test-files', 'nope.bin')
|
||||||
|
config = storage.get_config_from_file(filename, allow_no_file=True)
|
||||||
|
assert config.boardVersion == ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_get_config_from_file_file_not_fonud_raise():
|
||||||
|
"""If we don't allow opening a file that doesn't exist (e.g. for the editor), check we get an error."""
|
||||||
|
filename = os.path.join(HERE, 'test-files', 'nope.bin')
|
||||||
|
with pytest.raises(FileNotFoundError):
|
||||||
|
_ = storage.get_config_from_file(filename)
|
||||||
|
|
||||||
|
|
||||||
@with_pb2s
|
@with_pb2s
|
||||||
def test_config_parses(storage_dump):
|
def test_config_parses(storage_dump):
|
||||||
"""Test that we need the config_pb2 to exist/be compiled for reading the config to work."""
|
"""Test that we need the config_pb2 to exist/be compiled for reading the config to work."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user