How to use the handprint.files.readable function in handprint

To help you get started, we’ve selected a few handprint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github caltechlibrary / handprint / handprint / __main__.py View on Github external
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)
        exit(1)
    if relaxed and not compare:
        warn('Option {0}r without {0}c has no effect. {1}', prefix, hint)
github caltechlibrary / handprint / handprint / credentials / microsoft_auth.py View on Github external
def __init__(self):
        cfile = path.join(self.credentials_dir(), credentials_filename('microsoft'))
        if __debug__: log('credentials file for microsoft is {}', cfile)
        if not path.exists(cfile):
            raise AuthFailure('Credentials for Microsoft have not been installed')
        elif not readable(cfile):
            raise AuthFailure('Microsoft credentials file unreadable: {}'.format(cfile))

        try:
            with open(cfile, 'r') as file:
                creds = json.load(file)
                self.credentials = creds['subscription_key']
        except Exception as ex:
            raise AuthFailure(
                'Unable to parse Microsoft exceptions file: {}'.format(str(ex)))
github caltechlibrary / handprint / handprint / images.py View on Github external
def image_size(file):
    '''Returns the size of the image in 'file', in units of bytes.'''
    if not file or not readable(file):
        return 0
    return path.getsize(file)
github caltechlibrary / handprint / handprint / manager.py View on Github external
annot_path = self._renamed(base_path, str(service), 'png')
            inform('Creating annotated image for {}.', service_name)
            with self._lock:
                self._save(annotated_image(image.file, output.boxes, service), annot_path)
        if self._extended_results:
            txt_file  = self._renamed(base_path, str(service), 'txt')
            json_file = self._renamed(base_path, str(service), 'json')
            inform('Saving all data for {}.', service_name)
            self._save(json.dumps(output.data), json_file)
            inform('Saving extracted text for {}.', service_name)
            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)
github caltechlibrary / handprint / handprint / manager.py View on Github external
def _resized_image(self, file):
        (max_width, max_height) = self._max_dimensions
        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) 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
github caltechlibrary / handprint / handprint / credentials / google_auth.py View on Github external
def __init__(self):
        cfile = path.join(self.credentials_dir(), credentials_filename('google'))
        if __debug__: log('credentials file for google is {}', cfile)
        if not path.exists(cfile):
            raise AuthFailure('Credentials for Google have not been installed')
        elif not readable(cfile):
            raise AuthFailure('Google credentials file unreadable: {}'.format(cfile))

        # Haven't been able to make it work; only the environment variable
        # approach has been working for me.
        #
        # with open(self.credentials_file, 'r') as file:
        #     self.credentials = json.load(file)
        os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = cfile
github caltechlibrary / handprint / handprint / main_body.py View on Github external
def __init__(self, base_name, extended, from_file, output_dir, threads):
        '''Initialize internal state and prepare for running services.'''

        if not network_available():
            raise ServiceFailure('No network.')

        if from_file:
            if not path.exists(from_file):
                raise RuntimeError('File not found: {}'.format(from_file))
            if not readable(from_file):
                raise RuntimeError('File not readable: {}'.format(from_file))

        if output_dir:
            if path.isdir(output_dir):
                if not writable(output_dir):
                    raise RuntimeError('Directory not writable: {}'.format(output_dir))
            else:
                os.mkdir(output_dir)
                if __debug__: log('created output_dir directory {}', output_dir)

        self._base_name  = base_name
        self._extended   = extended
        self._from_file  = from_file
        self._output_dir = output_dir
        self._threads    = threads
github caltechlibrary / handprint / handprint / credentials / amazon_auth.py View on Github external
def __init__(self):
        cfile = path.join(self.credentials_dir(), credentials_filename('amazon'))
        if __debug__: log('credentials file for amazon is {}', cfile)
        if not path.exists(cfile):
            raise AuthFailure('Credentials for Amazon have not been installed')
        elif not readable(cfile):
            raise AuthFailure('Amazon credentials file unreadable: {}'.format(cfile))

        try:
            with open(cfile, 'r') as file:
                self.credentials = json.load(file)
        except Exception as ex:
            raise AuthFailure(
                'Unable to parse Amazon exceptions file: {}'.format(str(ex)))