How to use the handprint.files.filename_extension 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 / 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)
github caltechlibrary / handprint / handprint / manager.py View on Github external
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
github caltechlibrary / handprint / handprint / main_body.py View on Github external
# 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).
        targets = list(targets)
        keep = []
        for item in targets:
            ext  = filename_extension(item)
            base = filename_basename(item)
            if ext != _OUTPUT_EXT and (base + _OUTPUT_EXT in targets):
                # png version of file is also present => skip this other version
                continue
            keep.append(item)
        return keep
github caltechlibrary / handprint / handprint / manager.py View on Github external
# 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.
            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)
github caltechlibrary / handprint / handprint / main_body.py View on Github external
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).
        targets = list(targets)
        keep = []
github caltechlibrary / handprint / handprint / images.py View on Github external
def converted_image(orig_file, to_format, dest_file = None):
    '''Returns a tuple of (success, output file, error message).
    Returns a tuple of (new_file, error).  The value of 'error' will be None
    if no error occurred; otherwise, the value will be a string summarizing the
    error that occurred and 'new_file' will be set to None.
    '''
    dest_format = canonical_format_name(to_format)
    if dest_file is None:
        dest_file = filename_basename(file) + '.' + dest_format
    # PIL is unable to read PDF files, so in that particular case, we have to
    # convert it using another tool.
    if filename_extension(orig_file) == '.pdf':
        import fitz
        doc = fitz.open(orig_file)
        if len(doc) >= 1:
            if len(doc) >= 2:
                if __debug__: log('{} has > 1 images; using only 1st', orig_file)
            # FIXME: if there's more than 1 image, we could extra the rest.
            # Doing so will require some architectural changes first.
            if __debug__: log('extracting 1st image from {}', dest_file)
            page = doc[0]
            pix = page.getPixmap(alpha = False)
            if __debug__: log('writing {}', dest_file)
            pix.writeImage(dest_file, dest_format)
            return (dest_file, None)
        else:
            if __debug__: log('fitz says there is no image image in {}', orig_file)
            return (None, 'Cannot find an image inside {}'.format(orig_file))