From 06b282e7cd9293a121a99a9570e3eb6309d4d7c6 Mon Sep 17 00:00:00 2001 From: derped Date: Sat, 23 Nov 2024 23:32:57 +0100 Subject: [PATCH] Update broken/incomplete code to match type interfaces --- fck/fileutils.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/fck/fileutils.py b/fck/fileutils.py index 6318ede..c8c961c 100644 --- a/fck/fileutils.py +++ b/fck/fileutils.py @@ -1,5 +1,6 @@ -from typing import Generator, List, Tuple from os import listdir, path +from typing import Generator, List + from .file import FILE @@ -37,32 +38,47 @@ def search(pathlist: List[str]) -> Generator[str, None, None]: print(f"[WARN]: No such file or directory: {fpath}") -def sfv_read(filename: str) -> Generator[FILE, None, None]: +def sfv_read(filename: str) -> Generator[str, 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] != ';') + with open(filename, "r") as file: + yield from ( + " ".join(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: +def file_write(outpath: str, files: List[FILE]) -> bool: + try: + with open(outpath, "w") as file: + files.sort() + # add csum checks here + [file.write(x.mkFileStr()) for x in files] + except FileExistsError: + return False + return True + + +def sfv_write(checked_files: List[FILE], filename: str) -> None: """ Write sfv file. """ try: - with open(filename, 'w') as file: + with open(filename, "w") as cfile: 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] + for file in checked_files: + if repr(file.csum) == file.csum.NULL: + print(f"Skipping: {file!r} because no checksum was generated") + else: + cfile.write(f"{file!r}\t{file.csum!r}") - file.write('\n'.join([x for x in ["hello" "world"]])) + # file.write('\n'.join([x for x in ["hello" "world"]])) except FileExistsError: pass