Skip to content

Commit e87c7cc

Browse files
committed
Add support to load a file as little endian
1 parent 84c3875 commit e87c7cc

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# Changelog
22
Changelog for dpdumperlib
33

4+
## [0.0.4] - 2024-09-28
5+
### Added
6+
- Parameter to set endianness of data loaded from file
7+
48
## [0.0.3] - 2024-08-22
59
### Added
610
- Keep a copy of non-remapped address and data arrays in the ICDefinition

dpdumperlib/io/file_utils.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""This file contains functions to aid with common file operations"""
22

33
import os
4-
from typing import List
4+
from typing import List, Literal
55

6-
def load_file_data(in_file_path: str, bytes_per_entry: int) -> List[int]:
6+
def load_file_data(in_file_path: str, bytes_per_entry: int, reverse_byte_order: bool = False) -> List[int]:
77
data_array: List[int] = []
8+
endianness: Literal['big', 'little'] = 'little' if reverse_byte_order else 'big'
89

910
with open(in_file_path, "rb") as f:
1011
# Read the file size
@@ -18,6 +19,6 @@ def load_file_data(in_file_path: str, bytes_per_entry: int) -> List[int]:
1819
# Push data in an array
1920
data: bytes
2021
while (data := f.read(bytes_per_entry)):
21-
data_array.append(int.from_bytes(data, byteorder='big', signed=False))
22+
data_array.append(int.from_bytes(data, byteorder=endianness, signed=False))
2223

2324
return data_array

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
44

55
[project]
66
name = "dpdumperlib"
7-
version = "0.0.3"
7+
version = "0.0.4"
88
description = "Library with some utility code for the dpdumper tool"
99
authors = [
1010
{ name = "Fabio Battaglia", email = "hkzlabnet@gmail.com" }

tests/test_file_utils.py

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ def test_loading_1K(filename_randomdata_1k):
2222

2323
data_8B: List[int] = FileUtils.load_file_data(filename_randomdata_1k, 8)
2424
assert(len(data_8B) == 128)
25+
assert(data_8B[0].to_bytes(length=8, byteorder='big', signed=False) == b'\xB2\x20\xA5\x60\x35\x3B\x63\x6D')
26+
27+
data_8BLE: List[int] = FileUtils.load_file_data(filename_randomdata_1k, 8, reverse_byte_order=True)
28+
assert(len(data_8BLE) == 128)
29+
assert(data_8BLE[0].to_bytes(length=8, byteorder='little', signed=False) == b'\xB2\x20\xA5\x60\x35\x3B\x63\x6D')
2530

2631
# Size of file is not divisible by 3
2732
with pytest.raises(Exception):

0 commit comments

Comments
 (0)