-
-
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: entity-ize server responses
Signed-off-by: Jack Cherng <[email protected]>
- Loading branch information
Showing
7 changed files
with
215 additions
and
260 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,34 @@ | ||
from ..constant import HTTP_HEADERS | ||
from ..constant import TEXT_DELIMITER | ||
from ..fanhuaji import Fanhuaji | ||
from ..functions import prepare_fanhuaji_convert_args | ||
from ..libs import requests | ||
from ..log import msg, print_msg | ||
from ..settings import get_setting | ||
from typing import Dict | ||
from ..log import msg | ||
from typing import Dict, Optional | ||
import sublime | ||
import sublime_plugin | ||
|
||
|
||
class FanhuajiConvertCommand(sublime_plugin.TextCommand): | ||
def is_enabled(self) -> bool: | ||
return sum(map(len, self.view.sel())) > 0 | ||
return self.view.has_non_empty_selection_region() | ||
|
||
def is_visible(self) -> bool: | ||
return self.is_enabled() | ||
|
||
def run(self, edit: sublime.Edit, args: Dict = {}) -> None: | ||
real_args = prepare_fanhuaji_convert_args(self.view) | ||
real_args.update(args) | ||
def run(self, edit: sublime.Edit, args: Optional[Dict] = None) -> None: | ||
args = prepare_fanhuaji_convert_args(self.view, args) | ||
|
||
try: | ||
result = self._do_api_convert(real_args) | ||
except requests.exceptions.ConnectionError as e: | ||
result = Fanhuaji.convert(args) | ||
except Exception as e: | ||
sublime.error_message(msg(f"Failed to reach the server: {e}")) | ||
return | ||
except requests.exceptions.RequestException as e: | ||
sublime.error_message(msg(f"Request exception: {e}")) | ||
return | ||
except ValueError as e: | ||
sublime.error_message(msg(f"Failed to decode the returned JSON: {e}")) | ||
return | ||
|
||
if int(result["code"]) != 0: | ||
sublime.error_message(msg(f'Error message from the server: {result["msg"]}')) | ||
return | ||
|
||
texts = result["data"]["text"].split(TEXT_DELIMITER) | ||
blocks = tuple({"region": z[0], "text": z[1]} for z in zip(self.view.sel(), texts)) | ||
|
||
for block in reversed(blocks): | ||
self.view.replace(edit, block["region"], block["text"]) | ||
|
||
def _do_api_convert(self, args: Dict) -> Dict: | ||
if get_setting("debug"): | ||
print_msg(f"Request with: {args}") | ||
|
||
url = get_setting("api_server") + "/convert" | ||
verify_ssl = bool(get_setting("ssl_cert_verification")) | ||
|
||
response = requests.post(url=url, data=args, headers=HTTP_HEADERS, verify=verify_ssl) | ||
region_text_pairs = tuple(zip(self.view.sel(), texts)) | ||
|
||
return sublime.decode_value(response.text) | ||
for region, text in reversed(region_text_pairs): | ||
self.view.replace(edit, region, text) |
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 |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from .constant import ST_VERSION, ST_PLATFORM_ARCH | ||
from .libs import requests | ||
from .log import print_msg | ||
from .settings import get_setting | ||
from .types import TD_ApiConvertResponse, TD_ConverterInfo | ||
from typing import Any, Dict, Tuple | ||
import sublime | ||
|
||
HTTP_HEADERS = { | ||
"user-agent": f"Sublime Text {ST_VERSION} ({ST_PLATFORM_ARCH}) Fanhuaji", | ||
} | ||
|
||
|
||
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, "繁", ""), | ||
}, | ||
) | ||
|
||
@classmethod | ||
def convert(cls, args: Dict[str, Any]) -> TD_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) | ||
except requests.exceptions.ConnectionError as e: | ||
raise RuntimeError(f"Failed to reach the server: {e}") | ||
except requests.exceptions.RequestException as e: | ||
raise RuntimeError(f"Request exception: {e}") | ||
|
||
return sublime.decode_value(response.text) |
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,42 +1,36 @@ | ||
from .constant import TEXT_DELIMITER | ||
from .constant import CONVERTERS_INFO | ||
from .settings import get_setting | ||
from .types import TD_ConverterInfo | ||
from functools import lru_cache | ||
from typing import Any, Dict | ||
from typing import Any, Dict, Optional | ||
import sublime | ||
|
||
|
||
@lru_cache() | ||
def get_converter_info(index: int) -> TD_ConverterInfo: | ||
return CONVERTERS_INFO[index] | ||
|
||
|
||
def prepare_fanhuaji_convert_args(view: sublime.View) -> Dict[str, Any]: | ||
args: Dict[str, Any] = get_setting("convert_params") | ||
def prepare_fanhuaji_convert_args(view: sublime.View, args: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: | ||
_args: Dict[str, Any] = get_setting("convert_params") | ||
|
||
# 轉換模組 | ||
if "modules" in args and isinstance(args["modules"], dict): | ||
args["modules"] = sublime.encode_value(args["modules"]) | ||
if "modules" in _args and isinstance(_args["modules"], dict): | ||
_args["modules"] = sublime.encode_value(_args["modules"]) | ||
|
||
# 轉換前取代 | ||
if "userPreReplace" in args and isinstance(args["userPreReplace"], dict): | ||
args["userPreReplace"] = "\n".join(f"{from_}={to_}" for from_, to_ in args["userPreReplace"].items()) | ||
if "userPreReplace" in _args and isinstance(_args["userPreReplace"], dict): | ||
_args["userPreReplace"] = "\n".join(f"{old}={new}" for old, new in _args["userPreReplace"].items()) | ||
|
||
# 轉換後取代 | ||
if "userPostReplace" in args and isinstance(args["userPostReplace"], dict): | ||
args["userPostReplace"] = "\n".join(f"{from_}={to_}" for from_, to_ in args["userPostReplace"].items()) | ||
if "userPostReplace" in _args and isinstance(_args["userPostReplace"], dict): | ||
_args["userPostReplace"] = "\n".join(f"{old}={new}" for old, new in _args["userPostReplace"].items()) | ||
|
||
# 保護字詞 | ||
if "userProtectReplace" in args and isinstance(args["userProtectReplace"], list): | ||
args["userProtectReplace"] = "\n".join(args["userProtectReplace"]) | ||
if "userProtectReplace" in _args and isinstance(_args["userProtectReplace"], list): | ||
_args["userProtectReplace"] = "\n".join(_args["userProtectReplace"]) | ||
|
||
# 參數: API 全域 | ||
args["apiKey"] = get_setting("api_key") | ||
args["prettify"] = False | ||
_args["apiKey"] = get_setting("api_key") | ||
_args["prettify"] = False | ||
|
||
# 參數: API convert 端點 | ||
args["text"] = TEXT_DELIMITER.join(view.substr(region) for region in view.sel()) | ||
args["diffEnable"] = False | ||
_args["text"] = TEXT_DELIMITER.join(view.substr(region) for region in view.sel()) | ||
_args["diffEnable"] = False | ||
|
||
_args.update(args or {}) | ||
|
||
return args | ||
return _args |
Oops, something went wrong.