File: _theming.py

package info (click to toggle)
nc-py-api 0.19.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,320 kB
  • sloc: python: 12,415; makefile: 238; xml: 100; javascript: 56; sh: 14
file content (54 lines) | stat: -rw-r--r-- 1,870 bytes parent folder | download
1
2
3
4
5
6
7
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
"""Nextcloud stuff for work with Theming app."""

from typing import TypedDict


class ThemingInfo(TypedDict):
    """Nextcloud Theme information."""

    name: str
    """Name of the Nextcloud instance"""
    url: str
    """Url that set in Theme app"""
    slogan: str
    """Slogan, e.g. 'a safe home for all your data'"""
    color: tuple[int, int, int]
    color_text: tuple[int, int, int]
    color_element: tuple[int, int, int]
    color_element_bright: tuple[int, int, int]
    color_element_dark: tuple[int, int, int]
    logo: str
    """Url of the instance's logo"""
    background: str
    """Either an URL of the background image or a hex color value"""
    background_plain: bool
    background_default: bool


def convert_str_color(theming_capability: dict, key: str) -> tuple[int, int, int]:
    """Returns a tuple of integers representing the RGB color for the specified theme key."""
    if key not in theming_capability:
        return 0, 0, 0
    value = theming_capability[key]
    if not value or value == "#":
        return 0, 0, 0
    return int(value[1:3], 16), int(value[3:5], 16), int(value[5:7], 16)


def get_parsed_theme(theming_capability: dict) -> ThemingInfo:
    """Returns parsed ``theme`` information."""
    i = theming_capability
    return ThemingInfo(
        name=i["name"],
        url=i["url"],
        slogan=i["slogan"],
        color=convert_str_color(i, "color"),
        color_text=convert_str_color(i, "color-text"),
        color_element=convert_str_color(i, "color-element"),
        color_element_bright=convert_str_color(i, "color-element-bright"),
        color_element_dark=convert_str_color(i, "color-element-dark"),
        logo=i["logo"],
        background=i.get("background", ""),
        background_plain=i.get("background-plain", False),
        background_default=i.get("background-default", False),
    )