File size: 1,947 Bytes
346533a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f045361
346533a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from pathlib import Path
from typing import List, Optional, Union

import toml
from pydantic import BaseModel, Extra

from .dirs import get_config_file

config_file_path = get_config_file("config.toml")


class MemeConfig(BaseModel):
    load_builtin_memes: bool = True
    meme_dirs: List[Path] = []
    meme_disabled_list: List[str] = []


class ResourceConfig(BaseModel):
    resource_url: Optional[str] = None
    resource_urls: List[str] = [
        "https://raw.githubusercontent.com/MeetWq/meme-generator/",
        "https://ghproxy.com/https://raw.githubusercontent.com/MeetWq/meme-generator/",
        "https://fastly.jsdelivr.net/gh/MeetWq/meme-generator@",
        "https://raw.fastgit.org/MeetWq/meme-generator/",
        "https://raw.fgit.ml/MeetWq/meme-generator/",
        "https://raw.gitmirror.com/MeetWq/meme-generator/",
        "https://raw.kgithub.com/MeetWq/meme-generator/",
    ]


class GifConfig(BaseModel):
    gif_max_size: float = 10
    gif_max_frames: int = 100


class TranslatorConfig(BaseModel):
    baidu_trans_appid: str = ""
    baidu_trans_apikey: str = ""


class ServerConfig(BaseModel):
    host: str = "127.0.0.1"
    port: int = 7860


class LogConfig(BaseModel):
    log_level: Union[int, str] = "INFO"


class Config(BaseModel, extra=Extra.ignore):
    meme: MemeConfig = MemeConfig()
    resource: ResourceConfig = ResourceConfig()
    gif: GifConfig = GifConfig()
    translate: TranslatorConfig = TranslatorConfig()
    server: ServerConfig = ServerConfig()
    log: LogConfig = LogConfig()

    @classmethod
    def load(cls) -> "Config":
        return cls.parse_obj(toml.load(config_file_path))

    def dump(self):
        with open(config_file_path, "w", encoding="utf8") as f:
            toml.dump(json.loads(self.json()), f)


if not config_file_path.exists():
    meme_config = Config()
    config_file_path.write_text("", encoding="utf8")
else:
    meme_config = Config.load()