Linglun-Converter/utils/io.py

181 lines
6.7 KiB
Python
Raw Normal View History

2023-02-02 02:11:28 +08:00
from typing import Any, Literal, Optional, TextIO
2023-02-12 16:14:09 +08:00
import urllib.request
import urllib.error
import TrimLog
from TrimLog import Console, object_constants
is_logging: bool = True
2023-02-02 02:11:28 +08:00
MainConsole = Console()
osc = object_constants.ObjectStateConstant()
logger = TrimLog.Logger(
is_logging=is_logging,
printing=not osc.isRelease,
2023-02-11 23:23:05 +08:00
in_suffix=".llc",
)
2023-02-11 23:23:05 +08:00
2023-02-12 16:14:09 +08:00
2023-02-11 23:23:05 +08:00
try:
myWords = (
2023-02-12 16:14:09 +08:00
urllib.request.urlopen(
'https://gitee.com/TriM-Organization/LinglunStudio/raw/master/resources/myWords.txt'
2023-02-11 23:23:05 +08:00
)
2023-02-12 16:14:09 +08:00
.read()
.decode('utf-8')
.strip("\n")
2023-02-11 23:23:05 +08:00
.split("\n")
)
2023-02-12 16:14:09 +08:00
except (ConnectionError, urllib.error.HTTPError) as E:
logger.warning(f"读取言·论信息发生 互联网连接 错误:\n{E}")
2023-02-12 03:42:56 +08:00
myWords = ["以梦想为驱使 创造属于自己的未来"]
2023-02-11 23:23:05 +08:00
# noinspection PyBroadException
2023-02-12 16:14:09 +08:00
except BaseException as E:
logger.warning(f"读取言·论信息发生 未知 错误:\n{E}")
2023-03-05 18:57:01 +08:00
myWords = ["灵光焕发 深艺献心"]
2023-02-02 02:11:28 +08:00
JustifyMethod = Literal["default", "left", "center", "right", "full"]
OverflowMethod = Literal["fold", "crop", "ellipsis", "ignore"]
# 高级的打印函数
def prt(
*objects: Any,
sep: str = " ",
end: str = "\n",
justify: Optional[JustifyMethod] = None,
overflow: Optional[OverflowMethod] = None,
no_wrap: Optional[bool] = None,
emoji: Optional[bool] = None,
markup: Optional[bool] = None,
highlight: Optional[bool] = None,
width: Optional[int] = None,
height: Optional[int] = None,
crop: bool = True,
soft_wrap: Optional[bool] = None,
new_line_start: bool = False,
) -> None:
"""打印到控制台。
Args:
objects (位置性的args): 要记录到终端的对象
sep (str, 可选): 要在打印数据之间写入的字符串默认为""
end (str, optio可选nal): 在打印数据结束时写入的字符串默认值为"\\\\n"
justify (str, 可选): 校正位置可为"default", "left", "right", "center" "full". 默认为`None`
overflow (str, 可选): 控制溢出"ignore"忽略, "crop"裁剪, "fold"折叠, "ellipsis"省略号默认为`None`
no_wrap (Optional[bool], 可选): 禁用文字包装默认为`None`
emoji (Optional[bool], 可选): 启用表情符号代码或使用控制台默认的`None`默认为`None`
markup (Optional[bool], 可选): 启用标记`None`使用控制台默认值默认为`None`
highlight (Optional[bool], 可选): 启用自动高亮`None`使用控制台默认值默认为`None`
width (Optional[int], 可选): 输出的宽度`None`自动检测默认为`None`
height
crop (Optional[bool], 可选): 裁剪输出到终端的宽度默认为`True`
soft_wrap (bool, 可选): 启用软包装模式禁止文字包装和裁剪`None``用于 控制台默认值默认为`None`
new_line_start (bool, False): 如果输出包含多行在开始时插入一个新行默认值为`False`
"""
MainConsole.print(
*objects,
sep=sep,
end=end,
style="#F0F2F4 on #121110",
justify=justify,
overflow=overflow,
no_wrap=no_wrap,
emoji=emoji,
markup=markup,
highlight=highlight,
width=width,
height=height,
crop=crop,
soft_wrap=soft_wrap,
new_line_start=new_line_start,
)
# 高级的输入函数
def ipt(
*objects: Any,
sep: str = " ",
justify: Optional[JustifyMethod] = None,
overflow: Optional[OverflowMethod] = None,
no_wrap: Optional[bool] = None,
emoji: Optional[bool] = None,
markup: Optional[bool] = None,
highlight: Optional[bool] = None,
width: Optional[int] = None,
height: Optional[int] = None,
crop: bool = True,
soft_wrap: Optional[bool] = None,
new_line_start: bool = False,
password: bool = False,
stream: Optional[TextIO] = None,
) -> str:
"""显示一个提示并等待用户的输入。
它的工作方式与Python内建的 :func:`input` 函数相同如果Python内建的 :mod:`readline` 模块先前已经加载则提供详细的行编辑和历史功能
Args:
objects (位置性的args): 要记录到终端的对象
sep (str, 可选): 要在打印数据之间写入的字符串默认为""
justify (str, 可选): 校正位置可为"default", "left", "right", "center" "full". 默认为`None`
overflow (str, 可选): 控制溢出"ignore"忽略, "crop"裁剪, "fold"折叠, "ellipsis"省略号默认为`None`
no_wrap (Optional[bool], 可选): 禁用文字包装默认为`None`
emoji (Optional[bool], 可选): 启用表情符号代码或使用控制台默认的`None`默认为`None`
markup (Optional[bool], 可选): 启用标记`None`使用控制台默认值默认为`None`
highlight (Optional[bool], 可选): 启用自动高亮`None`使用控制台默认值默认为`None`
width (Optional[int], 可选): 输出的宽度`None`自动检测默认为`None`
crop (Optional[bool], 可选): 裁剪输出到终端的宽度默认为`True`
height
soft_wrap (bool, 可选): 启用软包装模式禁止文字包装和裁剪`None``用于 控制台默认值默认为`None`
new_line_start (bool, False): 如果输出包含多行在开始时插入一个新行默认值为`False`
password (bool, 可选): 隐藏已经输入的文案默认值为`False`
stream (TextIO, 可选): 可选从文件中读取而非控制台默认为 `None`
Returns:
str: 从stdin读取的字符串
"""
MainConsole.print(
*objects,
sep=sep,
end="",
style="#F0F2F4 on #121110",
justify=justify,
overflow=overflow,
no_wrap=no_wrap,
emoji=emoji,
markup=markup,
highlight=highlight,
width=width,
height=height,
crop=crop,
soft_wrap=soft_wrap,
new_line_start=new_line_start,
)
return MainConsole.input(password=password, stream=stream)
def format_ipt(
notice: str,
fun,
err_note: str = "",
*extraArg,
):
"""循环输入,以某种格式
notice: 输入时的提示
fun: 格式函数
err_note: 输入不符格式时的提示
*extraArg: 对于函数的其他参数"""
while True:
result = ipt(notice)
# noinspection PyBroadException
try:
fun_result = fun(result, *extraArg)
break
2023-02-11 23:23:05 +08:00
# noinspection PyBroadException
except BaseException:
2023-02-02 02:11:28 +08:00
prt(err_note)
continue
return result, fun_result