Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
service = add_creds.lower()
if service not in services_list():
alert('Unknown service: "{}". {}', service, hint)
exit(1)
if not files or len(files) > 1:
alert('Option {}a requires one file. {}', prefix, hint)
exit(1)
creds_file = files[0]
if not readable(creds_file):
alert('File not readable: {}', creds_file)
exit(1)
Credentials.save_credentials(service, creds_file)
inform('Saved credentials for service "{}".', service)
exit(0)
if no_grid and not extended and not compare:
alert('{0}G without {0}e or {0}c produces no output. {1}', prefix, hint)
exit(1)
if any(item.startswith('-') for item in files):
alert('Unrecognized option in arguments. {}', hint)
exit(1)
if not files and from_file == 'F':
alert('Need images or URLs to have something to do. {}', hint)
exit(1)
if relaxed and not compare:
warn('Option {0}r without {0}c has no effect. {1}', prefix, hint)
services = services_list() if services == 'S' else services.lower().split(',')
if not all(s in services_list() for s in services):
alert('"{}" is not a known services. {}', services, hint)
exit(1)
base_name = 'document' if base_name == 'B' else base_name
compare = 'relaxed' if (compare and relaxed) else compare
threads = int(max(1, available_cpus()/2 if threads == 'T' else int(threads)))
# Do the real work --------------------------------------------------------
try:
body = MainBody(base_name, extended, from_file, output_dir, threads)
body.run(services, files, make_grid, compare)
except (KeyboardInterrupt, UserCancelled) as ex:
if __debug__: log('received {}', sys.exc_info()[0].__name__)
inform('Quitting.')
exit(0)
except Exception as ex:
if debugging:
import traceback
alert('{}\n{}', str(ex), traceback.format_exc())
import pdb; pdb.set_trace()
else:
alert(str(ex))
exit(2)
inform('Done.')
exit(1)
creds_file = files[0]
if not readable(creds_file):
alert('File not readable: {}', creds_file)
exit(1)
Credentials.save_credentials(service, creds_file)
inform('Saved credentials for service "{}".', service)
exit(0)
if no_grid and not extended and not compare:
alert('{0}G without {0}e or {0}c produces no output. {1}', prefix, hint)
exit(1)
if any(item.startswith('-') for item in files):
alert('Unrecognized option in arguments. {}', hint)
exit(1)
if not files and from_file == 'F':
alert('Need images or URLs to have something to do. {}', hint)
exit(1)
if relaxed and not compare:
warn('Option {0}r without {0}c has no effect. {1}', prefix, hint)
services = services_list() if services == 'S' else services.lower().split(',')
if not all(s in services_list() for s in services):
alert('"{}" is not a known services. {}', services, hint)
exit(1)
base_name = 'document' if base_name == 'B' else base_name
from_file = None if from_file == 'F' else from_file
output_dir = None if output_dir == 'O' else output_dir
compare = 'relaxed' if (compare and relaxed) else compare
threads = int(max(1, available_cpus()/2 if threads == 'T' else int(threads)))
# Do the real work --------------------------------------------------------
if version:
print_version()
exit(0)
if list:
inform('Known services: {}', ', '.join(services_list()))
exit(0)
print_intro(ui)
if add_creds != 'A':
service = add_creds.lower()
if service not in services_list():
alert('Unknown service: "{}". {}', service, hint)
exit(1)
if not files or len(files) > 1:
alert('Option {}a requires one file. {}', prefix, hint)
exit(1)
creds_file = files[0]
if not readable(creds_file):
alert('File not readable: {}', creds_file)
exit(1)
Credentials.save_credentials(service, creds_file)
inform('Saved credentials for service "{}".', service)
exit(0)
if no_grid and not extended and not compare:
alert('{0}G without {0}e or {0}c produces no output. {1}', prefix, hint)
exit(1)
if any(item.startswith('-') for item in files):
alert('Unrecognized option in arguments. {}', hint)
exit(1)
if not files and from_file == 'F':
alert('Need images or URLs to have something to do. {}', hint)
inform('Known services: {}', ', '.join(services_list()))
exit(0)
print_intro(ui)
if add_creds != 'A':
service = add_creds.lower()
if service not in services_list():
alert('Unknown service: "{}". {}', service, hint)
exit(1)
if not files or len(files) > 1:
alert('Option {}a requires one file. {}', prefix, hint)
exit(1)
creds_file = files[0]
if not readable(creds_file):
alert('File not readable: {}', creds_file)
exit(1)
Credentials.save_credentials(service, creds_file)
inform('Saved credentials for service "{}".', service)
exit(0)
if no_grid and not extended and not compare:
alert('{0}G without {0}e or {0}c produces no output. {1}', prefix, hint)
exit(1)
if any(item.startswith('-') for item in files):
alert('Unrecognized option in arguments. {}', hint)
exit(1)
if not files and from_file == 'F':
alert('Need images or URLs to have something to do. {}', hint)
exit(1)
if relaxed and not compare:
warn('Option {0}r without {0}c has no effect. {1}', prefix, hint)
exit(0)
if no_grid and not extended and not compare:
alert('{0}G without {0}e or {0}c produces no output. {1}', prefix, hint)
exit(1)
if any(item.startswith('-') for item in files):
alert('Unrecognized option in arguments. {}', hint)
exit(1)
if not files and from_file == 'F':
alert('Need images or URLs to have something to do. {}', hint)
exit(1)
if relaxed and not compare:
warn('Option {0}r without {0}c has no effect. {1}', prefix, hint)
services = services_list() if services == 'S' else services.lower().split(',')
if not all(s in services_list() for s in services):
alert('"{}" is not a known services. {}', services, hint)
exit(1)
base_name = 'document' if base_name == 'B' else base_name
from_file = None if from_file == 'F' else from_file
output_dir = None if output_dir == 'O' else output_dir
compare = 'relaxed' if (compare and relaxed) else compare
threads = int(max(1, available_cpus()/2 if threads == 'T' else int(threads)))
# Do the real work --------------------------------------------------------
try:
body = MainBody(base_name, extended, from_file, output_dir, threads)
body.run(services, files, make_grid, compare)
except (KeyboardInterrupt, UserCancelled) as ex:
if __debug__: log('received {}', sys.exc_info()[0].__name__)
inform('Quitting.')
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
'''Run all requested services on the image indicated by "item", using
"index" and "base_name" to construct a download copy of the item if
it has to be downloaded from a URL first.
'''
# Shortcuts to make the code more readable.
services = self._services
inform('Starting on {}', styled(item, 'white'))
try:
(item_file, item_fmt) = self._get(item, base_name, index)
if not item_file:
return
dest_dir = self._output_dir if self._output_dir else path.dirname(item_file)
if not writable(dest_dir):
alert('Cannot write output in {}.', dest_dir)
return
# Normalize input image to the lowest common denominator.
image = self._normalized(item, item_fmt, item_file, dest_dir)
if not image.file:
warn('Skipping {}', relative(item_file))
return
# Send the file to the services and get Result tuples back.
if self._num_threads == 1:
# For 1 thread, avoid thread pool to make debugging easier.
results = [self._send(image, s) for s in services]
else:
with ThreadPoolExecutor(max_workers = self._num_threads) as tpe:
results = list(tpe.map(self._send, repeat(image), iter(services)))
name_tail = '.handprint' + file_ext
new_file = file if name_tail in file else filename_basename(file) + name_tail
if path.exists(new_file) and readable(new_file):
(image_width, image_height) = image_dimensions(new_file)
if image_width < max_width and image_height < max_height:
inform('Using reduced image found in {}', relative(new_file))
return new_file
else:
# We found a "-reduced" file, perhaps from a previous run, but
# for the current set of services, dimension are too large.
if __debug__: log('existing resized file larger than {}x{}: {}',
max_width, max_height, new_file)
inform('Dimensions too large; reducing dimensions: {}', relative(file))
(resized, error) = reduced_image_dimensions(file, new_file, max_width, max_height)
if error:
alert('Failed to re-dimension {}: {}', 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.')