Esempio 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)
Esempio n. 2
0
def open_translations(
        locale: str,
        directory_path: Path) -> Optional[gettext.GNUTranslations]:
    locale_path_name = locale.replace('-', '_')
    po_file_path = directory_path / 'locale' / locale_path_name / 'LC_MESSAGES' / 'betty.po'
    try:
        translation_version = hashfile(po_file_path)
    except FileNotFoundError:
        return None
    translation_cache_directory_path = fs.CACHE_DIRECTORY_PATH / 'translations' / translation_version
    cache_directory_path = translation_cache_directory_path / locale_path_name / 'LC_MESSAGES'
    mo_file_path = cache_directory_path / 'betty.mo'

    with suppress(FileNotFoundError):
        with open(mo_file_path, 'rb') as f:
            return gettext.GNUTranslations(f)

    cache_directory_path.mkdir(exist_ok=True, parents=True)
    with suppress(FileExistsError):
        shutil.copyfile(po_file_path, cache_directory_path / 'betty.po')

    with contextlib.redirect_stdout(StringIO()):
        CommandLineInterface().run([
            '', 'compile', '-d', translation_cache_directory_path, '-l',
            locale_path_name, '-D', 'betty'
        ])
    with open(mo_file_path, 'rb') as f:
        return gettext.GNUTranslations(f)
Esempio n. 3
0
def open_translations(
        locale: str, directory_path: str) -> Optional[gettext.GNUTranslations]:
    locale_path_name = locale.replace('-', '_')
    po_file_path = path.join(directory_path, 'locale', locale_path_name,
                             'LC_MESSAGES', 'betty.po')
    try:
        translation_version = hashfile(po_file_path)
    except FileNotFoundError:
        return None
    translation_cache_directory_path = path.join(_CACHE_DIRECTORY_PATH,
                                                 'translations',
                                                 translation_version)
    cache_directory_path = path.join(translation_cache_directory_path,
                                     locale_path_name, 'LC_MESSAGES')
    mo_file_path = path.join(cache_directory_path, 'betty.mo')

    with suppress(FileNotFoundError):
        with open(mo_file_path, 'rb') as f:
            return gettext.GNUTranslations(f)

    makedirs(cache_directory_path, exist_ok=True)
    with suppress(FileExistsError):
        shutil.copyfile(po_file_path,
                        path.join(cache_directory_path, 'betty.po'))

    with contextlib.redirect_stdout(StringIO()):
        CommandLineInterface().run([
            '', 'compile', '-d', translation_cache_directory_path, '-l',
            locale_path_name, '-D', 'betty'
        ])
    with open(mo_file_path, 'rb') as f:
        return gettext.GNUTranslations(f)
Esempio n. 4
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)
Esempio n. 5
0
def _filter_image(site: Site,
                  file: File,
                  width: Optional[int] = None,
                  height: Optional[int] = None) -> str:
    if width is None and height is None:
        raise ValueError('At least the width or height must be given.')

    with Image.open(file.path) as 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
            suffix = '-x%d'
            convert = resizeimage.resize_height
        elif height is None:
            size = width
            suffix = '%dx-'
            convert = resizeimage.resize_width
        else:
            size = (width, height)
            suffix = '%dx%d'
            convert = resizeimage.resize_cover

        file_directory_path = os.path.join(
            site.configuration.www_directory_path, 'file')
        destination_name = '%s-%s.%s' % (file.id, suffix % size,
                                         file.extension)
        destination_path = '/file/%s' % destination_name
        cache_directory_path = join(site.configuration.cache_directory_path,
                                    'image')
        cache_file_path = join(
            cache_directory_path,
            '%s-%s' % (hashfile(file.path), destination_name))
        output_file_path = join(file_directory_path, destination_name)

        try:
            os.link(cache_file_path, output_file_path)
        except FileExistsError:
            pass
        except FileNotFoundError:
            if exists(output_file_path):
                return destination_path
            makedirs(cache_directory_path)
            convert(image, size).save(cache_file_path)
            makedirs(file_directory_path)
            os.link(cache_file_path, output_file_path)

    return destination_path
Esempio n. 6
0
def _do_filter_image(file_path: str, cache_directory_path: str,
                     destination_directory_path: str, destination_name: str,
                     convert: Callable, size: Tuple[int, 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:
        os.link(cache_file_path, destination_file_path)
    except FileNotFoundError:
        makedirs(cache_directory_path)
        with Image.open(file_path) as image:
            convert(image, size).save(cache_file_path)
        makedirs(destination_directory_path)
        os.link(cache_file_path, destination_file_path)
Esempio n. 7
0
 def test_hashfile_with_different_files(self):
     file_path_1 = Path(__file__).parents[1] / 'assets' / 'public' / 'static' / 'betty-16x16.png'
     file_path_2 = Path(__file__).parents[1] / 'assets' / 'public' / 'static' / 'betty-512x512.png'
     self.assertNotEquals(hashfile(file_path_1), hashfile(file_path_2))
Esempio n. 8
0
 def test_hashfile_with_identical_file(self):
     file_path = Path(__file__).parents[1] / 'assets' / 'public' / 'static' / 'betty-16x16.png'
     self.assertEquals(hashfile(file_path), hashfile(file_path))
Esempio n. 9
0
 def test_hashfile_with_different_files(self):
     file_path_1 = join(dirname(dirname(__file__)), 'assets', 'public',
                        'static', 'betty-16x16.png')
     file_path_2 = join(dirname(dirname(__file__)), 'assets', 'public',
                        'static', 'betty-512x512.png')
     self.assertNotEquals(hashfile(file_path_1), hashfile(file_path_2))
Esempio n. 10
0
 def test_hashfile_with_identical_file(self):
     file_path = join(dirname(dirname(__file__)), 'assets', 'public',
                      'static', 'betty-16x16.png')
     self.assertEquals(hashfile(file_path), hashfile(file_path))
Esempio n. 11
0
 def test_hashfile_with_different_files(self):
     file_path_1 = join(dirname(dirname(__file__)),
                        'resources/public/betty-16x16.png')
     file_path_2 = join(dirname(dirname(__file__)),
                        'resources/public/betty-512x512.png')
     self.assertNotEquals(hashfile(file_path_1), hashfile(file_path_2))
Esempio n. 12
0
 def test_hashfile_with_identical_file(self):
     file_path = join(dirname(dirname(__file__)),
                      'resources/public/betty-16x16.png')
     self.assertEquals(hashfile(file_path), hashfile(file_path))