allow for "opening" an empty config from file

This commit is contained in:
Brian S. Stephan 2023-06-30 23:12:53 -05:00
parent 7d5052e811
commit 7aee99ef4f
Signed by: bss
GPG Key ID: 3DE06D3180895FCB
2 changed files with 25 additions and 3 deletions

View File

@ -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))

View File

@ -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."""