Skip to content

Keys

S3Key

Base class for s3 keys.

Source code in saritasa_s3_tools/keys.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class S3Key:
    """Base class for s3 keys."""

    uuid_regex = r"[\d|\w]{8}-[\d|\w]{4}-[\d|\w]{4}-[\d|\w]{4}-[\d|\w]{12}"

    @abc.abstractmethod
    def __call__(self, filename: str | None) -> str:
        """Abstract method for calling keys."""

    def remove_special_characters(self, filename: str) -> str:
        """Remove characters from filename that are not allowed in some OS."""
        special_characters = r"<>:\"/\\|?*"
        return filename.translate({ord(i): None for i in special_characters})

    def normalize_string_value(self, value: str) -> str:
        """Normalize string value.

        1. Remove leading and trailing whitespaces.
        2. Replace all space characters with the Space char.
        3. Normalize Unicode string using `NFKC` form. See the details:
        https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize

        """
        cleaned = " ".join(value.strip().split()).strip()
        return unicodedata.normalize("NFKC", cleaned)

    def clean_filename(self, filename: str) -> str:
        """Remove `garbage` characters that cause problems with file names."""
        cleaned = self.remove_special_characters(filename)
        normalized = self.normalize_string_value(cleaned)

        return normalized

    def get_random_filename(self, filename: str) -> str:
        """Get random filename.

        Generation random filename that contains unique identifier and
        filename extension like: ``photo.jpg``.

        Args:
        ----
            filename (str): Name of file.

        Returns:
        -------
            new_filename (str): ``9841422d-c041-45a5-b7b3-467179f4f127.ext``.

        """
        path = str(uuid.uuid4())
        ext = pathlib.Path(filename).suffix.lower()

        return f"{path}{ext}"

    def validate(self, key: str) -> bool:
        """Check that input key is matching Key pattern."""
        return True  # pragma: no cover

__call__(filename) abstractmethod

Abstract method for calling keys.

Source code in saritasa_s3_tools/keys.py
13
14
15
@abc.abstractmethod
def __call__(self, filename: str | None) -> str:
    """Abstract method for calling keys."""

clean_filename(filename)

Remove garbage characters that cause problems with file names.

Source code in saritasa_s3_tools/keys.py
34
35
36
37
38
39
def clean_filename(self, filename: str) -> str:
    """Remove `garbage` characters that cause problems with file names."""
    cleaned = self.remove_special_characters(filename)
    normalized = self.normalize_string_value(cleaned)

    return normalized

get_random_filename(filename)

Get random filename.

Generation random filename that contains unique identifier and filename extension like: photo.jpg.


filename (str): Name of file.

new_filename (str): ``9841422d-c041-45a5-b7b3-467179f4f127.ext``.
Source code in saritasa_s3_tools/keys.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def get_random_filename(self, filename: str) -> str:
    """Get random filename.

    Generation random filename that contains unique identifier and
    filename extension like: ``photo.jpg``.

    Args:
    ----
        filename (str): Name of file.

    Returns:
    -------
        new_filename (str): ``9841422d-c041-45a5-b7b3-467179f4f127.ext``.

    """
    path = str(uuid.uuid4())
    ext = pathlib.Path(filename).suffix.lower()

    return f"{path}{ext}"

normalize_string_value(value)

Normalize string value.

  1. Remove leading and trailing whitespaces.
  2. Replace all space characters with the Space char.
  3. Normalize Unicode string using NFKC form. See the details: https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize
Source code in saritasa_s3_tools/keys.py
22
23
24
25
26
27
28
29
30
31
32
def normalize_string_value(self, value: str) -> str:
    """Normalize string value.

    1. Remove leading and trailing whitespaces.
    2. Replace all space characters with the Space char.
    3. Normalize Unicode string using `NFKC` form. See the details:
    https://docs.python.org/3/library/unicodedata.html#unicodedata.normalize

    """
    cleaned = " ".join(value.strip().split()).strip()
    return unicodedata.normalize("NFKC", cleaned)

remove_special_characters(filename)

Remove characters from filename that are not allowed in some OS.

