66 lines
2.0 KiB
Python
66 lines
2.0 KiB
Python
from typing import Generator, List, Tuple
|
|
from os import listdir, path
|
|
from .file import FILE
|
|
|
|
|
|
def search(pathlist: List[str]) -> Generator[FILE, None, None]:
|
|
"""
|
|
Generate file paths from given list of Paths.
|
|
|
|
+------------+
|
|
| Parameters |
|
|
+------------+
|
|
| pathlist: List[str]
|
|
| List of files and directories.
|
|
|
|
+--------+
|
|
| Yields |
|
|
---------+
|
|
| file: Tuple[(None, str)]
|
|
| file is a Tuple containing:
|
|
| - a files path string
|
|
| - a crc32 sum (None if unknown)
|
|
"""
|
|
for fpath in pathlist:
|
|
if path.isfile(fpath):
|
|
if len(fpath) > 4 and fpath[-4:] == ".sfv":
|
|
yield from sfv_read(fpath)
|
|
else:
|
|
yield FILE(path.realpath(fpath))
|
|
continue
|
|
if path.isdir(fpath):
|
|
yield from search([path.join(fpath, x) for x in listdir(fpath)])
|
|
continue
|
|
print(f"[WARN]: No such file or directory: {fpath}")
|
|
|
|
|
|
def sfv_read(filename: str) -> Generator[FILE, None, None]:
|
|
"""
|
|
Read sfv file.
|
|
"""
|
|
try:
|
|
with open(filename, 'r') as file:
|
|
yield from (FILE(' '.join(x.split()[:-1]), x.split()[-1])
|
|
for x in file.read().split('\n')
|
|
if len(x) != 0 and x[0] != ';')
|
|
except UnicodeDecodeError:
|
|
print(f"[ERR]: {filename} is not a text file.")
|
|
except FileNotFoundError:
|
|
print(f"[WARN]: No such file or directory: {filename}")
|
|
|
|
|
|
def sfv_write(checked_files: List[Tuple[str, str, bool]], filename: str) -> None:
|
|
"""
|
|
Write sfv file.
|
|
"""
|
|
try:
|
|
with open(filename, 'w') as file:
|
|
checked_files.sort()
|
|
if any([not x[2] for x in checked_files]):
|
|
print(f"[WARN]: {filename} will contain unverified checksums.")
|
|
# [file.write(f"{str(x[0])}\t{str(x[1])}") for x in checked_files]
|
|
|
|
file.write('\n'.join([x for x in ["hello" "world"]]))
|
|
except FileExistsError:
|
|
pass
|