Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ name: CI

on:
push:
pull_request:

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
Expand All @@ -17,9 +18,9 @@ jobs:
name: 'win64'

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v3
uses: actions/setup-python@v5
with:
python-version: '3.13'
architecture: ${{matrix.architecture}}
Expand All @@ -32,6 +33,9 @@ jobs:
- name: Build win bin
run: pyinstaller -F main.py -n 'chaoxing' --add-data "resource;resource" --hidden-import=chardet

- name: Smoke test executable
run: ./dist/chaoxing.exe --help

- name: Create release package
run: |
mkdir release
Expand Down
20 changes: 20 additions & 0 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Installed wheel
on: [push, pull_request, workflow_dispatch]
permissions:
contents: read
jobs:
installed-wheel:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.13'
- run: pip install build
- run: python -m build
- run: pip install dist/*.whl
- name: Run installed package outside checkout
working-directory: /tmp
run: |
chaoxing --help
python -c "from api.cxsecret_font import FontHashDAO; FontHashDAO()"
15 changes: 11 additions & 4 deletions api/cxsecret_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import json
import os
import sys
import site
from io import BytesIO
from pathlib import Path
from typing import Dict, IO, Optional, Union
Expand Down Expand Up @@ -42,10 +43,16 @@ def resource_path(relative_path: str) -> str:
# PyInstaller创建临时文件夹,定位路径
base_path = sys._MEIPASS
except Exception:
# 非打包环境,使用当前目录
base_path = os.path.abspath(".")

return os.path.join(base_path, relative_path)
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

candidate = os.path.join(base_path, relative_path)
if os.path.isfile(candidate):
return candidate
for data_root in (base_path, sys.prefix, site.getuserbase()):
installed = os.path.join(data_root, "share", "chaoxing", os.path.basename(relative_path))
if os.path.isfile(installed):
return installed
return candidate


class FontHashDAO:
Expand Down
9 changes: 9 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,16 @@ def format_time(num, suffix='', divisor=''):
return f"{mins:02d}:{sec:02d}"


def configure_console():
"""Keep Chinese CLI output usable with Windows redirected/frozen streams."""
if sys.platform == "win32":
for stream in (sys.stdout, sys.stderr):
if stream is not None and hasattr(stream, "reconfigure"):
stream.reconfigure(encoding="utf-8", errors="replace")


def main():
configure_console()
"""主程序入口"""
try:
# 初始化配置
Expand Down
13 changes: 12 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[build-system]
requires = ["setuptools>=68"]
build-backend = "setuptools.build_meta"

[project]
name = "chaoxing"
version = "3.1.3"
Expand All @@ -8,7 +12,14 @@ requires-python = ">=3.13,<4.0"
dynamic = ["dependencies"]

[tool.setuptools]
py-modules = []
py-modules = ["main"]
packages = ["api"]

[tool.setuptools.data-files]
"share/chaoxing" = ["config_template.ini", "resource/font_map_table.json"]

[project.scripts]
chaoxing = "main:main"

[tool.setuptools.dynamic]
dependencies = {file=["requirements.txt"]}
15 changes: 15 additions & 0 deletions tests/test_console.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import io
import sys
import unittest
from unittest import mock
import main

class ConsoleTest(unittest.TestCase):
def test_windows_redirected_output_preserves_chinese(self):
raw = io.BytesIO()
output = io.TextIOWrapper(raw, encoding="cp1252", newline="\n")
with mock.patch.object(sys, "platform", "win32"), mock.patch.object(sys, "stdout", output), mock.patch.object(sys, "stderr", output):
main.configure_console()
print("中文帮助")
output.flush()
self.assertEqual(raw.getvalue().decode("utf-8"), "中文帮助\n")
27 changes: 27 additions & 0 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import os
import tempfile
import unittest
from unittest import mock
from pathlib import Path
from api import cxsecret_font
from api.cxsecret_font import FontHashDAO

class ResourceTest(unittest.TestCase):
def test_font_mapping_loads_outside_repository(self):
previous = os.getcwd()
try:
with tempfile.TemporaryDirectory() as directory:
os.chdir(directory)
FontHashDAO()
finally:
os.chdir(previous)

def test_user_installation_resources_load_outside_prefix(self):
with tempfile.TemporaryDirectory() as directory:
root = Path(directory)
data = root / "user/share/chaoxing"
data.mkdir(parents=True)
(data / "font_map_table.json").write_text('{"example": "synthetic-hash"}')
with mock.patch.object(cxsecret_font, "__file__", str(root / "site/api/module.py")), mock.patch.object(cxsecret_font.sys, "prefix", str(root / "prefix")), mock.patch.object(cxsecret_font.site, "getuserbase", return_value=str(root / "user")):
dao = FontHashDAO()
self.assertEqual(dao.find_char("synthetic-hash"), "example")