Skip to content

addon_helpers

create_short_name(name)

create a shortname from the full name when a shortname is not present

Source code in server/kitsu/addon_helpers.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def create_short_name(name: str) -> str:
    """create a shortname from the full name when a shortname is not present"""
    code = name.lower()

    if "_" in code:
        subwords = code.split("_")
        code = "".join([subword[0] for subword in subwords])[:4]
    elif len(name) > 4:
        vowels = ["a", "e", "i", "o", "u"]
        filtered_word = "".join([char for char in code if char not in vowels])
        code = filtered_word[:4]

    # if there is a number at the end of the code, add it to the code
    last_char = code[-1]
    if last_char.isdigit():
        code += last_char

    return code

remove_accents(input_str)

swap accented characters for a-z equivilants ž => z

Source code in server/kitsu/addon_helpers.py
65
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
91
92
93
94
def remove_accents(input_str: str) -> str:
    """swap accented characters for a-z equivilants ž => z"""

    nfkd_form = unicodedata.normalize("NFKD", input_str)
    result = "".join([c for c in nfkd_form if not unicodedata.combining(c)])

    # manually replace exceptions
    # @see https://stackoverflow.com/questions/3194516/replace-special-characters-with-ascii-equivalent
    replacement_map = {
        "Æ": "AE",
        "Ð": "D",
        "Ø": "O",
        "Þ": "TH",
        "ß": "ss",
        "æ": "ae",
        "ð": "d",
        "ø": "o",
        "þ": "th",
        "Œ": "OE",
        "œ": "oe",
        "ƒ": "f",
    }
    for k, v in replacement_map.items():
        if k in result:
            result = result.replace(k, v)

    # remove any unsupported characters
    result = re.sub(r"[^a-zA-Z0-9_\.\-]", "", result)

    return result

required_values(entity, keys, allow_empty_value=False)

check the entity dict has the required keys and a value for each

Source code in server/kitsu/addon_helpers.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def required_values(
    entity: dict[str, Any],
    keys: list[str],
    allow_empty_value: bool = False
) -> list[Any]:
    """check the entity dict has the required keys and a value for each"""
    values = []
    for key in keys:
        if key not in entity:
            raise ValueError(f"Key '{key}' not set for entity: {entity}")
        if not allow_empty_value and not entity.get(key):
            raise ValueError(
                f"Value for '{key}' cannot be empty for entity: {entity}"
            )
        values.append(entity.get(key))
    return values

to_entity_name(name)

convert names so they will pass AYON Entity name validation @see ayon_server.types.NAME_REGEX r"^a-zA-Z0-9_?$"

Source code in server/kitsu/addon_helpers.py
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
def to_entity_name(name: str) -> str:
    r"""convert names so they will pass AYON Entity name validation
    @see ayon_server.types.NAME_REGEX
        r"^[a-zA-Z0-9_]([a-zA-Z0-9_\.\-]*[a-zA-Z0-9_])?$"
    """

    if not name:
        raise ValueError("Entity name cannot be empty")

    name = name.strip()

    # replace whitespace
    name = re.sub(r"\s+", "_", name)
    # remove any invalid characters
    name = re.sub(r"[^a-zA-Z0-9_\.\-]", "", name)

    # first and last characters cannot be . or -
    name = re.sub(r"^[^a-zA-Z0-9_]+", "", name)
    name = re.sub(r"[^a-zA-Z0-9_]+$", "", name)
    return name

to_username(first_name, last_name=None)

converts usernames from kitsu - converts accents

Source code in server/kitsu/addon_helpers.py
51
52
53
54
55
56
57
58
59
60
61
62
def to_username(first_name: str, last_name: str | None = None) -> str:
    """converts usernames from kitsu - converts accents"""

    name = (
        f"{first_name.strip()}.{last_name.strip()}"
        if last_name
        else first_name.strip()
    )

    name = name.lower()
    name = remove_accents(name)
    return to_entity_name(name)