Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _converted_file(self, file, to_format, dest_dir):
basename = path.basename(filename_basename(file))
new_file = path.join(dest_dir, basename + '.handprint.' + to_format)
if path.exists(new_file):
inform('Using existing converted image in {}', relative(new_file))
return new_file
else:
inform('Converting to {} format: {}', to_format, relative(file))
(converted, error) = converted_image(file, to_format, new_file)
if error:
alert('Failed to convert {}: {}', relative(file), error)
return None
return converted
def _smaller_file(self, file):
if not file:
return None
file_ext = filename_extension(file)
name_tail = '.handprint' + file_ext
new_file = file if name_tail in file else filename_basename(file) + name_tail
if path.exists(new_file):
if image_size(new_file) < self._max_size:
inform('Reusing resized image found in {}', relative(new_file))
return new_file
else:
# We found a ".handprint.ext" file, perhaps from a previous run,
# but for the current set of services, it's larger than allowed.
if __debug__: log('existing resized file larger than {}b: {}',
humanize.intcomma(self._max_size), new_file)
inform('Size too large; reducing size: {}', relative(file))
(resized, error) = reduced_image_size(file, new_file, self._max_size)
if error:
alert('Failed to resize {}: {}', relative(file), error)
return None
return resized
def _save(self, result, file):
# First perform some sanity checks.
if result is None:
warn('No data for {}', file)
return
if isinstance(result, tuple):
# Assumes 2 elements: data, and error
(data, error) = result
if error:
alert('Error: {}', error)
warn('Unable to write {}', file)
return
else:
result = data
if __debug__: log('writing output to file {}', relative(file))
if isinstance(result, str):
with open(file, 'w') as f:
f.write(result)
elif isinstance(result, io.BytesIO):
with open(file, 'wb') as f:
shutil.copyfileobj(result, f)
else:
# There's no other type in the code, so if we get here ...
raise InternalError('Unexpected data in save_output() -- please report this.')
self._save(output.text, txt_file)
if self._compare:
gt_file = alt_extension(image.item_file, 'gt.txt')
report_path = self._renamed(image.item_file, str(service), 'tsv')
relaxed = (self._compare == 'relaxed')
if readable(gt_file) and nonempty(gt_file):
if __debug__: log('reading ground truth from {}', gt_file)
gt_text = open(gt_file, 'r').read()
inform('Saving {} comparison to ground truth', service_name)
self._save(text_comparison(output.text, gt_text, relaxed), report_path)
elif not nonempty(gt_file):
warn('Skipping {} comparison because {} is empty',
service_name, relative(gt_file))
else:
warn('Skipping {} comparison because {} not available',
service_name, relative(gt_file))
return Result(service, image, annot_path, report_path)
if not output_dir:
output_dir = os.getcwd()
file = path.realpath(path.join(output_dir, base + '.' + orig_fmt))
if not download_file(item, file):
warn('Unable to download {}', item)
return (None, None)
url_file = path.realpath(path.join(output_dir, base + '.url'))
with open(url_file, 'w') as f:
f.write(url_file_content(item))
inform('Wrote URL to {}', styled(relative(url_file), 'white_on_gray'))
else:
file = path.realpath(path.join(os.getcwd(), item))
orig_fmt = filename_extension(file)[1:]
if not path.getsize(file) > 0:
warn('File has zero length: {}', relative(file))
return (None, None)
if __debug__: log('{} has original format {}', relative(file), orig_fmt)
return (file, orig_fmt)