summaryrefslogtreecommitdiff
path: root/ATRI/service/plugin.py
blob: e14ef34a54aeafddfa17ead0d8f023d2cf2512b5 (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
import json
from typing import Optional
from pydantic import BaseModel

from ATRI.log import logger
from ATRI.exceptions import InvalidWriteText

from . import SERVICE_PATH


class Plugin:
    class PluginInfo(BaseModel):
        name: str
        _type: str
        docs: Optional[str] = None
        command: list
    
    @classmethod
    def register(cls, plugin_name: str, _type: str,
                        doc: Optional[str] = None,
                        command: Optional[list] = None) -> None:
        filename = f'{plugin_name}.plugins.json'
        path = SERVICE_PATH / 'plugins' / filename
        path.parent.mkdir(exist_ok=True, parents=True)
        try:
            data = json.loads(path.read_bytes())
        except:
            data = {}
        
        data = cls.PluginInfo(
            name=plugin_name,
            _type=_type,
            docs=doc,
            command=command
        )
        try:
            with open(path, 'w', encoding='utf-8') as target:
                target.write(
                    json.dumps(
                        data.dict(), indent=4
                    )
                )
        except InvalidWriteText:
            raise InvalidWriteText('Writing file failed!')
        else:
            pass
        docs_judge = "N" if not doc else "Y"

        a = ' '
        log_center = ''
        log_head = f"Success register plugin: [{plugin_name}]."
        log_suffix = f"Docs [{docs_judge}]. Type [{_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)