def fail(self, message = None): if self._colorize: self._spinner.fail(color(self._current_message, 'error', self._colorize)) self._spinner.stop() self.start(message) self._spinner.fail(color(message, 'error', self._colorize)) self._spinner.stop() else: msg('ERROR: ' + message)
def stop(self, message = None): if self._quiet: return if self._colorize: self._spinner.succeed(color(self._current_message, 'info', self._colorize)) self._spinner.stop() self.start(message) self._spinner.succeed(color(message, 'info', self._colorize)) self._spinner.stop() else: msg(message)
def warn(self, message=None): if self._quiet: return if self._colorize: self._spinner.succeed( color(self._current_message, 'info', self._colorize)) self._spinner.stop() self._spinner.warn(color(message, 'warn', self._colorize)) self._current_message = '' else: msg('WARNING: ' + message)
def start(self, message = None): if self._quiet: return if message is None: message = '' if self._colorize: text = color(message, 'info') self._spinner = Halo(spinner='bouncingBall', text = text) self._spinner.start() self._current_message = message else: msg(message)
def run(classes, item, index, base_name, output_dir, creds_dir, annotate, say): spinner = ProgressIndicator(say.use_color(), say.be_quiet()) try: spinner.start('Starting on {}'.format(relative(item))) if is_url(item): # Make sure the URLs point to images. if __debug__: log('Testing if URL contains an image: {}', item) try: response = request.urlopen(item) except Exception as err: if __debug__: log('Network access resulted in error: {}', str(err)) spinner.fail('Skipping URL due to error: {}'.format(err)) return if response.headers.get_content_maintype() != 'image': spinner.fail('Did not find an image at {}'.format(item)) return fmt = response.headers.get_content_subtype() base = '{}-{}'.format(base_name, index) file = path.realpath(path.join(output_dir, base + '.' + fmt)) error = download(item, file) if not error: spinner.update('Wrote contents to {}'.format(relative(file))) else: spinner.fail('Failed to download {}: {}'.format(item, error)) return url_file = path.realpath(path.join(output_dir, base + '.url')) with open(url_file, 'w') as f: f.write(url_file_content(item)) spinner.update('Wrote URL to {}'.format(relative(url_file))) else: file = path.realpath(path.join(os.getcwd(), item)) fmt = filename_extension(file) dest_dir = output_dir if output_dir else path.dirname(file) if not writable(dest_dir): say.fatal('Cannot write output in {}.'.format(dest_dir)) return # Iterate over the methods. for method_class in classes: method = method_class() method.init_credentials(creds_dir) last_time = timer() # If need to convert format, best do it after resizing original fmt. need_convert = fmt not in method.accepted_formats() # Test the dimensions, not bytes, because of compression. if image_dimensions(file) > method.max_dimensions(): file = file_after_resizing(file, method, spinner) if file and need_convert: file = file_after_converting(file, 'jpg', method, spinner) if not file: return spinner.update('Sending to {} {}'.format( color(method, 'white', say.use_color()), # Need explicit color research or colorization goes wrong. color('and waiting for response', 'info', say.use_color()))) try: result = method.result(file) except RateLimitExceeded as err: time_passed = timer() - last_time if time_passed < 1 / method.max_rate(): spinner.warn('Pausing due to rate limits') time.sleep(1 / method.max_rate() - time_passed) if result.error: spinner.fail(result.error) return file_name = path.basename(file) base_path = path.join(dest_dir, file_name) txt_file = alt_extension(base_path, str(method) + '.txt') json_file = alt_extension(base_path, str(method) + '.json') annot_file = alt_extension(base_path, str(method) + '.jpg') spinner.update('Text -> {}'.format(relative(txt_file))) save_output(result.text, txt_file) spinner.update('All data -> {}'.format(relative(json_file))) save_output(json.dumps(result.data), json_file) if annotate: spinner.update('Annotated image -> {}'.format( relative(annot_file))) save_output(annotated_image(file, result.boxes), annot_file) spinner.stop('Done with {}'.format(relative(item))) except (KeyboardInterrupt, UserCancelled) as err: spinner.warn('Interrupted') raise except AuthenticationFailure as err: spinner.fail('Unable to continue using {}: {}'.format(method, err)) return except Exception as err: spinner.fail(say.error_text('Stopping due to a problem')) raise