Spaces:
Running
Running
File size: 5,806 Bytes
6a07cb2 |
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
from dataclasses import dataclass
from typing import Optional
from dataclasses_json import dataclass_json
import json
from pathlib import Path
@dataclass
class Slack:
url: str
channel: str
username: str
icon_emoji: str
channel_id: Optional[str] = None
bot_token: Optional[str] = None
class Config(object):
def __init__(self, env="dev"):
if env == "dev":
config_path = Path(__file__).parent.parent / "config/config.dev.json"
else:
config_path = Path(__file__).parent.parent / "config/config.prod.json"
config = self._read_config(config_path)
self.awss3_bucket, self.awss3_path = self.__parse_awss3_storage(
config.get("storage", {}).get("awss3", {})
)
(
self.requested_img_path,
self.text_removed_img_path,
) = self.__parse_local_storage(config.get("storage", {}).get("local", {}))
self.mq_url = self._parse_amqp_server(config["mq_server"])
(
self.req_queue,
self.req_pattern,
self.resp_queue,
self.success_resp_pattern,
self.failure_resp_pattern,
) = self._parse_queue(config["queue"])
self.slack = self._parse_slack(config["alarm"]["slack"])
def _read_config(self, config_path) -> dict:
with open(config_path, mode="r") as f:
config = json.load(f)
return config
def _parse_amqp_server(self, amqp_server) -> str:
username = amqp_server["username"]
password = amqp_server["password"]
url = amqp_server["url"]
port = amqp_server["port"]
amqp_url = f"amqps://{username}:{password}@{url}:{port}"
return amqp_url
def _parse_queue(self, queue) -> tuple:
req_queue = queue["request_name"]
req_pattern = queue["request_pattern"]
resp_queue = queue.get("response_name")
success_resp_pattern = queue["success_response_pattern"]
failure_resp_pattern = queue["failure_response_pattern"]
return (
req_queue,
req_pattern,
resp_queue,
success_resp_pattern,
failure_resp_pattern,
)
def __parse_awss3_storage(self, awss3_storage) -> tuple:
awss3_bucket = awss3_storage.get("default_bucket")
awss3_path = awss3_storage.get("default_path")
return awss3_bucket, awss3_path
def __parse_local_storage(self, local_storage) -> tuple:
requested_img_path = local_storage.get("requested")
text_removed_img_path = local_storage.get("text_removed")
return requested_img_path, text_removed_img_path
def _parse_slack(self, slack) -> Slack:
url = slack["url"]
channel = slack["channel"]
username = slack["username"]
icon_emoji = slack["icon_emoji"]
channel_id = slack.get("channel_id", None)
bot_token = slack.get("bot_token", None)
return Slack(url, channel, username, icon_emoji, channel_id, bot_token)
class ImageTrConfig(object):
def __init__(self, env="dev"):
if env == "dev":
config_path = Path(__file__).parent.parent / "config/config.dev.json"
else:
config_path = Path(__file__).parent.parent / "config/config.prod.json"
config = self._read_config(config_path)
(
self.awss3_bucket,
self.awss3_inpainting_path,
self.awss3_translation_path,
) = self.__parse_awss3_storage(config.get("storage", {}).get("awss3", {}))
self.mq_url = self._parse_amqp_server(config["mq_server"])
(
self.req_queue,
self.req_pattern,
self.resp_queue,
self.success_resp_pattern,
self.failure_resp_pattern,
) = self._parse_queue(config["queue"])
self.slack = self._parse_slack(config["alarm"]["slack"])
def _read_config(self, config_path) -> dict:
with open(config_path, mode="r") as f:
config = json.load(f)
return config
def _parse_amqp_server(self, amqp_server) -> str:
username = amqp_server["username"]
password = amqp_server["password"]
url = amqp_server["url"]
port = amqp_server["port"]
amqp_url = f"amqps://{username}:{password}@{url}:{port}"
return amqp_url
def _parse_queue(self, queue) -> tuple:
req_queue = queue["request_name"]
req_pattern = queue["request_pattern"]
resp_queue = queue.get("response_name")
success_resp_pattern = queue["success_response_pattern"]
failure_resp_pattern = queue["failure_response_pattern"]
return (
req_queue,
req_pattern,
resp_queue,
success_resp_pattern,
failure_resp_pattern,
)
def __parse_awss3_storage(self, awss3_storage) -> tuple:
awss3_bucket = awss3_storage.get("default_bucket")
awss3_inpainting_path = awss3_storage.get("inpainting_path")
awss3_translation_path = awss3_storage.get("translation_path")
return awss3_bucket, awss3_inpainting_path, awss3_translation_path
def __parse_local_storage(self, local_storage) -> tuple:
requested_img_path = local_storage.get("requested")
text_removed_img_path = local_storage.get("text_removed")
return requested_img_path, text_removed_img_path
def _parse_slack(self, slack) -> Slack:
url = slack["url"]
channel = slack["channel"]
username = slack["username"]
icon_emoji = slack["icon_emoji"]
channel_id = slack.get("channel_id", None)
bot_token = slack.get("bot_token", None)
return Slack(url, channel, username, icon_emoji, channel_id, bot_token)
|