Exemplo n.º 1
0
def _execute_filter_image(image: Image, file_path: str,
                          cache_directory_path: str,
                          destination_directory_path: str,
                          destination_name: str, width: int,
                          height: int) -> None:
    makedirs(destination_directory_path)
    cache_file_path = join(cache_directory_path,
                           '%s-%s' % (hashfile(file_path), destination_name))
    destination_file_path = join(destination_directory_path, destination_name)

    try:
        link_or_copy(cache_file_path, destination_file_path)
    except FileNotFoundError:
        makedirs(cache_directory_path)
        with image:
            if width is not None:
                width = min(width, image.width)
            if height is not None:
                height = min(height, image.height)

            if width is None:
                size = height
                convert = resizeimage.resize_height
            elif height is None:
                size = width
                convert = resizeimage.resize_width
            else:
                size = (width, height)
                convert = resizeimage.resize_cover
            convert(image, size).save(cache_file_path)
        makedirs(destination_directory_path)
        link_or_copy(cache_file_path, destination_file_path)
Exemplo n.º 2
0
def _execute_filter_image(image: Image, file_path: Path,
                          cache_directory_path: Path,
                          destination_directory_path: Path,
                          destination_name: str, width: int,
                          height: int) -> None:
    destination_directory_path.mkdir(exist_ok=True, parents=True)
    cache_file_path = cache_directory_path / (
        '%s-%s' % (hashfile(file_path), destination_name))
    destination_file_path = destination_directory_path / destination_name

    try:
        link_or_copy(cache_file_path, destination_file_path)
    except FileNotFoundError:
        cache_directory_path.mkdir(exist_ok=True, parents=True)
        with image:
            if width is not None:
                width = min(width, image.width)
            if height is not None:
                height = min(height, image.height)

            if width is None:
                size = height
                convert = resizeimage.resize_height
            elif height is None:
                size = width
                convert = resizeimage.resize_width
            else:
                size = (width, height)
                convert = resizeimage.resize_cover
            convert(image, size).save(cache_file_path)
        destination_directory_path.mkdir(exist_ok=True, parents=True)
        link_or_copy(cache_file_path, destination_file_path)
Exemplo n.º 3
0
 def test(self):
     with TemporaryDirectory() as working_directory_path:
         content = 'I will say zis only once.'
         source_path = path.join(working_directory_path, 'source')
         destination_path = path.join(working_directory_path, 'destination')
         with open(source_path, 'a') as f:
             f.write(content)
         link_or_copy(source_path, destination_path)
         with open(destination_path) as f:
             self.assertEqual(content, f.read())
Exemplo n.º 4
0
 def test_with_os_error(self, m_link):
     m_link.side_effect = OSError
     with TemporaryDirectory() as working_directory_path_str:
         working_directory_path = Path(working_directory_path_str)
         content = 'I will say zis only once.'
         source_path = working_directory_path / 'source'
         destination_path = working_directory_path / 'destination'
         with open(source_path, 'a') as f:
             f.write(content)
         link_or_copy(source_path, destination_path)
         with open(destination_path) as f:
             self.assertEqual(content, f.read())
Exemplo n.º 5
0
def _do_filter_file(file_path: str, destination_directory_path: str,
                    destination_name: str) -> None:
    makedirs(destination_directory_path)
    destination_file_path = os.path.join(destination_directory_path,
                                         destination_name)
    link_or_copy(file_path, destination_file_path)
Exemplo n.º 6
0
def _do_filter_file(file_source_path: Path,
                    file_destination_path: Path) -> None:
    file_destination_path.parent.mkdir(exist_ok=True, parents=True)
    link_or_copy(file_source_path, file_destination_path)