theming: Add typing information.

This commit is contained in:
Emmanuel Gil Peyrot 2018-07-22 17:16:45 +02:00
parent b5bd6a12dd
commit 30547aa81a

View file

@ -74,6 +74,7 @@ except ImportError:
import curses import curses
import functools import functools
import os import os
from typing import Dict, List, Union, Tuple, Optional
from pathlib import Path from pathlib import Path
from os import path from os import path
from poezio import colors, xdg from poezio import colors, xdg
@ -94,7 +95,7 @@ class Theme:
""" """
@classmethod @classmethod
def color_role(cls, role): def color_role(cls, role: str):
role_mapping = { role_mapping = {
'moderator': cls.COLOR_USER_MODERATOR, 'moderator': cls.COLOR_USER_MODERATOR,
'participant': cls.COLOR_USER_PARTICIPANT, 'participant': cls.COLOR_USER_PARTICIPANT,
@ -105,7 +106,7 @@ class Theme:
return role_mapping.get(role, cls.COLOR_USER_NONE) return role_mapping.get(role, cls.COLOR_USER_NONE)
@classmethod @classmethod
def char_affiliation(cls, affiliation): def char_affiliation(cls, affiliation: str):
affiliation_mapping = { affiliation_mapping = {
'owner': cls.CHAR_AFFILIATION_OWNER, 'owner': cls.CHAR_AFFILIATION_OWNER,
'admin': cls.CHAR_AFFILIATION_ADMIN, 'admin': cls.CHAR_AFFILIATION_ADMIN,
@ -115,7 +116,7 @@ class Theme:
return affiliation_mapping.get(affiliation, cls.CHAR_AFFILIATION_NONE) return affiliation_mapping.get(affiliation, cls.CHAR_AFFILIATION_NONE)
@classmethod @classmethod
def color_show(cls, show): def color_show(cls, show: str):
show_mapping = { show_mapping = {
'xa': cls.COLOR_STATUS_XA, 'xa': cls.COLOR_STATUS_XA,
'none': cls.COLOR_STATUS_NONE, 'none': cls.COLOR_STATUS_NONE,
@ -129,7 +130,7 @@ class Theme:
return show_mapping.get(show, cls.COLOR_STATUS_NONE) return show_mapping.get(show, cls.COLOR_STATUS_NONE)
@classmethod @classmethod
def char_subscription(cls, sub, keep='incomplete'): def char_subscription(cls, sub: str, keep: str = 'incomplete'):
sub_mapping = { sub_mapping = {
'from': cls.CHAR_ROSTER_FROM, 'from': cls.CHAR_ROSTER_FROM,
'both': cls.CHAR_ROSTER_BOTH, 'both': cls.CHAR_ROSTER_BOTH,
@ -382,7 +383,7 @@ theme = Theme()
# Each time we use a color tuple, we check if it has already been used. # Each time we use a color tuple, we check if it has already been used.
# If not we create a new color_pair and keep it in that dict, to use it # If not we create a new color_pair and keep it in that dict, to use it
# the next time. # the next time.
curses_colors_dict = {} curses_colors_dict = {} # type: Dict[Union[Tuple[int, int], Tuple[int, int, str]], int]
# yapf: disable # yapf: disable
@ -406,7 +407,7 @@ table_256_to_16 = [
] ]
# yapf: enable # yapf: enable
load_path = [] load_path = [] # type: List[str]
def color_256_to_16(color): def color_256_to_16(color):
@ -415,14 +416,14 @@ def color_256_to_16(color):
return table_256_to_16[color] return table_256_to_16[color]
def dump_tuple(tup): def dump_tuple(tup: Union[Tuple[int, int], Tuple[int, int, str]]) -> str:
""" """
Dump a tuple to a string of fg,bg,attr (optional) Dump a tuple to a string of fg,bg,attr (optional)
""" """
return ','.join(str(i) for i in tup) return ','.join(str(i) for i in tup)
def read_tuple(_str): def read_tuple(_str: str) -> Tuple[Tuple[int, int], str]:
""" """
Read a tuple dumped with dump_tumple Read a tuple dumped with dump_tumple
""" """
@ -432,7 +433,7 @@ def read_tuple(_str):
@functools.lru_cache(maxsize=128) @functools.lru_cache(maxsize=128)
def to_curses_attr(color_tuple): def to_curses_attr(color_tuple: Union[Tuple[int, int], Tuple[int, int, str]]) -> int:
""" """
Takes a color tuple (as defined at the top of this file) and Takes a color tuple (as defined at the top of this file) and
returns a valid curses attr that can be passed directly to attron() or attroff() returns a valid curses attr that can be passed directly to attron() or attroff()
@ -476,14 +477,14 @@ def to_curses_attr(color_tuple):
return curses_pair return curses_pair
def get_theme(): def get_theme() -> Theme:
""" """
Returns the current theme Returns the current theme
""" """
return theme return theme
def update_themes_dir(option=None, value=None): def update_themes_dir(option: Optional[str] = None, value: Optional[str] = None):
global load_path global load_path
load_path = [] load_path = []
@ -516,17 +517,17 @@ def update_themes_dir(option=None, value=None):
log.debug('Theme load path: %s', load_path) log.debug('Theme load path: %s', load_path)
def prepare_ccolor_palette(theme): def prepare_ccolor_palette(theme: Theme) -> None:
""" """
Prepare the Consistent Color Generation (XEP-0392) palette for a theme. Prepare the Consistent Color Generation (XEP-0392) palette for a theme.
""" """
if theme.CCG_PALETTE is not None: if theme.CCG_PALETTE is not None:
return return None
if any(bg != -1 for fg, bg in theme.LIST_COLOR_NICKNAMES): if any(bg != -1 for fg, bg in theme.LIST_COLOR_NICKNAMES):
# explicitly disable CCG, cant handle dynamic background colors # explicitly disable CCG, cant handle dynamic background colors
theme.CCG_PALETTE = {} theme.CCG_PALETTE = {}
return return None
theme.CCG_PALETTE = colors.generate_ccg_palette( theme.CCG_PALETTE = colors.generate_ccg_palette(
[ [
@ -536,14 +537,15 @@ def prepare_ccolor_palette(theme):
], ],
theme.CCG_Y, theme.CCG_Y,
) )
return None
def reload_theme(): def reload_theme() -> Optional[str]:
theme_name = config.get('theme') theme_name = config.get('theme')
global theme global theme
if theme_name == 'default' or not theme_name.strip(): if theme_name == 'default' or not theme_name.strip():
theme = Theme() theme = Theme()
return return None
new_theme = None new_theme = None
exc = None exc = None
try: try: