summaryrefslogtreecommitdiff
path: root/ATRI/service/__init__.py
blob: 6b18ba1566d21b40b0d734076c066a2620a73fc4 (plain)
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
90
91
92
93
94
95
import os
import json
from pathlib import Path
from pydantic import BaseModel
from typing import Optional

from ATRI.log import logger
from ATRI.exceptions import WriteError


SERVICE_DIR = Path('.') / 'ATRI' / 'data' / 'service'
PLUGIN_INFO_DIR = Path('.') / 'ATRI' / 'data' / 'service' / 'plugins'
os.makedirs(SERVICE_DIR, exist_ok=True)
os.makedirs(PLUGIN_INFO_DIR, exist_ok=True)

sleep = False

class Service:
    class ServiceInfo(BaseModel):
        name: str
        _type: str
        docs: Optional[str] = None
        command: Optional[list] = None
    
    @classmethod
    def register(
        cls,
        service_name: str,
        service_type: str,
        docs: Optional[str] = None,
        command: Optional[list] = None
    ) -> None:
        """
        启动时保存各个服务信息,便于后续网页控制台调用
        增强管理
        """
        file_name = f"{service_name}.function.json"
        path = SERVICE_DIR / file_name
        path.parent.mkdir(exist_ok=True, parents=True)
        try:
            data = json.loads(path.read_bytes())
        except:
            data = {}
        
        data = cls.ServiceInfo(
            name=service_name,
            _type=service_type,
            docs=docs,
            command=command
        )
        try:
            with open(path, 'w', encoding='utf-8') as target:
                target.write(
                    json.dumps(
                        data.dict(), indent=4
                    )
                )
        except WriteError:
            raise WriteError("Writing file failed!")
        else:
            pass
        
        # TODO: shitcode-style
        docs_judge = "N" if not docs else "Y"
        a = " "
        log_center = ""
        log_head = f"Success register service: [{service_name}]."
        log_suffix = f"Docs [{docs_judge}]. Type [{service_type}]"
        log_head_lenght = len(log_head)
        log_suffix_lenght = len(log_suffix)
        log_center_lenght = 120 - (
            log_head_lenght + log_suffix_lenght
        )
        for _ in range(log_center_lenght): log_center = log_center + a
        log_print = log_head + log_center + log_suffix
        logger.info(log_print)
    
    @staticmethod
    def is_dormant() -> bool:
        """
        为bot提供休眠,期间不会响应除了 superusers 以外的用户的信息
        触发在于 Rule
        """
        return False if sleep else True
    
    @staticmethod
    def control_dormant(_type: bool) -> None:
        """
        更改休眠状态
        """
        global sleep
        if _type:
            sleep = True
        else:
            sleep = False