Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _get(self, item, base_name, index):
# Shortcuts to make the code more readable.
output_dir = self._output_dir
# For URLs, we download the corresponding files and name them with
# the base_name.
if is_url(item):
# First make sure the URL actually points to an image.
if __debug__: log('testing if URL contains an image: {}', item)
try:
response = urllib.request.urlopen(item)
except Exception as ex:
warn('Skipping URL due to error: {}', ex)
return (None, None)
if response.headers.get_content_maintype() != 'image':
warn('Did not find an image at {}', item)
return (None, None)
orig_fmt = response.headers.get_content_subtype()
base = '{}-{}'.format(base_name, index)
# If we weren't given an output dir, then for URLs, we have no
# choice but to use the current dir to download the file.
# Important: don't change self._output_dir because if other
# inputs *are* files, then those files will need other output dirs.
def targets_from_arguments(self, files, from_file):
targets = []
if from_file:
if __debug__: log('reading {}', from_file)
targets = filter(None, open(from_file).read().splitlines())
else:
for item in files:
if is_url(item):
targets.append(item)
elif path.isfile(item) and filename_extension(item) in ACCEPTED_FORMATS:
targets.append(item)
elif path.isdir(item):
# It's a directory, so look for files within.
targets += files_in_directory(item, extensions = ACCEPTED_FORMATS)
else:
warn('"{}" not a file or directory', item)
# Filter files created in past runs.
targets = filter(lambda name: '.handprint' not in name, targets)
# If there is both a file in the format we generate and another
# format of that file, ignore the other formats and just use ours.
# Note: the value of targets is an iterator, but b/c it's tested inside
# the loop, a separate list is needed (else get unexpected results).