-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate.py
48 lines (38 loc) · 1.43 KB
/
translate.py
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
from asyncio import gather
from tools import _LOADMODULE
from os import listdir
from interface import Translator
from tools import (
isPyFile,
proxy,
Filter,
Map,
singleton,
dropWhileEnd,
)
from typing import Any, Callable, Coroutine, Dict, List
@singleton
class Translate:
def __init__(self, proxy_url: str="") -> None:
def dropPy(s: str) -> str:
return dropWhileEnd(lambda x: x == ".", s)
def loadMod(s: str) -> Any:
return getattr(_LOADMODULE(f"translator.{s}"), s)
def instance(obj: Any) -> Any:
return obj(proxy(proxy_url))
def isTranslator(obj: Any) -> bool:
return isinstance(obj, Translator)
def toTranslator(obj: Any) -> Translator:
return obj
def toAsync(obj: Translator) -> Callable[[str], Coroutine[Any, Any, Dict[str, str]]]:
return obj.Do
self.__modules = list(Map(toAsync) \
(Map(toTranslator) \
(Filter(isTranslator) \
(Map(instance) \
(Map(loadMod) \
(Map(dropPy) \
(Filter(isPyFile) \
(listdir("translator")))))))))
async def Do(self, text: str) -> List[Dict[str, str]]:
return [] if text == '' else await gather(*map(lambda f: f(text), self.__modules))