-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslation.py
More file actions
32 lines (26 loc) · 926 Bytes
/
Copy pathtranslation.py
File metadata and controls
32 lines (26 loc) · 926 Bytes
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
from configparser import ConfigParser
from dataclasses import dataclass
from typing import Optional
from languages import Language
from service import Service
from httpx import AsyncClient # type: ignore
@dataclass
class Translation:
text: str
language: Optional[Language]
translation: str
class Translator(Service):
async def translate(self, client: AsyncClient, /, *, text: str) -> Translation:
data = await self.async_post(client, path="/", data={"text": text})
language = data["language"]
return Translation(
data["text"],
Language[language] if language is not None else None,
data["translation"],
)
@classmethod
def from_config(cls, path: str = "config.ini") -> "Translator":
config = ConfigParser()
config.read(path)
section = config["TRANSLATION FUNCTION"]
return cls(url=section["url"])