Skip to content

Commit e8c1476

Browse files
committed
Rename a few functions
1 parent 6841490 commit e8c1476

File tree

3 files changed

+28
-28
lines changed

3 files changed

+28
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ print(get_info(url))
161161
file = "/home/ben/RLC-410-5MP_20_20052300.zip"
162162
print(get_info(file))
163163
with ReolinkFirmware.from_file(file) as fw:
164-
fw.extract_pak()
164+
fw.extract()
165165
```
166166

167167
In most cases where a URL is used, it will be a direct link to the file

reolinkfw/__init__.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def get_fs_info(self) -> list[dict[str, str]]:
151151
})
152152
return result
153153

154-
async def get_info_from_pak(self) -> dict[str, Any]:
154+
async def get_info(self) -> dict[str, Any]:
155155
ha = await asyncio.to_thread(self.sha256)
156156
app = self._fs_sections[-1]
157157
with self.open(app) as f:
@@ -204,7 +204,7 @@ def extract_file_system(self, section: Section, dest: Optional[Path] = None) ->
204204
else:
205205
raise Exception("Unknown file system")
206206

207-
def extract_pak(self, dest: Optional[Path] = None, force: bool = False) -> None:
207+
def extract(self, dest: Optional[Path] = None, force: bool = False) -> None:
208208
dest = (Path.cwd() / "reolink_firmware") if dest is None else dest
209209
dest.mkdir(parents=True, exist_ok=force)
210210
rootfsdir = [s.name for s in self if s.name in ROOTFS_SECTIONS][0]
@@ -227,20 +227,20 @@ async def download(url: StrOrURL) -> Union[bytes, int]:
227227
return await resp.read() if resp.status == 200 else resp.status
228228

229229

230-
def extract_paks(zip: Union[StrPath, IO[bytes]]) -> list[tuple[str, ReolinkFirmware]]:
231-
"""Return a list of tuples, one for each PAK file found in the ZIP.
230+
def firmwares_from_zip(zip: Union[StrPath, IO[bytes]]) -> list[tuple[str, ReolinkFirmware]]:
231+
"""Return a list of tuples, one for each firmware found in the ZIP.
232232
233-
It is the caller's responsibility to close the PAK files.
233+
It is the caller's responsibility to close the firmware files.
234234
"""
235-
paks = []
235+
fws = []
236236
with ZipFile(zip) as myzip:
237237
for name in myzip.namelist():
238238
file = myzip.open(name)
239239
if is_pak_file(file):
240-
paks.append((file.name, ReolinkFirmware.from_fd(file)))
240+
fws.append((file.name, ReolinkFirmware.from_fd(file)))
241241
else:
242242
file.close()
243-
return paks
243+
return fws
244244

245245

246246
def get_info_from_files(files: Mapping[Files, Optional[bytes]]) -> dict[str, Optional[str]]:
@@ -323,19 +323,19 @@ async def direct_download_url(url: str) -> str:
323323
return url
324324

325325

326-
async def get_paks(file_or_url: StrPathURL, use_cache: bool = True) -> list[tuple[Optional[str], ReolinkFirmware]]:
327-
"""Return PAK files read from an on-disk file or a URL.
326+
async def firmwares_from_file(file_or_url: StrPathURL, use_cache: bool = True) -> list[tuple[Optional[str], ReolinkFirmware]]:
327+
"""Return firmwares read from an on-disk file or a URL.
328328
329329
The file or resource may be a ZIP or a PAK. On success return a
330330
list of 2-tuples where each tuple is of the form
331-
`(pak_name, pak_file)`. When the argument is a URL, `pak_name` may
331+
`(filename, firmware)`. When the argument is a URL, `filename` may
332332
be None. If the file is a ZIP the list might be empty.
333-
It is the caller's responsibility to close the PAK files.
333+
It is the caller's responsibility to close the firmware files.
334334
"""
335335
file_or_url = str(file_or_url)
336336
if is_url(file_or_url):
337337
if use_cache and has_cache(file_or_url):
338-
return await get_paks(get_cache_file(file_or_url))
338+
return await firmwares_from_file(get_cache_file(file_or_url))
339339
file_or_url = await direct_download_url(file_or_url)
340340
zip_or_pak_bytes = await download(file_or_url)
341341
if isinstance(zip_or_pak_bytes, int):
@@ -348,13 +348,13 @@ async def get_paks(file_or_url: StrPathURL, use_cache: bool = True) -> list[tupl
348348
else:
349349
zipfile = io.BytesIO(zip_or_pak_bytes)
350350
if is_zipfile(zipfile):
351-
return await asyncio.to_thread(extract_paks, zipfile)
351+
return await asyncio.to_thread(firmwares_from_zip, zipfile)
352352
zipfile.close()
353353
raise Exception("Not a ZIP or a PAK file")
354354
elif is_local_file(file_or_url):
355355
file_or_url = Path(file_or_url)
356356
if is_zipfile(file_or_url):
357-
return await asyncio.to_thread(extract_paks, file_or_url)
357+
return await asyncio.to_thread(firmwares_from_zip, file_or_url)
358358
elif is_pak_file(file_or_url):
359359
return [(file_or_url.name, ReolinkFirmware.from_file(file_or_url))]
360360
raise Exception("Not a ZIP or a PAK file")
@@ -367,12 +367,12 @@ async def get_info(file_or_url: StrPathURL, use_cache: bool = True) -> list[dict
367367
The file or resource may be a ZIP or a PAK.
368368
"""
369369
try:
370-
paks = await get_paks(file_or_url, use_cache)
370+
fws = await firmwares_from_file(file_or_url, use_cache)
371371
except Exception as e:
372372
return [{"file": file_or_url, "error": str(e)}]
373-
if not paks:
373+
if not fws:
374374
return [{"file": file_or_url, "error": "No PAKs found in ZIP file"}]
375-
info = [{**await pakfile.get_info_from_pak(), "file": file_or_url, "pak": pakname} for pakname, pakfile in paks]
376-
for _, pakfile in paks:
377-
pakfile.close()
375+
info = [{**await fw.get_info(), "file": file_or_url, "pak": pakname} for pakname, fw in fws]
376+
for _, fw in fws:
377+
fw.close()
378378
return info

reolinkfw/__main__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from datetime import datetime
88
from pathlib import Path, PurePath
99

10-
from reolinkfw import __version__, get_info, get_paks
10+
from reolinkfw import __version__, firmwares_from_file, get_info
1111

1212
HW_FIELDS = ("board_type", "detail_machine_type", "board_name")
1313

@@ -43,14 +43,14 @@ async def info(args: Namespace) -> None:
4343

4444

4545
async def extract(args: Namespace) -> None:
46-
paks = await get_paks(args.file_or_url, not args.no_cache)
47-
if not paks:
46+
fws = await firmwares_from_file(args.file_or_url, not args.no_cache)
47+
if not fws:
4848
raise Exception("No PAKs found in ZIP file")
4949
dest = Path.cwd() if args.dest is None else args.dest
50-
for pakname, pakfile in paks:
51-
name = pakfile.sha256() if pakname is None else PurePath(pakname).stem
52-
await asyncio.to_thread(pakfile.extract_pak, dest / name, args.force)
53-
pakfile.close()
50+
for pakname, fw in fws:
51+
name = fw.sha256() if pakname is None else PurePath(pakname).stem
52+
await asyncio.to_thread(fw.extract, dest / name, args.force)
53+
fw.close()
5454

5555

5656
def main() -> None:

0 commit comments

Comments
 (0)