-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: utilize dataclass and dacite
Signed-off-by: Jack Cherng <[email protected]>
- Loading branch information
Showing
99 changed files
with
735 additions
and
217 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from .fanhuaji_convert import FanhuajiConvertCommand | ||
from .fanhuaji_convert_panel import FanhuajiConvertPanelCommand | ||
|
||
__all__ = ( | ||
"FanhuajiConvertCommand", | ||
"FanhuajiConvertPanelCommand", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,138 @@ | ||
from typing import Any, Dict, Tuple | ||
from typing import Any, Dict | ||
|
||
import sublime | ||
|
||
from ..vendor import dacite, requests | ||
from .constant import ST_PLATFORM_ARCH, ST_VERSION | ||
from .libs import requests | ||
from .log import print_msg | ||
from .settings import get_setting | ||
from .types import TD_ApiConvertResponse, TD_ConverterInfo | ||
from .types import ApiConvertResponse, ConverterInfo | ||
|
||
HTTP_HEADERS = { | ||
"user-agent": f"Sublime Text {ST_VERSION} ({ST_PLATFORM_ARCH}) Fanhuaji", | ||
} | ||
|
||
|
||
class FanhuajiApiUrl: | ||
@classmethod | ||
def base_url(cls) -> str: | ||
return get_setting("api_server").rstrip("/") | ||
|
||
@classmethod | ||
def convert(cls) -> str: | ||
return f"{cls.base_url()}/convert" | ||
|
||
@classmethod | ||
def service_info(cls) -> str: | ||
return f"{cls.base_url()}/service-info" | ||
|
||
|
||
class Fanhuaji: | ||
converters: Tuple[TD_ConverterInfo, ...] = ( | ||
{ | ||
"name_api": "Simplified", | ||
"name_eng": "Simplified", | ||
"name_chi": "简体化", | ||
"details": "将文字转换为简体。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_ORANGISH, "简", ""), | ||
}, | ||
{ | ||
"name_api": "Traditional", | ||
"name_eng": "Traditional", | ||
"name_chi": "繁體化", | ||
"details": "將文字轉換為繁體。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_ORANGISH, "繁", ""), | ||
}, | ||
{ | ||
"name_api": "China", | ||
"name_eng": "China Localization", | ||
"name_chi": "中国化", | ||
"details": "将文字转换为简体,并使用中国地区的词语修正。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_CYANISH, "中", ""), | ||
}, | ||
{ | ||
"name_api": "Hongkong", | ||
"name_eng": "Hongkong Localization", | ||
"name_chi": "香港化", | ||
"details": "將文字轉換為繁體,並使用香港地區的詞語修正。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_CYANISH, "港", ""), | ||
}, | ||
{ | ||
"name_api": "Taiwan", | ||
"name_eng": "Taiwan Localization", | ||
"name_chi": "台灣化", | ||
"details": "將文字轉換為繁體,並使用台灣地區的詞語修正。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_CYANISH, "台", ""), | ||
}, | ||
{ | ||
"name_api": "Pinyin", | ||
"name_eng": "Pinyin", | ||
"name_chi": "拼音化", | ||
"details": "將文字轉為拼音。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_GREENISH, "拼", ""), | ||
}, | ||
{ | ||
"name_api": "Bopomofo", | ||
"name_eng": "Bopomofo", | ||
"name_chi": "注音化", | ||
"details": "將文字轉為注音。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_GREENISH, "注", ""), | ||
}, | ||
{ | ||
"name_api": "Mars", | ||
"name_eng": "Mars", | ||
"name_chi": "火星化", | ||
"details": "將文字轉換為繁體火星文。", | ||
"annotation": "", | ||
"st_kind": (sublime.KIND_ID_COLOR_GREENISH, "火", ""), | ||
}, | ||
{ | ||
"name_api": "WikiSimplified", | ||
"name_eng": "Simplified (Wikipeida)", | ||
"name_chi": "维基简体化", | ||
"details": "只使用维基百科的词库将文字转换为简体。", | ||
"annotation": "(少用)", | ||
"st_kind": (sublime.KIND_ID_COLOR_LIGHT, "简", ""), | ||
}, | ||
{ | ||
"name_api": "WikiTraditional", | ||
"name_eng": "Traditional (Wikipeida)", | ||
"name_chi": "維基繁體化", | ||
"details": "只使用維基百科的詞庫將文字轉換為繁體。", | ||
"annotation": "(少用)", | ||
"st_kind": (sublime.KIND_ID_COLOR_LIGHT, "繁", ""), | ||
}, | ||
CONVERTERS = ( | ||
ConverterInfo( | ||
name_api="Simplified", | ||
name_eng="Simplified Chinese", | ||
name_chi="简体化", | ||
details="将文字转换为简体。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_ORANGISH, "简", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Traditional", | ||
name_eng="Traditional Chinese", | ||
name_chi="繁體化", | ||
details="將文字轉換為繁體。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_ORANGISH, "繁", ""), | ||
), | ||
ConverterInfo( | ||
name_api="China", | ||
name_eng="China Localization", | ||
name_chi="中国化", | ||
details="将文字转换为简体,并使用中国地区的词语修正。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_CYANISH, "中", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Hongkong", | ||
name_eng="Hongkong Localization", | ||
name_chi="香港化", | ||
details="將文字轉換為繁體,並使用香港地區的詞語修正。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_CYANISH, "港", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Taiwan", | ||
name_eng="Taiwan Localization", | ||
name_chi="台灣化", | ||
details="將文字轉換為繁體,並使用台灣地區的詞語修正。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_CYANISH, "台", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Pinyin", | ||
name_eng="Pinyin", | ||
name_chi="拼音化", | ||
details="將文字轉為拼音。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_GREENISH, "拼", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Bopomofo", | ||
name_eng="Bopomofo", | ||
name_chi="注音化", | ||
details="將文字轉為注音。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_GREENISH, "注", ""), | ||
), | ||
ConverterInfo( | ||
name_api="Mars", | ||
name_eng="Mars", | ||
name_chi="火星化", | ||
details="將文字轉換為繁體火星文。", | ||
annotation="", | ||
st_kind=(sublime.KIND_ID_COLOR_GREENISH, "火", ""), | ||
), | ||
ConverterInfo( | ||
name_api="WikiSimplified", | ||
name_eng="Simplified Chinese (Wikipeida)", | ||
name_chi="维基简体化", | ||
details="只使用维基百科的词库将文字转换为简体。", | ||
annotation="(少用)", | ||
st_kind=(sublime.KIND_ID_COLOR_LIGHT, "简", ""), | ||
), | ||
ConverterInfo( | ||
name_api="WikiTraditional", | ||
name_eng="Traditional Chinese (Wikipeida)", | ||
name_chi="維基繁體化", | ||
details="只使用維基百科的詞庫將文字轉換為繁體。", | ||
annotation="(少用)", | ||
st_kind=(sublime.KIND_ID_COLOR_LIGHT, "繁", ""), | ||
), | ||
) | ||
|
||
TEXT_DELIMITER = r"\n\5\9\8\n" | ||
""" | ||
The delimiter used to concat/split multiple selected text, | ||
so we could convert multiple text with only a single API call. | ||
This delimiter should be a extremely rarely used string. | ||
""" | ||
|
||
@classmethod | ||
def convert(cls, args: Dict[str, Any]) -> TD_ApiConvertResponse: | ||
def convert(cls, args: Dict[str, Any]) -> ApiConvertResponse: | ||
if get_setting("debug"): | ||
print_msg(f"Request {args = }") | ||
|
||
url = get_setting("api_server") + "/convert" | ||
verify_ssl = bool(get_setting("ssl_cert_verification")) | ||
|
||
try: | ||
response = requests.post(url, data=args, headers=HTTP_HEADERS, verify=verify_ssl) | ||
response: requests.Response = requests.post( | ||
url=FanhuajiApiUrl.convert(), | ||
data=args, | ||
headers=HTTP_HEADERS, | ||
verify=bool(get_setting("ssl_cert_verification")), | ||
) | ||
except requests.exceptions.ConnectionError as e: | ||
raise RuntimeError(f"Failed to reach the server: {e}") | ||
raise RuntimeError(f"Failed to reach the server: {e}") from e | ||
except requests.exceptions.RequestException as e: | ||
raise RuntimeError(f"Request exception: {e}") | ||
raise RuntimeError(f"Request exception: {e}") from e | ||
|
||
return sublime.decode_value(response.text) | ||
return dacite.from_dict(ApiConvertResponse, sublime.decode_value(response.text)) |
Oops, something went wrong.