structs.py 2.25 KB
Newer Older
mathieui's avatar
mathieui committed
1 2 3
"""
Module defining structures useful to the core class and related methods
"""
4 5
from dataclasses import dataclass
from typing import Any, Callable, List, Dict
6

mathieui's avatar
mathieui committed
7 8 9 10
__all__ = [
    'ERROR_AND_STATUS_CODES', 'DEPRECATED_ERRORS', 'POSSIBLE_SHOW', 'Status',
    'Command', 'Completion'
]
mathieui's avatar
mathieui committed
11 12 13

# http://xmpp.org/extensions/xep-0045.html#errorstatus
ERROR_AND_STATUS_CODES = {
14 15 16 17 18 19 20 21
    '401': 'A password is required',
    '403': 'Permission denied',
    '404': 'The room doesn’t exist',
    '405': 'Your are not allowed to create a new room',
    '406': 'A reserved nick must be used',
    '407': 'You are not in the member list',
    '409': 'This nickname is already in use or has been reserved',
    '503': 'The maximum number of users has been reached',
mathieui's avatar
mathieui committed
22
}
mathieui's avatar
mathieui committed
23 24 25

# http://xmpp.org/extensions/xep-0086.html
DEPRECATED_ERRORS = {
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    '302': 'Redirect',
    '400': 'Bad request',
    '401': 'Not authorized',
    '402': 'Payment required',
    '403': 'Forbidden',
    '404': 'Not found',
    '405': 'Not allowed',
    '406': 'Not acceptable',
    '407': 'Registration required',
    '408': 'Request timeout',
    '409': 'Conflict',
    '500': 'Internal server error',
    '501': 'Feature not implemented',
    '502': 'Remote server error',
    '503': 'Service unavailable',
    '504': 'Remote server timeout',
    '510': 'Disconnected',
mathieui's avatar
mathieui committed
43 44
}

45 46 47 48 49 50 51 52 53 54
POSSIBLE_SHOW = {
    'available': None,
    'chat': 'chat',
    'away': 'away',
    'afk': 'away',
    'dnd': 'dnd',
    'busy': 'dnd',
    'xa': 'xa'
}

mathieui's avatar
mathieui committed
55

56
@dataclass
57 58
class Status:
    __slots__ = ('show', 'message')
59 60
    show: str
    message: str
61

mathieui's avatar
mathieui committed
62

63 64 65 66 67
class Completion:
    """
    A completion result essentially currying the input completion call.
    """
    __slots__ = ['func', 'args', 'kwargs', 'comp_list']
68 69 70 71 72 73 74
    def __init__(
        self,
        func: Callable[..., Any],
        comp_list: List[str],
        *args: Any,
        **kwargs: Any
    ) -> None:
75 76 77 78 79 80 81
        self.func = func
        self.comp_list = comp_list
        self.args = args
        self.kwargs = kwargs

    def run(self):
        return self.func(self.comp_list, *self.args, **self.kwargs)
82 83 84 85 86 87 88 89 90

@dataclass
class Command:
    __slots__ = ('func', 'desc', 'comp', 'short_desc', 'usage')
    func: Callable[..., Any]
    desc: str
    comp: Callable[['windows.Input'], Completion]
    short_desc: str
    usage: str