implement precheck function, fix regex for zero or two plus spacing in data size configuration

This commit is contained in:
Xevion
2020-08-28 11:00:31 -05:00
parent 06b6bead23
commit bb88a218fd
2 changed files with 25 additions and 3 deletions

View File

@@ -72,5 +72,5 @@ def convert_to_bytes(display_size: str) -> int:
:param display_size: A string representation of data size, a integer followed by 1-2 letters indicating unit.
:return: The number of bytes the given string is equivalent to.
"""
match = re.match(r"(\d+) (\w{1,2})", display_size)
match = re.match(r"(\d+)\s*(\w{1,2})", display_size)
return int(match.group(1)) * byte_magnitudes[match.group(2)]

View File

@@ -8,6 +8,7 @@ import io
import logging
import os
import shutil
from threading import Thread
from typing import Tuple, AnyStr, Optional, List, Dict
import imageio
@@ -17,6 +18,7 @@ from PIL import Image
from google.cloud import vision
from . import TEMP_PATH, INPUT_PATH, RAW_EXTS
from .config import config
from .xmp import XMPParser
log = logging.getLogger("process")
@@ -40,19 +42,39 @@ class MasterFileProcessor(object):
self.buffer_size, self.single_override = buffer_size, single_override
self.waiting: Dict[int, FileProcessor] = {} # FileProcessors that are ready to process, but are not.
self.running: Dict[int, FileProcessor] = {} # FileProcessors that are currently being processed in threads.
self.running: Dict[int, Tuple[FileProcessor, Thread]] = {} # FileProcessors that are currently being processed in threads.
self.finished: Dict[int, FileProcessor] = {} # FileProcessors that have finished processing.
processors = [FileProcessor(file) for file in files]
processors.sort(key=lambda fp: fp.size)
for index, fp in enumerate(processors):
self.waiting[index] = fp
self._precheck()
def _precheck(self) -> None:
"""
Checks that the MasterFileProcessor can successfully process all files with the current configuration options.
:except Exception: when the current configuration will be unable to complete based on the current parameters.
"""
# single_override ensures that the application will always complete, even if slowly, one-by-one
if not self.single_override:
# Check that all files are under the set buffer limit
for key, fp in self.waiting.keys():
if fp.size > self.buffer_size:
raise Exception("Invalid Configuration - the buffer size is too low. Please raise the buffer size "
"or enable single_override.")
# Check that image_count is at least 1
if self.image_count <= 0:
raise Exception("Invalid Configuration - the image_count is too low. Please set it to a positive "
"non-zero integer or enable single_override.")
def load(self) -> None:
"""
Starts FileProcessor threads, loading one or more threads simultaneously based on configuration options.
Starts FileProcessor threads, loading zero or more threads simultaneously based on configuration options.
"""
def _finished(self, key: int) -> None: