added in type hinting, removed unnecessary size calc methods and added bits of logging

This commit is contained in:
Xevion
2019-11-01 22:44:29 -05:00
parent 9d56c5ebfc
commit a6bfd8daef
2 changed files with 8 additions and 13 deletions

View File

@@ -44,9 +44,10 @@ def run():
file = FileProcessor(file)
file.run(client)
except:
logging.warning('Removing temporary directory before raising exception.')
os.rmdir(TEMP_PATH)
raise
# Remove the directory, we are done here
print('Cleaning up temporary directory...')
logging.info('Removing temporary directory.')
os.rmdir(TEMP_PATH)

View File

@@ -13,7 +13,7 @@ from . import TEMP_PATH, INPUT_PATH, OUTPUT_PATH
from .xmp import XMPParser
class FileProcessor(object):
def __init__(self, file_name):
def __init__(self, file_name : str):
self.file_name = file_name
self.base, self.ext = os.path.splitext(self.file_name) # fileNAME and fileEXTENSIOn
# Path to temporary file that will be optimized for upload to Google
@@ -24,13 +24,13 @@ class FileProcessor(object):
return self.ext.lower() == 'xmp'
# Optimizes a file using JPEG thumbnailing and compression.
def _optimize(self, file_path, size=(512, 512), quality=85, copy=None):
image = Image.open(file_path)
def _optimize(self, file : str, size : tuple =(512, 512), quality=85, copy=None):
image = Image.open(file)
image.thumbnail(size, resample=Image.ANTIALIAS)
if copy:
image.save(copy, format='jpeg', optimize=True, quality=quality)
else:
image.save(file_path, format='jpeg', optimize=True, quality=quality)
image.save(file, format='jpeg', optimize=True, quality=quality)
def optimize(self):
if self.hasXMP:
@@ -42,7 +42,7 @@ class FileProcessor(object):
else:
self._optimize(os.path.join(INPUT_PATH, self.file_name), copy=self.temp_file_path)
def run(self, client):
def run(self, client : vision.ImageAnnotatorClient):
try:
if self.hasXMP:
self.optimize()
@@ -87,10 +87,4 @@ class FileProcessor(object):
# Remove the temporary file (if it exists)
def _cleanup(self):
if os.path.exists(self.temp_file_path):
os.remove(self.temp_file_path)
# Get the size of the file. Is concerned with filesize type. 1024KiB -> 1MiB
def _size(self, file_path):
size, type = os.path.getsize(file_path) / 1024, 'KiB'
if size >= 1024: size /= 1024; type = 'MiB'
return round(size, 2), type
os.remove(self.temp_file_path)