-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgateway.py
More file actions
89 lines (71 loc) · 2.43 KB
/
Copy pathgateway.py
File metadata and controls
89 lines (71 loc) · 2.43 KB
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
55
56
57
58
59
60
61
62
63
64
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
from functools import wraps
from logging import getLogger
from typing import List
from csp import ts
from csp_gateway import (
Controls,
Gateway as BaseGateway,
GatewayChannels as GatewayChannelsBase,
GatewayModule,
GatewaySettings as BaseGatewaySettings,
)
from pydantic import Field, root_validator
from csp_bot import __version__
from csp_bot.commands import BaseCommandModel
from csp_bot.structs import BotCommand, Message
log = getLogger(__name__)
__all__ = (
"GatewayChannels",
"GatewayModule",
"GatewaySettings",
"CspBotGateway",
"Channels",
"Gateway",
"Module",
"Settings",
)
class GatewayChannels(GatewayChannelsBase):
messages_in: ts[Message] = None
messages_out: ts[Message] = None
commands: ts[BotCommand] = None
controls: ts[Controls] = None
"""Channel for webserver/graph admin. """
class GatewaySettings(BaseGatewaySettings):
# Override from csp-gateway
TITLE: str = "CSP Bot"
DESCRIPTION: str = "# Welcome to CSP Bot API\nContains REST/Websocket interfaces to underlying CSP Gateway engine"
VERSION: str = __version__
class CspBotGateway(BaseGateway):
settings: GatewaySettings = Field(default_factory=GatewaySettings)
commands: List[BaseCommandModel] = []
@root_validator(pre=True)
def _root_validate(cls, values):
"""Root validator to append "user_commands" to list of commands."""
values["commands"] = values.get("commands") or []
values["commands"].extend(values.pop("user_commands", []))
return values
def __hash__(self):
return hash(id(self))
def __init__(
self,
modules: List[GatewayModule] = None,
channels: GatewayChannels = None,
commands: List[BaseCommandModel] = None,
*args: str,
**kwargs: str,
):
# The normal initialization
channels = channels or GatewayChannels()
super().__init__(modules=modules, channels=channels, commands=commands, *args, **kwargs)
# Register the commands, couldnt do from hydra easily
from csp_bot.bot import Bot
for module in self.modules:
if isinstance(module, Bot):
module.load_commands(self.commands)
@wraps(BaseGateway.start)
def start(self, *args, **kwargs):
super(CspBotGateway, self).start(*args, **kwargs)
Channels = GatewayChannels
Gateway = CspBotGateway
Module = GatewayModule
Settings = GatewaySettings