Source code in saritasa_s3_tools/keys.py
17
18
19
20
def remove_special_characters(self, filename: str) -> str:
    """Remove characters from filename that are not allowed in some OS."""
    special_characters = r"<>:\"/\\|?*"
    return filename.translate({ord(i): None for i in special_characters})

validate(key)

Check that input key is matching Key pattern.

Source code in saritasa_s3_tools/keys.py
61
62
63
def validate(self, key: str) -> bool:
    """Check that input key is matching Key pattern."""
    return True  # pragma: no cover

WithPrefixUUIDFileName

Bases: S3Key

Generate S3 key with prefix folder and uuid filename.

Example:

prefix/{UUID.extension}
Source code in saritasa_s3_tools/keys.py
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
class WithPrefixUUIDFileName(S3Key):
    """Generate S3 key with prefix folder and uuid filename.

    Example:
    -------
        prefix/{UUID.extension}

    """

    def __init__(self, prefix: str) -> None:
        self.prefix = prefix.removesuffix("/")

    def __call__(self, filename: str | None) -> str:
        """Return prefixed S3 key."""
        if not filename:
            return f"{self.prefix}/{uuid.uuid4()}.incorrect"
        return f"{self.prefix}/{self.get_random_filename(filename)}"

    def validate(self, key: str) -> bool:
        """Check that input key is matching Key pattern."""
        return bool(
            re.compile(pattern=rf"{self.prefix}/{self.uuid_regex}\..+").match(
                key,
            ),
        )

__call__(filename)

Return prefixed S3 key.

Source code in saritasa_s3_tools/keys.py
78
79
80
81
82
def __call__(self, filename: str | None) -> str:
    """Return prefixed S3 key."""
    if not filename:
        return f"{self.prefix}/{uuid.uuid4()}.incorrect"
    return f"{self.prefix}/{self.get_random_filename(filename)}"

validate(key)

Check that input key is matching Key pattern.

Source code in saritasa_s3_tools/keys.py
84
85
86
87
88
89
90
def validate(self, key: str) -> bool:
    """Check that input key is matching Key pattern."""
    return bool(
        re.compile(pattern=rf"{self.prefix}/{self.uuid_regex}\..+").match(
            key,
        ),
    )

WithPrefixUUIDFolder

Bases: S3Key

Generate S3 key with prefix folder and uuid folder.

Example:

prefix/{UUID}/filename
Source code in saritasa_s3_tools/keys.py
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class WithPrefixUUIDFolder(S3Key):
    """Generate S3 key with prefix folder and uuid folder.

    Example:
    -------
        prefix/{UUID}/filename

    """

    def __init__(self, prefix: str) -> None:
        self.prefix = prefix.removesuffix("/")

    def __call__(self, filename: str | None) -> str:
        """Create key for destination using filename."""
        if not filename:
            return f"{self.prefix}/{uuid.uuid4()}/{uuid.uuid4()}.incorrect"
        return f"{self.prefix}/{uuid.uuid4()}/{self.clean_filename(filename)}"

    def validate(self, key: str) -> bool:
        """Check that input key is matching Key pattern."""
        return bool(
            re.compile(
                pattern=rf"{self.prefix}/{self.uuid_regex}/.+\..+",
            ).match(
                key,
            ),
        )

__call__(filename)

Create key for destination using filename.

Source code in saritasa_s3_tools/keys.py
105
106
107
108
109
def __call__(self, filename: str | None) -> str:
    """Create key for destination using filename."""
    if not filename:
        return f"{self.prefix}/{uuid.uuid4()}/{uuid.uuid4()}.incorrect"
    return f"{self.prefix}/{uuid.uuid4()}/{self.clean_filename(filename)}"

validate(key)

Check that input key is matching Key pattern.

Source code in saritasa_s3_tools/keys.py
111
112
113
114
115
116
117
118
119
def validate(self, key: str) -> bool:
    """Check that input key is matching Key pattern."""
    return bool(
        re.compile(
            pattern=rf"{self.prefix}/{self.uuid_regex}/.+\..+",
        ).match(
            key,
        ),
    )