Skip to content

Commit

Permalink
chore: change read method to unmarshal
Browse files Browse the repository at this point in the history
change to more descriptive name. read() may be used in the class
for something else in the future
  • Loading branch information
swysocki committed Jul 31, 2022
1 parent eb30138 commit 27e13a3
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
6 changes: 3 additions & 3 deletions gpt_image/disk.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ def open(self):
+ self.geometry.header_length
]
self.table.primary_header = Header(self.geometry)
self.table.primary_header.read(primary_header_b, self.geometry)
self.table.primary_header.unmarshal(primary_header_b, self.geometry)
self.table.secondary_header = Header(self.geometry, is_backup=True)
self.table.secondary_header.read(backup_header_b, self.geometry)
self.table.secondary_header.unmarshal(backup_header_b, self.geometry)
# read the partition tables
primary_part_table_b = disk_bytes[
self.geometry.primary_array_byte : self.geometry.primary_array_byte
Expand All @@ -63,7 +63,7 @@ def open(self):
partition_bytes = primary_part_table_b[
offset : offset + PartitionEntryArray.EntryLength
]
new_part = Partition.read(partition_bytes, self.geometry.sector_size)
new_part = Partition.unmarshal(partition_bytes, self.geometry.sector_size)
if new_part.type_guid != Partition._EMPTY_GUID:
self.table.partitions.entries.append(new_part)

Expand Down
9 changes: 4 additions & 5 deletions gpt_image/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Partition:
partition_guid: string UUID partition GUID
attribute_flags: PartitionAttribute or int of attribute flag bit to set
automatically generated
first_lba: integer LBA of partition start, automatically calculated
first_lba: integer LBA of partition start, automatically calculated
last_lba: integer LBA of partition end, automatically calculated
alignment: integer partition block alignment
"""
Expand All @@ -65,7 +65,6 @@ def __init__(
"""Initialize Partition Object"""
self.type_guid = type_guid
self.partition_name = name
self._attribute_flags = 0
self.partition_guid = partition_guid
# if the partition GUID is empty, generate one
if not partition_guid:
Expand All @@ -78,7 +77,7 @@ def __init__(

def __repr__(self) -> str:
partitionvalue = {k: v for k, v in vars(self).items() if not k.startswith("_")}
partitionvalue['attribute_flags'] = self.attribute_flags
partitionvalue["attribute_flags"] = self.attribute_flags
return json.dumps(partitionvalue, indent=2, ensure_ascii=False)

@property
Expand Down Expand Up @@ -112,7 +111,7 @@ def marshal(self) -> bytes:
"""Marshal to byte structure"""
attributes_value = 0
for attrib in self.attribute_flags:
attributes_value += 2**attrib
attributes_value += 2 ** attrib
partition_bytes = self._PARTITION_FORMAT.pack(
uuid.UUID(self.type_guid).bytes_le,
uuid.UUID(self.partition_guid).bytes_le,
Expand All @@ -124,7 +123,7 @@ def marshal(self) -> bytes:
return partition_bytes

@staticmethod
def read(partition_bytes: bytes, sector_size: int) -> "Partition":
def unmarshal(partition_bytes: bytes, sector_size: int) -> "Partition":
"""Create a Partition object from existing bytes"""
if len(partition_bytes) != PartitionEntryArray.EntryLength:
raise ValueError(f"Invalid Partition Entry length: {len(partition_bytes)}")
Expand Down
4 changes: 2 additions & 2 deletions gpt_image/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def marshal(self) -> bytes:
return pmbr_bytes

@staticmethod
def read(pmbr_bytes: bytes, geometry: Geometry) -> "ProtectiveMBR":
def unmarshal(pmbr_bytes: bytes, geometry: Geometry) -> "ProtectiveMBR":
(
_,
boot_indicator,
Expand Down Expand Up @@ -158,7 +158,7 @@ def marshal(self) -> bytes:
return header_bytes + padding

@staticmethod
def read(header_bytes: bytes, geometry: Geometry) -> "Header":
def unmarshal(header_bytes: bytes, geometry: Geometry) -> "Header":
(
signature,
revision,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_partition_read():
)
part_array.add(part)
part_bytes = part.marshal()
new_part = Partition.read(part_bytes, geo.sector_size)
new_part = Partition.unmarshal(part_bytes, geo.sector_size)
assert new_part.partition_name == PART_NAME
assert new_part.size == 2 * 1024
assert new_part.type_guid.upper() == Partition.LINUX_FILE_SYSTEM
Expand Down Expand Up @@ -126,7 +126,7 @@ def test_partition_entry_marshall():
)
part1_bytes = part_array_bytes[:128]
part2_bytes = part_array_bytes[128:256]
part1_unmarshal = Partition.read(part1_bytes, geo.sector_size)
part2_unmarshal = Partition.read(part2_bytes, geo.sector_size)
part1_unmarshal = Partition.unmarshal(part1_bytes, geo.sector_size)
part2_unmarshal = Partition.unmarshal(part2_bytes, geo.sector_size)
assert part1_unmarshal.partition_name == PART_NAME
assert part2_unmarshal.partition_name == PART_NAME_2
4 changes: 2 additions & 2 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_protective_mbr_read(new_geometry: Geometry):
pmbr = ProtectiveMBR(new_geometry)
pmbr_bytes = pmbr.marshal()

pmbr_o = ProtectiveMBR.read(pmbr_bytes, new_geometry)
pmbr_o = ProtectiveMBR.unmarshal(pmbr_bytes, new_geometry)
assert pmbr_o.boot_indicator == 0
assert pmbr_o.start_chs == b"\x00"
assert pmbr_o.partition_type == b"\xEE"
Expand Down Expand Up @@ -74,7 +74,7 @@ def test_header_read(new_geometry: Geometry):
header_b = header.marshal()
assert len(header_b) == new_geometry.sector_size

header_o = Header.read(header_b[: Header._HEADER_SIZE], new_geometry)
header_o = Header.unmarshal(header_b[: Header._HEADER_SIZE], new_geometry)
assert header_o.signature == Header._SIGNATURE
assert header_o.revision == Header._REVISION
assert header_o.header_size == Header._HEADER_SIZE
Expand Down

0 comments on commit 27e13a3

Please sign in to comment.