# # CatNet Python Server # # Copyright (C) 2023-2024 John Solntsev # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . # import logging import os log = logging.getLogger(__name__) def file_iterator(filename): log.debug("Try access to file \"%s\"", filename) if not os.path.isfile(filename): raise FileNotFoundError("File \"%s\" not found", filename) log.debug("Get size of file \"%s\"", filename) yield os.path.getsize(filename) log.debug("Open file \"%s\" to byte-read", filename) with open(filename, "rb") as f: while True: file_data = f.read(1024) yield file_data if len(file_data) >= 1024: log.debug("File size greeter 1024 bytes, yield data. Continue to read") else: log.debug("File size lower 1024 bytes, yield data. Break to read") break