Skip to content

Commit

Permalink
chore: format using ruff
Browse files Browse the repository at this point in the history
Signed-off-by: yshalsager <[email protected]>
  • Loading branch information
yshalsager committed Oct 30, 2024
1 parent 09fc8ef commit b84964c
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 250 deletions.
9 changes: 0 additions & 9 deletions .flake8

This file was deleted.

1 change: 1 addition & 0 deletions xiaomi_flashable_firmware_creator/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Xiaomi Flashable Firmware Creator initialization."""

from pathlib import Path

work_dir = Path(__package__).parent.absolute()
3 changes: 2 additions & 1 deletion xiaomi_flashable_firmware_creator/__main__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Xiaomi Flashable Firmware Creator main entry point."""

from xiaomi_flashable_firmware_creator.xiaomi_flashable_firmware_creator import main

if __name__ == "__main__":
if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,12 @@ def prepare(self) -> List[str]:
Extract payload from zip in order to get partition names
:return: a list of partition names strings
"""
self.payload_file = open(
self.extractor.extract("payload.bin", self._tmp_dir), "rb"
)
self.payload_file = open(self.extractor.extract('payload.bin', self._tmp_dir), 'rb')
self.payload = Payload(self.payload_file)
self.payload.Init()
self.partitions = {
i.partition_name: i for i in self.payload.manifest.partitions
}
self.partitions = {i.partition_name: i for i in self.payload.manifest.partitions}
self.all_partitions = set(self.partitions.keys())
self.files = sorted([f"firmware-update/{i}.img" for i in self.all_partitions])
self.files = sorted([f'firmware-update/{i}.img' for i in self.all_partitions])
return self.files

def extract(self, files_to_extract: List[str]):
Expand All @@ -46,15 +42,15 @@ def extract(self, files_to_extract: List[str]):
:param files_to_extract: a list of files to extract
:return:
"""
Path(self._tmp_dir / "firmware-update").mkdir(parents=True, exist_ok=True)
Path(self._tmp_dir / 'firmware-update').mkdir(parents=True, exist_ok=True)
files_to_extract: set = set(self.files).intersection(set(files_to_extract))
for file in files_to_extract:
# partition: RepeatedCompositeContainer = self.partitions.get(file.split('/')[-1].split('.')[0])
partition = self.partitions.get(file.split("/")[-1].split(".")[0])
partition = self.partitions.get(file.split('/')[-1].split('.')[0])
with open(
Path(self._tmp_dir / f"firmware-update/{partition.partition_name}.img"),
"wb",
Path(self._tmp_dir / f'firmware-update/{partition.partition_name}.img'),
'wb',
) as out_f:
parse_payload(self.payload, partition, out_f)
self.payload_file.close()
Path(self._tmp_dir / "payload.bin").unlink()
Path(self._tmp_dir / 'payload.bin').unlink()
22 changes: 6 additions & 16 deletions xiaomi_flashable_firmware_creator/extractors/zip_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ def __init__(self, zip_file, tmp_dir):
:param zip_file: a path object or a string to a zip that contains a full recovery ROM.
:param tmp_dir: output directory to place the extracted zip in.
"""
self.zip_url = (
zip_file if "http" in zip_file or "ota.d.miui.com" in zip_file else ""
)
self.zip_url = zip_file if 'http' in zip_file or 'ota.d.miui.com' in zip_file else ''
self.zip_file_path = (
Path(zip_file) if not self.zip_url and isinstance(zip_file, str) else ""
Path(zip_file) if not self.zip_url and isinstance(zip_file, str) else ''
)
self.files = []
self._extractor = (
RemoteZip(self.zip_url) if self.zip_url else ZipFile(self.zip_file_path)
)
self._extractor = RemoteZip(self.zip_url) if self.zip_url else ZipFile(self.zip_file_path)
self.handler = (
AndroidOneZip(self.zip_file_path, tmp_dir, self._extractor)
if "payload.bin" in str(self._extractor.namelist())
if 'payload.bin' in str(self._extractor.namelist())
else StandardZip(self.zip_file_path, tmp_dir, self._extractor)
)

Expand All @@ -53,9 +49,7 @@ def exists(self) -> bool:
:return: True if zip file exists, False otherwise.
"""
return (
self.zip_file_path.exists() if self.zip_file_path else head(self.zip_url).ok
)
return self.zip_file_path.exists() if self.zip_file_path else head(self.zip_url).ok

def get_files_list(self):
"""
Expand All @@ -71,11 +65,7 @@ def get_file_name(self) -> str:
:return: a string of the input zip file name.
"""
return (
self.zip_file_path.name
if self.zip_file_path
else self.zip_url.split("/")[-1]
)
return self.zip_file_path.name if self.zip_file_path else self.zip_url.split('/')[-1]

def prepare(self):
if isinstance(self.handler, AndroidOneZip):
Expand Down
Loading

0 comments on commit b84964c

Please sign in to comment.