Coverage for functions \ flipdare \ util \ scaled_image.py: 91%

34 statements  

« prev     ^ index     » next       coverage.py v7.13.0, created at 2026-05-08 12:22 +1000

1#!/usr/bin/env python 

2# Copyright (c) 2026 Flipdare Pty Ltd. All rights reserved. 

3# 

4# This file is part of Flipdare's proprietary software and contains 

5# confidential and copyrighted material. Unauthorised copying, 

6# modification, distribution, or use of this file is strictly 

7# prohibited without prior written permission from Flipdare Pty Ltd. 

8# 

9# This software includes third-party components licensed under MIT, 

10# BSD, and Apache 2.0 licences. See THIRD_PARTY_NOTICES for details. 

11# 

12 

13 

14from __future__ import annotations 

15 

16from enum import Enum 

17from typing import TYPE_CHECKING 

18 

19if TYPE_CHECKING: 

20 

21 from flipdare.generated.model.internal.image_model import ImageModel 

22 

23 

24__all__ = ["ScaledImageType", "ScaledImage"] 

25 

26 

27class ScaledImageType(Enum): 

28 EMAIL = "email" 

29 THUMBNAIL = "thumbnail" 

30 NONE = "none" 

31 

32 @property 

33 def scaled_width(self) -> float | None: 

34 if self == ScaledImageType.EMAIL: 

35 return 96.0 

36 if self == ScaledImageType.THUMBNAIL: 

37 return 124.0 

38 # self == ScaledImageType.NONE: 

39 return None 

40 

41 

42class ScaledImage: 

43 def __init__(self, model: ImageModel) -> None: 

44 self.model = model 

45 

46 def scaled_width(self, scale_type: ScaledImageType) -> int: 

47 return self.scale(scale_type)[0] 

48 

49 def scaled_height(self, scale_type: ScaledImageType) -> int: 

50 return self.scale(scale_type)[1] 

51 

52 def scale(self, scale_type: ScaledImageType) -> tuple[int, int]: 

53 width = self.model.w 

54 height = self.model.h 

55 

56 if width == 0 or height == 0: 

57 return 0, 0 

58 

59 if scale_type == ScaledImageType.NONE: 

60 return width, height 

61 

62 scaled_width: float | None = scale_type.scaled_width 

63 if scaled_width is None: 

64 return width, height 

65 

66 scaled_height = (scaled_width / width) * height 

67 # convert both to int 

68 return int(scaled_width), int(scaled_height)