mirror of
https://github.com/KenanZhu/AutoLibrary.git
synced 2026-06-18 23:43:02 +08:00
Compare commits
34 Commits
5e898180c7
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 6d05d4c7cb | |||
| 01f4ccaa0e | |||
| 0bad34d7a8 | |||
| 61d1b44402 | |||
| 72301c63fd | |||
| 609850ab60 | |||
| 8e14d45b71 | |||
| f6ef9af39d | |||
| 72dce83c55 | |||
| c337904010 | |||
| 779aad13b8 | |||
| f3360423e5 | |||
| bea12d5f0c | |||
| b24f39456e | |||
| bb63ee6f03 | |||
| 3ebebe015f | |||
| 02b3a62868 | |||
| d7e19dcd52 | |||
| 59c06b3a19 | |||
| b78fd2d1e4 | |||
| 2aace40a26 | |||
| df7ad92f7f | |||
| 910e3e3224 | |||
| f7167c13f4 | |||
| eb8da498a2 | |||
| b279b51b42 | |||
| 43336f98d2 | |||
| e77c561685 | |||
| 345cb95b98 | |||
| caa563e770 | |||
| 280028259f | |||
| a6bc103c73 | |||
| 2226e8ac90 | |||
| 106463b9e5 |
@@ -7,7 +7,6 @@ on:
|
|||||||
push:
|
push:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
|
|
||||||
pull_request:
|
pull_request:
|
||||||
branches:
|
branches:
|
||||||
- main
|
- main
|
||||||
@@ -15,7 +14,6 @@ on:
|
|||||||
- opened
|
- opened
|
||||||
- synchronize
|
- synchronize
|
||||||
- reopened
|
- reopened
|
||||||
# Allow manual trigger for testing
|
|
||||||
workflow_dispatch:
|
workflow_dispatch:
|
||||||
|
|
||||||
#
|
#
|
||||||
@@ -49,11 +47,13 @@ jobs:
|
|||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: '3.13'
|
python-version: '3.13'
|
||||||
|
cache: 'pip'
|
||||||
|
cache-dependency-path: requirements.txt
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirement.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
- name: Solve ddddocr compatibility and copy model files
|
- name: Solve ddddocr compatibility and copy model files
|
||||||
run: |
|
run: |
|
||||||
@@ -125,7 +125,7 @@ jobs:
|
|||||||
" binaries=[],"
|
" binaries=[],"
|
||||||
" datas=["
|
" datas=["
|
||||||
" ('models\\common.onnx', 'ddddocr'),"
|
" ('models\\common.onnx', 'ddddocr'),"
|
||||||
" ('src\\gui\\resources\\icons\\AutoLibrary_32x32.ico', 'gui\\resources\\icons'),"
|
" ('src\\gui\\resources\\icons\\AutoLibrary_Logo_64.ico', 'gui\\resources\\icons'),"
|
||||||
" ],"
|
" ],"
|
||||||
" hiddenimports=[],"
|
" hiddenimports=[],"
|
||||||
" hookspath=[],"
|
" hookspath=[],"
|
||||||
@@ -153,7 +153,7 @@ jobs:
|
|||||||
" target_arch=None,"
|
" target_arch=None,"
|
||||||
" codesign_identity=None,"
|
" codesign_identity=None,"
|
||||||
" entitlements_file=None,"
|
" entitlements_file=None,"
|
||||||
" icon=['src\\gui\\resources\\icons\\AutoLibrary_32x32.ico'],"
|
" icon=['src\\gui\\resources\\icons\\AutoLibrary_Logo_64.ico'],"
|
||||||
")"
|
")"
|
||||||
""
|
""
|
||||||
"coll = COLLECT("
|
"coll = COLLECT("
|
||||||
@@ -169,9 +169,11 @@ jobs:
|
|||||||
$specLines | Out-File -FilePath "Main.spec" -Encoding UTF8
|
$specLines | Out-File -FilePath "Main.spec" -Encoding UTF8
|
||||||
|
|
||||||
Write-Host "✓ Main.spec (non-single file) generated successfully"
|
Write-Host "✓ Main.spec (non-single file) generated successfully"
|
||||||
Write-Host "`nGenerated Main.spec ============"
|
Write-Host "`n========================================"
|
||||||
|
Write-Host "Generated Main.spec"
|
||||||
|
Write-Host "========================================"
|
||||||
Get-Content "Main.spec" | Write-Host
|
Get-Content "Main.spec" | Write-Host
|
||||||
Write-Host "==================================`n"
|
Write-Host "========================================`n"
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|
||||||
- name: Build with PyInstaller
|
- name: Build with PyInstaller
|
||||||
@@ -186,7 +188,7 @@ jobs:
|
|||||||
$distDir = "dist/AutoLibrary-$version"
|
$distDir = "dist/AutoLibrary-$version"
|
||||||
$zipName = "AutoLibrary.$tagName-windows-x86_64.zip"
|
$zipName = "AutoLibrary.$tagName-windows-x86_64.zip"
|
||||||
|
|
||||||
echo "ZIP_NAME=$zipName" >> $env:GITHUB_OUTPUT
|
"ZIP_NAME=$zipName" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
|
||||||
|
|
||||||
Write-Host "Looking for distribution directory: $distDir"
|
Write-Host "Looking for distribution directory: $distDir"
|
||||||
if (Test-Path $distDir) {
|
if (Test-Path $distDir) {
|
||||||
@@ -210,12 +212,283 @@ jobs:
|
|||||||
|
|
||||||
- name: Upload build summary
|
- name: Upload build summary
|
||||||
run: |
|
run: |
|
||||||
Write-Host "## Build Test Summary" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8
|
"## Build Test Summary" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8
|
||||||
Write-Host "" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "✓ Pull request build test completed successfully!" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"========================================" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Version: ${{ steps.get_version.outputs.VERSION }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"✓ Pull request build test completed successfully!" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Tag: ${{ steps.get_version.outputs.TAG_NAME }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Version: ${{ steps.get_version.outputs.VERSION }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Pull Request #${{ github.event.pull_request.number }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Tag: ${{ steps.get_version.outputs.TAG_NAME }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Branch: ${{ github.event.pull_request.head.ref }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Pull Request #${{ github.event.pull_request.number || 'N/A' }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Event: ${{ github.event_name }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Branch: ${{ github.event.pull_request.head.ref || github.ref }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
|
"- Event: ${{ github.event_name }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Build macOS
|
||||||
|
#
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
with:
|
||||||
|
ref: ${{ github.ref }}
|
||||||
|
|
||||||
|
- name: Get version info
|
||||||
|
id: get_version
|
||||||
|
run: |
|
||||||
|
version="pr-test"
|
||||||
|
tag_name="pr-test"
|
||||||
|
echo "✓ Mode: Pull Request Test Build"
|
||||||
|
echo "✓ Tag: $tag_name"
|
||||||
|
echo "✓ Version: $version"
|
||||||
|
echo "VERSION=$version" >> $GITHUB_OUTPUT
|
||||||
|
echo "TAG_NAME=$tag_name" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v6
|
||||||
|
with:
|
||||||
|
python-version: '3.13'
|
||||||
|
cache: 'pip'
|
||||||
|
cache-dependency-path: requirements.txt
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
- name: Solve ddddocr compatibility and copy model files
|
||||||
|
run: |
|
||||||
|
ddddocr_path=$(python -c "import ddddocr, os; print(os.path.dirname(ddddocr.__file__))")
|
||||||
|
echo "ddddocr package location: $ddddocr_path"
|
||||||
|
|
||||||
|
init_file="$ddddocr_path/__init__.py"
|
||||||
|
if [ -f "$init_file" ]; then
|
||||||
|
echo "Fixing ddddocr compatibility in: $init_file"
|
||||||
|
# macOS 使用 BSD sed,要求 -i 后跟备份后缀名(空字符串 = 不备份)
|
||||||
|
sed -i '' 's/Image\.ANTIALIAS/Image.Resampling.LANCZOS/g' "$init_file"
|
||||||
|
echo "✓ Fixed: Image.ANTIALIAS -> Image.Resampling.LANCZOS"
|
||||||
|
else
|
||||||
|
echo "✗ ddddocr __init__.py not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p models
|
||||||
|
echo "✓ Created models directory"
|
||||||
|
|
||||||
|
# 多路径 fallback 搜索 ONNX 模型,提升 ddddocr 版本兼容性
|
||||||
|
onnx_source=""
|
||||||
|
onnx_dest="models/common.onnx"
|
||||||
|
candidate_paths=(
|
||||||
|
"$ddddocr_path/common.onnx"
|
||||||
|
"$ddddocr_path/models/common.onnx"
|
||||||
|
"$ddddocr_path/ddddocr/common.onnx"
|
||||||
|
)
|
||||||
|
for candidate in "${candidate_paths[@]}"; do
|
||||||
|
if [ -f "$candidate" ]; then
|
||||||
|
onnx_source="$candidate"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
# 若以上候选均未命中,回退到全包搜索
|
||||||
|
if [ -z "$onnx_source" ]; then
|
||||||
|
echo "⚠ 未在已知路径找到 ONNX 模型,回退到全包搜索..."
|
||||||
|
onnx_source=$(find "$ddddocr_path" -name "*.onnx" -type f 2>/dev/null | head -1)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$onnx_source" ] && [ -f "$onnx_source" ]; then
|
||||||
|
cp "$onnx_source" "$onnx_dest"
|
||||||
|
echo "✓ ONNX model copied: $onnx_source -> $onnx_dest"
|
||||||
|
else
|
||||||
|
echo "✗ ONNX model not found in ddddocr package"
|
||||||
|
echo " Searched directory: $ddddocr_path"
|
||||||
|
ls -la "$ddddocr_path/" || true
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$onnx_dest" ]; then
|
||||||
|
file_size=$(du -h "$onnx_dest" | cut -f1)
|
||||||
|
echo "✓ Model file verified: $onnx_dest (Size: $file_size)"
|
||||||
|
else
|
||||||
|
echo "✗ Failed to copy model file"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Compile Qt Resource files
|
||||||
|
run: |
|
||||||
|
cd batchs
|
||||||
|
bash compile_rc.sh
|
||||||
|
|
||||||
|
- name: Compile Qt UI files
|
||||||
|
run: |
|
||||||
|
cd batchs
|
||||||
|
bash compile_ui.sh
|
||||||
|
|
||||||
|
- name: Generate 'Main.spec'
|
||||||
|
env:
|
||||||
|
APP_VERSION: ${{ steps.get_version.outputs.VERSION }}
|
||||||
|
run: |
|
||||||
|
version="$APP_VERSION"
|
||||||
|
exe_name="AutoLibrary-$version"
|
||||||
|
echo "Generating Main.spec for version: $version"
|
||||||
|
echo "App name: $exe_name"
|
||||||
|
|
||||||
|
printf '%s\n' \
|
||||||
|
'# -*- mode: python ; coding: utf-8 -*-' \
|
||||||
|
'' \
|
||||||
|
'a = Analysis(' \
|
||||||
|
" ['src/Main.py']," \
|
||||||
|
' pathex=[],' \
|
||||||
|
' binaries=[],' \
|
||||||
|
' datas=[' \
|
||||||
|
" ('models/common.onnx', 'ddddocr')," \
|
||||||
|
" ('src/gui/resources/icons/AutoLibrary_Logo_64.ico', 'gui/resources/icons')," \
|
||||||
|
' ],' \
|
||||||
|
' hiddenimports=[],' \
|
||||||
|
' hookspath=[],' \
|
||||||
|
' hooksconfig={},' \
|
||||||
|
' runtime_hooks=[],' \
|
||||||
|
' excludes=[],' \
|
||||||
|
' noarchive=False,' \
|
||||||
|
' optimize=0,' \
|
||||||
|
')' \
|
||||||
|
'pyz = PYZ(a.pure)' \
|
||||||
|
'' \
|
||||||
|
'exe = EXE(' \
|
||||||
|
' pyz,' \
|
||||||
|
' a.scripts,' \
|
||||||
|
' [],' \
|
||||||
|
' exclude_binaries=True,' \
|
||||||
|
" name='AutoLibrary'," \
|
||||||
|
' debug=False,' \
|
||||||
|
' bootloader_ignore_signals=False,' \
|
||||||
|
' strip=False,' \
|
||||||
|
' upx=True,' \
|
||||||
|
' upx_exclude=[],' \
|
||||||
|
' runtime_tmpdir=None,' \
|
||||||
|
' console=False,' \
|
||||||
|
' disable_windowed_traceback=False,' \
|
||||||
|
' argv_emulation=False,' \
|
||||||
|
' target_arch=None,' \
|
||||||
|
' codesign_identity=None,' \
|
||||||
|
' entitlements_file=None,' \
|
||||||
|
" icon=['src/gui/resources/icons/AutoLibrary_Logo_64.ico']," \
|
||||||
|
')' \
|
||||||
|
'' \
|
||||||
|
'coll = COLLECT(' \
|
||||||
|
' exe,' \
|
||||||
|
' a.binaries,' \
|
||||||
|
' a.zipfiles,' \
|
||||||
|
' a.datas,' \
|
||||||
|
' strip=False,' \
|
||||||
|
' upx=True,' \
|
||||||
|
' upx_exclude=[],' \
|
||||||
|
" name='${exe_name}'," \
|
||||||
|
')' \
|
||||||
|
'' \
|
||||||
|
'app = BUNDLE(' \
|
||||||
|
' coll,' \
|
||||||
|
" name='AutoLibrary.app'," \
|
||||||
|
" icon='src/gui/resources/icons/AutoLibrary_Logo.icns'," \
|
||||||
|
" bundle_identifier='com.kenanzhu.autolibrary'," \
|
||||||
|
' info_plist={' \
|
||||||
|
" 'NSHighResolutionCapable': 'True'," \
|
||||||
|
" 'CFBundleName': 'AutoLibrary'," \
|
||||||
|
" 'CFBundleDisplayName': 'AutoLibrary'," \
|
||||||
|
" 'CFBundleShortVersionString': '${version}'," \
|
||||||
|
" 'CFBundleVersion': '${version}'," \
|
||||||
|
" 'CFBundleExecutable': 'AutoLibrary'," \
|
||||||
|
" 'NSPrincipalClass': 'NSApplication'," \
|
||||||
|
" 'LSMinimumSystemVersion': '11.0'," \
|
||||||
|
" 'NSRequiresAquaSystemAppearance': 'False'," \
|
||||||
|
" 'NSHumanReadableCopyright': 'Copyright 2025 - 2026 KenanZhu. All rights reserved.'," \
|
||||||
|
' },' \
|
||||||
|
')' \
|
||||||
|
> Main.spec
|
||||||
|
|
||||||
|
echo "✓ Main.spec generated successfully"
|
||||||
|
echo ""
|
||||||
|
echo "========================================"
|
||||||
|
echo "Generated Main.spec"
|
||||||
|
echo "========================================"
|
||||||
|
cat Main.spec
|
||||||
|
echo "========================================"
|
||||||
|
|
||||||
|
- name: Build with PyInstaller
|
||||||
|
run: |
|
||||||
|
python -m PyInstaller Main.spec
|
||||||
|
|
||||||
|
- name: Create DMG
|
||||||
|
run: |
|
||||||
|
tag_name="${{ steps.get_version.outputs.TAG_NAME }}"
|
||||||
|
dmg_name="AutoLibrary.$tag_name-macos-arm64.dmg"
|
||||||
|
app_path="dist/AutoLibrary.app"
|
||||||
|
|
||||||
|
if [ ! -d "$app_path" ]; then
|
||||||
|
echo "✗ App bundle not found: $app_path"
|
||||||
|
echo "Files in dist directory:"
|
||||||
|
ls -la dist/
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Creating DMG from: $app_path"
|
||||||
|
xattr -cr "$app_path" 2>/dev/null || true
|
||||||
|
|
||||||
|
tmp_dmg_dir=$(mktemp -d)
|
||||||
|
cp -R "$app_path" "$tmp_dmg_dir/"
|
||||||
|
ln -s /Applications "$tmp_dmg_dir/Applications"
|
||||||
|
|
||||||
|
hdiutil create \
|
||||||
|
-volname "AutoLibrary $tag_name" \
|
||||||
|
-srcfolder "$tmp_dmg_dir" \
|
||||||
|
-ov \
|
||||||
|
-format UDZO \
|
||||||
|
"$dmg_name"
|
||||||
|
|
||||||
|
rm -rf "$tmp_dmg_dir"
|
||||||
|
|
||||||
|
if [ -f "$dmg_name" ]; then
|
||||||
|
dmg_size=$(du -h "$dmg_name" | cut -f1)
|
||||||
|
echo "✓ DMG created: $dmg_name (Size: $dmg_size)"
|
||||||
|
else
|
||||||
|
echo "✗ Failed to create DMG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✓ Artifacts ready:"
|
||||||
|
echo " - $dmg_name"
|
||||||
|
echo " - $app_path"
|
||||||
|
|
||||||
|
- name: Archive artifacts
|
||||||
|
uses: actions/upload-artifact@v6
|
||||||
|
with:
|
||||||
|
name: AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-macos-arm64
|
||||||
|
path: |
|
||||||
|
AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-macos-arm64.dmg
|
||||||
|
dist/AutoLibrary.app/**
|
||||||
|
retention-days: 7
|
||||||
|
|
||||||
|
- name: Upload build summary
|
||||||
|
run: |
|
||||||
|
tag_name="${{ steps.get_version.outputs.TAG_NAME }}"
|
||||||
|
version="${{ steps.get_version.outputs.VERSION }}"
|
||||||
|
dmg_name="AutoLibrary.$tag_name-macos-arm64.dmg"
|
||||||
|
|
||||||
|
{
|
||||||
|
echo "## Build Test Summary (macOS)"
|
||||||
|
echo ""
|
||||||
|
echo "========================================"
|
||||||
|
echo "✓ Pull request build test completed successfully!"
|
||||||
|
echo "- Version: $version"
|
||||||
|
echo "- Tag: $tag_name"
|
||||||
|
echo "- Pull Request #${{ github.event.pull_request.number || 'N/A' }}"
|
||||||
|
echo "- Branch: ${{ github.event.pull_request.head.ref || github.ref }}"
|
||||||
|
echo "- Event: ${{ github.event_name }}"
|
||||||
|
echo "- DMG: $dmg_name"
|
||||||
|
echo ""
|
||||||
|
echo "### How to test on macOS"
|
||||||
|
echo '1. Download the DMG artifact'
|
||||||
|
echo '2. Open the DMG and drag AutoLibrary.app to /Applications'
|
||||||
|
echo '3. Right-click → Open the app (to bypass Gatekeeper)'
|
||||||
|
} >> $GITHUB_STEP_SUMMARY
|
||||||
|
|||||||
+325
-19
@@ -1,7 +1,10 @@
|
|||||||
name: Build
|
name: Build
|
||||||
|
|
||||||
# This workflow compiles the application for Windows platform using PyInstaller, and
|
# This workflow compiles the application for Windows and macOS platforms using
|
||||||
# archives the built artifacts as 'AutoLibrary.<tag_name>-windows-x86_64.zip'.
|
# PyInstaller, and archives the built artifacts.
|
||||||
|
#
|
||||||
|
# - Windows: AutoLibrary.<tag_name>-windows-x86_64.zip
|
||||||
|
# - macOS: AutoLibrary.<tag_name>-macos-arm64.dmg
|
||||||
#
|
#
|
||||||
# It is triggered when called by the release workflow.
|
# It is triggered when called by the release workflow.
|
||||||
|
|
||||||
@@ -76,20 +79,22 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
$versionInfoFile = "src/gui/ALVersionInfo.py"
|
$versionInfoFile = "src/gui/ALVersionInfo.py"
|
||||||
Write-Host "Verifying $versionInfoFile content:"
|
Write-Host "Verifying $versionInfoFile content:"
|
||||||
Write-Host "=================================="
|
Write-Host "========================================"
|
||||||
Get-Content $versionInfoFile | Write-Host
|
Get-Content $versionInfoFile | Write-Host
|
||||||
Write-Host "=================================="
|
Write-Host "========================================"
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|
||||||
- name: Set up Python
|
- name: Set up Python
|
||||||
uses: actions/setup-python@v6
|
uses: actions/setup-python@v6
|
||||||
with:
|
with:
|
||||||
python-version: '3.13'
|
python-version: '3.13'
|
||||||
|
cache: 'pip'
|
||||||
|
cache-dependency-path: requirements.txt
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: |
|
run: |
|
||||||
python -m pip install --upgrade pip
|
python -m pip install --upgrade pip
|
||||||
pip install -r requirement.txt
|
pip install -r requirements.txt
|
||||||
|
|
||||||
- name: Solve ddddocr compatibility and copy model files
|
- name: Solve ddddocr compatibility and copy model files
|
||||||
run: |
|
run: |
|
||||||
@@ -161,7 +166,7 @@ jobs:
|
|||||||
" binaries=[],"
|
" binaries=[],"
|
||||||
" datas=["
|
" datas=["
|
||||||
" ('models\\common.onnx', 'ddddocr'),"
|
" ('models\\common.onnx', 'ddddocr'),"
|
||||||
" ('src\\gui\\resources\\icons\\AutoLibrary_32x32.ico', 'gui\\resources\\icons'),"
|
" ('src\\gui\\resources\\icons\\AutoLibrary_Logo_64.ico', 'gui\\resources\\icons'),"
|
||||||
" ],"
|
" ],"
|
||||||
" hiddenimports=[],"
|
" hiddenimports=[],"
|
||||||
" hookspath=[],"
|
" hookspath=[],"
|
||||||
@@ -189,7 +194,7 @@ jobs:
|
|||||||
" target_arch=None,"
|
" target_arch=None,"
|
||||||
" codesign_identity=None,"
|
" codesign_identity=None,"
|
||||||
" entitlements_file=None,"
|
" entitlements_file=None,"
|
||||||
" icon=['src\\gui\\resources\\icons\\AutoLibrary_32x32.ico'],"
|
" icon=['src\\gui\\resources\\icons\\AutoLibrary_Logo_64.ico'],"
|
||||||
")"
|
")"
|
||||||
""
|
""
|
||||||
"coll = COLLECT("
|
"coll = COLLECT("
|
||||||
@@ -205,9 +210,11 @@ jobs:
|
|||||||
$specLines | Out-File -FilePath "Main.spec" -Encoding UTF8
|
$specLines | Out-File -FilePath "Main.spec" -Encoding UTF8
|
||||||
|
|
||||||
Write-Host "✓ Main.spec (non-single file) generated successfully"
|
Write-Host "✓ Main.spec (non-single file) generated successfully"
|
||||||
Write-Host "`nGenerated Main.spec ============"
|
Write-Host "`n========================================"
|
||||||
|
Write-Host "Generated Main.spec"
|
||||||
|
Write-Host "========================================"
|
||||||
Get-Content "Main.spec" | Write-Host
|
Get-Content "Main.spec" | Write-Host
|
||||||
Write-Host "==================================`n"
|
Write-Host "========================================`n"
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|
||||||
- name: Build with PyInstaller
|
- name: Build with PyInstaller
|
||||||
@@ -222,7 +229,7 @@ jobs:
|
|||||||
$distDir = "dist/AutoLibrary-$version"
|
$distDir = "dist/AutoLibrary-$version"
|
||||||
$zipName = "AutoLibrary.$tagName-windows-x86_64.zip"
|
$zipName = "AutoLibrary.$tagName-windows-x86_64.zip"
|
||||||
|
|
||||||
echo "ZIP_NAME=$zipName" >> $env:GITHUB_OUTPUT
|
"ZIP_NAME=$zipName" | Out-File -FilePath $env:GITHUB_OUTPUT -Encoding utf8 -Append
|
||||||
|
|
||||||
Write-Host "Looking for distribution directory: $distDir"
|
Write-Host "Looking for distribution directory: $distDir"
|
||||||
if (Test-Path $distDir) {
|
if (Test-Path $distDir) {
|
||||||
@@ -242,16 +249,315 @@ jobs:
|
|||||||
name: AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-windows-x86_64
|
name: AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-windows-x86_64
|
||||||
path: |
|
path: |
|
||||||
${{ steps.zip_release.outputs.ZIP_NAME }}
|
${{ steps.zip_release.outputs.ZIP_NAME }}
|
||||||
retention-days: ${{ github.event_name != 'workflow_call' && 7 || 90 }}
|
retention-days: ${{ inputs.is_test == 'true' && 7 || 90 }}
|
||||||
|
|
||||||
- name: Upload build summary
|
- name: Upload build summary
|
||||||
if: ${{ github.event_name != 'workflow_call' }}
|
if: ${{ inputs.is_test == 'true' }}
|
||||||
run: |
|
run: |
|
||||||
Write-Host "## Build Summary" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8
|
"## Build Summary" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8
|
||||||
Write-Host "" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "✓ Build test completed successfully!" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"========================================" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Version: ${{ steps.get_version.outputs.VERSION }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"✓ Build test completed successfully!" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Tag: ${{ steps.get_version.outputs.TAG_NAME }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Version: ${{ steps.get_version.outputs.VERSION }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Event: ${{ github.event_name }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Tag: ${{ steps.get_version.outputs.TAG_NAME }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
Write-Host "- Ref: ${{ github.ref }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
"- Event: ${{ github.event_name }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
|
"- Ref: ${{ github.ref }}" | Out-File -FilePath $env:GITHUB_STEP_SUMMARY -Encoding utf8 -Append
|
||||||
shell: pwsh
|
shell: pwsh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Build macOS
|
||||||
|
#
|
||||||
|
|
||||||
|
build-macos:
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- name: Checkout code
|
||||||
|
uses: actions/checkout@v6
|
||||||
|
with:
|
||||||
|
ref: ${{ github.ref }}
|
||||||
|
|
||||||
|
- name: Download build version of ALVersionInfo.py
|
||||||
|
uses: actions/download-artifact@v6
|
||||||
|
with:
|
||||||
|
name: updated-version-info-for-build
|
||||||
|
path: src/gui/
|
||||||
|
|
||||||
|
- name: Get version info
|
||||||
|
id: get_version
|
||||||
|
run: |
|
||||||
|
is_test="${{ inputs.is_test }}"
|
||||||
|
if [ "$is_test" = "true" ]; then
|
||||||
|
version="test"
|
||||||
|
tag_name="test"
|
||||||
|
echo "✓ Mode: Test Build"
|
||||||
|
else
|
||||||
|
version="${{ inputs.version }}"
|
||||||
|
tag_name="${{ inputs.tag_name }}"
|
||||||
|
|
||||||
|
if [ -z "$version" ]; then
|
||||||
|
version="test"
|
||||||
|
tag_name="test"
|
||||||
|
echo "✓ Mode: Independent Build (No inputs provided)"
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✓ Tag: $tag_name"
|
||||||
|
echo "✓ Version: $version"
|
||||||
|
|
||||||
|
echo "VERSION=$version" >> $GITHUB_OUTPUT
|
||||||
|
echo "TAG_NAME=$tag_name" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Verify 'ALVersionInfo.py' was updated
|
||||||
|
run: |
|
||||||
|
version_info_file="src/gui/ALVersionInfo.py"
|
||||||
|
echo "Verifying $version_info_file content:"
|
||||||
|
echo "========================================"
|
||||||
|
cat "$version_info_file"
|
||||||
|
echo "========================================"
|
||||||
|
|
||||||
|
- name: Set up Python
|
||||||
|
uses: actions/setup-python@v6
|
||||||
|
with:
|
||||||
|
python-version: '3.13'
|
||||||
|
cache: 'pip'
|
||||||
|
cache-dependency-path: requirements.txt
|
||||||
|
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
python -m pip install --upgrade pip
|
||||||
|
pip install -r requirements.txt
|
||||||
|
|
||||||
|
- name: Solve ddddocr compatibility and copy model files
|
||||||
|
run: |
|
||||||
|
ddddocr_path=$(python -c "import ddddocr, os; print(os.path.dirname(ddddocr.__file__))")
|
||||||
|
echo "ddddocr package location: $ddddocr_path"
|
||||||
|
|
||||||
|
init_file="$ddddocr_path/__init__.py"
|
||||||
|
if [ -f "$init_file" ]; then
|
||||||
|
echo "Fixing ddddocr compatibility in: $init_file"
|
||||||
|
# macOS 使用 BSD sed,要求 -i 后跟备份后缀名(空字符串 = 不备份)
|
||||||
|
sed -i '' 's/Image\.ANTIALIAS/Image.Resampling.LANCZOS/g' "$init_file"
|
||||||
|
echo "✓ Fixed: Image.ANTIALIAS -> Image.Resampling.LANCZOS"
|
||||||
|
else
|
||||||
|
echo "✗ ddddocr __init__.py not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
mkdir -p models
|
||||||
|
echo "✓ Created models directory"
|
||||||
|
|
||||||
|
# 多路径 fallback 搜索 ONNX 模型,提升 ddddocr 版本兼容性
|
||||||
|
onnx_source=""
|
||||||
|
onnx_dest="models/common.onnx"
|
||||||
|
candidate_paths=(
|
||||||
|
"$ddddocr_path/common.onnx"
|
||||||
|
"$ddddocr_path/models/common.onnx"
|
||||||
|
"$ddddocr_path/ddddocr/common.onnx"
|
||||||
|
)
|
||||||
|
for candidate in "${candidate_paths[@]}"; do
|
||||||
|
if [ -f "$candidate" ]; then
|
||||||
|
onnx_source="$candidate"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
# 若以上候选均未命中,回退到全包搜索
|
||||||
|
if [ -z "$onnx_source" ]; then
|
||||||
|
echo "⚠ 未在已知路径找到 ONNX 模型,回退到全包搜索..."
|
||||||
|
onnx_source=$(find "$ddddocr_path" -name "*.onnx" -type f 2>/dev/null | head -1)
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -n "$onnx_source" ] && [ -f "$onnx_source" ]; then
|
||||||
|
cp "$onnx_source" "$onnx_dest"
|
||||||
|
echo "✓ ONNX model copied: $onnx_source -> $onnx_dest"
|
||||||
|
else
|
||||||
|
echo "✗ ONNX model not found in ddddocr package"
|
||||||
|
echo " Searched directory: $ddddocr_path"
|
||||||
|
ls -la "$ddddocr_path/" || true
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -f "$onnx_dest" ]; then
|
||||||
|
file_size=$(du -h "$onnx_dest" | cut -f1)
|
||||||
|
echo "✓ Model file verified: $onnx_dest (Size: $file_size)"
|
||||||
|
else
|
||||||
|
echo "✗ Failed to copy model file"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Compile Qt Resource files
|
||||||
|
run: |
|
||||||
|
cd batchs
|
||||||
|
bash compile_rc.sh
|
||||||
|
|
||||||
|
- name: Compile Qt UI files
|
||||||
|
run: |
|
||||||
|
cd batchs
|
||||||
|
bash compile_ui.sh
|
||||||
|
|
||||||
|
- name: Generate 'Main.spec'
|
||||||
|
env:
|
||||||
|
APP_VERSION: ${{ steps.get_version.outputs.VERSION }}
|
||||||
|
run: |
|
||||||
|
version="$APP_VERSION"
|
||||||
|
exe_name="AutoLibrary-$version"
|
||||||
|
echo "Generating Main.spec for version: $version"
|
||||||
|
echo "App name: $exe_name"
|
||||||
|
|
||||||
|
printf '%s\n' \
|
||||||
|
'# -*- mode: python ; coding: utf-8 -*-' \
|
||||||
|
'' \
|
||||||
|
'a = Analysis(' \
|
||||||
|
" ['src/Main.py']," \
|
||||||
|
' pathex=[],' \
|
||||||
|
' binaries=[],' \
|
||||||
|
' datas=[' \
|
||||||
|
" ('models/common.onnx', 'ddddocr')," \
|
||||||
|
" ('src/gui/resources/icons/AutoLibrary_Logo_64.ico', 'gui/resources/icons')," \
|
||||||
|
' ],' \
|
||||||
|
' hiddenimports=[],' \
|
||||||
|
' hookspath=[],' \
|
||||||
|
' hooksconfig={},' \
|
||||||
|
' runtime_hooks=[],' \
|
||||||
|
' excludes=[],' \
|
||||||
|
' noarchive=False,' \
|
||||||
|
' optimize=0,' \
|
||||||
|
')' \
|
||||||
|
'pyz = PYZ(a.pure)' \
|
||||||
|
'' \
|
||||||
|
'exe = EXE(' \
|
||||||
|
' pyz,' \
|
||||||
|
' a.scripts,' \
|
||||||
|
' [],' \
|
||||||
|
' exclude_binaries=True,' \
|
||||||
|
" name='AutoLibrary'," \
|
||||||
|
' debug=False,' \
|
||||||
|
' bootloader_ignore_signals=False,' \
|
||||||
|
' strip=False,' \
|
||||||
|
' upx=True,' \
|
||||||
|
' upx_exclude=[],' \
|
||||||
|
' runtime_tmpdir=None,' \
|
||||||
|
' console=False,' \
|
||||||
|
' disable_windowed_traceback=False,' \
|
||||||
|
' argv_emulation=False,' \
|
||||||
|
' target_arch=None,' \
|
||||||
|
' codesign_identity=None,' \
|
||||||
|
' entitlements_file=None,' \
|
||||||
|
" icon=['src/gui/resources/icons/AutoLibrary_Logo_64.ico']," \
|
||||||
|
')' \
|
||||||
|
'' \
|
||||||
|
'coll = COLLECT(' \
|
||||||
|
' exe,' \
|
||||||
|
' a.binaries,' \
|
||||||
|
' a.zipfiles,' \
|
||||||
|
' a.datas,' \
|
||||||
|
' strip=False,' \
|
||||||
|
' upx=True,' \
|
||||||
|
' upx_exclude=[],' \
|
||||||
|
" name='${exe_name}'," \
|
||||||
|
')' \
|
||||||
|
'' \
|
||||||
|
'app = BUNDLE(' \
|
||||||
|
' coll,' \
|
||||||
|
" name='AutoLibrary.app'," \
|
||||||
|
" icon='src/gui/resources/icons/AutoLibrary_Logo.icns'," \
|
||||||
|
" bundle_identifier='com.kenanzhu.autolibrary'," \
|
||||||
|
' info_plist={' \
|
||||||
|
" 'NSHighResolutionCapable': 'True'," \
|
||||||
|
" 'CFBundleName': 'AutoLibrary'," \
|
||||||
|
" 'CFBundleDisplayName': 'AutoLibrary'," \
|
||||||
|
" 'CFBundleShortVersionString': '${version}'," \
|
||||||
|
" 'CFBundleVersion': '${version}'," \
|
||||||
|
" 'CFBundleExecutable': 'AutoLibrary'," \
|
||||||
|
" 'NSPrincipalClass': 'NSApplication'," \
|
||||||
|
" 'LSMinimumSystemVersion': '11.0'," \
|
||||||
|
" 'NSRequiresAquaSystemAppearance': 'False'," \
|
||||||
|
" 'NSHumanReadableCopyright': 'Copyright 2025 - 2026 KenanZhu. All rights reserved.'," \
|
||||||
|
' },' \
|
||||||
|
')' \
|
||||||
|
> Main.spec
|
||||||
|
|
||||||
|
echo "✓ Main.spec generated successfully"
|
||||||
|
echo ""
|
||||||
|
echo "========================================"
|
||||||
|
echo "Generated Main.spec"
|
||||||
|
echo "========================================"
|
||||||
|
cat Main.spec
|
||||||
|
echo "========================================"
|
||||||
|
|
||||||
|
- name: Build with PyInstaller
|
||||||
|
run: |
|
||||||
|
python -m PyInstaller Main.spec
|
||||||
|
|
||||||
|
- name: Create DMG
|
||||||
|
run: |
|
||||||
|
tag_name="${{ steps.get_version.outputs.TAG_NAME }}"
|
||||||
|
dmg_name="AutoLibrary.$tag_name-macos-arm64.dmg"
|
||||||
|
app_path="dist/AutoLibrary.app"
|
||||||
|
|
||||||
|
if [ ! -d "$app_path" ]; then
|
||||||
|
echo "✗ App bundle not found: $app_path"
|
||||||
|
echo "Files in dist directory:"
|
||||||
|
ls -la dist/
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "Creating DMG from: $app_path"
|
||||||
|
xattr -cr "$app_path" 2>/dev/null || true
|
||||||
|
|
||||||
|
tmp_dmg_dir=$(mktemp -d)
|
||||||
|
cp -R "$app_path" "$tmp_dmg_dir/"
|
||||||
|
ln -s /Applications "$tmp_dmg_dir/Applications"
|
||||||
|
|
||||||
|
hdiutil create \
|
||||||
|
-volname "AutoLibrary $tag_name" \
|
||||||
|
-srcfolder "$tmp_dmg_dir" \
|
||||||
|
-ov \
|
||||||
|
-format UDZO \
|
||||||
|
"$dmg_name"
|
||||||
|
|
||||||
|
rm -rf "$tmp_dmg_dir"
|
||||||
|
|
||||||
|
if [ -f "$dmg_name" ]; then
|
||||||
|
dmg_size=$(du -h "$dmg_name" | cut -f1)
|
||||||
|
echo "✓ DMG created: $dmg_name (Size: $dmg_size)"
|
||||||
|
else
|
||||||
|
echo "✗ Failed to create DMG"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "✓ Artifacts ready:"
|
||||||
|
echo " - $dmg_name"
|
||||||
|
echo " - $app_path"
|
||||||
|
|
||||||
|
- name: Archive artifacts
|
||||||
|
uses: actions/upload-artifact@v6
|
||||||
|
with:
|
||||||
|
name: AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-macos-arm64
|
||||||
|
path: |
|
||||||
|
AutoLibrary.${{ steps.get_version.outputs.TAG_NAME }}-macos-arm64.dmg
|
||||||
|
dist/AutoLibrary.app/**
|
||||||
|
retention-days: ${{ inputs.is_test == 'true' && 7 || 90 }}
|
||||||
|
|
||||||
|
- name: Upload build summary
|
||||||
|
if: ${{ inputs.is_test == 'true' }}
|
||||||
|
run: |
|
||||||
|
tag_name="${{ steps.get_version.outputs.TAG_NAME }}"
|
||||||
|
version="${{ steps.get_version.outputs.VERSION }}"
|
||||||
|
dmg_name="AutoLibrary.$tag_name-macos-arm64.dmg"
|
||||||
|
|
||||||
|
{
|
||||||
|
echo "## Build Summary (macOS)"
|
||||||
|
echo ""
|
||||||
|
echo "========================================"
|
||||||
|
echo "✓ Build test completed successfully!"
|
||||||
|
echo "- Version: $version"
|
||||||
|
echo "- Tag: $tag_name"
|
||||||
|
echo "- Event: ${{ github.event_name }}"
|
||||||
|
echo "- Ref: ${{ github.ref }}"
|
||||||
|
echo "- DMG: $dmg_name"
|
||||||
|
echo ""
|
||||||
|
echo "### How to test on macOS"
|
||||||
|
echo '1. Download the DMG artifact'
|
||||||
|
echo '2. Open the DMG and drag AutoLibrary.app to /Applications'
|
||||||
|
echo '3. Right-click → Open the app (to bypass Gatekeeper)'
|
||||||
|
} >> $GITHUB_STEP_SUMMARY
|
||||||
|
|||||||
@@ -83,7 +83,9 @@ jobs:
|
|||||||
|
|
||||||
echo "✓ File replaced: $FILE_PATH"
|
echo "✓ File replaced: $FILE_PATH"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Updated file content ==================="
|
echo "========================================"
|
||||||
|
echo "Updated file content"
|
||||||
|
echo "========================================"
|
||||||
cat "$FILE_PATH"
|
cat "$FILE_PATH"
|
||||||
echo "========================================"
|
echo "========================================"
|
||||||
|
|
||||||
@@ -151,3 +153,9 @@ jobs:
|
|||||||
COMMIT_SHA=$(git rev-parse --short HEAD)
|
COMMIT_SHA=$(git rev-parse --short HEAD)
|
||||||
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
|
echo "commit_sha=$COMMIT_SHA" >> $GITHUB_OUTPUT
|
||||||
echo "✓ New commit SHA: $COMMIT_SHA"
|
echo "✓ New commit SHA: $COMMIT_SHA"
|
||||||
|
echo "## Commit Release Summary" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "========================================" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Version: ${{ inputs.version }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Tag: ${{ inputs.tag_name }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Commit SHA: $COMMIT_SHA" >> $GITHUB_STEP_SUMMARY
|
||||||
|
|||||||
@@ -21,8 +21,10 @@ name: Release
|
|||||||
# Commits version changes to release branch and creates the release tag.
|
# Commits version changes to release branch and creates the release tag.
|
||||||
|
|
||||||
# 4. Build:
|
# 4. Build:
|
||||||
# Compiles the application for Windows platform using PyInstaller, and
|
# Compiles the application for Windows and macOS platforms using PyInstaller, and
|
||||||
# archives the built artifacts as 'AutoLibrary.<tag_name>-windows-x86_64.zip'.
|
# archives the built artifacts.
|
||||||
|
# - Windows: AutoLibrary.<tag_name>-windows-x86_64.zip
|
||||||
|
# - macOS: AutoLibrary.<tag_name>-macos-arm64.dmg
|
||||||
|
|
||||||
# 5. Release:
|
# 5. Release:
|
||||||
# Creates GitHub release with generated artifacts and release notes
|
# Creates GitHub release with generated artifacts and release notes
|
||||||
@@ -47,7 +49,7 @@ on:
|
|||||||
jobs:
|
jobs:
|
||||||
#
|
#
|
||||||
# Start :
|
# Start :
|
||||||
# virtual job that indacates the start of the release process
|
# virtual job that indicates the start of the release process
|
||||||
#
|
#
|
||||||
|
|
||||||
start:
|
start:
|
||||||
@@ -158,7 +160,7 @@ jobs:
|
|||||||
needs:
|
needs:
|
||||||
- update-version
|
- update-version
|
||||||
- commit-release
|
- commit-release
|
||||||
if: always() && needs.update-version.result == 'success' && needs.commit-release.result == 'success'
|
if: always() && needs.update-version.result == 'success' && (needs.commit-release.result == 'success' || needs.commit-release.result == 'skipped')
|
||||||
uses: ./.github/workflows/build.yml
|
uses: ./.github/workflows/build.yml
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -181,12 +183,18 @@ jobs:
|
|||||||
contents: write
|
contents: write
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Download artifacts
|
- name: Download Windows artifacts
|
||||||
uses: actions/download-artifact@v6
|
uses: actions/download-artifact@v6
|
||||||
with:
|
with:
|
||||||
name: AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-windows-x86_64
|
name: AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-windows-x86_64
|
||||||
path: artifacts/
|
path: artifacts/
|
||||||
|
|
||||||
|
- name: Download macOS artifacts
|
||||||
|
uses: actions/download-artifact@v6
|
||||||
|
with:
|
||||||
|
name: AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-macos-arm64
|
||||||
|
path: artifacts/
|
||||||
|
|
||||||
- name: Create release
|
- name: Create release
|
||||||
id: create_release
|
id: create_release
|
||||||
uses: softprops/action-gh-release@v2
|
uses: softprops/action-gh-release@v2
|
||||||
@@ -195,6 +203,7 @@ jobs:
|
|||||||
name: AutoLibrary ${{ needs.extract-version.outputs.tag_name }}
|
name: AutoLibrary ${{ needs.extract-version.outputs.tag_name }}
|
||||||
files: |
|
files: |
|
||||||
artifacts/AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-windows-x86_64.zip
|
artifacts/AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-windows-x86_64.zip
|
||||||
|
artifacts/AutoLibrary.${{ needs.extract-version.outputs.tag_name }}-macos-arm64.dmg
|
||||||
draft: false
|
draft: false
|
||||||
prerelease: ${{ needs.extract-version.outputs.is_rc == 'true' }}
|
prerelease: ${{ needs.extract-version.outputs.is_rc == 'true' }}
|
||||||
generate_release_notes: true
|
generate_release_notes: true
|
||||||
@@ -205,7 +214,7 @@ jobs:
|
|||||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
# End :
|
# End :
|
||||||
# virtual job that indacates the end of the release process
|
# virtual job that indicates the end of the release process
|
||||||
#
|
#
|
||||||
|
|
||||||
end:
|
end:
|
||||||
@@ -227,7 +236,7 @@ jobs:
|
|||||||
- release
|
- release
|
||||||
- extract-version
|
- extract-version
|
||||||
- commit-release
|
- commit-release
|
||||||
if: ${{ needs.release.result == 'success' && needs.commit-release.result == 'success' }}
|
if: ${{ needs.release.result == 'success' && (needs.commit-release.result == 'success' || needs.commit-release.result == 'skipped') }}
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -267,9 +276,13 @@ jobs:
|
|||||||
git checkout ${MAIN_BRANCH}
|
git checkout ${MAIN_BRANCH}
|
||||||
|
|
||||||
# Show branch status before merge
|
# Show branch status before merge
|
||||||
echo "=== Branch status before merge ==="
|
echo "========================================"
|
||||||
|
echo "Branch status before merge"
|
||||||
|
echo "========================================"
|
||||||
git log --oneline --graph --all -5
|
git log --oneline --graph --all -5
|
||||||
echo "=== Diff between ${MAIN_BRANCH} and origin/${BRANCH_NAME} ==="
|
echo "========================================"
|
||||||
|
echo "Diff: ${MAIN_BRANCH} vs origin/${BRANCH_NAME}"
|
||||||
|
echo "========================================"
|
||||||
git diff ${MAIN_BRANCH} origin/${BRANCH_NAME} --stat || echo "No differences found"
|
git diff ${MAIN_BRANCH} origin/${BRANCH_NAME} --stat || echo "No differences found"
|
||||||
|
|
||||||
# Force create a merge commit even if there are no changes
|
# Force create a merge commit even if there are no changes
|
||||||
@@ -279,7 +292,9 @@ jobs:
|
|||||||
-m "chore(release): merge ${BRANCH_NAME} to ${MAIN_BRANCH} [auto release commit]"
|
-m "chore(release): merge ${BRANCH_NAME} to ${MAIN_BRANCH} [auto release commit]"
|
||||||
|
|
||||||
# Show merge result
|
# Show merge result
|
||||||
echo "=== Merge result ==="
|
echo "========================================"
|
||||||
|
echo "Merge result"
|
||||||
|
echo "========================================"
|
||||||
git log --oneline --graph -3
|
git log --oneline --graph -3
|
||||||
|
|
||||||
# Push to main
|
# Push to main
|
||||||
@@ -310,6 +325,7 @@ jobs:
|
|||||||
|
|
||||||
echo "## Release Cleanup Summary" >> $GITHUB_STEP_SUMMARY
|
echo "## Release Cleanup Summary" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "========================================" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "✓ Release completed successfully!" >> $GITHUB_STEP_SUMMARY
|
echo "✓ Release completed successfully!" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "" >> $GITHUB_STEP_SUMMARY
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
echo "### Actions Performed:" >> $GITHUB_STEP_SUMMARY
|
echo "### Actions Performed:" >> $GITHUB_STEP_SUMMARY
|
||||||
|
|||||||
@@ -128,10 +128,15 @@ jobs:
|
|||||||
echo "Build version file location: $VER_INFO_BUILDFILE"
|
echo "Build version file location: $VER_INFO_BUILDFILE"
|
||||||
echo "Commit version file location: $VER_INFO_COMMITFILE"
|
echo "Commit version file location: $VER_INFO_COMMITFILE"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Build version ALVersionInfo.py content ="
|
echo "========================================"
|
||||||
|
echo "Build version ALVersionInfo.py"
|
||||||
|
echo "========================================"
|
||||||
cat "$VER_INFO_BUILDFILE"
|
cat "$VER_INFO_BUILDFILE"
|
||||||
|
echo "========================================"
|
||||||
echo ""
|
echo ""
|
||||||
echo "Commit version ALVersionInfo.py content "
|
echo "========================================"
|
||||||
|
echo "Commit version ALVersionInfo.py"
|
||||||
|
echo "========================================"
|
||||||
cat "$VER_INFO_COMMITFILE"
|
cat "$VER_INFO_COMMITFILE"
|
||||||
echo "========================================"
|
echo "========================================"
|
||||||
|
|
||||||
@@ -140,11 +145,16 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
if git diff --quiet src/gui/ALVersionInfo.py; then
|
if git diff --quiet src/gui/ALVersionInfo.py; then
|
||||||
echo "has_changes=false" >> $GITHUB_OUTPUT
|
echo "has_changes=false" >> $GITHUB_OUTPUT
|
||||||
echo "! No changes detected in ALVersionInfo.py"
|
echo "⚠ No changes detected in ALVersionInfo.py"
|
||||||
else
|
else
|
||||||
echo "has_changes=true" >> $GITHUB_OUTPUT
|
echo "has_changes=true" >> $GITHUB_OUTPUT
|
||||||
echo "✓ ALVersionInfo.py has been modified"
|
echo "✓ ALVersionInfo.py has been modified"
|
||||||
fi
|
fi
|
||||||
|
echo "## Update Version Summary" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "========================================" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Version: ${{ steps.get_version.outputs.VERSION }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
echo "- Tag: ${{ steps.get_version.outputs.TAG_NAME }}" >> $GITHUB_STEP_SUMMARY
|
||||||
|
|
||||||
- name: Upload modified ALVersionInfo.py ready for build
|
- name: Upload modified ALVersionInfo.py ready for build
|
||||||
if: steps.check_changes.outputs.has_changes == 'true'
|
if: steps.check_changes.outputs.has_changes == 'true'
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
6. 定时任务 - 使用内置定时任务管理,添加定时任务,指定时间后按当前预约信息自动运行,支持设置重复任务
|
6. 定时任务 - 使用内置定时任务管理,添加定时任务,指定时间后按当前预约信息自动运行,支持设置重复任务
|
||||||
7. 驱动管理 - 内置浏览器驱动自动管理,支持自动检测浏览器版本并下载对应驱动,无需手动下载
|
7. 驱动管理 - 内置浏览器驱动自动管理,支持自动检测浏览器版本并下载对应驱动,无需手动下载
|
||||||
|
|
||||||
*具体操作方法和注意事项请访问我们的 [帮助手册](https://www.autolibrary.kenanzhu.com/manuals)*
|
*具体操作方法和注意事项请访问我们的 [帮助手册](https://manuals.autolibrary.kenanzhu.com)*
|
||||||
|
|
||||||
### 如何使用
|
### 如何使用
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
This folder is used to store the manuals.
|
||||||
|
|
||||||
|
Our manuals are available at https://manuals.autolibrary.kenanzhu.com
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
This folder is used to store the manuals.
|
|
||||||
|
|
||||||
Our manuals are available at https://www.autolibrary.kenanzhu.com/manuals
|
|
||||||
Binary file not shown.
@@ -0,0 +1,39 @@
|
|||||||
|
attrs==26.1.0
|
||||||
|
certifi==2026.2.25
|
||||||
|
cffi==2.0.0
|
||||||
|
charset-normalizer==3.4.6
|
||||||
|
ddddocr==1.0.6
|
||||||
|
flatbuffers==25.12.19
|
||||||
|
h11==0.16.0
|
||||||
|
idna==3.11
|
||||||
|
lupa==2.8
|
||||||
|
mpmath==1.3.0
|
||||||
|
numpy==2.4.3
|
||||||
|
onnxruntime==1.24.4
|
||||||
|
outcome==1.3.0.post0
|
||||||
|
packaging==26.0
|
||||||
|
pefile==2024.8.26
|
||||||
|
pillow==12.1.1
|
||||||
|
protobuf==7.34.0
|
||||||
|
pybrowsers==1.3.2
|
||||||
|
pycparser==3.0
|
||||||
|
pyinstaller==6.19.0
|
||||||
|
pyinstaller-hooks-contrib==2026.3
|
||||||
|
PySide6==6.10.2
|
||||||
|
PySide6_Addons==6.10.2
|
||||||
|
PySide6_Essentials==6.10.2
|
||||||
|
PySocks==1.7.1
|
||||||
|
pywin32-ctypes==0.2.3
|
||||||
|
requests==2.32.5
|
||||||
|
selenium==4.38.0
|
||||||
|
setuptools==82.0.1
|
||||||
|
shiboken6==6.10.2
|
||||||
|
sniffio==1.3.1
|
||||||
|
sortedcontainers==2.4.0
|
||||||
|
sympy==1.14.0
|
||||||
|
trio==0.33.0
|
||||||
|
trio-websocket==0.12.2
|
||||||
|
typing_extensions==4.15.0
|
||||||
|
urllib3==2.6.3
|
||||||
|
websocket-client==1.9.0
|
||||||
|
wsproto==1.3.2
|
||||||
+1
-1
@@ -24,7 +24,7 @@ def main():
|
|||||||
translator = QTranslator()
|
translator = QTranslator()
|
||||||
if translator.load(":/res/translators/qtbase_zh_CN.ts"):
|
if translator.load(":/res/translators/qtbase_zh_CN.ts"):
|
||||||
app.installTranslator(translator)
|
app.installTranslator(translator)
|
||||||
app.setStyle('Fusion')
|
app.setStyle("Fusion")
|
||||||
app.setApplicationName("AutoLibrary")
|
app.setApplicationName("AutoLibrary")
|
||||||
if not initializeApp():
|
if not initializeApp():
|
||||||
sys.exit(-1)
|
sys.exit(-1)
|
||||||
|
|||||||
+199
-395
@@ -14,6 +14,16 @@ from datetime import (
|
|||||||
|
|
||||||
from lupa import LuaRuntime as _LuaRuntime
|
from lupa import LuaRuntime as _LuaRuntime
|
||||||
|
|
||||||
|
from autoscript._helpers import (
|
||||||
|
_TYPE_DEFAULT_VAR,
|
||||||
|
_assignPath,
|
||||||
|
_checkDateFormat,
|
||||||
|
_checkTimeFormat,
|
||||||
|
_checkType,
|
||||||
|
_cleanLuaError,
|
||||||
|
_navigatePath,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from lupa.lua55 import LuaError as _LuaError, LuaSyntaxError as _LuaSyntaxError
|
from lupa.lua55 import LuaError as _LuaError, LuaSyntaxError as _LuaSyntaxError
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -24,435 +34,229 @@ except ImportError:
|
|||||||
_LuaSyntaxError = Exception
|
_LuaSyntaxError = Exception
|
||||||
|
|
||||||
|
|
||||||
__all__ = ["execute", "addTargetVar", "resetEngine"]
|
__all__ = ["ASEngine"]
|
||||||
|
|
||||||
|
|
||||||
# Engine state
|
class ASEngine:
|
||||||
_TARGET_VARS: dict[str, dict] = {}
|
|
||||||
_lua = None
|
|
||||||
|
|
||||||
# Built-in meta variable definitions (name / type / display-name)
|
@staticmethod
|
||||||
META_VARS: dict[str, dict[str, str]] = {
|
def getCurrentDate(
|
||||||
"CURRENT_DATE": {"name": "CURRENT_DATE", "type": "Date", "display": "当前日期"},
|
) -> str:
|
||||||
"CURRENT_TIME": {"name": "CURRENT_TIME", "type": "Time", "display": "当前时间"},
|
|
||||||
}
|
|
||||||
|
|
||||||
# Per-type fallback value when target_data entry is missing.
|
return date.today().isoformat()
|
||||||
_DEFAULT_BY_TYPE: dict[str, str | int | float | bool] = {
|
|
||||||
"String": "",
|
|
||||||
"Int": 0,
|
|
||||||
"Float": 0.0,
|
|
||||||
"Boolean": False,
|
|
||||||
}
|
|
||||||
|
|
||||||
def _getLua(
|
@staticmethod
|
||||||
):
|
def getCurrentTime(
|
||||||
"""
|
) -> str:
|
||||||
Return the sandboxed Lua runtime singleton.
|
|
||||||
"""
|
|
||||||
|
|
||||||
global _lua
|
return datetime.now().strftime("%H:%M")
|
||||||
if _lua is None:
|
|
||||||
_lua = _LuaRuntime(unpack_returned_tuples = True)
|
|
||||||
_sandbox(_lua)
|
|
||||||
_registerHelpers(_lua)
|
|
||||||
return _lua
|
|
||||||
|
|
||||||
def _sandbox(
|
@staticmethod
|
||||||
lua,
|
def _sandbox(
|
||||||
) -> None:
|
lua,
|
||||||
"""
|
) -> None:
|
||||||
Remove dangerous Lua globals while keeping os.date / os.time for date-time helpers.
|
|
||||||
"""
|
|
||||||
|
|
||||||
lua.execute("""
|
lua.execute("""
|
||||||
io = nil
|
io = nil
|
||||||
require = nil
|
require = nil
|
||||||
dofile = nil
|
dofile = nil
|
||||||
loadfile = nil
|
loadfile = nil
|
||||||
load = nil
|
load = nil
|
||||||
package = nil
|
package = nil
|
||||||
rawget = nil
|
rawget = nil
|
||||||
rawset = nil
|
rawset = nil
|
||||||
rawequal = nil
|
rawequal = nil
|
||||||
getfenv = nil
|
getfenv = nil
|
||||||
setfenv = nil
|
setfenv = nil
|
||||||
debug = nil
|
debug = nil
|
||||||
-- selectively disable dangerous os functions, keep date / time
|
if os then
|
||||||
if os then
|
os.execute = nil
|
||||||
os.execute = nil
|
os.exit = nil
|
||||||
os.exit = nil
|
os.getenv = nil
|
||||||
os.getenv = nil
|
os.remove = nil
|
||||||
os.remove = nil
|
os.rename = nil
|
||||||
os.rename = nil
|
os.tmpname = nil
|
||||||
os.tmpname = nil
|
os.setlocale = nil
|
||||||
os.setlocale = nil
|
end
|
||||||
end
|
""")
|
||||||
""")
|
|
||||||
|
|
||||||
def _registerHelpers(
|
@staticmethod
|
||||||
lua,
|
def _registerHelpers(
|
||||||
) -> None:
|
lua,
|
||||||
"""
|
) -> None:
|
||||||
Inject Date / Time helpers as pure Lua functions.
|
|
||||||
|
|
||||||
Date values are os.time timestamps (seconds since epoch).
|
lua.execute("""
|
||||||
Time values are minutes since midnight (0-1439).
|
function date(y, m, d)
|
||||||
|
return os.time({year = y, month = m, day = d})
|
||||||
|
end
|
||||||
|
|
||||||
This keeps Date / Time as native Lua numbers during script execution,
|
function time(h, m)
|
||||||
enabling type-safe arithmetic (+, -) and comparisons (<, <=, ==, ~=).
|
return h * 60 + m
|
||||||
"""
|
end
|
||||||
|
|
||||||
lua.execute("""
|
function datenow()
|
||||||
function date(y, m, d)
|
local now = os.date("*t")
|
||||||
return os.time({year = y, month = m, day = d})
|
return os.time({year = now.year, month = now.month, day = now.day})
|
||||||
end
|
end
|
||||||
|
|
||||||
function time(h, m)
|
function timenow()
|
||||||
return h * 60 + m
|
local now = os.date("*t")
|
||||||
end
|
return now.hour * 60 + now.min
|
||||||
|
end
|
||||||
|
|
||||||
function CURRENT_DATE()
|
function dateadd(date_val, n)
|
||||||
local now = os.date("*t")
|
return date_val + n * 86400
|
||||||
return os.time({year = now.year, month = now.month, day = now.day})
|
end
|
||||||
end
|
|
||||||
|
|
||||||
function CURRENT_TIME()
|
function timeadd(time_val, n)
|
||||||
local now = os.date("*t")
|
return (time_val + n * 60) % 1440
|
||||||
return now.hour * 60 + now.min
|
end
|
||||||
end
|
|
||||||
|
|
||||||
function date_add(date_val, n)
|
function strtodate(iso_str)
|
||||||
return date_val + n * 86400
|
local y, m, d = iso_str:match("(%d+)-(%d+)-(%d+)")
|
||||||
end
|
return os.time({year = y, month = m, day = d})
|
||||||
|
end
|
||||||
|
|
||||||
function time_add(time_val, n)
|
function strtotime(hm_str)
|
||||||
return (time_val + n * 60) % 1440
|
local h, m = hm_str:match("(%d+):(%d+)")
|
||||||
end
|
return h * 60 + m
|
||||||
|
end
|
||||||
|
|
||||||
-- push helpers: string -> native type
|
function datetostr(ts)
|
||||||
function _to_date(iso_str)
|
return os.date("%Y-%m-%d", ts)
|
||||||
local y, m, d = iso_str:match("(%d+)-(%d+)-(%d+)")
|
end
|
||||||
return os.time({year = y, month = m, day = d})
|
|
||||||
end
|
|
||||||
|
|
||||||
function _to_time(hm_str)
|
function timetostr(m)
|
||||||
local h, m = hm_str:match("(%d+):(%d+)")
|
return string.format("%02d:%02d", math.floor(m / 60), m % 60)
|
||||||
return h * 60 + m
|
end
|
||||||
end
|
""")
|
||||||
|
|
||||||
-- pull helpers: native type -> string
|
def __init__(
|
||||||
function _from_date(ts)
|
self,
|
||||||
return os.date("%Y-%m-%d", ts)
|
targetVars: list[tuple] = None,
|
||||||
end
|
):
|
||||||
|
|
||||||
function _from_time(m)
|
self._targetVars: dict[str, dict] = {}
|
||||||
return string.format("%02d:%02d", math.floor(m / 60), m % 60)
|
self._lua = None
|
||||||
end
|
|
||||||
""")
|
|
||||||
|
|
||||||
def _navigatePath(
|
if targetVars:
|
||||||
data: dict,
|
for item in targetVars:
|
||||||
key_path: list,
|
name, varType, keyPath = item[0], item[1], item[2]
|
||||||
default = None,
|
self.addTargetVar(name, varType, keyPath)
|
||||||
):
|
|
||||||
"""
|
|
||||||
Walk *key_path* into *data* and return the value at the leaf.
|
|
||||||
"""
|
|
||||||
|
|
||||||
d = data
|
def _getLua(
|
||||||
for key in key_path[:-1]:
|
self,
|
||||||
d = d.get(key, {})
|
):
|
||||||
if not isinstance(d, dict):
|
|
||||||
return default
|
|
||||||
return d.get(key_path[-1], default)
|
|
||||||
|
|
||||||
def _assignPath(
|
if self._lua is None:
|
||||||
data: dict,
|
self._lua = _LuaRuntime(unpack_returned_tuples=True)
|
||||||
key_path: list,
|
self._sandbox(self._lua)
|
||||||
value,
|
self._registerHelpers(self._lua)
|
||||||
) -> None:
|
return self._lua
|
||||||
"""
|
|
||||||
Walk *key_path* into *data* and set *value* at the leaf.
|
|
||||||
"""
|
|
||||||
|
|
||||||
d = data
|
def _push(
|
||||||
for key in key_path[:-1]:
|
self,
|
||||||
d = d.setdefault(key, {})
|
targetData: dict,
|
||||||
d[key_path[-1]] = value
|
) -> None:
|
||||||
|
|
||||||
def _pyTypeToASType(
|
lua = self._getLua()
|
||||||
value
|
g = lua.globals()
|
||||||
) -> str:
|
strToDate = g["strtodate"]
|
||||||
"""
|
strToTime = g["strtotime"]
|
||||||
Map a Python runtime value to its AutoScript type name.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if isinstance(value, bool):
|
for varName, info in self._targetVars.items():
|
||||||
return "Boolean"
|
keyPath = info["keyPath"]
|
||||||
if isinstance(value, int):
|
vt = info["type"]
|
||||||
return "Int"
|
raw = _navigatePath(targetData, keyPath)
|
||||||
if isinstance(value, float):
|
if vt == "Date":
|
||||||
return "Float"
|
if not isinstance(raw, str) or not raw.strip():
|
||||||
if isinstance(value, str):
|
raise ValueError(
|
||||||
return "String"
|
f"Date 类型变量 '{varName}' 对应的数据为空或不是字符串类型,"
|
||||||
return "Unknown"
|
f"请检查路径 {keyPath} 的值是否为合法的日期字符串 (YYYY-MM-DD)"
|
||||||
|
)
|
||||||
|
raw = raw.strip()
|
||||||
|
_checkDateFormat(raw, varName)
|
||||||
|
g[varName] = strToDate(raw)
|
||||||
|
elif vt == "Time":
|
||||||
|
if not isinstance(raw, str) or not raw.strip():
|
||||||
|
raise ValueError(
|
||||||
|
f"Time 类型变量 '{varName}' 对应的数据为空或不是字符串类型,"
|
||||||
|
f"请检查路径 {keyPath} 的值是否为合法的时间字符串 (HH:MM)"
|
||||||
|
)
|
||||||
|
raw = raw.strip()
|
||||||
|
_checkTimeFormat(raw, varName)
|
||||||
|
g[varName] = strToTime(raw)
|
||||||
|
else:
|
||||||
|
if raw is None:
|
||||||
|
raw = _TYPE_DEFAULT_VAR.get(vt, False)
|
||||||
|
g[varName] = raw
|
||||||
|
|
||||||
def _checkDateFormat(
|
def _pull(
|
||||||
date_str: str,
|
self,
|
||||||
var_name: str = "",
|
targetData: dict,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""
|
|
||||||
Validate that *date_str* is in YYYY-MM-DD format.
|
|
||||||
Raises ValueError with a descriptive message on failure.
|
|
||||||
"""
|
|
||||||
|
|
||||||
prefix = f"Date 类型变量 '{var_name}' 的" if var_name else ""
|
lua = self._getLua()
|
||||||
try:
|
g = lua.globals()
|
||||||
date.fromisoformat(date_str)
|
dateToStr = g["datetostr"]
|
||||||
except ValueError:
|
timeToStr = g["timetostr"]
|
||||||
raise ValueError(
|
|
||||||
f"{prefix}值 '{date_str}' 不是合法的日期格式,"
|
|
||||||
f"应为 YYYY-MM-DD"
|
|
||||||
)
|
|
||||||
|
|
||||||
def _checkTimeFormat(
|
for varName, info in self._targetVars.items():
|
||||||
time_str: str,
|
try:
|
||||||
var_name: str = "",
|
luaVal = g[varName]
|
||||||
) -> None:
|
except KeyError:
|
||||||
"""
|
continue
|
||||||
Validate that *time_str* is in HH:MM format.
|
vt = info["type"]
|
||||||
Raises ValueError with a descriptive message on failure.
|
if vt == "Date":
|
||||||
"""
|
luaVal = dateToStr(luaVal)
|
||||||
|
elif vt == "Time":
|
||||||
|
luaVal = timeToStr(luaVal)
|
||||||
|
elif vt == "Float" and isinstance(luaVal, int) and not isinstance(luaVal, bool):
|
||||||
|
luaVal = float(luaVal)
|
||||||
|
_checkType(varName, vt, luaVal)
|
||||||
|
_assignPath(targetData, info["keyPath"], luaVal)
|
||||||
|
|
||||||
prefix = f"Time 类型变量 '{var_name}' 的" if var_name else ""
|
def addTargetVar(
|
||||||
try:
|
self,
|
||||||
datetime.strptime(time_str, "%H:%M")
|
name: str,
|
||||||
except ValueError:
|
varType: str,
|
||||||
raise ValueError(
|
keyPath: list,
|
||||||
f"{prefix}值 '{time_str}' 不是合法的时间格式,"
|
) -> None:
|
||||||
f"应为 HH:MM"
|
|
||||||
)
|
|
||||||
|
|
||||||
def _checkType(
|
upperName = name.upper().strip()
|
||||||
var_name: str,
|
self._targetVars[upperName] = {
|
||||||
var_type: str,
|
"type": varType,
|
||||||
value,
|
"keyPath": keyPath,
|
||||||
) -> None:
|
}
|
||||||
"""
|
|
||||||
Validate that *value* matches the declared variable type.
|
|
||||||
|
|
||||||
Date / Time values arrive as ISO / HH:MM strings (already converted
|
def execute(
|
||||||
from Lua native types during the pull phase).
|
self,
|
||||||
Int / Float / Boolean / String check Python type identity.
|
scriptText: str,
|
||||||
Int -> Float widening is allowed.
|
targetData: dict,
|
||||||
"""
|
) -> None:
|
||||||
|
|
||||||
if var_type == "Date":
|
if not scriptText or not scriptText.strip():
|
||||||
if not isinstance(value, str):
|
return
|
||||||
raise ValueError(
|
|
||||||
f"Date 类型变量 '{var_name}' 只能接受日期字符串,"
|
|
||||||
f"不能接受 {_pyTypeToASType(value)} 类型"
|
|
||||||
)
|
|
||||||
_checkDateFormat(value, var_name)
|
|
||||||
return
|
|
||||||
if var_type == "Time":
|
|
||||||
if not isinstance(value, str):
|
|
||||||
raise ValueError(
|
|
||||||
f"Time 类型变量 '{var_name}' 只能接受时间字符串,"
|
|
||||||
f"不能接受 {_pyTypeToASType(value)} 类型"
|
|
||||||
)
|
|
||||||
_checkTimeFormat(value, var_name)
|
|
||||||
return
|
|
||||||
if var_type == "Int":
|
|
||||||
if isinstance(value, bool):
|
|
||||||
raise ValueError(
|
|
||||||
f"Int 类型变量 '{var_name}' 不能接受 Boolean 类型的值"
|
|
||||||
)
|
|
||||||
if not isinstance(value, int) and not (isinstance(value, float) and value == int(value)):
|
|
||||||
raise ValueError(
|
|
||||||
f"Int 类型变量 '{var_name}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
if var_type == "Float":
|
|
||||||
if isinstance(value, bool):
|
|
||||||
raise ValueError(
|
|
||||||
f"Float 类型变量 '{var_name}' 不能接受 Boolean 类型的值"
|
|
||||||
)
|
|
||||||
if not isinstance(value, (int, float)):
|
|
||||||
raise ValueError(
|
|
||||||
f"Float 类型变量 '{var_name}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
if var_type == "Boolean":
|
|
||||||
if not isinstance(value, bool):
|
|
||||||
raise ValueError(
|
|
||||||
f"Boolean 类型变量 '{var_name}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
if var_type == "String":
|
|
||||||
if not isinstance(value, str):
|
|
||||||
raise ValueError(
|
|
||||||
f"String 类型变量 '{var_name}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
def addTargetVar(
|
|
||||||
name: str,
|
|
||||||
var_type: str,
|
|
||||||
key_path: list,
|
|
||||||
_display_name: str = None,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Register a new target variable bound to a path in the application data dict.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
name (str): The canonical variable name (e.g. "RESERVE_DATE").
|
|
||||||
var_type (str): "Int" | "Float" | "Boolean" | "Date" | "Time" | "String".
|
|
||||||
key_path (list): Nested path into target_data, e.g. ["reserve_info", "date"].
|
|
||||||
"""
|
|
||||||
|
|
||||||
upper_name = name.upper().strip()
|
|
||||||
_TARGET_VARS[upper_name] = {
|
|
||||||
"type": var_type,
|
|
||||||
"key_path": key_path,
|
|
||||||
}
|
|
||||||
|
|
||||||
def resetEngine(
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Reset the engine to its initial state: clear all target variables
|
|
||||||
and release the Lua runtime.
|
|
||||||
"""
|
|
||||||
|
|
||||||
global _TARGET_VARS, _lua
|
|
||||||
_TARGET_VARS = {}
|
|
||||||
_lua = None
|
|
||||||
|
|
||||||
def _push(
|
|
||||||
target_data: dict,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Push target_data values into Lua globals.
|
|
||||||
Date / Time strings are converted to native Lua types (timestamp / minutes).
|
|
||||||
|
|
||||||
Raises ValueError for missing / malformed Date or Time values so that
|
|
||||||
execute() can surface them as user-visible AutoScript execution errors.
|
|
||||||
"""
|
|
||||||
|
|
||||||
lua = _getLua()
|
|
||||||
g = lua.globals()
|
|
||||||
_toDate = g["_to_date"]
|
|
||||||
_toTime = g["_to_time"]
|
|
||||||
|
|
||||||
for var_name, info in _TARGET_VARS.items():
|
|
||||||
key_path = info["key_path"]
|
|
||||||
vt = info["type"]
|
|
||||||
raw = _navigatePath(target_data, key_path)
|
|
||||||
if vt == "Date":
|
|
||||||
if not isinstance(raw, str) or not raw.strip():
|
|
||||||
raise ValueError(
|
|
||||||
f"Date 类型变量 '{var_name}' 对应的数据为空或不是字符串类型,"
|
|
||||||
f"请检查路径 {key_path} 的值是否为合法的日期字符串 (YYYY-MM-DD)"
|
|
||||||
)
|
|
||||||
raw = raw.strip()
|
|
||||||
_checkDateFormat(raw, var_name)
|
|
||||||
g[var_name] = _toDate(raw)
|
|
||||||
elif vt == "Time":
|
|
||||||
if not isinstance(raw, str) or not raw.strip():
|
|
||||||
raise ValueError(
|
|
||||||
f"Time 类型变量 '{var_name}' 对应的数据为空或不是字符串类型,"
|
|
||||||
f"请检查路径 {key_path} 的值是否为合法的时间字符串 (HH:MM)"
|
|
||||||
)
|
|
||||||
raw = raw.strip()
|
|
||||||
_checkTimeFormat(raw, var_name)
|
|
||||||
g[var_name] = _toTime(raw)
|
|
||||||
else:
|
|
||||||
if raw is None:
|
|
||||||
raw = _DEFAULT_BY_TYPE.get(vt, False)
|
|
||||||
g[var_name] = raw
|
|
||||||
|
|
||||||
def _pull(
|
|
||||||
target_data: dict,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Pull Lua global values back into target_data.
|
|
||||||
Date / Time native types are converted back to ISO / HH:MM strings.
|
|
||||||
"""
|
|
||||||
|
|
||||||
lua = _getLua()
|
|
||||||
g = lua.globals()
|
|
||||||
_fromDate = g["_from_date"]
|
|
||||||
_fromTime = g["_from_time"]
|
|
||||||
|
|
||||||
for var_name, info in _TARGET_VARS.items():
|
|
||||||
try:
|
try:
|
||||||
lua_val = g[var_name]
|
self._push(targetData)
|
||||||
except KeyError:
|
self._getLua().execute(scriptText)
|
||||||
continue
|
self._pull(targetData)
|
||||||
vt = info["type"]
|
except _LuaSyntaxError as e:
|
||||||
if vt == "Date":
|
raise ValueError(
|
||||||
lua_val = _fromDate(lua_val)
|
f"AutoScript 语法错误: {_cleanLuaError(str(e))}"
|
||||||
elif vt == "Time":
|
)
|
||||||
lua_val = _fromTime(lua_val)
|
except _LuaError as e:
|
||||||
elif vt == "Float" and isinstance(lua_val, int) and not isinstance(lua_val, bool):
|
raise ValueError(
|
||||||
lua_val = float(lua_val)
|
f"AutoScript 运行时错误: {_cleanLuaError(str(e))}"
|
||||||
_checkType(var_name, vt, lua_val)
|
)
|
||||||
_assignPath(target_data, info["key_path"], lua_val)
|
except ValueError as e:
|
||||||
|
raise ValueError(f"AutoScript 数据错误: {e}")
|
||||||
|
except Exception as e:
|
||||||
|
raise ValueError(f"AutoScript 未知错误: {e}")
|
||||||
|
|
||||||
def _cleanLuaError(
|
def reset(
|
||||||
raw_msg: str
|
self,
|
||||||
) -> str:
|
) -> None:
|
||||||
"""
|
|
||||||
Strip internal source prefix and stack traceback from a Lua error message.
|
|
||||||
"""
|
|
||||||
|
|
||||||
msg = raw_msg.replace('[string "<python>"]:', "").strip()
|
self._targetVars = {}
|
||||||
stack_idx = msg.find("stack traceback:")
|
self._lua = None
|
||||||
if stack_idx != -1:
|
|
||||||
msg = msg[:stack_idx].strip()
|
|
||||||
return msg
|
|
||||||
|
|
||||||
def execute(
|
|
||||||
script_text: str,
|
|
||||||
target_data: dict,
|
|
||||||
) -> None:
|
|
||||||
"""
|
|
||||||
Execute an AutoScript (Lua) on the given target data.
|
|
||||||
|
|
||||||
The script runs in a sandboxed Lua environment with target variables
|
|
||||||
exposed as globals. The following helpers are available as Lua functions:
|
|
||||||
|
|
||||||
date(y, m, d) -> timestamp (os.time seconds)
|
|
||||||
time(h, m) -> minutes since midnight (0-1439)
|
|
||||||
CURRENT_DATE() -> today's timestamp
|
|
||||||
CURRENT_TIME() -> current minutes since midnight
|
|
||||||
date_add(ts, n) -> ts + n * 86400
|
|
||||||
time_add(m, n) -> (m + n * 60) % 1440
|
|
||||||
|
|
||||||
Date and Time values are native Lua numbers during execution.
|
|
||||||
Arithmetic (+, -) and comparisons (<, <=, ==, ~=, >, >=) work
|
|
||||||
with strong type safety — no implicit string coercion.
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: On Lua compilation/runtime errors or type mismatches.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not script_text or not script_text.strip():
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
_push(target_data)
|
|
||||||
_getLua().execute(script_text)
|
|
||||||
_pull(target_data)
|
|
||||||
except _LuaSyntaxError as e:
|
|
||||||
raise ValueError(
|
|
||||||
f"AutoScript 语法错误: {_cleanLuaError(str(e))}"
|
|
||||||
)
|
|
||||||
except _LuaError as e:
|
|
||||||
raise ValueError(
|
|
||||||
f"AutoScript 运行时错误: {_cleanLuaError(str(e))}"
|
|
||||||
)
|
|
||||||
except ValueError as e:
|
|
||||||
raise ValueError(f"AutoScript 数据错误: {e}")
|
|
||||||
except Exception as e:
|
|
||||||
raise ValueError(f"AutoScript 未知错误: {e}")
|
|
||||||
|
|||||||
+30
-52
@@ -7,45 +7,17 @@ This software is provided "as is", without any warranty of any kind.
|
|||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
See the LICENSE file for details.
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
from autoscript.ASEngine import (
|
from .ASEngine import ASEngine
|
||||||
execute,
|
|
||||||
addTargetVar,
|
|
||||||
resetEngine,
|
|
||||||
META_VARS,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
__version__ = "1.0.0" # autoscript version
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"execute",
|
|
||||||
"addTargetVar",
|
|
||||||
"resetEngine",
|
|
||||||
"registerDefaultTargetVars",
|
|
||||||
"buildMockTargetData",
|
|
||||||
"META_VARS",
|
|
||||||
"ALL_VARIABLES",
|
|
||||||
"_TARGET_VAR_DEFS",
|
|
||||||
"_MOCK_TYPE_VALUES",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
# Key paths into target_data dict for each target variable.
|
|
||||||
# (name, type, key_path, display_name)
|
|
||||||
_TARGET_VAR_DEFS = [
|
_TARGET_VAR_DEFS = [
|
||||||
("USERNAME", "String", ["username"], "用户名"),
|
("USERNAME", "String", ["username"], "用户名"),
|
||||||
("USER_ENABLE", "Boolean",["enabled"], "用户启用"),
|
("USER_ENABLE", "Boolean", ["enabled"], "用户启用"),
|
||||||
("RESERVE_DATE", "Date", ["reserve_info", "date"], "预约日期"),
|
("RESERVE_DATE", "Date", ["reserve_info", "date"], "预约日期"),
|
||||||
("RESERVE_BEGIN_TIME", "Time", ["reserve_info", "begin_time", "time"], "预约开始时间"),
|
("RESERVE_BEGIN_TIME", "Time", ["reserve_info", "begin_time", "time"], "预约开始时间"),
|
||||||
("RESERVE_END_TIME", "Time", ["reserve_info", "end_time", "time"], "预约结束时间"),
|
("RESERVE_END_TIME", "Time", ["reserve_info", "end_time", "time"], "预约结束时间"),
|
||||||
]
|
]
|
||||||
|
|
||||||
# All variables (display_name -> (name, type)), derived from target vars + meta vars.
|
|
||||||
ALL_VARIABLES = {
|
|
||||||
display_name: (name, var_type)
|
|
||||||
for name, var_type, _, display_name in _TARGET_VAR_DEFS
|
|
||||||
} | {
|
|
||||||
v["display"]: (v["name"], v["type"])
|
|
||||||
for v in META_VARS.values()
|
|
||||||
}
|
|
||||||
_MOCK_TYPE_VALUES = {
|
_MOCK_TYPE_VALUES = {
|
||||||
"String": "__mock__",
|
"String": "__mock__",
|
||||||
"Boolean": True,
|
"Boolean": True,
|
||||||
@@ -55,26 +27,32 @@ _MOCK_TYPE_VALUES = {
|
|||||||
"Float": 0.0,
|
"Float": 0.0,
|
||||||
}
|
}
|
||||||
|
|
||||||
def buildMockTargetData(
|
|
||||||
|
def createAllVariablesTable(
|
||||||
) -> dict:
|
) -> dict:
|
||||||
"""
|
|
||||||
Build a target_data dict filled with type-appropriate mock values
|
return {
|
||||||
for all registered target variables.
|
displayName: (name, varType)
|
||||||
"""
|
for name, varType, _, displayName in _TARGET_VAR_DEFS
|
||||||
|
}
|
||||||
|
|
||||||
|
def createTargetVarDefs(
|
||||||
|
) -> list:
|
||||||
|
|
||||||
|
return list(_TARGET_VAR_DEFS)
|
||||||
|
|
||||||
|
def createMockTargetData(
|
||||||
|
) -> dict:
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
for _, var_type, key_path, _ in _TARGET_VAR_DEFS:
|
for _, varType, keyPath, _ in _TARGET_VAR_DEFS:
|
||||||
d = data
|
d = data
|
||||||
for key in key_path[:-1]:
|
for key in keyPath[:-1]:
|
||||||
d = d.setdefault(key, {})
|
d = d.setdefault(key, {})
|
||||||
d[key_path[-1]] = _MOCK_TYPE_VALUES.get(var_type, "")
|
d[keyPath[-1]] = _MOCK_TYPE_VALUES.get(varType, "")
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def registerDefaultTargetVars(
|
def createEngine(
|
||||||
) -> None:
|
) -> ASEngine:
|
||||||
"""
|
|
||||||
Register all built-in target variables with the engine.
|
return ASEngine(_TARGET_VAR_DEFS)
|
||||||
This must be called before any script execution.
|
|
||||||
Calling multiple times is idempotent (re-registers same keys).
|
|
||||||
"""
|
|
||||||
for name, var_type, key_path, display_name in _TARGET_VAR_DEFS:
|
|
||||||
addTargetVar(name, var_type, key_path, display_name)
|
|
||||||
|
|||||||
@@ -0,0 +1,153 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from datetime import (
|
||||||
|
date,
|
||||||
|
datetime,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_TYPE_DEFAULT_VAR: dict[str, str | int | float | bool] = {
|
||||||
|
"String": "",
|
||||||
|
"Int": 0,
|
||||||
|
"Float": 0.0,
|
||||||
|
"Boolean": False,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def _navigatePath(
|
||||||
|
data: dict,
|
||||||
|
keyPath: list,
|
||||||
|
default=None,
|
||||||
|
):
|
||||||
|
|
||||||
|
d = data
|
||||||
|
for key in keyPath[:-1]:
|
||||||
|
d = d.get(key, {})
|
||||||
|
if not isinstance(d, dict):
|
||||||
|
return default
|
||||||
|
return d.get(keyPath[-1], default)
|
||||||
|
|
||||||
|
def _assignPath(
|
||||||
|
data: dict,
|
||||||
|
keyPath: list,
|
||||||
|
value,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
d = data
|
||||||
|
for key in keyPath[:-1]:
|
||||||
|
d = d.setdefault(key, {})
|
||||||
|
d[keyPath[-1]] = value
|
||||||
|
|
||||||
|
def _checkDateFormat(
|
||||||
|
dateStr: str,
|
||||||
|
varName: str = "",
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
prefix = f"Date 类型变量 '{varName}' 的" if varName else ""
|
||||||
|
try:
|
||||||
|
date.fromisoformat(dateStr)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(
|
||||||
|
f"{prefix}值 '{dateStr}' 不是合法的日期格式,"
|
||||||
|
f"应为 YYYY-MM-DD"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _checkTimeFormat(
|
||||||
|
timeStr: str,
|
||||||
|
varName: str = "",
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
prefix = f"Time 类型变量 '{varName}' 的" if varName else ""
|
||||||
|
try:
|
||||||
|
datetime.strptime(timeStr, "%H:%M")
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(
|
||||||
|
f"{prefix}值 '{timeStr}' 不是合法的时间格式,"
|
||||||
|
f"应为 HH:MM"
|
||||||
|
)
|
||||||
|
|
||||||
|
def _checkType(
|
||||||
|
varName: str,
|
||||||
|
varType: str,
|
||||||
|
value,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if varType == "Date":
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise ValueError(
|
||||||
|
f"Date 类型变量 '{varName}' 只能接受日期字符串,"
|
||||||
|
f"不能接受 {_pyTypeToASType(value)} 类型"
|
||||||
|
)
|
||||||
|
_checkDateFormat(value, varName)
|
||||||
|
return
|
||||||
|
if varType == "Time":
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise ValueError(
|
||||||
|
f"Time 类型变量 '{varName}' 只能接受时间字符串,"
|
||||||
|
f"不能接受 {_pyTypeToASType(value)} 类型"
|
||||||
|
)
|
||||||
|
_checkTimeFormat(value, varName)
|
||||||
|
return
|
||||||
|
if varType == "Int":
|
||||||
|
if isinstance(value, bool):
|
||||||
|
raise ValueError(
|
||||||
|
f"Int 类型变量 '{varName}' 不能接受 Boolean 类型的值"
|
||||||
|
)
|
||||||
|
if not isinstance(value, int) and not (isinstance(value, float) and value == int(value)):
|
||||||
|
raise ValueError(
|
||||||
|
f"Int 类型变量 '{varName}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
if varType == "Float":
|
||||||
|
if isinstance(value, bool):
|
||||||
|
raise ValueError(
|
||||||
|
f"Float 类型变量 '{varName}' 不能接受 Boolean 类型的值"
|
||||||
|
)
|
||||||
|
if not isinstance(value, (int, float)):
|
||||||
|
raise ValueError(
|
||||||
|
f"Float 类型变量 '{varName}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
if varType == "Boolean":
|
||||||
|
if not isinstance(value, bool):
|
||||||
|
raise ValueError(
|
||||||
|
f"Boolean 类型变量 '{varName}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
if varType == "String":
|
||||||
|
if not isinstance(value, str):
|
||||||
|
raise ValueError(
|
||||||
|
f"String 类型变量 '{varName}' 不能接受 {_pyTypeToASType(value)} 类型的值"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
def _pyTypeToASType(
|
||||||
|
value,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
if isinstance(value, bool):
|
||||||
|
return "Boolean"
|
||||||
|
if isinstance(value, int):
|
||||||
|
return "Int"
|
||||||
|
if isinstance(value, float):
|
||||||
|
return "Float"
|
||||||
|
if isinstance(value, str):
|
||||||
|
return "String"
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
|
def _cleanLuaError(
|
||||||
|
rawMsg: str,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
msg = rawMsg.replace('[string "<python>"]:', "").strip()
|
||||||
|
stackIdx = msg.find("stack traceback:")
|
||||||
|
if stackIdx != -1:
|
||||||
|
msg = msg[:stackIdx].strip()
|
||||||
|
return msg
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from base.MsgBase import MsgBase
|
|
||||||
|
|
||||||
|
|
||||||
class LibOperator(MsgBase):
|
|
||||||
"""
|
|
||||||
Base abstract class for library operation.
|
|
||||||
|
|
||||||
This class provides the foundation for library-related operations, inheriting
|
|
||||||
message handling and tracing abilities from MsgBase. It serves as an abstract
|
|
||||||
base class that must be subclassed to implement specific library functionality.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
pass
|
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Base module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- MsgBase: Base class for messages.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
- LibOperator: Base class for library operators.
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Boot module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- AppInitializer: Application initializer class.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
+81
-34
@@ -13,17 +13,19 @@ from PySide6.QtCore import (
|
|||||||
Qt,
|
Qt,
|
||||||
QTimer
|
QTimer
|
||||||
)
|
)
|
||||||
from PySide6.QtGui import (
|
from PySide6.QtGui import QIcon
|
||||||
QFont,
|
|
||||||
QIcon
|
|
||||||
)
|
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
QApplication,
|
QApplication,
|
||||||
QDialog
|
QDialog,
|
||||||
|
QTabWidget,
|
||||||
|
QTextBrowser
|
||||||
)
|
)
|
||||||
|
|
||||||
from gui.ALVersionInfo import (
|
from gui.ALVersionInfo import (
|
||||||
AL_VERSION, AL_COMMIT_SHA, AL_COMMIT_DATE, AL_BUILD_DATE
|
AL_VERSION,
|
||||||
|
AL_COMMIT_SHA,
|
||||||
|
AL_COMMIT_DATE,
|
||||||
|
AL_BUILD_DATE
|
||||||
)
|
)
|
||||||
from gui.resources.ui.Ui_ALAboutDialog import Ui_ALAboutDialog
|
from gui.resources.ui.Ui_ALAboutDialog import Ui_ALAboutDialog
|
||||||
from gui.resources import ALResource
|
from gui.resources import ALResource
|
||||||
@@ -46,12 +48,23 @@ class ALAboutDialog(QDialog, Ui_ALAboutDialog):
|
|||||||
):
|
):
|
||||||
|
|
||||||
self.LogoIconLabel.setPixmap(QIcon(":/res/icons/AutoLibrary_Logo_64.svg").pixmap(48, 48))
|
self.LogoIconLabel.setPixmap(QIcon(":/res/icons/AutoLibrary_Logo_64.svg").pixmap(48, 48))
|
||||||
info_text = self.generateAboutText()
|
self.TabWidget = QTabWidget()
|
||||||
self.AboutInfoBrowser.setHtml(info_text)
|
self.TabWidget.setDocumentMode(True)
|
||||||
browser_font = self.AboutInfoBrowser.font()
|
AboutBrowser = QTextBrowser()
|
||||||
browser_font.setFamily("Courier New")
|
AboutBrowser.setHtml(self.generateAboutText())
|
||||||
self.AboutInfoBrowser.setFont(browser_font)
|
AboutBrowser.setOpenExternalLinks(True)
|
||||||
self.AboutInfoBrowser.setTextInteractionFlags(Qt.TextBrowserInteraction)
|
AboutBrowser.setLineWrapMode(QTextBrowser.LineWrapMode.NoWrap)
|
||||||
|
AboutBrowser.setTextInteractionFlags(Qt.TextBrowserInteraction)
|
||||||
|
BrowserFont = AboutBrowser.font()
|
||||||
|
BrowserFont.setFamilies(["Courier New", "Consolas", "Menlo", "DejaVu Sans Mono", "monospace"])
|
||||||
|
AboutBrowser.setFont(BrowserFont)
|
||||||
|
self.TabWidget.addTab(AboutBrowser, "关于")
|
||||||
|
LicenseBrowser = QTextBrowser()
|
||||||
|
LicenseBrowser.setHtml(self.generateLicenseText())
|
||||||
|
LicenseBrowser.setOpenExternalLinks(True)
|
||||||
|
LicenseBrowser.setTextInteractionFlags(Qt.TextBrowserInteraction)
|
||||||
|
self.TabWidget.addTab(LicenseBrowser, "许可证")
|
||||||
|
self.AboutInfoLayout.addWidget(self.TabWidget)
|
||||||
|
|
||||||
def connectSignals(
|
def connectSignals(
|
||||||
self
|
self
|
||||||
@@ -64,33 +77,57 @@ class ALAboutDialog(QDialog, Ui_ALAboutDialog):
|
|||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
os_info = self.getOSInfo()
|
os_info = self.getOSInfo()
|
||||||
|
run_on = f"{os_info['system']} {os_info['version']} {os_info['architecture']}"
|
||||||
|
selenium_ver = self.getSeleniumVersion()
|
||||||
about_text = f"""
|
about_text = f"""
|
||||||
<h4>Version Information:</h4>
|
<b style="font-size:14px;">VERSION: {AL_VERSION}</b><br>
|
||||||
Version: {AL_VERSION}<br>
|
|
||||||
Commit SHA: {AL_COMMIT_SHA}<br>
|
Commit SHA: {AL_COMMIT_SHA}<br>
|
||||||
Commit date: {AL_COMMIT_DATE}<br>
|
Commit date: {AL_COMMIT_DATE}<br>
|
||||||
Build date: {AL_BUILD_DATE}<br>
|
Build date: {AL_BUILD_DATE}<br>
|
||||||
Python version: {platform.python_version()}<br>
|
<br>
|
||||||
Qt version: {self.getQtVersion()}<br>
|
<b style="font-size:14px;">SYSTEM</b><br>
|
||||||
|
Running on: {run_on}<br>
|
||||||
<h4>System Information:</h4>
|
|
||||||
Processor: {platform.processor()}<br>
|
Processor: {platform.processor()}<br>
|
||||||
Operating system: {os_info['system']}<br>
|
<br>
|
||||||
System version: {os_info['version']}<br>
|
<b style="font-size:14px;">DEPENDENCIES</b><br>
|
||||||
System architecture: {os_info['architecture']}<br>
|
Python: {platform.python_version()}<br>
|
||||||
|
Qt(PySide6): {self.getQtVersion()}<br>
|
||||||
<h4>Project Information:</h4>
|
Selenium: {selenium_ver}<br>
|
||||||
License: MIT License<br>
|
<br>
|
||||||
Project repository: <a href="https://www.github.com/KenanZhu/AutoLibrary" style="text-decoration: none;">https://www.github.com/KenanZhu/AutoLibrary</a><br>
|
<b style="font-size:14px;">PROJECT</b><br>
|
||||||
Project website: <a href="https://www.autolibrary.kenanzhu.com" style="text-decoration: none;">https://www.autolibrary.kenanzhu.com</a><br>
|
Website: <a href="https://www.autolibrary.kenanzhu.com" style="text-decoration:none;">https://www.autolibrary.kenanzhu.com</a><br>
|
||||||
|
Repository: <a href="https://www.github.com/KenanZhu/AutoLibrary" style="text-decoration:none;">https://www.github.com/KenanZhu/AutoLibrary</a><br>
|
||||||
<h4>Author Information:</h4>
|
<br>
|
||||||
Developer: KenanZhu<br>
|
<b style="font-size:14px;">AUTHOR</b><br>
|
||||||
Contact: nanoki_zh@163.com<br>
|
Developer/Maintainer: KenanZhu<br>
|
||||||
GitHub: <a href="https://www.github.com/KenanZhu" style="text-decoration: none;">https://www.github.com/KenanZhu</a><br>
|
Contact: <a href="mailto:nanoki_zh@163.com">nanoki_zh@163.com</a><br>
|
||||||
|
GitHub: <a href="https://www.github.com/KenanZhu" style="text-decoration:none;">https://www.github.com/KenanZhu</a><br>
|
||||||
"""
|
"""
|
||||||
return about_text
|
return about_text
|
||||||
|
|
||||||
|
def generateLicenseText(
|
||||||
|
self
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
return """
|
||||||
|
<b>MIT License</b>
|
||||||
|
<p>Copyright © 2025 - 2026 KenanZhu</p>
|
||||||
|
<p>Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:</p>
|
||||||
|
<p>The above copyright notice and this permission notice shall be included in
|
||||||
|
all copies or substantial portions of the Software.</p>
|
||||||
|
<p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||||
|
THE SOFTWARE.</p>"""
|
||||||
|
|
||||||
def getOSInfo(
|
def getOSInfo(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
@@ -132,13 +169,23 @@ GitHub: <a href="https://www.github.com/KenanZhu" style="text-decoration: none;"
|
|||||||
except:
|
except:
|
||||||
return "Unknown"
|
return "Unknown"
|
||||||
|
|
||||||
|
def getSeleniumVersion(
|
||||||
|
self
|
||||||
|
):
|
||||||
|
|
||||||
|
try:
|
||||||
|
import selenium
|
||||||
|
return selenium.__version__
|
||||||
|
except Exception:
|
||||||
|
return "Unknown"
|
||||||
|
|
||||||
def copyAboutInfo(
|
def copyAboutInfo(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
about_text = self.AboutInfoBrowser.toPlainText()
|
about_text = self.TabWidget.currentWidget().toPlainText()
|
||||||
clipboard = QApplication.clipboard()
|
Clipboard = QApplication.clipboard()
|
||||||
clipboard.setText(about_text)
|
Clipboard.setText(about_text)
|
||||||
original_text = self.CopyButton.text()
|
original_text = self.CopyButton.text()
|
||||||
self.CopyButton.setText("已复制")
|
self.CopyButton.setText("已复制")
|
||||||
QTimer.singleShot(2000, lambda: self.CopyButton.setText(original_text))
|
QTimer.singleShot(2000, lambda: self.CopyButton.setText(original_text))
|
||||||
+286
-248
@@ -9,10 +9,18 @@ See the LICENSE file for details.
|
|||||||
"""
|
"""
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
|
|
||||||
from PySide6.QtCore import QDate, Qt, QTime, QTimer, Slot
|
from PySide6.QtCore import (
|
||||||
|
QDate,
|
||||||
|
QSize,
|
||||||
|
Qt,
|
||||||
|
QTime,
|
||||||
|
QTimer,
|
||||||
|
Slot
|
||||||
|
)
|
||||||
from PySide6.QtGui import (
|
from PySide6.QtGui import (
|
||||||
QColor,
|
QColor,
|
||||||
QFont,
|
QFont,
|
||||||
|
QIcon,
|
||||||
QSyntaxHighlighter,
|
QSyntaxHighlighter,
|
||||||
QTextCharFormat,
|
QTextCharFormat,
|
||||||
)
|
)
|
||||||
@@ -46,11 +54,10 @@ from PySide6.QtWidgets import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from autoscript import (
|
from autoscript import (
|
||||||
ALL_VARIABLES,
|
createAllVariablesTable,
|
||||||
_MOCK_TYPE_VALUES,
|
createMockTargetData,
|
||||||
_TARGET_VAR_DEFS,
|
createTargetVarDefs,
|
||||||
execute,
|
createEngine,
|
||||||
registerDefaultTargetVars,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -67,54 +74,54 @@ class ALScriptHighlighter(QSyntaxHighlighter):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._rules = []
|
self._rules = []
|
||||||
|
|
||||||
keywordFmt = QTextCharFormat()
|
KeywordFmt = QTextCharFormat()
|
||||||
keywordFmt.setForeground(QColor("#569CD6"))
|
KeywordFmt.setForeground(QColor("#569CD6"))
|
||||||
keywordFmt.setFontWeight(QFont.Weight.Bold)
|
KeywordFmt.setFontWeight(QFont.Weight.Bold)
|
||||||
for kw in [
|
for kw in [
|
||||||
"if", "elseif", "else", "end", "then",
|
"if", "elseif", "else", "end", "then",
|
||||||
"and", "or", "not",
|
"and", "or", "not",
|
||||||
"local", "function", "return", "nil",
|
"local", "function", "return", "nil",
|
||||||
]:
|
]:
|
||||||
self._rules.append((r"\b" + kw + r"\b", keywordFmt))
|
self._rules.append((r"\b" + kw + r"\b", KeywordFmt))
|
||||||
boolFmt = QTextCharFormat()
|
BoolFmt = QTextCharFormat()
|
||||||
boolFmt.setForeground(QColor("#4FC1FF"))
|
BoolFmt.setForeground(QColor("#4FC1FF"))
|
||||||
boolFmt.setFontWeight(QFont.Weight.Bold)
|
BoolFmt.setFontWeight(QFont.Weight.Bold)
|
||||||
self._rules.append((r"\btrue\b", boolFmt))
|
self._rules.append((r"\btrue\b", BoolFmt))
|
||||||
self._rules.append((r"\bfalse\b", boolFmt))
|
self._rules.append((r"\bfalse\b", BoolFmt))
|
||||||
cmpFmt = QTextCharFormat()
|
CmpFmt = QTextCharFormat()
|
||||||
cmpFmt.setForeground(QColor("#C586C0"))
|
CmpFmt.setForeground(QColor("#C586C0"))
|
||||||
cmpFmt.setFontWeight(QFont.Weight.Normal)
|
CmpFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
for op in [r"==", r"~=", r">=", r"<=", r">", r"<"]:
|
for op in [r"==", r"~=", r">=", r"<=", r">", r"<"]:
|
||||||
self._rules.append((op, cmpFmt))
|
self._rules.append((op, CmpFmt))
|
||||||
arithFmt = QTextCharFormat()
|
ArithFmt = QTextCharFormat()
|
||||||
arithFmt.setForeground(QColor("#C586C0"))
|
ArithFmt.setForeground(QColor("#C586C0"))
|
||||||
arithFmt.setFontWeight(QFont.Weight.Normal)
|
ArithFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
for op in [r"\+", r"-", r"\*", r"/", r"\.\."]:
|
for op in [r"\+", r"-", r"\*", r"/", r"\.\."]:
|
||||||
self._rules.append((op, arithFmt))
|
self._rules.append((op, ArithFmt))
|
||||||
funcFmt = QTextCharFormat()
|
FuncFmt = QTextCharFormat()
|
||||||
funcFmt.setForeground(QColor("#DCDCAA"))
|
FuncFmt.setForeground(QColor("#DCDCAA"))
|
||||||
funcFmt.setFontWeight(QFont.Weight.Normal)
|
FuncFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
for fn in ["CURRENT_DATE", "CURRENT_TIME", "date_add", "time_add"]:
|
for fn in [ "time", "date", "datenow", "timenow", "dateadd", "timeadd"]:
|
||||||
self._rules.append((r"\b" + fn + r"\b", funcFmt))
|
self._rules.append((r"\b" + fn + r"\b", FuncFmt))
|
||||||
varFmt = QTextCharFormat()
|
VarFmt = QTextCharFormat()
|
||||||
varFmt.setForeground(QColor("#9CDCFE"))
|
VarFmt.setForeground(QColor("#9CDCFE"))
|
||||||
varFmt.setFontWeight(QFont.Weight.Normal)
|
VarFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
var_names = [name for _, (name, _) in ALL_VARIABLES.items()]
|
var_names = [name for _, (name, _) in createAllVariablesTable().items()]
|
||||||
for var in var_names:
|
for var in var_names:
|
||||||
self._rules.append((r"\b" + var + r"\b", varFmt))
|
self._rules.append((r"\b" + var + r"\b", VarFmt))
|
||||||
strFmt = QTextCharFormat()
|
StrFmt = QTextCharFormat()
|
||||||
strFmt.setForeground(QColor("#CE9178"))
|
StrFmt.setForeground(QColor("#CE9178"))
|
||||||
strFmt.setFontWeight(QFont.Weight.Normal)
|
StrFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
self._rules.append((r'"[^"]*"', strFmt))
|
self._rules.append((r'"[^"]*"', StrFmt))
|
||||||
self._rules.append((r"'[^']*'", strFmt))
|
self._rules.append((r"'[^']*'", StrFmt))
|
||||||
numFmt = QTextCharFormat()
|
NumFmt = QTextCharFormat()
|
||||||
numFmt.setForeground(QColor("#B5CEA8"))
|
NumFmt.setForeground(QColor("#B5CEA8"))
|
||||||
numFmt.setFontWeight(QFont.Weight.Normal)
|
NumFmt.setFontWeight(QFont.Weight.Normal)
|
||||||
self._rules.append((r"\b\d+(?:\.\d+)?\b", numFmt))
|
self._rules.append((r"\b\d+(?:\.\d+)?\b", NumFmt))
|
||||||
commentFmt = QTextCharFormat()
|
CommentFmt = QTextCharFormat()
|
||||||
commentFmt.setForeground(QColor("#6A9955"))
|
CommentFmt.setForeground(QColor("#6A9955"))
|
||||||
commentFmt.setFontItalic(True)
|
CommentFmt.setFontItalic(True)
|
||||||
self._rules.append((r"--[^\n]*", commentFmt))
|
self._rules.append((r"--[^\n]*", CommentFmt))
|
||||||
|
|
||||||
def highlightBlock(
|
def highlightBlock(
|
||||||
self,
|
self,
|
||||||
@@ -140,22 +147,35 @@ class _DebugResultDialog(QDialog):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.setWindowTitle("调试运行结果 - AutoLibrary")
|
self.setWindowTitle("调试运行结果 - AutoLibrary")
|
||||||
self.setMinimumSize(600, 200)
|
self.setMinimumSize(600, 200)
|
||||||
layout = QVBoxLayout(self)
|
DbgLayout = QVBoxLayout(self)
|
||||||
table = QTableWidget(len(changes), 3)
|
DbgTable = QTableWidget(len(changes), 3)
|
||||||
table.setHorizontalHeaderLabels(["目标变量", "原始数据", "运行后数据"])
|
DbgTable.setHorizontalHeaderLabels(["目标变量", "原始数据", "运行后数据"])
|
||||||
table.horizontalHeader().setStretchLastSection(True)
|
DbgTable.horizontalHeader().setStretchLastSection(True)
|
||||||
table.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
|
DbgTable.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
|
||||||
table.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
DbgTable.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
|
||||||
for row, (display_name, name, var_type, before_val, after_val) in enumerate(changes):
|
for row, (display_name, name, var_type, before_val, after_val) in enumerate(changes):
|
||||||
label = f"{display_name}: {name}({var_type})"
|
label = f"{display_name}: {name}({var_type})"
|
||||||
table.setItem(row, 0, QTableWidgetItem(label))
|
DbgTable.setItem(row, 0, QTableWidgetItem(label))
|
||||||
table.setItem(row, 1, QTableWidgetItem(str(before_val)))
|
DbgTable.setItem(row, 1, QTableWidgetItem(str(before_val)))
|
||||||
table.setItem(row, 2, QTableWidgetItem(str(after_val)))
|
DbgTable.setItem(row, 2, QTableWidgetItem(str(after_val)))
|
||||||
layout.addWidget(table)
|
DbgLayout.addWidget(DbgTable)
|
||||||
btnBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
|
DbgBtnBox = QDialogButtonBox(QDialogButtonBox.StandardButton.Ok)
|
||||||
btnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
DbgBtnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
||||||
btnBox.accepted.connect(self.accept)
|
DbgBtnBox.accepted.connect(self.accept)
|
||||||
layout.addWidget(btnBox)
|
DbgLayout.addWidget(DbgBtnBox)
|
||||||
|
|
||||||
|
|
||||||
|
class _TabToSpacesEditor(QPlainTextEdit):
|
||||||
|
|
||||||
|
def keyPressEvent(
|
||||||
|
self,
|
||||||
|
event
|
||||||
|
):
|
||||||
|
|
||||||
|
if event.key() == Qt.Key.Key_Tab:
|
||||||
|
self.insertPlainText(" ")
|
||||||
|
return
|
||||||
|
super().keyPressEvent(event)
|
||||||
|
|
||||||
|
|
||||||
class ALAutoScriptEditDialog(QDialog):
|
class ALAutoScriptEditDialog(QDialog):
|
||||||
@@ -173,9 +193,9 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
|
|
||||||
self.setupUi()
|
self.setupUi()
|
||||||
self.connectSignals()
|
self.connectSignals()
|
||||||
self.textEdit.setPlainText(script)
|
self.TextEdit.setPlainText(script)
|
||||||
self._highlighter = ALScriptHighlighter(
|
self._Highlighter = ALScriptHighlighter(
|
||||||
self.textEdit.document()
|
self.TextEdit.document()
|
||||||
)
|
)
|
||||||
if mockData:
|
if mockData:
|
||||||
self.setMockData(mockData)
|
self.setMockData(mockData)
|
||||||
@@ -186,83 +206,80 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
|
|
||||||
self.setWindowTitle("AutoScript 编辑 - AutoLibrary")
|
self.setWindowTitle("AutoScript 编辑 - AutoLibrary")
|
||||||
self.setMinimumSize(660, 600)
|
self.setMinimumSize(660, 600)
|
||||||
layout = QVBoxLayout(self)
|
Layout = QVBoxLayout(self)
|
||||||
layout.setSpacing(3)
|
Layout.setSpacing(3)
|
||||||
layout.setContentsMargins(3, 3, 3, 3)
|
Layout.setContentsMargins(3, 3, 3, 3)
|
||||||
toolbarLayout = QHBoxLayout()
|
ToolbarLayout = QHBoxLayout()
|
||||||
self.zoomInBtn = QPushButton("+")
|
self.ZoomInBtn = QPushButton("+")
|
||||||
self.zoomInBtn.setFixedSize(25, 25)
|
self.ZoomInBtn.setFixedSize(25, 25)
|
||||||
self.zoomOutBtn = QPushButton("-")
|
self.ZoomOutBtn = QPushButton("-")
|
||||||
self.zoomOutBtn.setFixedSize(25, 25)
|
self.ZoomOutBtn.setFixedSize(25, 25)
|
||||||
self.zoomResetBtn = QPushButton(
|
self.ZoomResetBtn = QPushButton("")
|
||||||
QApplication.style().standardIcon(
|
self.ZoomResetBtn.setIcon(QIcon(":/res/icons/Reset.svg"))
|
||||||
QStyle.StandardPixmap.SP_BrowserReload
|
self.ZoomResetBtn.setIconSize(QSize(20, 20))
|
||||||
), ""
|
self.ZoomResetBtn.setFixedSize(25, 25)
|
||||||
)
|
self.ZoomResetBtn.setToolTip("重置缩放")
|
||||||
self.zoomResetBtn.setFixedSize(25, 25)
|
self.ZoomLabel = QLabel(f"{self._fontSize}px")
|
||||||
self.zoomResetBtn.setToolTip("重置缩放")
|
self.ZoomLabel.setFixedHeight(25)
|
||||||
self.zoomLabel = QLabel(f"{self._fontSize}px")
|
self.OrchBtn = QPushButton("编排")
|
||||||
self.zoomLabel.setFixedHeight(25)
|
self.OrchBtn.setFixedHeight(25)
|
||||||
self.orchBtn = QPushButton("编排")
|
self.OrchBtn.setToolTip("可视化生成 AutoScript 代码并插入到光标位置")
|
||||||
self.orchBtn.setFixedHeight(25)
|
ToolbarLayout.addWidget(self.OrchBtn)
|
||||||
self.orchBtn.setToolTip("可视化生成 AutoScript 代码并插入到光标位置")
|
self.DebugBtn = QPushButton("▶ 调试运行")
|
||||||
toolbarLayout.addWidget(self.orchBtn)
|
self.DebugBtn.setFixedHeight(25)
|
||||||
self.debugBtn = QPushButton("▶ 调试运行")
|
self.DebugBtn.setToolTip("使用右侧模拟数据执行脚本,查看目标变量变化")
|
||||||
self.debugBtn.setFixedHeight(25)
|
ToolbarLayout.addWidget(self.DebugBtn)
|
||||||
self.debugBtn.setToolTip("使用右侧模拟数据执行脚本,查看目标变量变化")
|
Sep = QFrame()
|
||||||
toolbarLayout.addWidget(self.debugBtn)
|
Sep.setFrameShape(QFrame.Shape.VLine)
|
||||||
sep = QFrame()
|
Sep.setFrameShadow(QFrame.Shadow.Sunken)
|
||||||
sep.setFrameShape(QFrame.Shape.VLine)
|
Sep.setFixedWidth(1)
|
||||||
sep.setFrameShadow(QFrame.Shadow.Sunken)
|
ToolbarLayout.addWidget(Sep)
|
||||||
sep.setFixedWidth(1)
|
ToolbarLayout.addWidget(self.ZoomInBtn)
|
||||||
toolbarLayout.addWidget(sep)
|
ToolbarLayout.addWidget(self.ZoomOutBtn)
|
||||||
toolbarLayout.addWidget(self.zoomInBtn)
|
ToolbarLayout.addWidget(self.ZoomResetBtn)
|
||||||
toolbarLayout.addWidget(self.zoomOutBtn)
|
ToolbarLayout.addWidget(self.ZoomLabel)
|
||||||
toolbarLayout.addWidget(self.zoomResetBtn)
|
ToolbarLayout.addStretch()
|
||||||
toolbarLayout.addWidget(self.zoomLabel)
|
self.CopyBtn = QPushButton("")
|
||||||
toolbarLayout.addStretch()
|
self.CopyBtn.setIcon(QIcon(":/res/icons/Copy.svg"))
|
||||||
self.copyBtn = QPushButton(
|
self.CopyBtn.setIconSize(QSize(20, 20))
|
||||||
QApplication.style().standardIcon(
|
self.CopyBtn.setFixedSize(25, 25)
|
||||||
QStyle.StandardPixmap.SP_FileDialogDetailedView
|
self.CopyBtn.setToolTip("复制脚本")
|
||||||
), ""
|
ToolbarLayout.addWidget(self.CopyBtn)
|
||||||
)
|
Layout.addLayout(ToolbarLayout)
|
||||||
self.copyBtn.setFixedSize(25, 25)
|
self.TextEdit = _TabToSpacesEditor(self)
|
||||||
self.copyBtn.setToolTip("复制脚本")
|
self.TextEdit.setTabStopDistance(40)
|
||||||
toolbarLayout.addWidget(self.copyBtn)
|
self.TextEdit.setLineWrapMode(
|
||||||
layout.addLayout(toolbarLayout)
|
|
||||||
self.textEdit = QPlainTextEdit(self)
|
|
||||||
self.textEdit.setLineWrapMode(
|
|
||||||
QPlainTextEdit.LineWrapMode.NoWrap
|
QPlainTextEdit.LineWrapMode.NoWrap
|
||||||
)
|
)
|
||||||
self.textEdit.setStyleSheet(
|
self.TextEdit.setStyleSheet(
|
||||||
"QPlainTextEdit {"
|
"QPlainTextEdit {"
|
||||||
" font-family: 'Courier New', 'Consolas', monospace;"
|
" font-family: 'Courier New', 'Consolas', monospace;"
|
||||||
f" font-size: {self._fontSize}px;"
|
f" font-size: {self._fontSize}px;"
|
||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
layout.addWidget(self.textEdit)
|
Layout.addWidget(self.TextEdit)
|
||||||
self.createButtonPanel(layout)
|
self.createButtonPanel(Layout)
|
||||||
self.btnBox = QDialogButtonBox(
|
self.BtnBox = QDialogButtonBox(
|
||||||
QDialogButtonBox.StandardButton.Ok |
|
QDialogButtonBox.StandardButton.Ok |
|
||||||
QDialogButtonBox.StandardButton.Cancel
|
QDialogButtonBox.StandardButton.Cancel
|
||||||
)
|
)
|
||||||
self.btnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
self.BtnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
||||||
self.btnBox.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
|
self.BtnBox.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
|
||||||
layout.addWidget(self.btnBox)
|
Layout.addWidget(self.BtnBox)
|
||||||
|
|
||||||
def createButtonPanel(
|
def createButtonPanel(
|
||||||
self,
|
self,
|
||||||
parent_layout
|
ParentLayout
|
||||||
):
|
):
|
||||||
|
|
||||||
splitter = QSplitter(Qt.Orientation.Horizontal)
|
Splitter = QSplitter(Qt.Orientation.Horizontal)
|
||||||
tabWidget = QTabWidget()
|
TabWidget = QTabWidget()
|
||||||
tabWidget.setMaximumHeight(150)
|
TabWidget.setMaximumHeight(150)
|
||||||
basicWidget = QWidget()
|
BasicWidget = QWidget()
|
||||||
basicLayout = QGridLayout(basicWidget)
|
BasicLayout = QGridLayout(BasicWidget)
|
||||||
basicLayout.setSpacing(4)
|
BasicLayout.setSpacing(4)
|
||||||
basicLayout.setContentsMargins(4, 4, 4, 4)
|
BasicLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
basicLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
BasicLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
controlButtons = [
|
controlButtons = [
|
||||||
("如果 (if...)", "if then\n \nend"),
|
("如果 (if...)", "if then\n \nend"),
|
||||||
("再如果 (elseif...)", "elseif then\n "),
|
("再如果 (elseif...)", "elseif then\n "),
|
||||||
@@ -270,22 +287,22 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
("结束 (end)", "end"),
|
("结束 (end)", "end"),
|
||||||
("跳过 (pass)", "-- pass"),
|
("跳过 (pass)", "-- pass"),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(basicLayout, controlButtons, 0, 0, 3)
|
self.addButtonsToGrid(BasicLayout, controlButtons, 0, 0, 3)
|
||||||
assignButtons = [
|
assignButtons = [
|
||||||
("赋值 (=)", " = "),
|
("赋值 (=)", " = "),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(basicLayout, assignButtons, 1, 2, 3)
|
self.addButtonsToGrid(BasicLayout, assignButtons, 1, 2, 3)
|
||||||
tabWidget.addTab(basicWidget, "基本语法")
|
TabWidget.addTab(BasicWidget, "基本语法")
|
||||||
operatorWidget = QWidget()
|
OperatorWidget = QWidget()
|
||||||
operatorLayout = QGridLayout(operatorWidget)
|
OperatorLayout = QGridLayout(OperatorWidget)
|
||||||
operatorLayout.setSpacing(4)
|
OperatorLayout.setSpacing(4)
|
||||||
operatorLayout.setContentsMargins(4, 4, 4, 4)
|
OperatorLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
operatorLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
OperatorLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
arithmeticButtons = [
|
arithmeticButtons = [
|
||||||
("加 (+)", " + "),
|
("加 (+)", " + "),
|
||||||
("减 (-)", " - "),
|
("减 (-)", " - "),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(operatorLayout, arithmeticButtons, 0, 0, 3)
|
self.addButtonsToGrid(OperatorLayout, arithmeticButtons, 0, 0, 3)
|
||||||
compareButtons = [
|
compareButtons = [
|
||||||
("等于 (==)", " == "),
|
("等于 (==)", " == "),
|
||||||
("不等于 (~=)", " ~= "),
|
("不等于 (~=)", " ~= "),
|
||||||
@@ -294,53 +311,73 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
("大于等于 (>=)", " >= "),
|
("大于等于 (>=)", " >= "),
|
||||||
("小于等于 (<=)", " <= "),
|
("小于等于 (<=)", " <= "),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(operatorLayout, compareButtons, 1, 0, 3)
|
self.addButtonsToGrid(OperatorLayout, compareButtons, 1, 0, 3)
|
||||||
logic_buttons = [
|
logic_buttons = [
|
||||||
("且 (and)", " and "),
|
("且 (and)", " and "),
|
||||||
("或 (or)", " or "),
|
("或 (or)", " or "),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(operatorLayout, logic_buttons, 2, 0, 3)
|
self.addButtonsToGrid(OperatorLayout, logic_buttons, 2, 0, 3)
|
||||||
tabWidget.addTab(operatorWidget, "运算符")
|
TabWidget.addTab(OperatorWidget, "运算符")
|
||||||
literalWidget = QWidget()
|
LiteralWidget = QWidget()
|
||||||
literalLayout = QGridLayout(literalWidget)
|
LiteralLayout = QGridLayout(LiteralWidget)
|
||||||
literalLayout.setSpacing(4)
|
LiteralLayout.setSpacing(4)
|
||||||
literalLayout.setContentsMargins(4, 4, 4, 4)
|
LiteralLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
literalLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
LiteralLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
bool_buttons = [
|
bool_buttons = [
|
||||||
("真 (true)", "true"),
|
("真 (true)", "true"),
|
||||||
("假 (false)", "false"),
|
("假 (false)", "false"),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(literalLayout, bool_buttons, 0, 0, 3)
|
self.addButtonsToGrid(LiteralLayout, bool_buttons, 0, 0, 3)
|
||||||
dateTimeButtons = [
|
dateTimeButtons = [
|
||||||
("日期", '"2099-01-01"'),
|
("日期", 'date(2026, 1, 1)'),
|
||||||
("时间", '"00:00"'),
|
("时间", 'time(0, 0)'),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(literalLayout, dateTimeButtons, 1, 0, 3)
|
self.addButtonsToGrid(LiteralLayout, dateTimeButtons, 1, 0, 3)
|
||||||
hintButtons = [
|
hintButtons = [
|
||||||
("字符串", '"请输入文本"'),
|
("字符串", '"请输入文本"'),
|
||||||
("数字", "123"),
|
("数字", "123"),
|
||||||
("注释", "-- 请输入注释"),
|
("注释", "-- 请输入注释"),
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(literalLayout, hintButtons, 2, 0, 3)
|
self.addButtonsToGrid(LiteralLayout, hintButtons, 2, 0, 3)
|
||||||
tabWidget.addTab(literalWidget, "字面量")
|
TabWidget.addTab(LiteralWidget, "字面量")
|
||||||
varWidget = QWidget()
|
VarWidget = QWidget()
|
||||||
varLayout = QGridLayout(varWidget)
|
VarLayout = QGridLayout(VarWidget)
|
||||||
varLayout.setSpacing(4)
|
VarLayout.setSpacing(4)
|
||||||
varLayout.setContentsMargins(4, 4, 4, 4)
|
VarLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
varLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
VarLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
varButtons = [
|
varButtons = [
|
||||||
(display_name, name) for display_name, (name, _) in ALL_VARIABLES.items()
|
(display_name, name) for display_name, (name, _) in createAllVariablesTable().items()
|
||||||
]
|
]
|
||||||
self.addButtonsToGrid(varLayout, varButtons, 0, 0, 3)
|
self.addButtonsToGrid(VarLayout, varButtons, 0, 0, 3)
|
||||||
tabWidget.addTab(varWidget, "变量")
|
TabWidget.addTab(VarWidget, "变量")
|
||||||
mockPanel = self.createMockPanel()
|
FuncWidget = QWidget()
|
||||||
mockPanel.setMinimumWidth(260)
|
FuncLayout = QGridLayout(FuncWidget)
|
||||||
splitter.addWidget(tabWidget)
|
FuncLayout.setSpacing(4)
|
||||||
splitter.addWidget(mockPanel)
|
FuncLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
splitter.setStretchFactor(0, 1)
|
FuncLayout.setAlignment(Qt.AlignLeft | Qt.AlignTop)
|
||||||
splitter.setStretchFactor(1, 1)
|
funcButtons = [
|
||||||
splitter.setSizes([530, 530])
|
("datenow()", "datenow()", "返回当前日期的 Unix 时间戳"),
|
||||||
parent_layout.addWidget(splitter)
|
("timenow()", "timenow()", "返回当前时间在一天中的分钟数"),
|
||||||
|
("dateadd(day, n)", "dateadd(, )", "日期偏移: dateadd(日期时间戳, 天数)"),
|
||||||
|
("timeadd(time, n)", "timeadd(, )", "时间偏移: timeadd(分钟数, 分钟数)"),
|
||||||
|
]
|
||||||
|
for i, (text, template, tooltip) in enumerate(funcButtons):
|
||||||
|
Btn = QPushButton(text)
|
||||||
|
Btn.setProperty("template", template)
|
||||||
|
Btn.clicked.connect(self.insertTemplate)
|
||||||
|
Btn.setFixedWidth(100)
|
||||||
|
Btn.setFixedHeight(25)
|
||||||
|
Btn.setToolTip(tooltip)
|
||||||
|
FuncLayout.addWidget(Btn, i // 2, i % 2)
|
||||||
|
TabWidget.addTab(FuncWidget, "工具函数")
|
||||||
|
MockPanel = self.createMockPanel()
|
||||||
|
MockPanel.setMinimumWidth(260)
|
||||||
|
Splitter.addWidget(TabWidget)
|
||||||
|
Splitter.addWidget(MockPanel)
|
||||||
|
Splitter.setStretchFactor(0, 1)
|
||||||
|
Splitter.setStretchFactor(1, 1)
|
||||||
|
Splitter.setSizes([530, 530])
|
||||||
|
ParentLayout.addWidget(Splitter)
|
||||||
|
|
||||||
def addButtonsToGrid(
|
def addButtonsToGrid(
|
||||||
self,
|
self,
|
||||||
@@ -355,13 +392,13 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
row = start_row
|
row = start_row
|
||||||
|
|
||||||
for btn_text, template in buttons:
|
for btn_text, template in buttons:
|
||||||
btn = QPushButton(btn_text)
|
Btn = QPushButton(btn_text)
|
||||||
btn.setProperty("template", template)
|
Btn.setProperty("template", template)
|
||||||
btn.clicked.connect(self.insertTemplate)
|
Btn.clicked.connect(self.insertTemplate)
|
||||||
btn.setFixedWidth(100)
|
Btn.setFixedWidth(100)
|
||||||
btn.setFixedHeight(25)
|
Btn.setFixedHeight(25)
|
||||||
btn.setToolTip(f"插入: {template}")
|
Btn.setToolTip(f"插入: {template}")
|
||||||
grid_layout.addWidget(btn, row, col)
|
grid_layout.addWidget(Btn, row, col)
|
||||||
col += 1
|
col += 1
|
||||||
if col >= start_col + max_columns:
|
if col >= start_col + max_columns:
|
||||||
col = start_col
|
col = start_col
|
||||||
@@ -371,18 +408,22 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
self
|
self
|
||||||
) -> QGroupBox:
|
) -> QGroupBox:
|
||||||
|
|
||||||
group = QGroupBox("模拟目标数据")
|
Group = QGroupBox("模拟目标数据")
|
||||||
form = QFormLayout(group)
|
Form = QFormLayout(Group)
|
||||||
form.setSpacing(4)
|
Form.setSpacing(4)
|
||||||
form.setContentsMargins(5, 10, 5, 5)
|
Form.setContentsMargins(5, 10, 5, 5)
|
||||||
self._mockWidgets = {}
|
self._mockWidgets = {}
|
||||||
for name, var_type, key_path, display_name in _TARGET_VAR_DEFS:
|
mockData = createMockTargetData()
|
||||||
default = _MOCK_TYPE_VALUES.get(var_type, "")
|
for name, var_type, key_path, display_name in createTargetVarDefs():
|
||||||
widget = self.makeMockInput(var_type, default)
|
d = mockData
|
||||||
label = QLabel(f"{display_name}: {name}({var_type})")
|
for key in key_path:
|
||||||
form.addRow(label, widget)
|
d = d[key]
|
||||||
self._mockWidgets[name] = (widget, var_type, key_path)
|
default = d
|
||||||
return group
|
Widget = self.makeMockInput(var_type, default)
|
||||||
|
Label = QLabel(f"{display_name}: {name}({var_type})")
|
||||||
|
Form.addRow(Label, Widget)
|
||||||
|
self._mockWidgets[name] = (Widget, var_type, key_path)
|
||||||
|
return Group
|
||||||
|
|
||||||
def makeMockInput(
|
def makeMockInput(
|
||||||
self,
|
self,
|
||||||
@@ -391,48 +432,48 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
) -> QWidget:
|
) -> QWidget:
|
||||||
|
|
||||||
if var_type == "String":
|
if var_type == "String":
|
||||||
w = QLineEdit()
|
W = QLineEdit()
|
||||||
w.setText(str(default))
|
W.setText(str(default))
|
||||||
return w
|
return W
|
||||||
if var_type == "Boolean":
|
if var_type == "Boolean":
|
||||||
w = QComboBox()
|
W = QComboBox()
|
||||||
w.addItems(["是", "否"])
|
W.addItems(["是", "否"])
|
||||||
w.setCurrentIndex(0 if default else 1)
|
W.setCurrentIndex(0 if default else 1)
|
||||||
return w
|
return W
|
||||||
if var_type == "Date":
|
if var_type == "Date":
|
||||||
w = QDateEdit()
|
W = QDateEdit()
|
||||||
w.setCalendarPopup(True)
|
W.setCalendarPopup(True)
|
||||||
w.setDisplayFormat("yyyy-MM-dd")
|
W.setDisplayFormat("yyyy-MM-dd")
|
||||||
w.setDate(QDate.fromString(str(default), "yyyy-MM-dd"))
|
W.setDate(QDate.fromString(str(default), "yyyy-MM-dd"))
|
||||||
return w
|
return W
|
||||||
if var_type == "Time":
|
if var_type == "Time":
|
||||||
w = QTimeEdit()
|
W = QTimeEdit()
|
||||||
w.setDisplayFormat("HH:mm")
|
W.setDisplayFormat("HH:mm")
|
||||||
w.setTime(QTime.fromString(str(default), "HH:mm"))
|
W.setTime(QTime.fromString(str(default), "HH:mm"))
|
||||||
return w
|
return W
|
||||||
if var_type == "Int":
|
if var_type == "Int":
|
||||||
w = QSpinBox()
|
W = QSpinBox()
|
||||||
w.setMinimum(-999999)
|
W.setMinimum(-999999)
|
||||||
w.setMaximum(999999)
|
W.setMaximum(999999)
|
||||||
w.setValue(int(default) if default else 0)
|
W.setValue(int(default) if default else 0)
|
||||||
return w
|
return W
|
||||||
if var_type == "Float":
|
if var_type == "Float":
|
||||||
w = QDoubleSpinBox()
|
W = QDoubleSpinBox()
|
||||||
w.setMinimum(-999999.0)
|
W.setMinimum(-999999.0)
|
||||||
w.setMaximum(999999.0)
|
W.setMaximum(999999.0)
|
||||||
w.setDecimals(2)
|
W.setDecimals(2)
|
||||||
w.setValue(float(default) if default else 0.0)
|
W.setValue(float(default) if default else 0.0)
|
||||||
return w
|
return W
|
||||||
w = QLineEdit()
|
W = QLineEdit()
|
||||||
w.setText(str(default))
|
W.setText(str(default))
|
||||||
return w
|
return W
|
||||||
|
|
||||||
def getMockData(
|
def getMockData(
|
||||||
self
|
self
|
||||||
) -> dict:
|
) -> dict:
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
for name, var_type, key_path, display_name in _TARGET_VAR_DEFS:
|
for name, var_type, key_path, display_name in createTargetVarDefs():
|
||||||
widget, _, _ = self._mockWidgets[name]
|
widget, _, _ = self._mockWidgets[name]
|
||||||
value = self.getMockValue(widget, var_type)
|
value = self.getMockValue(widget, var_type)
|
||||||
d = data
|
d = data
|
||||||
@@ -448,7 +489,7 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
|
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
for name, var_type, key_path, display_name in _TARGET_VAR_DEFS:
|
for name, var_type, key_path, display_name in createTargetVarDefs():
|
||||||
d = data
|
d = data
|
||||||
try:
|
try:
|
||||||
for key in key_path:
|
for key in key_path:
|
||||||
@@ -500,45 +541,45 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.btnBox.accepted.connect(self.accept)
|
self.BtnBox.accepted.connect(self.accept)
|
||||||
self.btnBox.rejected.connect(self.reject)
|
self.BtnBox.rejected.connect(self.reject)
|
||||||
self.orchBtn.clicked.connect(self.onOpenOrchDialog)
|
self.OrchBtn.clicked.connect(self.onOpenOrchDialog)
|
||||||
self.debugBtn.clicked.connect(self.onDebugRun)
|
self.DebugBtn.clicked.connect(self.onDebugRun)
|
||||||
self.zoomInBtn.clicked.connect(self.onZoomIn)
|
self.ZoomInBtn.clicked.connect(self.onZoomIn)
|
||||||
self.zoomOutBtn.clicked.connect(self.onZoomOut)
|
self.ZoomOutBtn.clicked.connect(self.onZoomOut)
|
||||||
self.zoomResetBtn.clicked.connect(self.onZoomReset)
|
self.ZoomResetBtn.clicked.connect(self.onZoomReset)
|
||||||
self.copyBtn.clicked.connect(self.onCopy)
|
self.CopyBtn.clicked.connect(self.onCopy)
|
||||||
|
|
||||||
def getScript(
|
def getScript(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
return self.textEdit.toPlainText()
|
return self.TextEdit.toPlainText()
|
||||||
|
|
||||||
def updateFontSize(
|
def updateFontSize(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.textEdit.setStyleSheet(
|
self.TextEdit.setStyleSheet(
|
||||||
"QPlainTextEdit {"
|
"QPlainTextEdit {"
|
||||||
" font-family: 'Courier New', 'Consolas', monospace;"
|
" font-family: 'Courier New', 'Consolas', monospace;"
|
||||||
f" font-size: {self._fontSize}px;"
|
f" font-size: {self._fontSize}px;"
|
||||||
"}"
|
"}"
|
||||||
)
|
)
|
||||||
self.zoomLabel.setText(f"{self._fontSize}px")
|
self.ZoomLabel.setText(f"{self._fontSize}px")
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def insertTemplate(
|
def insertTemplate(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
btn = self.sender()
|
Btn = self.sender()
|
||||||
if not isinstance(btn, QPushButton):
|
if not isinstance(Btn, QPushButton):
|
||||||
return
|
return
|
||||||
template = btn.property("template")
|
template = Btn.property("template")
|
||||||
if not template:
|
if not template:
|
||||||
return
|
return
|
||||||
cursor = self.textEdit.textCursor()
|
cursor = self.TextEdit.textCursor()
|
||||||
cursor.insertText(template)
|
cursor.insertText(template)
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
@@ -570,14 +611,11 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
clipboard = QApplication.clipboard()
|
Clipboard = QApplication.clipboard()
|
||||||
clipboard.setText(self.textEdit.toPlainText())
|
Clipboard.setText(self.TextEdit.toPlainText())
|
||||||
original = self.copyBtn.text()
|
self.CopyBtn.setEnabled(False)
|
||||||
self.copyBtn.setText("已复制")
|
|
||||||
self.copyBtn.setEnabled(False)
|
|
||||||
QTimer.singleShot(2000, lambda: (
|
QTimer.singleShot(2000, lambda: (
|
||||||
self.copyBtn.setText(original),
|
self.CopyBtn.setEnabled(True)
|
||||||
self.copyBtn.setEnabled(True)
|
|
||||||
))
|
))
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
@@ -586,33 +624,33 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
):
|
):
|
||||||
|
|
||||||
from gui.ALAutoScriptOrchDialog import ALAutoScriptOrchDialog
|
from gui.ALAutoScriptOrchDialog import ALAutoScriptOrchDialog
|
||||||
dlg = ALAutoScriptOrchDialog(self)
|
Dlg = ALAutoScriptOrchDialog(self)
|
||||||
if dlg.exec() == QDialog.DialogCode.Accepted:
|
if Dlg.exec() == QDialog.DialogCode.Accepted:
|
||||||
script = dlg.getScript()
|
script = Dlg.getScript()
|
||||||
if script:
|
if script:
|
||||||
cursor = self.textEdit.textCursor()
|
cursor = self.TextEdit.textCursor()
|
||||||
cursor.insertText(script)
|
cursor.insertText(script)
|
||||||
dlg.deleteLater()
|
Dlg.deleteLater()
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onDebugRun(
|
def onDebugRun(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
script = self.textEdit.toPlainText().strip()
|
script = self.TextEdit.toPlainText().strip()
|
||||||
if not script:
|
if not script:
|
||||||
QMessageBox.warning(self, "提示", "脚本内容为空。")
|
QMessageBox.warning(self, "提示", "脚本内容为空。")
|
||||||
return
|
return
|
||||||
target_data = self.getMockData()
|
target_data = self.getMockData()
|
||||||
before = deepcopy(target_data)
|
before = deepcopy(target_data)
|
||||||
try:
|
try:
|
||||||
registerDefaultTargetVars()
|
engine = createEngine()
|
||||||
execute(script, target_data)
|
engine.execute(script, target_data)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
QMessageBox.warning(self, "运行错误", str(e))
|
QMessageBox.warning(self, "运行错误", str(e))
|
||||||
return
|
return
|
||||||
changes = []
|
changes = []
|
||||||
for name, var_type, key_path, display_name in _TARGET_VAR_DEFS:
|
for name, var_type, key_path, display_name in createTargetVarDefs():
|
||||||
before_val = before
|
before_val = before
|
||||||
after_val = target_data
|
after_val = target_data
|
||||||
try:
|
try:
|
||||||
@@ -626,6 +664,6 @@ class ALAutoScriptEditDialog(QDialog):
|
|||||||
if not changes:
|
if not changes:
|
||||||
QMessageBox.information(self, "调试运行", "目标变量未发生变化。")
|
QMessageBox.information(self, "调试运行", "目标变量未发生变化。")
|
||||||
return
|
return
|
||||||
dlg = _DebugResultDialog(changes, self)
|
Dlg = _DebugResultDialog(changes, self)
|
||||||
dlg.exec()
|
Dlg.exec()
|
||||||
dlg.deleteLater()
|
Dlg.deleteLater()
|
||||||
|
|||||||
@@ -1,3 +1,10 @@
|
|||||||
from gui.ALAutoScriptOrchDialog._dialog import ALAutoScriptOrchDialog
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
__all__ = ["ALAutoScriptOrchDialog"]
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from ._dialog import ALAutoScriptOrchDialog
|
||||||
|
|||||||
@@ -57,81 +57,81 @@ class ConditionalBlock(QGroupBox):
|
|||||||
"margin-top: 5px; padding-top: 5px; }"
|
"margin-top: 5px; padding-top: 5px; }"
|
||||||
)
|
)
|
||||||
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
|
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Fixed)
|
||||||
mainLayout = QVBoxLayout(self)
|
MainLayout = QVBoxLayout(self)
|
||||||
mainLayout.setSpacing(6)
|
MainLayout.setSpacing(6)
|
||||||
mainLayout.setContentsMargins(8, 8, 8, 8)
|
MainLayout.setContentsMargins(8, 8, 8, 8)
|
||||||
headerLayout = QHBoxLayout()
|
HeaderLayout = QHBoxLayout()
|
||||||
headerLayout.setSpacing(8)
|
HeaderLayout.setSpacing(8)
|
||||||
self.typeCombo = QComboBox(self)
|
self.TypeCombo = QComboBox(self)
|
||||||
self.typeCombo.addItem("IF", "IF")
|
self.TypeCombo.addItem("IF", "IF")
|
||||||
self.typeCombo.addItem("ELSE IF", "ELSE IF")
|
self.TypeCombo.addItem("ELSE IF", "ELSE IF")
|
||||||
self.typeCombo.addItem("ELSE", "ELSE")
|
self.TypeCombo.addItem("ELSE", "ELSE")
|
||||||
self.typeCombo.setFixedHeight(25)
|
self.TypeCombo.setFixedHeight(25)
|
||||||
if self.blockIndex == 0:
|
if self.blockIndex == 0:
|
||||||
self.typeCombo.setEnabled(False)
|
self.TypeCombo.setEnabled(False)
|
||||||
headerLayout.addWidget(QLabel("类型:", self))
|
HeaderLayout.addWidget(QLabel("类型:", self))
|
||||||
headerLayout.addWidget(self.typeCombo)
|
HeaderLayout.addWidget(self.TypeCombo)
|
||||||
headerLayout.addStretch()
|
HeaderLayout.addStretch()
|
||||||
self.deleteBlockBtn = QPushButton("删除此块", self)
|
self.DeleteBlockBtn = QPushButton("删除此块", self)
|
||||||
self.deleteBlockBtn.setStyleSheet("color: red;")
|
self.DeleteBlockBtn.setStyleSheet("color: red;")
|
||||||
self.deleteBlockBtn.setFixedHeight(25)
|
self.DeleteBlockBtn.setFixedHeight(25)
|
||||||
headerLayout.addWidget(self.deleteBlockBtn)
|
HeaderLayout.addWidget(self.DeleteBlockBtn)
|
||||||
mainLayout.addLayout(headerLayout)
|
MainLayout.addLayout(HeaderLayout)
|
||||||
self.conditionWidget = QWidget(self)
|
self.ConditionWidget = QWidget(self)
|
||||||
self.conditionWidget.setSizePolicy(
|
self.ConditionWidget.setSizePolicy(
|
||||||
QSizePolicy.Preferred, QSizePolicy.Preferred
|
QSizePolicy.Preferred, QSizePolicy.Preferred
|
||||||
)
|
)
|
||||||
condLayout = QVBoxLayout(self.conditionWidget)
|
CondLayout = QVBoxLayout(self.ConditionWidget)
|
||||||
condLayout.setContentsMargins(4, 4, 4, 4)
|
CondLayout.setContentsMargins(4, 4, 4, 4)
|
||||||
condLayout.setSpacing(6)
|
CondLayout.setSpacing(6)
|
||||||
self.condRowsLayout = QVBoxLayout()
|
self.CondRowsLayout = QVBoxLayout()
|
||||||
self.condRowsLayout.setSpacing(4)
|
self.CondRowsLayout.setSpacing(4)
|
||||||
condLayout.addLayout(self.condRowsLayout)
|
CondLayout.addLayout(self.CondRowsLayout)
|
||||||
self.addCondBtn = QPushButton("+ 添加条件", self.conditionWidget)
|
self.AddCondBtn = QPushButton("+ 添加条件", self.ConditionWidget)
|
||||||
self.addCondBtn.setFixedHeight(25)
|
self.AddCondBtn.setFixedHeight(25)
|
||||||
condLayout.addWidget(self.addCondBtn)
|
CondLayout.addWidget(self.AddCondBtn)
|
||||||
mainLayout.addWidget(self.conditionWidget)
|
MainLayout.addWidget(self.ConditionWidget)
|
||||||
self.actionLabel = QLabel("执行步骤:", self)
|
self.ActionLabel = QLabel("执行步骤:", self)
|
||||||
self.actionLabel.setFixedHeight(25)
|
self.ActionLabel.setFixedHeight(25)
|
||||||
mainLayout.addWidget(self.actionLabel)
|
MainLayout.addWidget(self.ActionLabel)
|
||||||
self.actionsLayout = QVBoxLayout()
|
self.ActionsLayout = QVBoxLayout()
|
||||||
self.actionsLayout.setSpacing(4)
|
self.ActionsLayout.setSpacing(4)
|
||||||
mainLayout.addLayout(self.actionsLayout)
|
MainLayout.addLayout(self.ActionsLayout)
|
||||||
self.addActionBtn = QPushButton("+ 添加执行步骤", self)
|
self.AddActionBtn = QPushButton("+ 添加执行步骤", self)
|
||||||
self.addActionBtn.setFixedHeight(25)
|
self.AddActionBtn.setFixedHeight(25)
|
||||||
mainLayout.addWidget(self.addActionBtn)
|
MainLayout.addWidget(self.AddActionBtn)
|
||||||
self.setUpdatesEnabled(True)
|
self.setUpdatesEnabled(True)
|
||||||
|
|
||||||
def connectSignals(
|
def connectSignals(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.typeCombo.currentIndexChanged.connect(self.onTypeChanged)
|
self.TypeCombo.currentIndexChanged.connect(self.onTypeChanged)
|
||||||
self.addCondBtn.clicked.connect(self.addConditionRow)
|
self.AddCondBtn.clicked.connect(self.addConditionRow)
|
||||||
self.addActionBtn.clicked.connect(self.addActionStep)
|
self.AddActionBtn.clicked.connect(self.addActionStep)
|
||||||
|
|
||||||
def addInitialConditionRow(
|
def addInitialConditionRow(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
row = ConditionRowFrame(
|
Row = ConditionRowFrame(
|
||||||
self._varMgr, self.blockIndex,
|
self._varMgr, self.blockIndex,
|
||||||
isFirst=True, parent=self
|
isFirst=True, parent=self
|
||||||
)
|
)
|
||||||
self._conditionRows.append(row)
|
self._conditionRows.append(Row)
|
||||||
self.condRowsLayout.addWidget(row)
|
self.CondRowsLayout.addWidget(Row)
|
||||||
|
|
||||||
def addConditionRow(
|
def addConditionRow(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
row = ConditionRowFrame(
|
Row = ConditionRowFrame(
|
||||||
self._varMgr, self.blockIndex,
|
self._varMgr, self.blockIndex,
|
||||||
isFirst=False, parent=self
|
isFirst=False, parent=self
|
||||||
)
|
)
|
||||||
row.deleteBtn.clicked.connect(lambda: self.removeConditionRow(row))
|
Row.DeleteBtn.clicked.connect(lambda: self.removeConditionRow(Row))
|
||||||
self._conditionRows.append(row)
|
self._conditionRows.append(Row)
|
||||||
self.condRowsLayout.addWidget(row)
|
self.CondRowsLayout.addWidget(Row)
|
||||||
|
|
||||||
def removeConditionRow(
|
def removeConditionRow(
|
||||||
self,
|
self,
|
||||||
@@ -140,7 +140,7 @@ class ConditionalBlock(QGroupBox):
|
|||||||
|
|
||||||
if row in self._conditionRows and len(self._conditionRows) > 1:
|
if row in self._conditionRows and len(self._conditionRows) > 1:
|
||||||
self._conditionRows.remove(row)
|
self._conditionRows.remove(row)
|
||||||
self.condRowsLayout.removeWidget(row)
|
self.CondRowsLayout.removeWidget(row)
|
||||||
row.hide()
|
row.hide()
|
||||||
row.deleteLater()
|
row.deleteLater()
|
||||||
|
|
||||||
@@ -148,10 +148,10 @@ class ConditionalBlock(QGroupBox):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
step = ActionStepFrame(self._varMgr, self.blockIndex, parent=self)
|
Step = ActionStepFrame(self._varMgr, self.blockIndex, parent=self)
|
||||||
step.deleteBtn.clicked.connect(lambda: self.removeActionStep(step))
|
Step.DeleteBtn.clicked.connect(lambda: self.removeActionStep(Step))
|
||||||
self._actionWidgets.append(step)
|
self._actionWidgets.append(Step)
|
||||||
self.actionsLayout.addWidget(step)
|
self.ActionsLayout.addWidget(Step)
|
||||||
|
|
||||||
def removeActionStep(
|
def removeActionStep(
|
||||||
self,
|
self,
|
||||||
@@ -160,7 +160,7 @@ class ConditionalBlock(QGroupBox):
|
|||||||
|
|
||||||
if step in self._actionWidgets:
|
if step in self._actionWidgets:
|
||||||
self._actionWidgets.remove(step)
|
self._actionWidgets.remove(step)
|
||||||
self.actionsLayout.removeWidget(step)
|
self.ActionsLayout.removeWidget(step)
|
||||||
step.hide()
|
step.hide()
|
||||||
step.deleteLater()
|
step.deleteLater()
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ class ConditionalBlock(QGroupBox):
|
|||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
return self.typeCombo.currentData()
|
return self.TypeCombo.currentData()
|
||||||
|
|
||||||
def getConditionRows(
|
def getConditionRows(
|
||||||
self
|
self
|
||||||
@@ -203,7 +203,6 @@ class ConditionalBlock(QGroupBox):
|
|||||||
]
|
]
|
||||||
if not condTexts:
|
if not condTexts:
|
||||||
condTexts = ["true"]
|
condTexts = ["true"]
|
||||||
|
|
||||||
if len(condTexts) == 1:
|
if len(condTexts) == 1:
|
||||||
combined = condTexts[0]
|
combined = condTexts[0]
|
||||||
else:
|
else:
|
||||||
@@ -240,18 +239,18 @@ class ConditionalBlock(QGroupBox):
|
|||||||
prevType: str | None
|
prevType: str | None
|
||||||
):
|
):
|
||||||
|
|
||||||
model = self.typeCombo.model()
|
model = self.TypeCombo.model()
|
||||||
if model is None:
|
if model is None:
|
||||||
return
|
return
|
||||||
for data in ("ELSE IF", "ELSE"):
|
for data in ("ELSE IF", "ELSE"):
|
||||||
idx = self.typeCombo.findData(data)
|
idx = self.TypeCombo.findData(data)
|
||||||
if idx < 0:
|
if idx < 0:
|
||||||
continue
|
continue
|
||||||
item = model.item(idx)
|
item = model.item(idx)
|
||||||
shouldEnable = prevType != "ELSE"
|
shouldEnable = prevType != "ELSE"
|
||||||
item.setEnabled(shouldEnable)
|
item.setEnabled(shouldEnable)
|
||||||
if prevType == "ELSE" and self.typeCombo.currentData() in ("ELSE IF", "ELSE"):
|
if prevType == "ELSE" and self.TypeCombo.currentData() in ("ELSE IF", "ELSE"):
|
||||||
self.typeCombo.setCurrentIndex(0)
|
self.TypeCombo.setCurrentIndex(0)
|
||||||
|
|
||||||
@Slot(int)
|
@Slot(int)
|
||||||
def onTypeChanged(
|
def onTypeChanged(
|
||||||
@@ -259,8 +258,8 @@ class ConditionalBlock(QGroupBox):
|
|||||||
_idx
|
_idx
|
||||||
):
|
):
|
||||||
|
|
||||||
isCond = self.typeCombo.currentData() in ("IF", "ELSE IF")
|
isCond = self.TypeCombo.currentData() in ("IF", "ELSE IF")
|
||||||
self.conditionWidget.setVisible(isCond)
|
self.ConditionWidget.setVisible(isCond)
|
||||||
self.actionLabel.setText(
|
self.ActionLabel.setText(
|
||||||
"执行步骤:" if isCond else "ELSE 执行步骤:"
|
"执行步骤:" if isCond else "ELSE 执行步骤:"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ class ALAutoScriptOrchDialog(QDialog):
|
|||||||
self.setupUi()
|
self.setupUi()
|
||||||
self.connectSignals()
|
self.connectSignals()
|
||||||
self.addBlock()
|
self.addBlock()
|
||||||
self.scrollLayout.addStretch()
|
self.ScrollLayout.addStretch()
|
||||||
|
|
||||||
def setupUi(
|
def setupUi(
|
||||||
self
|
self
|
||||||
@@ -49,33 +49,33 @@ class ALAutoScriptOrchDialog(QDialog):
|
|||||||
self.setWindowTitle("AutoScript 指令编排 - AutoLibrary")
|
self.setWindowTitle("AutoScript 指令编排 - AutoLibrary")
|
||||||
self.setMinimumSize(640, 600)
|
self.setMinimumSize(640, 600)
|
||||||
self.setModal(True)
|
self.setModal(True)
|
||||||
mainLayout = QVBoxLayout(self)
|
MainLayout = QVBoxLayout(self)
|
||||||
scroll = QScrollArea()
|
Scroll = QScrollArea()
|
||||||
scroll.setWidgetResizable(True)
|
Scroll.setWidgetResizable(True)
|
||||||
scroll.setFrameShape(QFrame.NoFrame)
|
Scroll.setFrameShape(QFrame.NoFrame)
|
||||||
scrollContent = QWidget()
|
ScrollContent = QWidget()
|
||||||
self.scrollLayout = QVBoxLayout(scrollContent)
|
self.ScrollLayout = QVBoxLayout(ScrollContent)
|
||||||
self.scrollLayout.setSpacing(5)
|
self.ScrollLayout.setSpacing(5)
|
||||||
scroll.setWidget(scrollContent)
|
Scroll.setWidget(ScrollContent)
|
||||||
mainLayout.addWidget(scroll)
|
MainLayout.addWidget(Scroll)
|
||||||
self.addBlockBtn = QPushButton("+ 添加判断块")
|
self.AddBlockBtn = QPushButton("+ 添加判断块")
|
||||||
self.addBlockBtn.setFixedHeight(25)
|
self.AddBlockBtn.setFixedHeight(25)
|
||||||
mainLayout.addWidget(self.addBlockBtn)
|
MainLayout.addWidget(self.AddBlockBtn)
|
||||||
self.btnBox = QDialogButtonBox(
|
self.BtnBox = QDialogButtonBox(
|
||||||
QDialogButtonBox.StandardButton.Ok |
|
QDialogButtonBox.StandardButton.Ok |
|
||||||
QDialogButtonBox.StandardButton.Cancel
|
QDialogButtonBox.StandardButton.Cancel
|
||||||
)
|
)
|
||||||
self.btnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
self.BtnBox.button(QDialogButtonBox.StandardButton.Ok).setText("确定")
|
||||||
self.btnBox.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
|
self.BtnBox.button(QDialogButtonBox.StandardButton.Cancel).setText("取消")
|
||||||
mainLayout.addWidget(self.btnBox)
|
MainLayout.addWidget(self.BtnBox)
|
||||||
|
|
||||||
def connectSignals(
|
def connectSignals(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.btnBox.accepted.connect(self.onAccept)
|
self.BtnBox.accepted.connect(self.onAccept)
|
||||||
self.btnBox.rejected.connect(self.reject)
|
self.BtnBox.rejected.connect(self.reject)
|
||||||
self.addBlockBtn.clicked.connect(self.addBlock)
|
self.AddBlockBtn.clicked.connect(self.addBlock)
|
||||||
|
|
||||||
def updateBlockTypeRestrictions(
|
def updateBlockTypeRestrictions(
|
||||||
self
|
self
|
||||||
@@ -90,24 +90,24 @@ class ALAutoScriptOrchDialog(QDialog):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
block = ConditionalBlock(
|
Block = ConditionalBlock(
|
||||||
len(self._blocks), self._varMgr, parent=self
|
len(self._blocks), self._varMgr, parent=self
|
||||||
)
|
)
|
||||||
block.deleteBlockBtn.clicked.connect(lambda: self.removeBlock(block))
|
Block.DeleteBlockBtn.clicked.connect(lambda: self.removeBlock(Block))
|
||||||
block.typeCombo.currentIndexChanged.connect(self.updateBlockTypeRestrictions)
|
Block.TypeCombo.currentIndexChanged.connect(self.updateBlockTypeRestrictions)
|
||||||
block.addActionStep()
|
Block.addActionStep()
|
||||||
self._blocks.append(block)
|
self._blocks.append(Block)
|
||||||
self.updateBlockTypeRestrictions()
|
self.updateBlockTypeRestrictions()
|
||||||
if self.scrollLayout.count() > 0:
|
if self.ScrollLayout.count() > 0:
|
||||||
lastItem = self.scrollLayout.itemAt(
|
lastItem = self.ScrollLayout.itemAt(
|
||||||
self.scrollLayout.count() - 1
|
self.ScrollLayout.count() - 1
|
||||||
)
|
)
|
||||||
if lastItem and lastItem.spacerItem():
|
if lastItem and lastItem.spacerItem():
|
||||||
self.scrollLayout.insertWidget(
|
self.ScrollLayout.insertWidget(
|
||||||
self.scrollLayout.count() - 1, block
|
self.ScrollLayout.count() - 1, Block
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
self.scrollLayout.addWidget(block)
|
self.ScrollLayout.addWidget(Block)
|
||||||
|
|
||||||
def removeBlock(
|
def removeBlock(
|
||||||
self,
|
self,
|
||||||
@@ -119,16 +119,16 @@ class ALAutoScriptOrchDialog(QDialog):
|
|||||||
return
|
return
|
||||||
if block in self._blocks:
|
if block in self._blocks:
|
||||||
self._blocks.remove(block)
|
self._blocks.remove(block)
|
||||||
self.scrollLayout.removeWidget(block)
|
self.ScrollLayout.removeWidget(block)
|
||||||
block.hide()
|
block.hide()
|
||||||
block.deleteLater()
|
block.deleteLater()
|
||||||
for i, blk in enumerate(self._blocks):
|
for i, blk in enumerate(self._blocks):
|
||||||
blk.blockIndex = i
|
blk.blockIndex = i
|
||||||
if i == 0:
|
if i == 0:
|
||||||
blk.typeCombo.setEnabled(False)
|
blk.TypeCombo.setEnabled(False)
|
||||||
blk.typeCombo.setCurrentIndex(0)
|
blk.TypeCombo.setCurrentIndex(0)
|
||||||
else:
|
else:
|
||||||
blk.typeCombo.setEnabled(True)
|
blk.TypeCombo.setEnabled(True)
|
||||||
blk.refreshVarCombos()
|
blk.refreshVarCombos()
|
||||||
self.updateBlockTypeRestrictions()
|
self.updateBlockTypeRestrictions()
|
||||||
|
|
||||||
|
|||||||
@@ -12,11 +12,7 @@ See the LICENSE file for details.
|
|||||||
"""
|
"""
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from PySide6.QtCore import (
|
from PySide6.QtCore import QObject
|
||||||
QObject,
|
|
||||||
QDate,
|
|
||||||
QTime
|
|
||||||
)
|
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
QComboBox,
|
QComboBox,
|
||||||
QDateEdit,
|
QDateEdit,
|
||||||
@@ -31,65 +27,70 @@ from PySide6.QtWidgets import (
|
|||||||
QWidget,
|
QWidget,
|
||||||
)
|
)
|
||||||
|
|
||||||
from autoscript import (
|
from autoscript import createAllVariablesTable
|
||||||
ALL_VARIABLES,
|
|
||||||
)
|
|
||||||
|
|
||||||
# Types that support arithmetic operations (add/sub)
|
VARTYPE_INFOS = [
|
||||||
ARITH_TYPES = {"Date", "Time", "Int", "Float"}
|
# varType, isArithType
|
||||||
VAR_TYPE_ORDER = [
|
("String", False),
|
||||||
"String",
|
("Int", True),
|
||||||
"Int",
|
("Float", True),
|
||||||
"Float",
|
("Boolean", False),
|
||||||
"Boolean",
|
("Date", True),
|
||||||
"Date",
|
("Time", True),
|
||||||
"Time"
|
|
||||||
]
|
]
|
||||||
PRESET_VARIABLES = [
|
|
||||||
{
|
|
||||||
"name": name.upper(),
|
def getTypeOrder(
|
||||||
"type": vtype,
|
) -> list:
|
||||||
"display": display
|
|
||||||
}
|
return [t for t, _ in VARTYPE_INFOS]
|
||||||
for display, (name, vtype) in ALL_VARIABLES.items()
|
|
||||||
|
def getArithType(
|
||||||
|
varType: str
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
for t, a in VARTYPE_INFOS:
|
||||||
|
if t == varType:
|
||||||
|
return a
|
||||||
|
|
||||||
|
def getPresetVars(
|
||||||
|
) -> list:
|
||||||
|
|
||||||
|
return [
|
||||||
|
{"name": name.upper(), "type": vtype, "display": display}
|
||||||
|
for display, (name, vtype) in createAllVariablesTable().items()
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
COMPARE_OPTIONS = [
|
||||||
|
("等于", "=="),
|
||||||
|
("不等于", "~="),
|
||||||
|
("大于", ">"),
|
||||||
|
("小于", "<"),
|
||||||
|
("大于等于", ">="),
|
||||||
|
("小于等于", "<="),
|
||||||
]
|
]
|
||||||
PRESET_NAMES = {
|
LOGIC_OPTIONS = [
|
||||||
p["name"] for p in PRESET_VARIABLES
|
|
||||||
}
|
|
||||||
# Operator display names (UI-specific), using Lua operator symbols
|
|
||||||
_COMPARE_DISPLAY_MAP = {
|
|
||||||
"==": "等于",
|
|
||||||
"~=": "不等于",
|
|
||||||
">": "大于",
|
|
||||||
"<": "小于",
|
|
||||||
">=": "大于等于",
|
|
||||||
"<=": "小于等于",
|
|
||||||
}
|
|
||||||
COMPARE_OPERATORS = sorted(
|
|
||||||
[(name, op) for op, name in _COMPARE_DISPLAY_MAP.items()],
|
|
||||||
key=lambda x: len(x[1]), reverse=True
|
|
||||||
)
|
|
||||||
LOGIC_OPERATORS = [
|
|
||||||
("并且 (and)", "and"),
|
("并且 (and)", "and"),
|
||||||
("或者 (or)", "or"),
|
("或者 (or)", "or"),
|
||||||
]
|
]
|
||||||
ACTION_TYPES = [
|
ACTION_OPTIONS = [
|
||||||
("设置为", "set"),
|
("设置为", "set"),
|
||||||
("增加", "add"),
|
("增加", "add"),
|
||||||
("减少", "sub"),
|
("减少", "sub"),
|
||||||
]
|
]
|
||||||
DATE_RELATIVE_OPTIONS = [
|
DATE_OPTIONS = [
|
||||||
("前天", "day_before_yesterday"),
|
("前天", "day_before_yesterday"),
|
||||||
("昨天", "yesterday"),
|
("昨天", "yesterday"),
|
||||||
("今天", "today"),
|
("今天", "today"),
|
||||||
("明天", "tomorrow"),
|
("明天", "tomorrow"),
|
||||||
("后天", "day_after_tomorrow")
|
("后天", "day_after_tomorrow")
|
||||||
]
|
]
|
||||||
DATE_OFFSET_UNITS = [
|
DATE_OFFSET_OPTIONS = [
|
||||||
("天", "days"),
|
("天", "days"),
|
||||||
("周", "weeks"),
|
("周", "weeks"),
|
||||||
# NOTE: "月" and "年" use fixed day counts (30 / 365), not calendar months/years,
|
# NOTE: "月" and "年" use fixed day counts (30 / 365), not calendar months/years,
|
||||||
# because date_add() works with second-level offsets (n * 86400).
|
# because dateadd() works with second-level offsets (n * 86400).
|
||||||
("月", "months"),
|
("月", "months"),
|
||||||
("年", "years"),
|
("年", "years"),
|
||||||
]
|
]
|
||||||
@@ -103,100 +104,45 @@ class _DateInputContainer(QWidget):
|
|||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._dynamicItems = {} # index -> raw expression, for one-way parsed items
|
|
||||||
self.setupUi()
|
self.setupUi()
|
||||||
|
|
||||||
def setupUi(
|
def setupUi(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(0, 0, 0, 0)
|
Layout.setContentsMargins(0, 0, 0, 0)
|
||||||
layout.setSpacing(4)
|
Layout.setSpacing(4)
|
||||||
self._modeCombo = QComboBox(self)
|
self._ModeCombo = QComboBox(self)
|
||||||
self._modeCombo.addItem("相对日期", "relative")
|
self._ModeCombo.addItem("相对日期", "relative")
|
||||||
self._modeCombo.addItem("绝对日期", "absolute")
|
self._ModeCombo.addItem("绝对日期", "absolute")
|
||||||
self._modeCombo.setFixedHeight(25)
|
self._ModeCombo.setFixedHeight(25)
|
||||||
self._stack = QStackedWidget(self)
|
self._Stack = QStackedWidget(self)
|
||||||
self._relCombo = QComboBox(self)
|
self._RelCombo = QComboBox(self)
|
||||||
for display, data in DATE_RELATIVE_OPTIONS:
|
for display, data in DATE_OPTIONS:
|
||||||
self._relCombo.addItem(display, data)
|
self._RelCombo.addItem(display, data)
|
||||||
self._relCombo.setFixedHeight(25)
|
self._RelCombo.setFixedHeight(25)
|
||||||
self._stack.addWidget(self._relCombo)
|
self._Stack.addWidget(self._RelCombo)
|
||||||
self._dateEdit = QDateEdit(self)
|
self._DateEdit = QDateEdit(self)
|
||||||
self._dateEdit.setDisplayFormat("yyyy-MM-dd")
|
self._DateEdit.setDisplayFormat("yyyy-MM-dd")
|
||||||
self._dateEdit.setCalendarPopup(True)
|
self._DateEdit.setCalendarPopup(True)
|
||||||
self._dateEdit.setFixedHeight(25)
|
self._DateEdit.setFixedHeight(25)
|
||||||
self._stack.addWidget(self._dateEdit)
|
self._Stack.addWidget(self._DateEdit)
|
||||||
self._modeCombo.currentIndexChanged.connect(
|
self._ModeCombo.currentIndexChanged.connect(
|
||||||
lambda i: self._stack.setCurrentIndex(i)
|
lambda i: self._Stack.setCurrentIndex(i)
|
||||||
)
|
)
|
||||||
layout.addWidget(self._modeCombo)
|
Layout.addWidget(self._ModeCombo)
|
||||||
layout.addWidget(self._stack)
|
Layout.addWidget(self._Stack)
|
||||||
layout.addStretch()
|
Layout.addStretch()
|
||||||
|
|
||||||
_RE_DATE_ADD_CURRENT = re.compile(
|
|
||||||
r'^date_add\(CURRENT_DATE\(\),\s*(-?\d+)\)$', re.IGNORECASE
|
|
||||||
)
|
|
||||||
|
|
||||||
def getValue(
|
def getValue(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
mode = self._modeCombo.currentData()
|
mode = self._ModeCombo.currentData()
|
||||||
if mode == "relative":
|
if mode == "relative":
|
||||||
idx = self._relCombo.currentIndex()
|
return self._RelCombo.currentText()
|
||||||
if idx in self._dynamicItems:
|
return self._DateEdit.date().toString("yyyy-MM-dd")
|
||||||
return self._dynamicItems[idx]
|
|
||||||
return self._relCombo.currentText()
|
|
||||||
return self._dateEdit.date().toString("yyyy-MM-dd")
|
|
||||||
|
|
||||||
def setValue(
|
|
||||||
self,
|
|
||||||
expr: str
|
|
||||||
):
|
|
||||||
|
|
||||||
s = expr.strip()
|
|
||||||
up = s.upper()
|
|
||||||
if up == "CURRENT_DATE()":
|
|
||||||
self._modeCombo.setCurrentIndex(0)
|
|
||||||
self._relCombo.setCurrentIndex(2)
|
|
||||||
return
|
|
||||||
m_add = self._RE_DATE_ADD_CURRENT.match(up)
|
|
||||||
if m_add:
|
|
||||||
n = int(m_add.group(1))
|
|
||||||
_OFFSET_IDX = {-2: 0, -1: 1, 0: 2, 1: 3, 2: 4}
|
|
||||||
idx = _OFFSET_IDX.get(n)
|
|
||||||
if idx is not None:
|
|
||||||
self._modeCombo.setCurrentIndex(0)
|
|
||||||
self._relCombo.setCurrentIndex(idx)
|
|
||||||
return
|
|
||||||
label = f"{n}天后" if n >= 0 else f"{-n}天前"
|
|
||||||
raw = f"CURRENT_DATE {'+' if n >= 0 else '-'} {abs(n)}"
|
|
||||||
self._modeCombo.setCurrentIndex(0)
|
|
||||||
for ci in range(self._relCombo.count()):
|
|
||||||
if ci in self._dynamicItems and self._dynamicItems[ci] == raw:
|
|
||||||
self._relCombo.setCurrentIndex(ci)
|
|
||||||
return
|
|
||||||
idx = self._relCombo.count()
|
|
||||||
self._relCombo.addItem(label)
|
|
||||||
self._dynamicItems[idx] = raw
|
|
||||||
self._relCombo.setCurrentIndex(idx)
|
|
||||||
return
|
|
||||||
m_date_ctor = re.match(r"^DATE\((\d+),\s*(\d+),\s*(\d+)\)$", up)
|
|
||||||
if m_date_ctor:
|
|
||||||
self._modeCombo.setCurrentIndex(1)
|
|
||||||
self._dateEdit.setDate(QDate(
|
|
||||||
int(m_date_ctor.group(1)),
|
|
||||||
int(m_date_ctor.group(2)),
|
|
||||||
int(m_date_ctor.group(3)),
|
|
||||||
))
|
|
||||||
return
|
|
||||||
m_date = re.match(r'^"(\d{4}-\d{2}-\d{2})"$', s)
|
|
||||||
if m_date:
|
|
||||||
self._modeCombo.setCurrentIndex(1)
|
|
||||||
parts = m_date.group(1).split("-")
|
|
||||||
self._dateEdit.setDate(QDate(int(parts[0]), int(parts[1]), int(parts[2])))
|
|
||||||
|
|
||||||
|
|
||||||
class _TimeInputContainer(QWidget):
|
class _TimeInputContainer(QWidget):
|
||||||
@@ -207,38 +153,19 @@ class _TimeInputContainer(QWidget):
|
|||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._timeEdit = QTimeEdit(self)
|
self._TimeEdit = QTimeEdit(self)
|
||||||
self._timeEdit.setDisplayFormat("HH:mm")
|
self._TimeEdit.setDisplayFormat("HH:mm")
|
||||||
self._timeEdit.setFixedHeight(25)
|
self._TimeEdit.setFixedHeight(25)
|
||||||
|
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(0, 0, 0, 0)
|
Layout.setContentsMargins(0, 0, 0, 0)
|
||||||
layout.addWidget(self._timeEdit)
|
Layout.addWidget(self._TimeEdit)
|
||||||
|
|
||||||
def getValue(
|
def getValue(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
return self._timeEdit.time().toString("HH:mm")
|
return self._TimeEdit.time().toString("HH:mm")
|
||||||
|
|
||||||
def setValue(
|
|
||||||
self,
|
|
||||||
expr: str
|
|
||||||
):
|
|
||||||
|
|
||||||
s = expr.strip()
|
|
||||||
up = s.upper()
|
|
||||||
m_time_ctor = re.match(r"^TIME\((\d+),\s*(\d+)\)$", up)
|
|
||||||
if m_time_ctor:
|
|
||||||
self._timeEdit.setTime(QTime(
|
|
||||||
int(m_time_ctor.group(1)),
|
|
||||||
int(m_time_ctor.group(2)),
|
|
||||||
))
|
|
||||||
return
|
|
||||||
m = re.match(r'^"(\d{1,2}:\d{2})"$', s)
|
|
||||||
if m:
|
|
||||||
parts = m.group(1).split(":")
|
|
||||||
self._timeEdit.setTime(QTime(int(parts[0]), int(parts[1])))
|
|
||||||
|
|
||||||
|
|
||||||
class _DateOffsetContainer(QWidget):
|
class _DateOffsetContainer(QWidget):
|
||||||
@@ -249,20 +176,20 @@ class _DateOffsetContainer(QWidget):
|
|||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._spinBox = QSpinBox(self)
|
self._SpinBox = QSpinBox(self)
|
||||||
self._spinBox.setRange(0, 99999)
|
self._SpinBox.setRange(0, 99999)
|
||||||
self._spinBox.setFixedHeight(25)
|
self._SpinBox.setFixedHeight(25)
|
||||||
self._unitCombo = QComboBox(self)
|
self._UnitCombo = QComboBox(self)
|
||||||
for display, data in DATE_OFFSET_UNITS:
|
for display, data in DATE_OFFSET_OPTIONS:
|
||||||
self._unitCombo.addItem(display, data)
|
self._UnitCombo.addItem(display, data)
|
||||||
self._unitCombo.setFixedHeight(25)
|
self._UnitCombo.setFixedHeight(25)
|
||||||
|
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(0, 0, 0, 0)
|
Layout.setContentsMargins(0, 0, 0, 0)
|
||||||
layout.setSpacing(4)
|
Layout.setSpacing(4)
|
||||||
layout.addWidget(self._spinBox)
|
Layout.addWidget(self._SpinBox)
|
||||||
layout.addWidget(self._unitCombo)
|
Layout.addWidget(self._UnitCombo)
|
||||||
layout.addStretch()
|
Layout.addStretch()
|
||||||
|
|
||||||
def getValue(
|
def getValue(
|
||||||
self
|
self
|
||||||
@@ -270,37 +197,20 @@ class _DateOffsetContainer(QWidget):
|
|||||||
|
|
||||||
return str(self.getOffsetDays())
|
return str(self.getOffsetDays())
|
||||||
|
|
||||||
def setValue(
|
|
||||||
self,
|
|
||||||
expr: str
|
|
||||||
):
|
|
||||||
|
|
||||||
s = expr.strip().lstrip("+")
|
|
||||||
try:
|
|
||||||
self._spinBox.setValue(int(s))
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def getOffsetDays(
|
def getOffsetDays(
|
||||||
self
|
self
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
||||||
val = self._spinBox.value()
|
val = self._SpinBox.value()
|
||||||
unit = self._unitCombo.currentData()
|
unit = self._UnitCombo.currentData()
|
||||||
if unit == "weeks":
|
if unit == "weeks":
|
||||||
return val * 7
|
return val*7
|
||||||
if unit == "months":
|
if unit == "months":
|
||||||
return val * 30
|
return val*30
|
||||||
if unit == "years":
|
if unit == "years":
|
||||||
return val * 365
|
return val*365
|
||||||
return val
|
return val
|
||||||
|
|
||||||
def getRawValue(
|
|
||||||
self
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
return str(self._spinBox.value())
|
|
||||||
|
|
||||||
|
|
||||||
class _TimeOffsetContainer(QWidget):
|
class _TimeOffsetContainer(QWidget):
|
||||||
|
|
||||||
@@ -310,14 +220,14 @@ class _TimeOffsetContainer(QWidget):
|
|||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self._spinBox = QSpinBox(self)
|
self._SpinBox = QSpinBox(self)
|
||||||
self._spinBox.setRange(0, 99999)
|
self._SpinBox.setRange(0, 99999)
|
||||||
self._spinBox.setSuffix(" 小时")
|
self._SpinBox.setSuffix(" 小时")
|
||||||
self._spinBox.setFixedHeight(25)
|
self._SpinBox.setFixedHeight(25)
|
||||||
|
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(0, 0, 0, 0)
|
Layout.setContentsMargins(0, 0, 0, 0)
|
||||||
layout.addWidget(self._spinBox)
|
Layout.addWidget(self._SpinBox)
|
||||||
|
|
||||||
def getValue(
|
def getValue(
|
||||||
self
|
self
|
||||||
@@ -325,28 +235,11 @@ class _TimeOffsetContainer(QWidget):
|
|||||||
|
|
||||||
return str(self.getOffsetHours())
|
return str(self.getOffsetHours())
|
||||||
|
|
||||||
def setValue(
|
|
||||||
self,
|
|
||||||
expr: str
|
|
||||||
):
|
|
||||||
|
|
||||||
s = expr.strip().lstrip("+")
|
|
||||||
try:
|
|
||||||
self._spinBox.setValue(int(s))
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def getOffsetHours(
|
def getOffsetHours(
|
||||||
self
|
self
|
||||||
) -> int:
|
) -> int:
|
||||||
|
|
||||||
return self._spinBox.value()
|
return self._SpinBox.value()
|
||||||
|
|
||||||
def getRawValue(
|
|
||||||
self
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
return str(self._spinBox.value())
|
|
||||||
|
|
||||||
|
|
||||||
class VariableManager(QObject):
|
class VariableManager(QObject):
|
||||||
@@ -360,13 +253,13 @@ class VariableManager(QObject):
|
|||||||
self._vars = []
|
self._vars = []
|
||||||
self._nameMap = {}
|
self._nameMap = {}
|
||||||
|
|
||||||
self._initPresetVars()
|
self.initPresetVars()
|
||||||
|
|
||||||
def _initPresetVars(
|
def initPresetVars(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
for p in PRESET_VARIABLES:
|
for p in getPresetVars():
|
||||||
entry = {"name": p["name"], "type": p["type"], "display": p["display"]}
|
entry = {"name": p["name"], "type": p["type"], "display": p["display"]}
|
||||||
self._vars.append(entry)
|
self._vars.append(entry)
|
||||||
self._nameMap[p["name"]] = entry
|
self._nameMap[p["name"]] = entry
|
||||||
@@ -399,19 +292,6 @@ class VariableManager(QObject):
|
|||||||
break
|
break
|
||||||
combo.blockSignals(False)
|
combo.blockSignals(False)
|
||||||
|
|
||||||
def findExactNameEntry(
|
|
||||||
self,
|
|
||||||
combo: QComboBox,
|
|
||||||
name: str
|
|
||||||
) -> int:
|
|
||||||
|
|
||||||
name = name.upper().strip()
|
|
||||||
for i in range(combo.count()):
|
|
||||||
d = combo.itemData(i)
|
|
||||||
if d and len(d) >= 1 and d[0].upper().strip() == name:
|
|
||||||
return i
|
|
||||||
return -1
|
|
||||||
|
|
||||||
|
|
||||||
def makeValueWidget(
|
def makeValueWidget(
|
||||||
var_type: str,
|
var_type: str,
|
||||||
@@ -484,11 +364,11 @@ def makeVarRefCombo(
|
|||||||
parent: QWidget = None
|
parent: QWidget = None
|
||||||
) -> QComboBox:
|
) -> QComboBox:
|
||||||
|
|
||||||
cb = QComboBox(parent)
|
Cb = QComboBox(parent)
|
||||||
cb.setFixedHeight(25)
|
Cb.setFixedHeight(25)
|
||||||
cb.setMinimumWidth(120)
|
Cb.setMinimumWidth(120)
|
||||||
cb.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
Cb.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
||||||
return cb
|
return Cb
|
||||||
|
|
||||||
def makeComboWidget(
|
def makeComboWidget(
|
||||||
items,
|
items,
|
||||||
@@ -496,12 +376,12 @@ def makeComboWidget(
|
|||||||
parent: QWidget = None
|
parent: QWidget = None
|
||||||
) -> QComboBox:
|
) -> QComboBox:
|
||||||
|
|
||||||
cb = QComboBox(parent)
|
Cb = QComboBox(parent)
|
||||||
for display, data in items:
|
for display, data in items:
|
||||||
cb.addItem(display, data)
|
Cb.addItem(display, data)
|
||||||
cb.setFixedHeight(25)
|
Cb.setFixedHeight(25)
|
||||||
cb.setMinimumWidth(min_width)
|
Cb.setMinimumWidth(min_width)
|
||||||
return cb
|
return Cb
|
||||||
|
|
||||||
def makeLabel(
|
def makeLabel(
|
||||||
text: str,
|
text: str,
|
||||||
@@ -509,11 +389,11 @@ def makeLabel(
|
|||||||
width: int = None
|
width: int = None
|
||||||
) -> QLabel:
|
) -> QLabel:
|
||||||
|
|
||||||
lbl = QLabel(text, parent)
|
Lbl = QLabel(text, parent)
|
||||||
lbl.setFixedHeight(25)
|
Lbl.setFixedHeight(25)
|
||||||
if width:
|
if width:
|
||||||
lbl.setFixedWidth(width)
|
Lbl.setFixedWidth(width)
|
||||||
return lbl
|
return Lbl
|
||||||
|
|
||||||
def getValueFromWidget(
|
def getValueFromWidget(
|
||||||
w: QWidget
|
w: QWidget
|
||||||
@@ -535,68 +415,6 @@ def getValueFromWidget(
|
|||||||
return w.text()
|
return w.text()
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
def setValueToWidget(
|
|
||||||
w: QWidget,
|
|
||||||
var_type: str,
|
|
||||||
expr: str
|
|
||||||
):
|
|
||||||
"""
|
|
||||||
Set a widget's value from a Lua script expression.
|
|
||||||
"""
|
|
||||||
|
|
||||||
if hasattr(w, "setValue"):
|
|
||||||
w.setValue(expr)
|
|
||||||
return
|
|
||||||
s = expr.strip()
|
|
||||||
up = s.upper()
|
|
||||||
if isinstance(w, QTimeEdit):
|
|
||||||
m_time_ctor = re.match(r"^TIME\((\d+),\s*(\d+)\)$", up)
|
|
||||||
if m_time_ctor:
|
|
||||||
w.setTime(QTime(int(m_time_ctor.group(1)), int(m_time_ctor.group(2))))
|
|
||||||
else:
|
|
||||||
m = re.match(r'^"(\d{1,2}:\d{2})"$', s)
|
|
||||||
if m:
|
|
||||||
parts = m.group(1).split(":")
|
|
||||||
w.setTime(QTime(int(parts[0]), int(parts[1])))
|
|
||||||
elif isinstance(w, QDateEdit):
|
|
||||||
m_date_ctor = re.match(r"^DATE\((\d+),\s*(\d+),\s*(\d+)\)$", up)
|
|
||||||
if m_date_ctor:
|
|
||||||
w.setDate(QDate(
|
|
||||||
int(m_date_ctor.group(1)),
|
|
||||||
int(m_date_ctor.group(2)),
|
|
||||||
int(m_date_ctor.group(3)),
|
|
||||||
))
|
|
||||||
else:
|
|
||||||
m = re.match(r'^"(\d{4}-\d{2}-\d{2})"$', s)
|
|
||||||
if m:
|
|
||||||
parts = m.group(1).split("-")
|
|
||||||
w.setDate(QDate(int(parts[0]), int(parts[1]), int(parts[2])))
|
|
||||||
elif isinstance(w, QComboBox):
|
|
||||||
for i in range(w.count()):
|
|
||||||
d = w.itemData(i)
|
|
||||||
if d is not None:
|
|
||||||
if str(d).upper() == up:
|
|
||||||
w.setCurrentIndex(i)
|
|
||||||
return
|
|
||||||
if w.itemText(i).upper() == up:
|
|
||||||
w.setCurrentIndex(i)
|
|
||||||
return
|
|
||||||
elif isinstance(w, QSpinBox):
|
|
||||||
try:
|
|
||||||
w.setValue(int(expr))
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
elif isinstance(w, QDoubleSpinBox):
|
|
||||||
try:
|
|
||||||
w.setValue(float(expr))
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
elif isinstance(w, QLineEdit):
|
|
||||||
inner = expr.strip()
|
|
||||||
if inner.startswith('"') and inner.endswith('"'):
|
|
||||||
inner = inner[1:-1].replace('\\"', '"')
|
|
||||||
w.setText(inner)
|
|
||||||
|
|
||||||
def encodeValueStr(
|
def encodeValueStr(
|
||||||
raw_value: str,
|
raw_value: str,
|
||||||
var_type: str
|
var_type: str
|
||||||
@@ -605,7 +423,7 @@ def encodeValueStr(
|
|||||||
Encode a raw widget value as a Lua expression.
|
Encode a raw widget value as a Lua expression.
|
||||||
|
|
||||||
Arithmetic expressions (A + B) are passed through for numeric types;
|
Arithmetic expressions (A + B) are passed through for numeric types;
|
||||||
Date/Time arithmetic is translated to ``date_add()`` / ``time_add()`` calls.
|
Date/Time arithmetic is translated to ``dateadd()`` / ``timeadd()`` calls.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
if var_type in ("Date", "Time"):
|
if var_type in ("Date", "Time"):
|
||||||
@@ -646,24 +464,24 @@ def encodeDateOrTime(
|
|||||||
right = m_arith.group(3).strip()
|
right = m_arith.group(3).strip()
|
||||||
operand = right if sign == "+" else f"-{right}"
|
operand = right if sign == "+" else f"-{right}"
|
||||||
if left == "CURRENT_DATE":
|
if left == "CURRENT_DATE":
|
||||||
return f"date_add(CURRENT_DATE(), {operand})"
|
return f"dateadd(datenow(), {operand})"
|
||||||
if left == "CURRENT_TIME":
|
if left == "CURRENT_TIME":
|
||||||
return f"time_add(CURRENT_TIME(), {operand})"
|
return f"timeadd(timenow(), {operand})"
|
||||||
if var_type == "Date":
|
if var_type == "Date":
|
||||||
return f"date_add({left}, {operand})"
|
return f"dateadd({left}, {operand})"
|
||||||
if var_type == "Time":
|
if var_type == "Time":
|
||||||
return f"time_add({left}, {operand})"
|
return f"timeadd({left}, {operand})"
|
||||||
return f"{left} {sign} {right}"
|
return f"{left} {sign} {right}"
|
||||||
if up == "CURRENT_DATE":
|
if up == "CURRENT_DATE":
|
||||||
return "CURRENT_DATE()"
|
return "datenow()"
|
||||||
if up == "CURRENT_TIME":
|
if up == "CURRENT_TIME":
|
||||||
return "CURRENT_TIME()"
|
return "timenow()"
|
||||||
_REL_MAP = {
|
_REL_MAP = {
|
||||||
"前天": "date_add(CURRENT_DATE(), -2)",
|
"前天": "dateadd(datenow(), -2)",
|
||||||
"昨天": "date_add(CURRENT_DATE(), -1)",
|
"昨天": "dateadd(datenow(), -1)",
|
||||||
"今天": "CURRENT_DATE()",
|
"今天": "datenow()",
|
||||||
"明天": "date_add(CURRENT_DATE(), 1)",
|
"明天": "dateadd(datenow(), 1)",
|
||||||
"后天": "date_add(CURRENT_DATE(), 2)",
|
"后天": "dateadd(datenow(), 2)",
|
||||||
}
|
}
|
||||||
if s in _REL_MAP:
|
if s in _REL_MAP:
|
||||||
return _REL_MAP[s]
|
return _REL_MAP[s]
|
||||||
@@ -683,23 +501,6 @@ def encodeDateOrTime(
|
|||||||
return s
|
return s
|
||||||
return f'"{s}"'
|
return f'"{s}"'
|
||||||
|
|
||||||
def stripOuterParens(
|
|
||||||
s: str
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
s = s.strip()
|
|
||||||
if s.startswith("(") and s.endswith(")"):
|
|
||||||
depth = 0
|
|
||||||
for i, ch in enumerate(s):
|
|
||||||
if ch == "(":
|
|
||||||
depth += 1
|
|
||||||
elif ch == ")":
|
|
||||||
depth -= 1
|
|
||||||
if depth == 0 and i < len(s) - 1:
|
|
||||||
return s
|
|
||||||
return s[1:-1].strip()
|
|
||||||
return s
|
|
||||||
|
|
||||||
# Pre-compiled patterns for detecting arithmetic expressions (A + B / A - B)
|
# Pre-compiled patterns for detecting arithmetic expressions (A + B / A - B)
|
||||||
_RE_ARITH_SPACED = re.compile(r'^(.+?)\s+([+-])\s+(.+)$')
|
_RE_ARITH_SPACED = re.compile(r'^(.+?)\s+([+-])\s+(.+)$')
|
||||||
_RE_ARITH_NOSPACE = re.compile(r'^([A-Za-z_]\w*)([+-])(\d+|[A-Za-z_]\w*)$')
|
_RE_ARITH_NOSPACE = re.compile(r'^([A-Za-z_]\w*)([+-])(\d+|[A-Za-z_]\w*)$')
|
||||||
@@ -713,59 +514,3 @@ def isArithExpr(
|
|||||||
|
|
||||||
s = expr.strip()
|
s = expr.strip()
|
||||||
return bool(_RE_ARITH_SPACED.match(s) or _RE_ARITH_NOSPACE.match(s))
|
return bool(_RE_ARITH_SPACED.match(s) or _RE_ARITH_NOSPACE.match(s))
|
||||||
|
|
||||||
def isVarReference(
|
|
||||||
expr: str
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Return True if *expr* looks like a variable name reference
|
|
||||||
(as opposed to a literal value or function call).
|
|
||||||
"""
|
|
||||||
|
|
||||||
s = expr.strip()
|
|
||||||
up = s.upper()
|
|
||||||
if up in ("TRUE", "FALSE"):
|
|
||||||
return False
|
|
||||||
if re.match(r"^DATE\(|^TIME\(|^DATE_ADD\(|^TIME_ADD\(|^CURRENT_DATE\(|^CURRENT_TIME\(|^CURRENT_", up):
|
|
||||||
return False
|
|
||||||
if up.startswith('"') or up.startswith("'"):
|
|
||||||
return False
|
|
||||||
if re.match(r"^[+-]?\d", s):
|
|
||||||
return False
|
|
||||||
if isArithExpr(s):
|
|
||||||
return False
|
|
||||||
return bool(re.match(r"^[A-Z_][A-Z0-9_]*$", up))
|
|
||||||
|
|
||||||
def isInsideLiteral(
|
|
||||||
text: str,
|
|
||||||
pos: int
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
in_single = False
|
|
||||||
in_double = False
|
|
||||||
for i, ch in enumerate(text):
|
|
||||||
if i >= pos:
|
|
||||||
break
|
|
||||||
if ch == "'" and not in_double:
|
|
||||||
in_single = not in_single
|
|
||||||
elif ch == '"' and not in_single:
|
|
||||||
in_double = not in_double
|
|
||||||
return in_single or in_double
|
|
||||||
|
|
||||||
def findOperatorIn(
|
|
||||||
text: str,
|
|
||||||
operators: list
|
|
||||||
) -> tuple[int, str] | None:
|
|
||||||
|
|
||||||
for op in operators:
|
|
||||||
op_upper = op.upper()
|
|
||||||
start = 0
|
|
||||||
while True:
|
|
||||||
idx = text.upper().find(op_upper, start)
|
|
||||||
if idx < 0:
|
|
||||||
break
|
|
||||||
if isInsideLiteral(text, idx):
|
|
||||||
start = idx + 1
|
|
||||||
continue
|
|
||||||
return (idx, op)
|
|
||||||
return None
|
|
||||||
|
|||||||
@@ -22,14 +22,14 @@ from PySide6.QtWidgets import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
from gui.ALAutoScriptOrchDialog._helpers import (
|
from gui.ALAutoScriptOrchDialog._helpers import (
|
||||||
ACTION_TYPES,
|
ACTION_OPTIONS,
|
||||||
ARITH_TYPES,
|
COMPARE_OPTIONS,
|
||||||
COMPARE_OPERATORS,
|
LOGIC_OPTIONS,
|
||||||
LOGIC_OPERATORS,
|
|
||||||
PRESET_VARIABLES,
|
|
||||||
VAR_TYPE_ORDER,
|
|
||||||
encodeValueStr,
|
encodeValueStr,
|
||||||
|
getPresetVars,
|
||||||
|
getTypeOrder,
|
||||||
getValueFromWidget,
|
getValueFromWidget,
|
||||||
|
getArithType,
|
||||||
makeComboWidget,
|
makeComboWidget,
|
||||||
makeLabel,
|
makeLabel,
|
||||||
makeOffsetWidget,
|
makeOffsetWidget,
|
||||||
@@ -66,42 +66,42 @@ class ConditionRowFrame(QFrame):
|
|||||||
self.setFrameShape(QFrame.StyledPanel)
|
self.setFrameShape(QFrame.StyledPanel)
|
||||||
self.setFrameShadow(QFrame.Raised)
|
self.setFrameShadow(QFrame.Raised)
|
||||||
self.setFixedHeight(32)
|
self.setFixedHeight(32)
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(2, 2, 2, 2)
|
Layout.setContentsMargins(2, 2, 2, 2)
|
||||||
layout.setSpacing(4)
|
Layout.setSpacing(4)
|
||||||
if self._isFirst:
|
if self._isFirst:
|
||||||
self.logicCombo = None
|
self.LogicCombo = None
|
||||||
else:
|
else:
|
||||||
self.logicCombo = makeComboWidget(LOGIC_OPERATORS, min_width=110, parent=self)
|
self.LogicCombo = makeComboWidget(LOGIC_OPTIONS, min_width=110, parent=self)
|
||||||
layout.addWidget(self.logicCombo)
|
Layout.addWidget(self.LogicCombo)
|
||||||
self.leftVarCombo = QComboBox(self)
|
self.LeftVarCombo = QComboBox(self)
|
||||||
self.leftVarCombo.setFixedHeight(25)
|
self.LeftVarCombo.setFixedHeight(25)
|
||||||
self.leftVarCombo.setMinimumWidth(120)
|
self.LeftVarCombo.setMinimumWidth(120)
|
||||||
self.leftVarCombo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
self.LeftVarCombo.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
|
||||||
self.populateLeftVarCombo()
|
self.populateLeftVarCombo()
|
||||||
layout.addWidget(self.leftVarCombo)
|
Layout.addWidget(self.LeftVarCombo)
|
||||||
self.opCombo = makeComboWidget(COMPARE_OPERATORS, min_width=80, parent=self)
|
self.OpCombo = makeComboWidget(COMPARE_OPTIONS, min_width=80, parent=self)
|
||||||
layout.addWidget(self.opCombo)
|
Layout.addWidget(self.OpCombo)
|
||||||
self._compTypeCombo = makeComboWidget([
|
self._CompTypeCombo = makeComboWidget([
|
||||||
("特定值", "literal"),
|
("特定值", "literal"),
|
||||||
("变量", "variable"),
|
("变量", "variable"),
|
||||||
], min_width=70, parent=self)
|
], min_width=70, parent=self)
|
||||||
layout.addWidget(self._compTypeCombo)
|
Layout.addWidget(self._CompTypeCombo)
|
||||||
self.rhsStack = QStackedWidget(self)
|
self.RhsStack = QStackedWidget(self)
|
||||||
self.rhsStack.setFixedHeight(25)
|
self.RhsStack.setFixedHeight(25)
|
||||||
self.initLiteralStack()
|
self.initLiteralStack()
|
||||||
self.rhsVarCombo = makeVarRefCombo(self)
|
self.RhsVarCombo = makeVarRefCombo(self)
|
||||||
self.rhsStack.addWidget(self.rhsVarCombo)
|
self.RhsStack.addWidget(self.RhsVarCombo)
|
||||||
self.rhsStack.setCurrentIndex(0)
|
self.RhsStack.setCurrentIndex(0)
|
||||||
layout.addWidget(self.rhsStack)
|
Layout.addWidget(self.RhsStack)
|
||||||
if not self._isFirst:
|
if not self._isFirst:
|
||||||
self.deleteBtn = QPushButton("×", self)
|
self.DeleteBtn = QPushButton("×", self)
|
||||||
self.deleteBtn.setFixedSize(25, 25)
|
self.DeleteBtn.setFixedSize(25, 25)
|
||||||
self.deleteBtn.setStyleSheet("color: red; font-weight: bold;")
|
self.DeleteBtn.setStyleSheet("color: red; font-weight: bold;")
|
||||||
layout.addWidget(self.deleteBtn)
|
Layout.addWidget(self.DeleteBtn)
|
||||||
else:
|
else:
|
||||||
self.deleteBtn = None
|
self.DeleteBtn = None
|
||||||
layout.addStretch()
|
Layout.addStretch()
|
||||||
self.setUpdatesEnabled(True)
|
self.setUpdatesEnabled(True)
|
||||||
|
|
||||||
def populateLeftVarCombo(
|
def populateLeftVarCombo(
|
||||||
@@ -111,53 +111,53 @@ class ConditionRowFrame(QFrame):
|
|||||||
wasBool = self._isBoolMode
|
wasBool = self._isBoolMode
|
||||||
boolName = None
|
boolName = None
|
||||||
if wasBool:
|
if wasBool:
|
||||||
data = self.leftVarCombo.currentData()
|
data = self.LeftVarCombo.currentData()
|
||||||
if data:
|
if data:
|
||||||
boolName = data[0]
|
boolName = data[0]
|
||||||
self._varMgr.populateCombo(self.leftVarCombo)
|
self._varMgr.populateCombo(self.LeftVarCombo)
|
||||||
# Append boolean literal sentinels at the end
|
# Append boolean literal sentinels at the end
|
||||||
self.leftVarCombo.insertSeparator(self.leftVarCombo.count())
|
self.LeftVarCombo.insertSeparator(self.LeftVarCombo.count())
|
||||||
self.leftVarCombo.addItem("true", ("true", "Boolean"))
|
self.LeftVarCombo.addItem("true", ("true", "Boolean"))
|
||||||
self.leftVarCombo.addItem("false", ("false", "Boolean"))
|
self.LeftVarCombo.addItem("false", ("false", "Boolean"))
|
||||||
if wasBool and boolName:
|
if wasBool and boolName:
|
||||||
for ci in range(self.leftVarCombo.count()):
|
for ci in range(self.LeftVarCombo.count()):
|
||||||
d = self.leftVarCombo.itemData(ci)
|
d = self.LeftVarCombo.itemData(ci)
|
||||||
if d and d[0] == boolName:
|
if d and d[0] == boolName:
|
||||||
self.leftVarCombo.setCurrentIndex(ci)
|
self.LeftVarCombo.setCurrentIndex(ci)
|
||||||
break
|
break
|
||||||
|
|
||||||
def populateRHSVarCombo(
|
def populateRHSVarCombo(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self._varMgr.populateCombo(self.rhsVarCombo)
|
self._varMgr.populateCombo(self.RhsVarCombo)
|
||||||
|
|
||||||
def initLiteralStack(
|
def initLiteralStack(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.literalStack = QStackedWidget(self)
|
self.LiteralStack = QStackedWidget(self)
|
||||||
self.literalStack.setFixedHeight(25)
|
self.LiteralStack.setFixedHeight(25)
|
||||||
self._literalWidgets = {}
|
self._literalWidgets = {}
|
||||||
for vt in VAR_TYPE_ORDER:
|
for vt in getTypeOrder():
|
||||||
w = makeValueWidget(vt, self.literalStack)
|
W = makeValueWidget(vt, self.LiteralStack)
|
||||||
self._literalWidgets[vt] = w
|
self._literalWidgets[vt] = W
|
||||||
self.literalStack.addWidget(w)
|
self.LiteralStack.addWidget(W)
|
||||||
self.literalStack.setCurrentWidget(self._literalWidgets.get("String"))
|
self.LiteralStack.setCurrentWidget(self._literalWidgets.get("String"))
|
||||||
self.rhsStack.addWidget(self.literalStack)
|
self.RhsStack.addWidget(self.LiteralStack)
|
||||||
|
|
||||||
def connectSignals(
|
def connectSignals(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.leftVarCombo.currentIndexChanged.connect(self.onLeftVarChanged)
|
self.LeftVarCombo.currentIndexChanged.connect(self.onLeftVarChanged)
|
||||||
self._compTypeCombo.currentIndexChanged.connect(self.onCompTypeChanged)
|
self._CompTypeCombo.currentIndexChanged.connect(self.onCompTypeChanged)
|
||||||
|
|
||||||
def getLogic(
|
def getLogic(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
return self.logicCombo.currentData() if self.logicCombo else ""
|
return self.LogicCombo.currentData() if self.LogicCombo else ""
|
||||||
|
|
||||||
def updateRHSLiteralWidget(
|
def updateRHSLiteralWidget(
|
||||||
self,
|
self,
|
||||||
@@ -166,33 +166,37 @@ class ConditionRowFrame(QFrame):
|
|||||||
|
|
||||||
if vartype not in self._literalWidgets:
|
if vartype not in self._literalWidgets:
|
||||||
vartype = "String"
|
vartype = "String"
|
||||||
self.literalStack.setCurrentWidget(self._literalWidgets[vartype])
|
self.LiteralStack.setCurrentWidget(self._literalWidgets[vartype])
|
||||||
|
|
||||||
def toScript(
|
def toScript(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
data = self.leftVarCombo.currentData()
|
data = self.LeftVarCombo.currentData()
|
||||||
if self._isBoolMode and data:
|
if self._isBoolMode and data:
|
||||||
return data[0]
|
return data[0]
|
||||||
if not data:
|
if not data:
|
||||||
return ""
|
return ""
|
||||||
name, vartype = data
|
name, vartype = data
|
||||||
# CURRENT_DATE / CURRENT_TIME are Lua functions — call them, not reference them
|
# CURRENT_DATE / CURRENT_TIME map to datenow() / timenow()
|
||||||
if name in ("CURRENT_DATE", "CURRENT_TIME"):
|
if name == "CURRENT_DATE":
|
||||||
name = f"{name}()"
|
name = "datenow()"
|
||||||
opSym = self.opCombo.currentData()
|
elif name == "CURRENT_TIME":
|
||||||
|
name = "timenow()"
|
||||||
|
opSym = self.OpCombo.currentData()
|
||||||
if self._rawRhsExpr:
|
if self._rawRhsExpr:
|
||||||
return f"{name} {opSym} {self._rawRhsExpr}"
|
return f"{name} {opSym} {self._rawRhsExpr}"
|
||||||
isVarRef = (self._compTypeCombo.currentData() == "variable")
|
isVarRef = (self._CompTypeCombo.currentData() == "variable")
|
||||||
if isVarRef:
|
if isVarRef:
|
||||||
rd = self.rhsVarCombo.currentData()
|
rd = self.RhsVarCombo.currentData()
|
||||||
if rd:
|
if rd:
|
||||||
rhsName = rd[0]
|
rhsName = rd[0]
|
||||||
if rhsName in ("CURRENT_DATE", "CURRENT_TIME"):
|
if rhsName == "CURRENT_DATE":
|
||||||
rhsName = f"{rhsName}()"
|
rhsName = "datenow()"
|
||||||
|
elif rhsName == "CURRENT_TIME":
|
||||||
|
rhsName = "timenow()"
|
||||||
return f"{name} {opSym} {rhsName}"
|
return f"{name} {opSym} {rhsName}"
|
||||||
rhsText = self.rhsVarCombo.currentText().strip()
|
rhsText = self.RhsVarCombo.currentText().strip()
|
||||||
if rhsText:
|
if rhsText:
|
||||||
return f"{name} {opSym} {rhsText}"
|
return f"{name} {opSym} {rhsText}"
|
||||||
return ""
|
return ""
|
||||||
@@ -219,15 +223,15 @@ class ConditionRowFrame(QFrame):
|
|||||||
self._rawRhsExpr = ""
|
self._rawRhsExpr = ""
|
||||||
if idx < 0:
|
if idx < 0:
|
||||||
return
|
return
|
||||||
data = self.leftVarCombo.itemData(idx)
|
data = self.LeftVarCombo.itemData(idx)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
name, vartype = data
|
name, vartype = data
|
||||||
isBool = name in ("true", "false")
|
isBool = name in ("true", "false")
|
||||||
self._isBoolMode = isBool
|
self._isBoolMode = isBool
|
||||||
self.opCombo.setVisible(not isBool)
|
self.OpCombo.setVisible(not isBool)
|
||||||
self._compTypeCombo.setVisible(not isBool)
|
self._CompTypeCombo.setVisible(not isBool)
|
||||||
self.rhsStack.setVisible(not isBool)
|
self.RhsStack.setVisible(not isBool)
|
||||||
if not isBool:
|
if not isBool:
|
||||||
self.updateRHSLiteralWidget(vartype)
|
self.updateRHSLiteralWidget(vartype)
|
||||||
|
|
||||||
@@ -238,8 +242,8 @@ class ConditionRowFrame(QFrame):
|
|||||||
):
|
):
|
||||||
|
|
||||||
self._rawRhsExpr = ""
|
self._rawRhsExpr = ""
|
||||||
isVar = (self._compTypeCombo.currentData() == "variable")
|
isVar = (self._CompTypeCombo.currentData() == "variable")
|
||||||
self.rhsStack.setCurrentIndex(1 if isVar else 0)
|
self.RhsStack.setCurrentIndex(1 if isVar else 0)
|
||||||
if isVar:
|
if isVar:
|
||||||
self.populateRHSVarCombo()
|
self.populateRHSVarCombo()
|
||||||
|
|
||||||
@@ -269,52 +273,52 @@ class ActionStepFrame(QFrame):
|
|||||||
self.setFrameShape(QFrame.StyledPanel)
|
self.setFrameShape(QFrame.StyledPanel)
|
||||||
self.setFrameShadow(QFrame.Raised)
|
self.setFrameShadow(QFrame.Raised)
|
||||||
self.setFixedHeight(35)
|
self.setFixedHeight(35)
|
||||||
layout = QHBoxLayout(self)
|
Layout = QHBoxLayout(self)
|
||||||
layout.setContentsMargins(2, 2, 2, 2)
|
Layout.setContentsMargins(2, 2, 2, 2)
|
||||||
layout.setSpacing(4)
|
Layout.setSpacing(4)
|
||||||
self.opTypeCombo = makeComboWidget(ACTION_TYPES, min_width=70, parent=self)
|
self.OpTypeCombo = makeComboWidget(ACTION_OPTIONS, min_width=70, parent=self)
|
||||||
layout.addWidget(self.opTypeCombo)
|
Layout.addWidget(self.OpTypeCombo)
|
||||||
layout.addWidget(makeLabel("设置", self))
|
Layout.addWidget(makeLabel("设置", self))
|
||||||
self.targetCombo = QComboBox(self)
|
self.TargetCombo = QComboBox(self)
|
||||||
self.targetCombo.setFixedHeight(25)
|
self.TargetCombo.setFixedHeight(25)
|
||||||
self.targetCombo.setMinimumWidth(120)
|
self.TargetCombo.setMinimumWidth(120)
|
||||||
self.populateTargetCombo()
|
self.populateTargetCombo()
|
||||||
layout.addWidget(self.targetCombo)
|
Layout.addWidget(self.TargetCombo)
|
||||||
layout.addWidget(makeLabel("为", self))
|
Layout.addWidget(makeLabel("为", self))
|
||||||
self.valueSrcCombo = makeComboWidget([
|
self.ValueSrcCombo = makeComboWidget([
|
||||||
("特定值", "literal"),
|
("特定值", "literal"),
|
||||||
("变量", "variable"),
|
("变量", "variable"),
|
||||||
], min_width=70, parent=self)
|
], min_width=70, parent=self)
|
||||||
layout.addWidget(self.valueSrcCombo)
|
Layout.addWidget(self.ValueSrcCombo)
|
||||||
self.valueStack = QStackedWidget(self)
|
self.ValueStack = QStackedWidget(self)
|
||||||
self.valueStack.setFixedHeight(25)
|
self.ValueStack.setFixedHeight(25)
|
||||||
self.initValueStacks()
|
self.initValueStacks()
|
||||||
layout.addWidget(self.valueStack)
|
Layout.addWidget(self.ValueStack)
|
||||||
self.existingVarCombo = makeVarRefCombo(self)
|
self.ExistingVarCombo = makeVarRefCombo(self)
|
||||||
self.existingVarCombo.setVisible(False)
|
self.ExistingVarCombo.setVisible(False)
|
||||||
layout.addWidget(self.existingVarCombo)
|
Layout.addWidget(self.ExistingVarCombo)
|
||||||
self.deleteBtn = QPushButton("×", self)
|
self.DeleteBtn = QPushButton("×", self)
|
||||||
self.deleteBtn.setFixedSize(25, 25)
|
self.DeleteBtn.setFixedSize(25, 25)
|
||||||
self.deleteBtn.setStyleSheet("color: red; font-weight: bold;")
|
self.DeleteBtn.setStyleSheet("color: red; font-weight: bold;")
|
||||||
layout.addWidget(self.deleteBtn)
|
Layout.addWidget(self.DeleteBtn)
|
||||||
self.setUpdatesEnabled(True)
|
self.setUpdatesEnabled(True)
|
||||||
|
|
||||||
def populateTargetCombo(
|
def populateTargetCombo(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.targetCombo.blockSignals(True)
|
self.TargetCombo.blockSignals(True)
|
||||||
self.targetCombo.clear()
|
self.TargetCombo.clear()
|
||||||
for p in PRESET_VARIABLES:
|
for p in getPresetVars():
|
||||||
if p["name"] in ("CURRENT_TIME", "CURRENT_DATE"):
|
if p["name"] in ("CURRENT_TIME", "CURRENT_DATE"):
|
||||||
continue
|
continue
|
||||||
info = self._varMgr.getInfoByName(p["name"])
|
info = self._varMgr.getInfoByName(p["name"])
|
||||||
if info:
|
if info:
|
||||||
self.targetCombo.addItem(
|
self.TargetCombo.addItem(
|
||||||
info["display"],
|
info["display"],
|
||||||
(info["name"], info["type"])
|
(info["name"], info["type"])
|
||||||
)
|
)
|
||||||
self.targetCombo.blockSignals(False)
|
self.TargetCombo.blockSignals(False)
|
||||||
|
|
||||||
def initValueStacks(
|
def initValueStacks(
|
||||||
self
|
self
|
||||||
@@ -322,46 +326,46 @@ class ActionStepFrame(QFrame):
|
|||||||
|
|
||||||
self._literalWidgets = {}
|
self._literalWidgets = {}
|
||||||
self._offsetWidgets = {}
|
self._offsetWidgets = {}
|
||||||
for vt in VAR_TYPE_ORDER:
|
for vt in getTypeOrder():
|
||||||
self._literalWidgets[vt] = makeValueWidget(vt, self.valueStack)
|
self._literalWidgets[vt] = makeValueWidget(vt, self.ValueStack)
|
||||||
self.valueStack.addWidget(self._literalWidgets[vt])
|
self.ValueStack.addWidget(self._literalWidgets[vt])
|
||||||
if vt in ARITH_TYPES:
|
if getArithType(vt):
|
||||||
self._offsetWidgets[vt] = makeOffsetWidget(vt, self.valueStack)
|
self._offsetWidgets[vt] = makeOffsetWidget(vt, self.ValueStack)
|
||||||
self.valueStack.addWidget(self._offsetWidgets[vt])
|
self.ValueStack.addWidget(self._offsetWidgets[vt])
|
||||||
else:
|
else:
|
||||||
lbl = QLabel("(不支持该操作)", self.valueStack)
|
Lbl = QLabel("(不支持该操作)", self.ValueStack)
|
||||||
lbl.setFixedHeight(25)
|
Lbl.setFixedHeight(25)
|
||||||
self._offsetWidgets[vt] = lbl
|
self._offsetWidgets[vt] = Lbl
|
||||||
self.valueStack.addWidget(lbl)
|
self.ValueStack.addWidget(Lbl)
|
||||||
|
|
||||||
def connectSignals(
|
def connectSignals(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.opTypeCombo.currentIndexChanged.connect(self.onOpTypeChanged)
|
self.OpTypeCombo.currentIndexChanged.connect(self.onOpTypeChanged)
|
||||||
self.targetCombo.currentIndexChanged.connect(self.onTargetChanged)
|
self.TargetCombo.currentIndexChanged.connect(self.onTargetChanged)
|
||||||
self.valueSrcCombo.currentIndexChanged.connect(self.onValueSrcChanged)
|
self.ValueSrcCombo.currentIndexChanged.connect(self.onValueSrcChanged)
|
||||||
|
|
||||||
def getTargetName(
|
def getTargetName(
|
||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
data = self.targetCombo.currentData()
|
data = self.TargetCombo.currentData()
|
||||||
return data[0] if data else ""
|
return data[0] if data else ""
|
||||||
|
|
||||||
def updateValueWidget(
|
def updateValueWidget(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
op = self.opTypeCombo.currentData()
|
op = self.OpTypeCombo.currentData()
|
||||||
isArith = (op in ("add", "sub"))
|
isArith = (op in ("add", "sub"))
|
||||||
actualType = self._currentTargetType
|
actualType = self._currentTargetType
|
||||||
if isArith and actualType in self._offsetWidgets:
|
if isArith and actualType in self._offsetWidgets:
|
||||||
self.valueStack.setCurrentWidget(self._offsetWidgets[actualType])
|
self.ValueStack.setCurrentWidget(self._offsetWidgets[actualType])
|
||||||
elif actualType in self._literalWidgets:
|
elif actualType in self._literalWidgets:
|
||||||
self.valueStack.setCurrentWidget(self._literalWidgets[actualType])
|
self.ValueStack.setCurrentWidget(self._literalWidgets[actualType])
|
||||||
else:
|
else:
|
||||||
self.valueStack.setCurrentWidget(self._literalWidgets.get("String"))
|
self.ValueStack.setCurrentWidget(self._literalWidgets.get("String"))
|
||||||
|
|
||||||
def toScript(
|
def toScript(
|
||||||
self
|
self
|
||||||
@@ -371,7 +375,7 @@ class ActionStepFrame(QFrame):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
target = self.getTargetName()
|
target = self.getTargetName()
|
||||||
op = self.opTypeCombo.currentData()
|
op = self.OpTypeCombo.currentData()
|
||||||
if op == "pass":
|
if op == "pass":
|
||||||
return " -- pass"
|
return " -- pass"
|
||||||
if not target:
|
if not target:
|
||||||
@@ -382,20 +386,20 @@ class ActionStepFrame(QFrame):
|
|||||||
encoded = encodeValueStr(rawVal, vartype)
|
encoded = encodeValueStr(rawVal, vartype)
|
||||||
return f" {target} = {encoded}"
|
return f" {target} = {encoded}"
|
||||||
elif op == "add":
|
elif op == "add":
|
||||||
if vartype == "Date" and hasattr(self.valueStack.currentWidget(), "getOffsetDays"):
|
if vartype == "Date" and hasattr(self.ValueStack.currentWidget(), "getOffsetDays"):
|
||||||
days = self.valueStack.currentWidget().getOffsetDays()
|
days = self.ValueStack.currentWidget().getOffsetDays()
|
||||||
return f" {target} = date_add({target}, {days})"
|
return f" {target} = dateadd({target}, {days})"
|
||||||
if vartype == "Time" and hasattr(self.valueStack.currentWidget(), "getOffsetHours"):
|
if vartype == "Time" and hasattr(self.ValueStack.currentWidget(), "getOffsetHours"):
|
||||||
hours = self.valueStack.currentWidget().getOffsetHours()
|
hours = self.ValueStack.currentWidget().getOffsetHours()
|
||||||
return f" {target} = time_add({target}, {hours})"
|
return f" {target} = timeadd({target}, {hours})"
|
||||||
return f" {target} = {target} + {rawVal}"
|
return f" {target} = {target} + {rawVal}"
|
||||||
elif op == "sub":
|
elif op == "sub":
|
||||||
if vartype == "Date" and hasattr(self.valueStack.currentWidget(), "getOffsetDays"):
|
if vartype == "Date" and hasattr(self.ValueStack.currentWidget(), "getOffsetDays"):
|
||||||
days = self.valueStack.currentWidget().getOffsetDays()
|
days = self.ValueStack.currentWidget().getOffsetDays()
|
||||||
return f" {target} = date_add({target}, -{days})"
|
return f" {target} = dateadd({target}, -{days})"
|
||||||
if vartype == "Time" and hasattr(self.valueStack.currentWidget(), "getOffsetHours"):
|
if vartype == "Time" and hasattr(self.ValueStack.currentWidget(), "getOffsetHours"):
|
||||||
hours = self.valueStack.currentWidget().getOffsetHours()
|
hours = self.ValueStack.currentWidget().getOffsetHours()
|
||||||
return f" {target} = time_add({target}, -{hours})"
|
return f" {target} = timeadd({target}, -{hours})"
|
||||||
return f" {target} = {target} - {rawVal}"
|
return f" {target} = {target} - {rawVal}"
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
@@ -403,10 +407,10 @@ class ActionStepFrame(QFrame):
|
|||||||
self
|
self
|
||||||
) -> str:
|
) -> str:
|
||||||
|
|
||||||
if self.valueSrcCombo.currentData() == "variable":
|
if self.ValueSrcCombo.currentData() == "variable":
|
||||||
data = self.existingVarCombo.currentData()
|
data = self.ExistingVarCombo.currentData()
|
||||||
return data[0] if data else ""
|
return data[0] if data else ""
|
||||||
w = self.valueStack.currentWidget()
|
w = self.ValueStack.currentWidget()
|
||||||
if w:
|
if w:
|
||||||
return getValueFromWidget(w)
|
return getValueFromWidget(w)
|
||||||
return ""
|
return ""
|
||||||
@@ -415,15 +419,15 @@ class ActionStepFrame(QFrame):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
currentData = self.targetCombo.currentData()
|
currentData = self.TargetCombo.currentData()
|
||||||
self.populateTargetCombo()
|
self.populateTargetCombo()
|
||||||
if currentData:
|
if currentData:
|
||||||
for i in range(self.targetCombo.count()):
|
for i in range(self.TargetCombo.count()):
|
||||||
d = self.targetCombo.itemData(i)
|
d = self.TargetCombo.itemData(i)
|
||||||
if d and d[0] == currentData[0]:
|
if d and d[0] == currentData[0]:
|
||||||
self.targetCombo.setCurrentIndex(i)
|
self.TargetCombo.setCurrentIndex(i)
|
||||||
break
|
break
|
||||||
self._varMgr.populateCombo(self.existingVarCombo)
|
self._varMgr.populateCombo(self.ExistingVarCombo)
|
||||||
|
|
||||||
@Slot(int)
|
@Slot(int)
|
||||||
def onTargetChanged(
|
def onTargetChanged(
|
||||||
@@ -433,13 +437,13 @@ class ActionStepFrame(QFrame):
|
|||||||
|
|
||||||
if idx < 0:
|
if idx < 0:
|
||||||
return
|
return
|
||||||
data = self.targetCombo.itemData(idx)
|
data = self.TargetCombo.itemData(idx)
|
||||||
if not data:
|
if not data:
|
||||||
return
|
return
|
||||||
_, vartype = data
|
_, vartype = data
|
||||||
self._currentTargetType = vartype
|
self._currentTargetType = vartype
|
||||||
self.updateValueWidget()
|
self.updateValueWidget()
|
||||||
self.onValueSrcChanged(self.valueSrcCombo.currentIndex())
|
self.onValueSrcChanged(self.ValueSrcCombo.currentIndex())
|
||||||
|
|
||||||
@Slot(int)
|
@Slot(int)
|
||||||
def onOpTypeChanged(
|
def onOpTypeChanged(
|
||||||
@@ -455,10 +459,10 @@ class ActionStepFrame(QFrame):
|
|||||||
idx
|
idx
|
||||||
):
|
):
|
||||||
|
|
||||||
isVar = (self.valueSrcCombo.currentData() == "variable")
|
isVar = (self.ValueSrcCombo.currentData() == "variable")
|
||||||
self.valueStack.setVisible(not isVar)
|
self.ValueStack.setVisible(not isVar)
|
||||||
self.existingVarCombo.setVisible(isVar)
|
self.ExistingVarCombo.setVisible(isVar)
|
||||||
if isVar:
|
if isVar:
|
||||||
self._varMgr.populateCombo(self.existingVarCombo)
|
self._varMgr.populateCombo(self.ExistingVarCombo)
|
||||||
else:
|
else:
|
||||||
self.updateValueWidget()
|
self.updateValueWidget()
|
||||||
|
|||||||
+100
-100
@@ -386,18 +386,18 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
|
|
||||||
user_config = self.defaultUserConfig()
|
user_config = self.defaultUserConfig()
|
||||||
for i in range(self.UserTreeWidget.topLevelItemCount()):
|
for i in range(self.UserTreeWidget.topLevelItemCount()):
|
||||||
group_item = self.UserTreeWidget.topLevelItem(i)
|
GroupItem = self.UserTreeWidget.topLevelItem(i)
|
||||||
group_config = {
|
group_config = {
|
||||||
"name": group_item.text(0),
|
"name": GroupItem.text(0),
|
||||||
"enabled": group_item.checkState(1) == Qt.CheckState.Checked,
|
"enabled": GroupItem.checkState(1) == Qt.CheckState.Checked,
|
||||||
"users": []
|
"users": []
|
||||||
}
|
}
|
||||||
for j in range(group_item.childCount()):
|
for j in range(GroupItem.childCount()):
|
||||||
user_item = group_item.child(j)
|
UserItem = GroupItem.child(j)
|
||||||
user = user_item.data(0, Qt.UserRole)
|
user = UserItem.data(0, Qt.UserRole)
|
||||||
if not user:
|
if not user:
|
||||||
continue
|
continue
|
||||||
user["enabled"] = user_item.checkState(1) == Qt.CheckState.Checked
|
user["enabled"] = UserItem.checkState(1) == Qt.CheckState.Checked
|
||||||
group_config["users"].append(user)
|
group_config["users"].append(user)
|
||||||
user_config["groups"].append(group_config)
|
user_config["groups"].append(group_config)
|
||||||
return user_config
|
return user_config
|
||||||
@@ -453,18 +453,18 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
try:
|
try:
|
||||||
if "groups" in users:
|
if "groups" in users:
|
||||||
for group_config in users["groups"]:
|
for group_config in users["groups"]:
|
||||||
group_item = QTreeWidgetItem(self.UserTreeWidget, ALUserTreeItemType.GROUP.value)
|
GroupItem = QTreeWidgetItem(self.UserTreeWidget, ALUserTreeItemType.GROUP.value)
|
||||||
group_item.setText(0, group_config["name"])
|
GroupItem.setText(0, group_config["name"])
|
||||||
group_item.setFlags(group_item.flags() | Qt.ItemIsEditable)
|
GroupItem.setFlags(GroupItem.flags() | Qt.ItemIsEditable)
|
||||||
group_item.setCheckState(1, Qt.Checked if group_config.get("enabled", True) else Qt.Unchecked)
|
GroupItem.setCheckState(1, Qt.Checked if group_config.get("enabled", True) else Qt.Unchecked)
|
||||||
for user_config in group_config["users"]:
|
for user_config in group_config["users"]:
|
||||||
user_item = QTreeWidgetItem(group_item, ALUserTreeItemType.USER.value)
|
UserItem = QTreeWidgetItem(GroupItem, ALUserTreeItemType.USER.value)
|
||||||
user_item.setText(0, user_config["username"])
|
UserItem.setText(0, user_config["username"])
|
||||||
user_item.setText(1, "" if user_config.get("enabled", True) else "跳过")
|
UserItem.setText(1, "" if user_config.get("enabled", True) else "跳过")
|
||||||
user_item.setData(0, Qt.UserRole, user_config)
|
UserItem.setData(0, Qt.UserRole, user_config)
|
||||||
user_item.setCheckState(1, Qt.Checked if user_config.get("enabled", True) else Qt.Unchecked)
|
UserItem.setCheckState(1, Qt.Checked if user_config.get("enabled", True) else Qt.Unchecked)
|
||||||
user_item.setDisabled(not group_config.get("enabled", True))
|
UserItem.setDisabled(not group_config.get("enabled", True))
|
||||||
group_item.setExpanded(True)
|
GroupItem.setExpanded(True)
|
||||||
except KeyError as e:
|
except KeyError as e:
|
||||||
QMessageBox.warning(
|
QMessageBox.warning(
|
||||||
self,
|
self,
|
||||||
@@ -638,43 +638,43 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
) -> QTreeWidgetItem:
|
) -> QTreeWidgetItem:
|
||||||
|
|
||||||
self.UserTreeWidget.itemChanged.disconnect(self.onUserTreeWidgetItemChanged)
|
self.UserTreeWidget.itemChanged.disconnect(self.onUserTreeWidgetItemChanged)
|
||||||
group_item = QTreeWidgetItem(self.UserTreeWidget, ALUserTreeItemType.GROUP.value)
|
GroupItem = QTreeWidgetItem(self.UserTreeWidget, ALUserTreeItemType.GROUP.value)
|
||||||
if not group_name:
|
if not group_name:
|
||||||
group_name = f"新分组-{self.UserTreeWidget.topLevelItemCount()}"
|
group_name = f"新分组-{self.UserTreeWidget.topLevelItemCount()}"
|
||||||
group_item.setText(0, group_name)
|
GroupItem.setText(0, group_name)
|
||||||
group_item.setFlags(group_item.flags() | Qt.ItemIsEditable)
|
GroupItem.setFlags(GroupItem.flags() | Qt.ItemIsEditable)
|
||||||
group_item.setCheckState(1, Qt.Checked)
|
GroupItem.setCheckState(1, Qt.Checked)
|
||||||
self.UserTreeWidget.setCurrentItem(group_item)
|
self.UserTreeWidget.setCurrentItem(GroupItem)
|
||||||
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
|
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
|
||||||
return group_item
|
return GroupItem
|
||||||
|
|
||||||
def delGroup(
|
def delGroup(
|
||||||
self,
|
self,
|
||||||
group_item: QTreeWidgetItem = None
|
GroupItem: QTreeWidgetItem = None
|
||||||
):
|
):
|
||||||
|
|
||||||
if group_item is None:
|
if GroupItem is None:
|
||||||
return
|
return
|
||||||
if group_item.type() != ALUserTreeItemType.GROUP.value:
|
if GroupItem.type() != ALUserTreeItemType.GROUP.value:
|
||||||
return
|
return
|
||||||
index = self.UserTreeWidget.indexOfTopLevelItem(group_item)
|
index = self.UserTreeWidget.indexOfTopLevelItem(GroupItem)
|
||||||
self.UserTreeWidget.takeTopLevelItem(index)
|
self.UserTreeWidget.takeTopLevelItem(index)
|
||||||
|
|
||||||
def addUser(
|
def addUser(
|
||||||
self,
|
self,
|
||||||
group_item: QTreeWidgetItem = None
|
GroupItem: QTreeWidgetItem = None
|
||||||
) -> QTreeWidgetItem:
|
) -> QTreeWidgetItem:
|
||||||
|
|
||||||
if group_item is None:
|
if GroupItem is None:
|
||||||
current_item = self.UserTreeWidget.currentItem()
|
CurrentItem = self.UserTreeWidget.currentItem()
|
||||||
if current_item is None:
|
if CurrentItem is None:
|
||||||
group_item = self.addGroup()
|
GroupItem = self.addGroup()
|
||||||
if group_item.type() == ALUserTreeItemType.USER.value:
|
if GroupItem.type() == ALUserTreeItemType.USER.value:
|
||||||
group_item = group_item.parent()
|
GroupItem = GroupItem.parent()
|
||||||
if group_item.checkState(1) == Qt.CheckState.Unchecked:
|
if GroupItem.checkState(1) == Qt.CheckState.Unchecked:
|
||||||
return None
|
return None
|
||||||
new_user = {
|
new_user = {
|
||||||
"username": f"新用户-{group_item.childCount()}",
|
"username": f"新用户-{GroupItem.childCount()}",
|
||||||
"password": "000000",
|
"password": "000000",
|
||||||
"enabled": True,
|
"enabled": True,
|
||||||
"reserve_info": {
|
"reserve_info": {
|
||||||
@@ -703,30 +703,30 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.UserTreeWidget.itemChanged.disconnect(self.onUserTreeWidgetItemChanged)
|
self.UserTreeWidget.itemChanged.disconnect(self.onUserTreeWidgetItemChanged)
|
||||||
user_item = QTreeWidgetItem(group_item, ALUserTreeItemType.USER.value)
|
UserItem = QTreeWidgetItem(GroupItem, ALUserTreeItemType.USER.value)
|
||||||
user_item.setText(0, new_user["username"])
|
UserItem.setText(0, new_user["username"])
|
||||||
user_item.setText(1, "")
|
UserItem.setText(1, "")
|
||||||
user_item.setData(0, Qt.UserRole, new_user)
|
UserItem.setData(0, Qt.UserRole, new_user)
|
||||||
user_item.setCheckState(1, Qt.CheckState.Checked)
|
UserItem.setCheckState(1, Qt.CheckState.Checked)
|
||||||
group_item.setExpanded(True)
|
GroupItem.setExpanded(True)
|
||||||
self.UserTreeWidget.setCurrentItem(user_item)
|
self.UserTreeWidget.setCurrentItem(UserItem)
|
||||||
self.setUserToWidget(new_user)
|
self.setUserToWidget(new_user)
|
||||||
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
|
self.UserTreeWidget.itemChanged.connect(self.onUserTreeWidgetItemChanged)
|
||||||
return user_item
|
return UserItem
|
||||||
|
|
||||||
def delUser(
|
def delUser(
|
||||||
self,
|
self,
|
||||||
user_item: QTreeWidgetItem = None
|
UserItem: QTreeWidgetItem = None
|
||||||
):
|
):
|
||||||
|
|
||||||
if user_item is None:
|
if UserItem is None:
|
||||||
return
|
return
|
||||||
if user_item.type() != ALUserTreeItemType.USER.value:
|
if UserItem.type() != ALUserTreeItemType.USER.value:
|
||||||
return
|
return
|
||||||
parent_item = user_item.parent()
|
ParentItem = UserItem.parent()
|
||||||
index = parent_item.indexOfChild(user_item)
|
index = ParentItem.indexOfChild(UserItem)
|
||||||
parent_item.takeChild(index)
|
ParentItem.takeChild(index)
|
||||||
if parent_item.childCount() == 0:
|
if ParentItem.childCount() == 0:
|
||||||
self.UserTreeWidget.setCurrentItem(None)
|
self.UserTreeWidget.setCurrentItem(None)
|
||||||
|
|
||||||
def renameItem(
|
def renameItem(
|
||||||
@@ -787,19 +787,19 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
room = self.RoomComboBox.currentText()
|
room = self.RoomComboBox.currentText()
|
||||||
floor_idx = self.__floor_rmap[floor]
|
floor_idx = self.__floor_rmap[floor]
|
||||||
room_idx = self.__room_rmap[room]
|
room_idx = self.__room_rmap[room]
|
||||||
dialog = ALSeatMapSelectDialog(
|
Dialog = ALSeatMapSelectDialog(
|
||||||
self,
|
self,
|
||||||
floor,
|
floor,
|
||||||
room,
|
room,
|
||||||
ALSeatMapTable[floor_idx][room_idx]
|
ALSeatMapTable[floor_idx][room_idx]
|
||||||
)
|
)
|
||||||
dialog.selectSeats(self.SeatIDEdit.text().split(","))
|
Dialog.selectSeats(self.SeatIDEdit.text().split(","))
|
||||||
if dialog.exec() == QDialog.DialogCode.Accepted:
|
if Dialog.exec() == QDialog.DialogCode.Accepted:
|
||||||
selected_seats = dialog.getSelectedSeats()
|
selected_seats = Dialog.getSelectedSeats()
|
||||||
if len(selected_seats) == 0:
|
if len(selected_seats) == 0:
|
||||||
self.SeatIDEdit.clear()
|
self.SeatIDEdit.clear()
|
||||||
return
|
return
|
||||||
self.SeatIDEdit.setText(",".join(dialog.getSelectedSeats()))
|
self.SeatIDEdit.setText(",".join(Dialog.getSelectedSeats()))
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onUserTreeWidgetCurrentItemChanged(
|
def onUserTreeWidgetCurrentItemChanged(
|
||||||
@@ -844,10 +844,10 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
if item.type() == ALUserTreeItemType.GROUP.value:
|
if item.type() == ALUserTreeItemType.GROUP.value:
|
||||||
is_checked = item.checkState(1) == Qt.CheckState.Checked
|
is_checked = item.checkState(1) == Qt.CheckState.Checked
|
||||||
for i in range(item.childCount()):
|
for i in range(item.childCount()):
|
||||||
child = item.child(i)
|
Child = item.child(i)
|
||||||
if self.UserTreeWidget.currentItem() == child:
|
if self.UserTreeWidget.currentItem() == Child:
|
||||||
self.UserTreeWidget.setCurrentItem(item)
|
self.UserTreeWidget.setCurrentItem(item)
|
||||||
child.setDisabled(not is_checked)
|
Child.setDisabled(not is_checked)
|
||||||
else:
|
else:
|
||||||
is_checked = item.checkState(1) == Qt.CheckState.Checked
|
is_checked = item.checkState(1) == Qt.CheckState.Checked
|
||||||
item.setText(1, "" if is_checked else "跳过")
|
item.setText(1, "" if is_checked else "跳过")
|
||||||
@@ -857,41 +857,41 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
menu: QMenu
|
menu: QMenu
|
||||||
):
|
):
|
||||||
|
|
||||||
add_group_action = QAction("添加分组", menu)
|
AddGroupAction = QAction("添加分组", menu)
|
||||||
add_group_action.triggered.connect(self.addGroup)
|
AddGroupAction.triggered.connect(self.addGroup)
|
||||||
menu.addAction(add_group_action)
|
menu.addAction(AddGroupAction)
|
||||||
|
|
||||||
def showGroupMenu(
|
def showGroupMenu(
|
||||||
self,
|
self,
|
||||||
menu: QMenu,
|
menu: QMenu,
|
||||||
group_item: QTreeWidgetItem = None
|
GroupItem: QTreeWidgetItem = None
|
||||||
):
|
):
|
||||||
|
|
||||||
add_user_action = QAction("添加用户", menu)
|
AddUserAction = QAction("添加用户", menu)
|
||||||
rename_group_action = QAction("重命名分组", menu)
|
RenameGroupAction = QAction("重命名分组", menu)
|
||||||
del_group_action = QAction("删除分组", menu)
|
DelGroupAction = QAction("删除分组", menu)
|
||||||
add_user_action.triggered.connect(lambda: self.addUser(group_item))
|
AddUserAction.triggered.connect(lambda: self.addUser(GroupItem))
|
||||||
rename_group_action.triggered.connect(lambda: self.renameItem(group_item))
|
RenameGroupAction.triggered.connect(lambda: self.renameItem(GroupItem))
|
||||||
del_group_action.triggered.connect(lambda: self.delGroup(group_item))
|
DelGroupAction.triggered.connect(lambda: self.delGroup(GroupItem))
|
||||||
menu.addAction(add_user_action)
|
menu.addAction(AddUserAction)
|
||||||
menu.addSeparator()
|
menu.addSeparator()
|
||||||
menu.addAction(rename_group_action)
|
menu.addAction(RenameGroupAction)
|
||||||
menu.addAction(del_group_action)
|
menu.addAction(DelGroupAction)
|
||||||
if group_item.checkState(1) == Qt.CheckState.Unchecked:
|
if GroupItem.checkState(1) == Qt.CheckState.Unchecked:
|
||||||
add_user_action.setEnabled(False)
|
AddUserAction.setEnabled(False)
|
||||||
|
|
||||||
def showUserMenu(
|
def showUserMenu(
|
||||||
self,
|
self,
|
||||||
menu: QMenu,
|
menu: QMenu,
|
||||||
user_item: QTreeWidgetItem = None
|
UserItem: QTreeWidgetItem = None
|
||||||
):
|
):
|
||||||
|
|
||||||
rename_user_action = QAction("重命名用户", menu)
|
RenameUserAction = QAction("重命名用户", menu)
|
||||||
del_user_action = QAction("删除用户", menu)
|
DelUserAction = QAction("删除用户", menu)
|
||||||
rename_user_action.triggered.connect(lambda: self.renameItem(user_item))
|
RenameUserAction.triggered.connect(lambda: self.renameItem(UserItem))
|
||||||
del_user_action.triggered.connect(lambda: self.delUser(user_item))
|
DelUserAction.triggered.connect(lambda: self.delUser(UserItem))
|
||||||
menu.addAction(rename_user_action)
|
menu.addAction(RenameUserAction)
|
||||||
menu.addAction(del_user_action)
|
menu.addAction(DelUserAction)
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onUserTreeWidgetContextMenu(
|
def onUserTreeWidgetContextMenu(
|
||||||
@@ -899,31 +899,31 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
pos
|
pos
|
||||||
):
|
):
|
||||||
|
|
||||||
current_item = self.UserTreeWidget.itemAt(pos)
|
CurrentItem = self.UserTreeWidget.itemAt(pos)
|
||||||
menu = QMenu(self.UserTreeWidget)
|
Menu = QMenu(self.UserTreeWidget)
|
||||||
if current_item is None:
|
if CurrentItem is None:
|
||||||
self.showTreeMenu(menu)
|
self.showTreeMenu(Menu)
|
||||||
elif current_item.type() == ALUserTreeItemType.GROUP.value:
|
elif CurrentItem.type() == ALUserTreeItemType.GROUP.value:
|
||||||
self.showGroupMenu(menu, current_item)
|
self.showGroupMenu(Menu, CurrentItem)
|
||||||
else:
|
else:
|
||||||
self.showUserMenu(menu, current_item)
|
self.showUserMenu(Menu, CurrentItem)
|
||||||
menu.exec_(self.UserTreeWidget.mapToGlobal(pos))
|
Menu.exec_(self.UserTreeWidget.mapToGlobal(pos))
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onAddUserButtonClicked(
|
def onAddUserButtonClicked(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
current_item = self.UserTreeWidget.currentItem()
|
CurrentItem = self.UserTreeWidget.currentItem()
|
||||||
self.addUser(current_item)
|
self.addUser(CurrentItem)
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onDelUserButtonClicked(
|
def onDelUserButtonClicked(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
current_item = self.UserTreeWidget.currentItem()
|
CurrentItem = self.UserTreeWidget.currentItem()
|
||||||
self.delUser(current_item)
|
self.delUser(CurrentItem)
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onBrowseBrowserDriverButtonClicked(
|
def onBrowseBrowserDriverButtonClicked(
|
||||||
@@ -944,10 +944,10 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
dialog = ALWebDriverDownloadDialog(self)
|
Dialog = ALWebDriverDownloadDialog(self)
|
||||||
dialog.show()
|
Dialog.show()
|
||||||
dialog.exec_()
|
Dialog.exec_()
|
||||||
selected_driver_info = dialog.getSelectedDriverInfo()
|
selected_driver_info = Dialog.getSelectedDriverInfo()
|
||||||
if selected_driver_info and selected_driver_info.driver_path:
|
if selected_driver_info and selected_driver_info.driver_path:
|
||||||
self.BrowserTypeComboBox.setCurrentText(selected_driver_info.driver_type.value)
|
self.BrowserTypeComboBox.setCurrentText(selected_driver_info.driver_type.value)
|
||||||
self.BrowseBrowserDriverEdit.setText(QDir.toNativeSeparators(str(selected_driver_info.driver_path)))
|
self.BrowseBrowserDriverEdit.setText(QDir.toNativeSeparators(str(selected_driver_info.driver_path)))
|
||||||
@@ -1133,8 +1133,8 @@ class ALConfigWidget(QWidget, Ui_ALConfigWidget):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
current_item = self.UserTreeWidget.currentItem()
|
CurrentItem = self.UserTreeWidget.currentItem()
|
||||||
if current_item and current_item.type() == ALUserTreeItemType.USER.value:
|
if CurrentItem and CurrentItem.type() == ALUserTreeItemType.USER.value:
|
||||||
self.UserTreeWidget.setCurrentItem(None)
|
self.UserTreeWidget.setCurrentItem(None)
|
||||||
if self.saveConfigs(
|
if self.saveConfigs(
|
||||||
self.__config_paths["run"],
|
self.__config_paths["run"],
|
||||||
|
|||||||
@@ -76,8 +76,8 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.icon = QIcon(":/res/icons/AutoLibrary_Logo_64.svg")
|
self.Icon = QIcon(":/res/icons/AutoLibrary_Logo_64.svg")
|
||||||
self.setWindowIcon(self.icon)
|
self.setWindowIcon(self.Icon)
|
||||||
self.MessageIOTextEdit.setFont(QFont("Courier New", 10))
|
self.MessageIOTextEdit.setFont(QFont("Courier New", 10))
|
||||||
self.ManualAction.triggered.connect(self.onManualActionTriggered)
|
self.ManualAction.triggered.connect(self.onManualActionTriggered)
|
||||||
self.AboutAction.triggered.connect(self.onAboutActionTriggered)
|
self.AboutAction.triggered.connect(self.onAboutActionTriggered)
|
||||||
@@ -106,15 +106,15 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
about_dialog = ALAboutDialog(self)
|
AboutDialog = ALAboutDialog(self)
|
||||||
about_dialog.exec()
|
AboutDialog.exec()
|
||||||
|
|
||||||
def onManualActionTriggered(
|
def onManualActionTriggered(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
url = QUrl("https://www.autolibrary.kenanzhu.com/manuals")
|
Url = QUrl("https://www.autolibrary.kenanzhu.com/manuals")
|
||||||
QDesktopServices.openUrl(url)
|
QDesktopServices.openUrl(Url)
|
||||||
|
|
||||||
def setupTray(
|
def setupTray(
|
||||||
self
|
self
|
||||||
@@ -123,7 +123,7 @@ class ALMainWindow(MsgBase, QMainWindow, Ui_ALMainWindow):
|
|||||||
if not QSystemTrayIcon.isSystemTrayAvailable():
|
if not QSystemTrayIcon.isSystemTrayAvailable():
|
||||||
self._showTrace("操作系统不支持系统托盘功能, 无法创建系统托盘图标", self.TraceLevel.WARNING)
|
self._showTrace("操作系统不支持系统托盘功能, 无法创建系统托盘图标", self.TraceLevel.WARNING)
|
||||||
return
|
return
|
||||||
self.TrayIcon = QSystemTrayIcon(self.icon, self)
|
self.TrayIcon = QSystemTrayIcon(self.Icon, self)
|
||||||
self.TrayIcon.setToolTip("AutoLibrary")
|
self.TrayIcon.setToolTip("AutoLibrary")
|
||||||
|
|
||||||
self.TrayMenu = QMenu()
|
self.TrayMenu = QMenu()
|
||||||
|
|||||||
+103
-108
@@ -12,13 +12,14 @@ import time
|
|||||||
import queue
|
import queue
|
||||||
|
|
||||||
from PySide6.QtCore import (
|
from PySide6.QtCore import (
|
||||||
Slot, Signal, QThread
|
Signal,
|
||||||
|
QThread,
|
||||||
)
|
)
|
||||||
|
|
||||||
from base.MsgBase import MsgBase
|
from base.MsgBase import MsgBase
|
||||||
from operators.AutoLib import AutoLib
|
from pages.AutoLib import AutoLib
|
||||||
from utils.JSONReader import JSONReader
|
from utils.JSONReader import JSONReader
|
||||||
from autoscript import execute, registerDefaultTargetVars
|
from autoscript import createEngine
|
||||||
|
|
||||||
|
|
||||||
class AutoLibWorker(MsgBase, QThread):
|
class AutoLibWorker(MsgBase, QThread):
|
||||||
@@ -30,7 +31,7 @@ class AutoLibWorker(MsgBase, QThread):
|
|||||||
self,
|
self,
|
||||||
input_queue: queue.Queue,
|
input_queue: queue.Queue,
|
||||||
output_queue: queue.Queue,
|
output_queue: queue.Queue,
|
||||||
config_paths: dict
|
config_paths: dict,
|
||||||
):
|
):
|
||||||
|
|
||||||
MsgBase.__init__(self, input_queue, output_queue)
|
MsgBase.__init__(self, input_queue, output_queue)
|
||||||
@@ -45,7 +46,7 @@ class AutoLibWorker(MsgBase, QThread):
|
|||||||
if current_time >= "23:30" or current_time <= "07:30":
|
if current_time >= "23:30" or current_time <= "07:30":
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
"当前时间不在图书馆开放时间内, 请在 07:30 - 23:30 之间尝试",
|
"当前时间不在图书馆开放时间内, 请在 07:30 - 23:30 之间尝试",
|
||||||
self.TraceLevel.WARNING
|
self.TraceLevel.WARNING,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
self._showLog(f"时间检查通过, 当前时间: {current_time}", self.TraceLevel.INFO)
|
self._showLog(f"时间检查通过, 当前时间: {current_time}", self.TraceLevel.INFO)
|
||||||
@@ -60,83 +61,113 @@ class AutoLibWorker(MsgBase, QThread):
|
|||||||
):
|
):
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
"配置文件路径不存在, 请检查配置文件路径是否正确",
|
"配置文件路径不存在, 请检查配置文件路径是否正确",
|
||||||
self.TraceLevel.ERROR
|
self.TraceLevel.ERROR,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
self._showLog(f"配置文件路径检查通过, 路径: {self.__config_paths}", self.TraceLevel.INFO)
|
self._showLog(
|
||||||
|
f"配置文件路径检查通过, 路径: {self.__config_paths}",
|
||||||
|
self.TraceLevel.INFO,
|
||||||
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def loadConfigs(
|
def loadConfigs(
|
||||||
self
|
self,
|
||||||
) -> bool:
|
) -> bool:
|
||||||
|
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"正在加载配置文件, 运行配置文件路径: {self.__config_paths["run"]}",
|
f"正在加载配置文件, 运行配置文件路径: {self.__config_paths["run"]}",
|
||||||
no_log=True
|
no_log=True,
|
||||||
)
|
)
|
||||||
self._run_config = JSONReader(self.__config_paths["run"]).data()
|
self._run_config = JSONReader(self.__config_paths["run"]).data()
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"正在加载配置文件, 用户配置文件路径: {self.__config_paths["user"]}",
|
f"正在加载配置文件, 用户配置文件路径: {self.__config_paths["user"]}",
|
||||||
no_log=True
|
no_log=True,
|
||||||
)
|
)
|
||||||
self._user_config = JSONReader(self.__config_paths["user"]).data()
|
self._user_config = JSONReader(self.__config_paths["user"]).data()
|
||||||
if self._run_config is None or self._user_config is None:
|
if self._run_config is None or self._user_config is None:
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
"配置文件加载失败, 请检查配置文件是否正确",
|
"配置文件加载失败, 请检查配置文件是否正确",
|
||||||
self.TraceLevel.ERROR
|
self.TraceLevel.ERROR,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
if not self._user_config.get("groups"):
|
if not self._user_config.get("groups"):
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
"用户配置文件中无有效任务组, 请检查用户配置文件是否正确",
|
"用户配置文件中无有效任务组, 请检查用户配置文件是否正确",
|
||||||
self.TraceLevel.WARNING
|
self.TraceLevel.WARNING,
|
||||||
)
|
)
|
||||||
return False
|
return False
|
||||||
self._showLog(
|
self._showLog(
|
||||||
f"配置文件加载成功, 任务组数量: {len(self._user_config.get('groups', []))}",
|
f"配置文件加载成功, 任务组数量: {len(self._user_config.get("groups"))}",
|
||||||
self.TraceLevel.INFO
|
self.TraceLevel.INFO,
|
||||||
)
|
)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _runName(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
return "常规任务"
|
||||||
|
|
||||||
|
def _beforeCreateAutoLib(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
|
||||||
|
return
|
||||||
|
|
||||||
|
def _onChecksFailed(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _onFinished(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
|
||||||
|
self.autoLibWorkerIsFinished.emit()
|
||||||
|
|
||||||
|
def _onError(
|
||||||
|
self,
|
||||||
|
error_msg: str,
|
||||||
|
):
|
||||||
|
|
||||||
|
self._showTrace(error_msg, self.TraceLevel.ERROR)
|
||||||
|
self.autoLibWorkerFinishedWithError.emit()
|
||||||
|
|
||||||
def run(
|
def run(
|
||||||
self
|
self,
|
||||||
):
|
):
|
||||||
|
|
||||||
auto_lib = None
|
auto_lib = None
|
||||||
self._showTrace("AutoLibrary 开始运行")
|
self._showTrace(f"{self._runName()} 开始运行")
|
||||||
if not self.checkTimeAvailable()\
|
|
||||||
or not self.checkConfigPaths():
|
if not self.checkTimeAvailable() or not self.checkConfigPaths():
|
||||||
# time or config existence check failed, skip and finish
|
if not self._onChecksFailed():
|
||||||
pass
|
return
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
if not self.loadConfigs():
|
if not self.loadConfigs():
|
||||||
raise Exception("配置文件加载失败")
|
raise Exception("配置文件加载失败")
|
||||||
|
self._beforeCreateAutoLib()
|
||||||
auto_lib = AutoLib(
|
auto_lib = AutoLib(
|
||||||
self._input_queue,
|
self._input_queue,
|
||||||
self._output_queue,
|
self._output_queue,
|
||||||
self._run_config
|
self._run_config,
|
||||||
)
|
)
|
||||||
groups = self._user_config.get("groups")
|
groups = self._user_config.get("groups")
|
||||||
for group in groups:
|
for group in groups:
|
||||||
if not group["enabled"]:
|
if not group.get("enabled", False):
|
||||||
self._showTrace(f"任务组 {group["name"]} 已跳过", no_log=True)
|
self._showTrace(f"任务组 {group.get("name", "未知")} 已跳过", no_log=True)
|
||||||
continue
|
continue
|
||||||
self._showTrace(f"正在运行任务组 {group["name"]}", no_log=True)
|
self._showTrace(f"正在运行任务组 {group.get("name", "未知")}", no_log=True)
|
||||||
auto_lib.run(
|
auto_lib.run({"users": group.get("users", [])})
|
||||||
{ "users": group.get("users", []) }
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self._showTrace(
|
self._onError(f"{self._runName()} 运行时发生异常 : {e}")
|
||||||
f"AutoLibrary 运行时发生异常 : {e}",
|
|
||||||
self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
self.autoLibWorkerFinishedWithError.emit()
|
|
||||||
return
|
return
|
||||||
if auto_lib:
|
if auto_lib:
|
||||||
auto_lib.close()
|
auto_lib.close()
|
||||||
self._showTrace("AutoLibrary 运行结束")
|
self._showTrace(f"{self._runName()} 运行结束")
|
||||||
self.autoLibWorkerIsFinished.emit()
|
self._onFinished()
|
||||||
|
|
||||||
|
|
||||||
class TimerTaskWorker(AutoLibWorker):
|
class TimerTaskWorker(AutoLibWorker):
|
||||||
@@ -148,70 +179,54 @@ class TimerTaskWorker(AutoLibWorker):
|
|||||||
timer_task: dict,
|
timer_task: dict,
|
||||||
input_queue: queue.Queue,
|
input_queue: queue.Queue,
|
||||||
output_queue: queue.Queue,
|
output_queue: queue.Queue,
|
||||||
config_paths: dict
|
config_paths: dict,
|
||||||
):
|
):
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue, config_paths)
|
super().__init__(input_queue, output_queue, config_paths)
|
||||||
self.__timer_task = timer_task
|
self.__timer_task = timer_task
|
||||||
|
|
||||||
self.autoLibWorkerIsFinished.connect(self.onTimerTaskIsFinished)
|
def _runName(
|
||||||
self.autoLibWorkerFinishedWithError.connect(self.onTimerTaskFinishedWithError)
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
def run(
|
return f"定时任务 '{self.__timer_task.get("name", "未知")}'"
|
||||||
self
|
|
||||||
|
def _beforeCreateAutoLib(
|
||||||
|
self,
|
||||||
|
):
|
||||||
|
|
||||||
|
self.applyRepeatAutoScript()
|
||||||
|
|
||||||
|
def _onChecksFailed(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
self._showTrace("定时任务跳过执行: 时间或配置文件检查未通过")
|
||||||
|
self.timerTaskWorkerIsFinished.emit(False, self.__timer_task)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _onFinished(
|
||||||
|
self,
|
||||||
):
|
):
|
||||||
|
|
||||||
self._showTrace(f"定时任务 {self.__timer_task['name']} 开始运行")
|
|
||||||
if not self.checkTimeAvailable() or not self.checkConfigPaths():
|
|
||||||
self._showTrace("定时任务跳过执行 (时间或配置文件检查未通过)")
|
|
||||||
self.timerTaskWorkerIsFinished.emit(False, self.__timer_task)
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
if not self.loadConfigs():
|
|
||||||
raise Exception("配置文件加载失败")
|
|
||||||
self.applyRepeatAutoScript()
|
|
||||||
auto_lib = AutoLib(
|
|
||||||
self._input_queue,
|
|
||||||
self._output_queue,
|
|
||||||
self._run_config
|
|
||||||
)
|
|
||||||
groups = self._user_config.get("groups")
|
|
||||||
for group in groups:
|
|
||||||
if not group["enabled"]:
|
|
||||||
self._showTrace(
|
|
||||||
f"任务组 {group['name']} 已跳过",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
self._showTrace(
|
|
||||||
f"正在运行任务组 {group['name']}",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
auto_lib.run(
|
|
||||||
{"users": group.get("users", [])}
|
|
||||||
)
|
|
||||||
auto_lib.close()
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(
|
|
||||||
f"定时任务 {self.__timer_task['name']} 运行时发生异常: {e}",
|
|
||||||
self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
self.timerTaskWorkerIsFinished.emit(True, self.__timer_task)
|
|
||||||
return
|
|
||||||
self._showTrace(f"定时任务 {self.__timer_task['name']} 运行结束")
|
|
||||||
self.timerTaskWorkerIsFinished.emit(False, self.__timer_task)
|
self.timerTaskWorkerIsFinished.emit(False, self.__timer_task)
|
||||||
|
|
||||||
|
def _onError(
|
||||||
|
self,
|
||||||
|
error_msg: str,
|
||||||
|
):
|
||||||
|
|
||||||
|
self._showTrace(error_msg, self.TraceLevel.ERROR)
|
||||||
|
self.timerTaskWorkerIsFinished.emit(True, self.__timer_task)
|
||||||
|
|
||||||
def applyRepeatAutoScript(
|
def applyRepeatAutoScript(
|
||||||
self
|
self,
|
||||||
):
|
):
|
||||||
|
|
||||||
auto_script = self.__timer_task.get("repeat_auto_script", "")
|
auto_script = self.__timer_task.get("repeat_auto_script", "")
|
||||||
if not auto_script or not auto_script.strip():
|
if not auto_script or not auto_script.strip():
|
||||||
return
|
return
|
||||||
self._showTrace(
|
self._showTrace("检测到重复定时任务 AutoScript, 开始执行...", no_log=True)
|
||||||
f"检测到重复定时任务 AutoScript, 开始执行...",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
groups = self._user_config.get("groups", [])
|
groups = self._user_config.get("groups", [])
|
||||||
affected_count = 0
|
affected_count = 0
|
||||||
for group in groups:
|
for group in groups:
|
||||||
@@ -219,35 +234,15 @@ class TimerTaskWorker(AutoLibWorker):
|
|||||||
continue
|
continue
|
||||||
for user in group.get("users", []):
|
for user in group.get("users", []):
|
||||||
try:
|
try:
|
||||||
registerDefaultTargetVars()
|
engine = createEngine()
|
||||||
execute(auto_script, user)
|
engine.execute(auto_script, user)
|
||||||
affected_count += 1
|
affected_count += 1
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
self._showTrace(
|
self._showTrace(
|
||||||
f"AutoScript 执行错误 (用户 {user['username']}): {e}",
|
f"AutoScript 执行错误 (用户 {user.get("username", "未知")}): {e}",
|
||||||
self.TraceLevel.ERROR
|
self.TraceLevel.ERROR,
|
||||||
)
|
)
|
||||||
self._showLog(
|
self._showLog(
|
||||||
f"AutoScript 执行完毕, "
|
f"AutoScript 执行完毕, 影响 {affected_count} 个用户",
|
||||||
f"影响 {affected_count} 个用户",
|
self.TraceLevel.INFO,
|
||||||
self.TraceLevel.INFO
|
|
||||||
)
|
)
|
||||||
|
|
||||||
@Slot()
|
|
||||||
def onTimerTaskIsFinished(
|
|
||||||
self
|
|
||||||
):
|
|
||||||
|
|
||||||
self._showTrace(f"定时任务 {self.__timer_task['name']} 运行结束")
|
|
||||||
self.timerTaskWorkerIsFinished.emit(False, self.__timer_task)
|
|
||||||
|
|
||||||
@Slot()
|
|
||||||
def onTimerTaskFinishedWithError(
|
|
||||||
self
|
|
||||||
):
|
|
||||||
|
|
||||||
self._showTrace(
|
|
||||||
f"定时任务 {self.__timer_task['name']} 运行时发生异常",
|
|
||||||
self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
self.timerTaskWorkerIsFinished.emit(True, self.__timer_task)
|
|
||||||
|
|||||||
@@ -8,7 +8,9 @@ You may use, modify, and distribute this file under the terms of the MIT License
|
|||||||
See the LICENSE file for details.
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
from PySide6.QtCore import (
|
from PySide6.QtCore import (
|
||||||
Qt, Slot, QEvent
|
Qt,
|
||||||
|
Slot,
|
||||||
|
QEvent
|
||||||
)
|
)
|
||||||
from PySide6.QtWidgets import (
|
from PySide6.QtWidgets import (
|
||||||
QFrame,
|
QFrame,
|
||||||
@@ -101,15 +103,15 @@ class ALSeatMapView(QGraphicsView):
|
|||||||
seats_number = [seat.strip() for seat in row.split(",")]
|
seats_number = [seat.strip() for seat in row.split(",")]
|
||||||
for seat_number in seats_number:
|
for seat_number in seats_number:
|
||||||
if seat_number:
|
if seat_number:
|
||||||
seat_widget = ALSeatFrame(seat_number)
|
SeatWidget = ALSeatFrame(seat_number)
|
||||||
seat_widget.clicked.connect(self.onSeatClicked)
|
SeatWidget.clicked.connect(self.onSeatClicked)
|
||||||
self.SeatsContainerLayout.addWidget(seat_widget, row_idx, col_idx)
|
self.SeatsContainerLayout.addWidget(SeatWidget, row_idx, col_idx)
|
||||||
self.__seat_frames[seat_number] = seat_widget
|
self.__seat_frames[seat_number] = SeatWidget
|
||||||
else:
|
else:
|
||||||
spacer = QFrame()
|
Spacer = QFrame()
|
||||||
spacer.setFixedSize(20, 30)
|
Spacer.setFixedSize(20, 30)
|
||||||
spacer.setStyleSheet("background-color: transparent; border: none;")
|
Spacer.setStyleSheet("background-color: transparent; border: none;")
|
||||||
self.SeatsContainerLayout.addWidget(spacer, row_idx, col_idx)
|
self.SeatsContainerLayout.addWidget(Spacer, row_idx, col_idx)
|
||||||
col_idx += 1
|
col_idx += 1
|
||||||
self.SeatsContainerLayout.setSpacing(20)
|
self.SeatsContainerLayout.setSpacing(20)
|
||||||
self.SeatsContainerLayout.setContentsMargins(20, 20, 20, 20)
|
self.SeatsContainerLayout.setContentsMargins(20, 20, 20, 20)
|
||||||
|
|||||||
+67
-68
@@ -56,7 +56,6 @@ class ALStatusLabel(QLabel):
|
|||||||
|
|
||||||
self.setFixedSize(36, 36)
|
self.setFixedSize(36, 36)
|
||||||
self.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
self.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
|
||||||
self.RunningAnimation = QPropertyAnimation(self, b"iconAngle")
|
self.RunningAnimation = QPropertyAnimation(self, b"iconAngle")
|
||||||
self.RunningAnimation.setDuration(1000)
|
self.RunningAnimation.setDuration(1000)
|
||||||
self.RunningAnimation.setStartValue(0)
|
self.RunningAnimation.setStartValue(0)
|
||||||
@@ -119,35 +118,35 @@ class ALStatusLabel(QLabel):
|
|||||||
event
|
event
|
||||||
):
|
):
|
||||||
|
|
||||||
painter = QPainter(self)
|
Painter = QPainter(self)
|
||||||
painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
Painter.setRenderHint(QPainter.RenderHint.Antialiasing)
|
||||||
center_x = self.width()/2
|
center_x = self.width()/2
|
||||||
center_y = self.height()/2
|
center_y = self.height()/2
|
||||||
radius = min(center_x, center_y) - 3
|
radius = min(center_x, center_y) - 3
|
||||||
match self.__status:
|
match self.__status:
|
||||||
case self.Status.WAITING:
|
case self.Status.WAITING:
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(2)
|
Pen.setWidth(2)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
pen.setColor(QColor("#969696")) # grey
|
Pen.setColor(QColor("#969696")) # grey
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawEllipse(
|
Painter.drawEllipse(
|
||||||
int(center_x - radius),
|
int(center_x - radius),
|
||||||
int(center_y - radius),
|
int(center_y - radius),
|
||||||
int(radius*2),
|
int(radius*2),
|
||||||
int(radius*2)
|
int(radius*2)
|
||||||
)
|
)
|
||||||
case self.Status.RUNNING:
|
case self.Status.RUNNING:
|
||||||
gradient = QConicalGradient(center_x, center_y, self.__icon_angle)
|
Gradient = QConicalGradient(center_x, center_y, self.__icon_angle)
|
||||||
gradient.setColorAt(0.0, QColor("#2294FF" if self.isDarkMode() else "#0094FF"))
|
Gradient.setColorAt(0.0, QColor("#2294FF" if self.isDarkMode() else "#0094FF"))
|
||||||
gradient.setColorAt(1.0, QColor("#2294FF00"))
|
Gradient.setColorAt(1.0, QColor("#2294FF00"))
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(3)
|
Pen.setWidth(3)
|
||||||
pen.setBrush(gradient)
|
Pen.setBrush(Gradient)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawEllipse(
|
Painter.drawEllipse(
|
||||||
int(center_x - radius),
|
int(center_x - radius),
|
||||||
int(center_y - radius),
|
int(center_y - radius),
|
||||||
int(radius*2),
|
int(radius*2),
|
||||||
@@ -155,102 +154,102 @@ class ALStatusLabel(QLabel):
|
|||||||
)
|
)
|
||||||
case self.Status.SUCCESS:
|
case self.Status.SUCCESS:
|
||||||
# draw the success green circle
|
# draw the success green circle
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(2)
|
Pen.setWidth(2)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
pen.setColor(QColor("#4CAF50" if self.isDarkMode() else "#00AF50")) # green
|
Pen.setColor(QColor("#4CAF50" if self.isDarkMode() else "#00AF50")) # green
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawEllipse(
|
Painter.drawEllipse(
|
||||||
int(center_x - radius),
|
int(center_x - radius),
|
||||||
int(center_y - radius),
|
int(center_y - radius),
|
||||||
int(radius*2),
|
int(radius*2),
|
||||||
int(radius*2)
|
int(radius*2)
|
||||||
)
|
)
|
||||||
# draw the success check mark '✓'
|
# draw the success check mark '✓'
|
||||||
painter.setPen(Qt.PenStyle.SolidLine)
|
Painter.setPen(Qt.PenStyle.SolidLine)
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(3)
|
Pen.setWidth(3)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
# white when dark mode, black when light mode
|
# white when dark mode, black when light mode
|
||||||
pen.setColor(self.getMarkColor())
|
Pen.setColor(self.getMarkColor())
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
mark_size = radius/2
|
mark_size = radius/2
|
||||||
mark_path = [
|
mark_path = [
|
||||||
(center_x - mark_size, center_y),
|
(center_x - mark_size, center_y),
|
||||||
(center_x - mark_size/3, center_y + mark_size/2),
|
(center_x - mark_size/3, center_y + mark_size/2),
|
||||||
(center_x + mark_size, center_y - mark_size/2)
|
(center_x + mark_size, center_y - mark_size/2)
|
||||||
]
|
]
|
||||||
painter.drawLine(
|
Painter.drawLine(
|
||||||
int(mark_path[0][0]),int(mark_path[0][1]),
|
int(mark_path[0][0]),int(mark_path[0][1]),
|
||||||
int(mark_path[1][0]),int(mark_path[1][1])
|
int(mark_path[1][0]),int(mark_path[1][1])
|
||||||
)
|
)
|
||||||
painter.drawLine(
|
Painter.drawLine(
|
||||||
int(mark_path[1][0]),int(mark_path[1][1]),
|
int(mark_path[1][0]),int(mark_path[1][1]),
|
||||||
int(mark_path[2][0]),int(mark_path[2][1])
|
int(mark_path[2][0]),int(mark_path[2][1])
|
||||||
)
|
)
|
||||||
case self.Status.WARNING:
|
case self.Status.WARNING:
|
||||||
# draw the warning orange circle
|
# draw the warning orange circle
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(2)
|
Pen.setWidth(2)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
pen.setColor(QColor("#FF9800")) # orange
|
Pen.setColor(QColor("#FF9800")) # orange
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawEllipse(
|
Painter.drawEllipse(
|
||||||
int(center_x - radius),
|
int(center_x - radius),
|
||||||
int(center_y - radius),
|
int(center_y - radius),
|
||||||
int(radius*2),
|
int(radius*2),
|
||||||
int(radius*2)
|
int(radius*2)
|
||||||
)
|
)
|
||||||
# draw the warning exclamation mark '!'
|
# draw the warning exclamation mark '!'
|
||||||
painter.setPen(Qt.PenStyle.SolidLine)
|
Painter.setPen(Qt.PenStyle.SolidLine)
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(3)
|
Pen.setWidth(3)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
# white when dark mode, black when light mode
|
# white when dark mode, black when light mode
|
||||||
pen.setColor(self.getMarkColor())
|
Pen.setColor(self.getMarkColor())
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawLine(
|
Painter.drawLine(
|
||||||
int(center_x), int(center_y - radius/2),
|
int(center_x), int(center_y - radius/2),
|
||||||
int(center_x), int(center_y + radius/6)
|
int(center_x), int(center_y + radius/6)
|
||||||
)
|
)
|
||||||
painter.drawPoint(
|
Painter.drawPoint(
|
||||||
int(center_x), int(center_y + radius/2)
|
int(center_x), int(center_y + radius/2)
|
||||||
)
|
)
|
||||||
case self.Status.FAILURE:
|
case self.Status.FAILURE:
|
||||||
# draw the failure red circle
|
# draw the failure red circle
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(2)
|
Pen.setWidth(2)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
pen.setColor(QColor("#DC0000")) # red
|
Pen.setColor(QColor("#DC0000")) # red
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
painter.drawEllipse(
|
Painter.drawEllipse(
|
||||||
int(center_x - radius),
|
int(center_x - radius),
|
||||||
int(center_y - radius),
|
int(center_y - radius),
|
||||||
int(radius*2),
|
int(radius*2),
|
||||||
int(radius*2)
|
int(radius*2)
|
||||||
)
|
)
|
||||||
# draw the failure cross mark '✗'
|
# draw the failure cross mark '✗'
|
||||||
painter.setPen(Qt.PenStyle.SolidLine)
|
Painter.setPen(Qt.PenStyle.SolidLine)
|
||||||
pen = painter.pen()
|
Pen = Painter.pen()
|
||||||
pen.setWidth(3)
|
Pen.setWidth(3)
|
||||||
pen.setBrush(Qt.BrushStyle.NoBrush)
|
Pen.setBrush(Qt.BrushStyle.NoBrush)
|
||||||
pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
Pen.setCapStyle(Qt.PenCapStyle.RoundCap)
|
||||||
# white when dark mode, black when light mode
|
# white when dark mode, black when light mode
|
||||||
pen.setColor(self.getMarkColor())
|
Pen.setColor(self.getMarkColor())
|
||||||
painter.setPen(pen)
|
Painter.setPen(Pen)
|
||||||
mark_size = radius/3
|
mark_size = radius/3
|
||||||
painter.drawLine(
|
Painter.drawLine(
|
||||||
int(center_x - mark_size), int(center_y - mark_size),
|
int(center_x - mark_size), int(center_y - mark_size),
|
||||||
int(center_x + mark_size), int(center_y + mark_size)
|
int(center_x + mark_size), int(center_y + mark_size)
|
||||||
)
|
)
|
||||||
painter.drawLine(
|
Painter.drawLine(
|
||||||
int(center_x + mark_size), int(center_y - mark_size),
|
int(center_x + mark_size), int(center_y - mark_size),
|
||||||
int(center_x - mark_size), int(center_y + mark_size)
|
int(center_x - mark_size), int(center_y + mark_size)
|
||||||
)
|
)
|
||||||
painter.end()
|
Painter.end()
|
||||||
super().paintEvent(event)
|
super().paintEvent(event)
|
||||||
|
|||||||
@@ -12,9 +12,23 @@ import uuid
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
from PySide6.QtCore import Slot, QDateTime, QUrl
|
from PySide6.QtCore import (
|
||||||
|
Slot,
|
||||||
|
QDateTime,
|
||||||
|
QUrl
|
||||||
|
)
|
||||||
from PySide6.QtGui import QDesktopServices
|
from PySide6.QtGui import QDesktopServices
|
||||||
from PySide6.QtWidgets import QLabel, QDialog, QWidget, QSpinBox, QHBoxLayout, QVBoxLayout, QGridLayout, QDateTimeEdit, QGroupBox, QPushButton
|
from PySide6.QtWidgets import (
|
||||||
|
QLabel,
|
||||||
|
QDialog,
|
||||||
|
QWidget,
|
||||||
|
QSpinBox,
|
||||||
|
QHBoxLayout,
|
||||||
|
QVBoxLayout,
|
||||||
|
QDateTimeEdit,
|
||||||
|
QGroupBox,
|
||||||
|
QPushButton
|
||||||
|
)
|
||||||
|
|
||||||
from gui.resources.ui.Ui_ALTimerTaskAddDialog import Ui_ALTimerTaskAddDialog
|
from gui.resources.ui.Ui_ALTimerTaskAddDialog import Ui_ALTimerTaskAddDialog
|
||||||
from utils.TimerUtils import TimerUtils
|
from utils.TimerUtils import TimerUtils
|
||||||
@@ -66,7 +80,6 @@ class ALTimerTaskAddDialog(QDialog, Ui_ALTimerTaskAddDialog):
|
|||||||
self.SpecificDateTimeEdit.setDateTime(QDateTime.currentDateTime().addSecs(60))
|
self.SpecificDateTimeEdit.setDateTime(QDateTime.currentDateTime().addSecs(60))
|
||||||
self.SpecificTimerLayout.addWidget(self.SpecificDateTimeEdit)
|
self.SpecificTimerLayout.addWidget(self.SpecificDateTimeEdit)
|
||||||
self.TimerConfigLayout.addWidget(self.SpecificTimerWidget)
|
self.TimerConfigLayout.addWidget(self.SpecificTimerWidget)
|
||||||
|
|
||||||
self.RelativeTimerWidget = QWidget()
|
self.RelativeTimerWidget = QWidget()
|
||||||
self.RelativeTimerLayout = QHBoxLayout(self.RelativeTimerWidget)
|
self.RelativeTimerLayout = QHBoxLayout(self.RelativeTimerWidget)
|
||||||
self.RelativeTimerLayout.setContentsMargins(0, 0, 0, 0)
|
self.RelativeTimerLayout.setContentsMargins(0, 0, 0, 0)
|
||||||
@@ -94,21 +107,20 @@ class ALTimerTaskAddDialog(QDialog, Ui_ALTimerTaskAddDialog):
|
|||||||
self.RelativeTimerLayout.addWidget(self.RelativeSecondSpinBox)
|
self.RelativeTimerLayout.addWidget(self.RelativeSecondSpinBox)
|
||||||
self.TimerConfigLayout.addWidget(self.RelativeTimerWidget)
|
self.TimerConfigLayout.addWidget(self.RelativeTimerWidget)
|
||||||
self.RelativeTimerWidget.setVisible(False)
|
self.RelativeTimerWidget.setVisible(False)
|
||||||
|
|
||||||
self.AutoScriptGroupBox = QGroupBox("AutoScript 指令")
|
self.AutoScriptGroupBox = QGroupBox("AutoScript 指令")
|
||||||
self.AutoScriptLayout = QVBoxLayout(self.AutoScriptGroupBox)
|
self.AutoScriptLayout = QVBoxLayout(self.AutoScriptGroupBox)
|
||||||
self.AutoScriptLayout.setContentsMargins(3, 3, 3, 3)
|
self.AutoScriptLayout.setContentsMargins(3, 3, 3, 3)
|
||||||
self.AutoScriptLayout.setSpacing(3)
|
self.AutoScriptLayout.setSpacing(3)
|
||||||
autoScriptBtnLayout = QHBoxLayout()
|
AutoScriptBtnLayout = QHBoxLayout()
|
||||||
self.AutoScriptEditButton = QPushButton("编辑")
|
self.AutoScriptEditButton = QPushButton("编辑")
|
||||||
self.AutoScriptEditButton.setMinimumHeight(25)
|
self.AutoScriptEditButton.setMinimumHeight(25)
|
||||||
self.AutoScriptEditButton.setFixedWidth(80)
|
self.AutoScriptEditButton.setFixedWidth(80)
|
||||||
autoScriptBtnLayout.addWidget(self.AutoScriptEditButton)
|
AutoScriptBtnLayout.addWidget(self.AutoScriptEditButton)
|
||||||
autoScriptBtnLayout.addStretch()
|
AutoScriptBtnLayout.addStretch()
|
||||||
self.AutoScriptHelpButton = QPushButton("?")
|
self.AutoScriptHelpButton = QPushButton("?")
|
||||||
self.AutoScriptHelpButton.setFixedSize(20, 20)
|
self.AutoScriptHelpButton.setFixedSize(20, 20)
|
||||||
self.AutoScriptHelpButton.setToolTip(
|
self.AutoScriptHelpButton.setToolTip(
|
||||||
"AutoScript 是一种轻量级 DSL\n"
|
"AutoScript 是一种轻量级 DSL 语言,基于 Lua 实现。\n"
|
||||||
"用于在重复定时任务执行前,对用户的预约数据进行预处理\n"
|
"用于在重复定时任务执行前,对用户的预约数据进行预处理\n"
|
||||||
"\n"
|
"\n"
|
||||||
"点击查看完整在线文档"
|
"点击查看完整在线文档"
|
||||||
@@ -118,12 +130,12 @@ class ALTimerTaskAddDialog(QDialog, Ui_ALTimerTaskAddDialog):
|
|||||||
"font-weight: bold; color: #555; }"
|
"font-weight: bold; color: #555; }"
|
||||||
"QPushButton:hover { background-color: #E0E0E0; }"
|
"QPushButton:hover { background-color: #E0E0E0; }"
|
||||||
)
|
)
|
||||||
autoScriptBtnLayout.addWidget(self.AutoScriptHelpButton)
|
AutoScriptBtnLayout.addWidget(self.AutoScriptHelpButton)
|
||||||
self.AutoScriptStatusLabel = QLabel("未设置")
|
self.AutoScriptStatusLabel = QLabel("未设置")
|
||||||
self.AutoScriptStatusLabel.setStyleSheet("color: #969696;")
|
self.AutoScriptStatusLabel.setStyleSheet("color: #969696;")
|
||||||
self.AutoScriptStatusLabel.setFixedHeight(25)
|
self.AutoScriptStatusLabel.setFixedHeight(25)
|
||||||
autoScriptBtnLayout.addWidget(self.AutoScriptStatusLabel)
|
AutoScriptBtnLayout.addWidget(self.AutoScriptStatusLabel)
|
||||||
self.AutoScriptLayout.addLayout(autoScriptBtnLayout)
|
self.AutoScriptLayout.addLayout(AutoScriptBtnLayout)
|
||||||
self.ALAddTimerTaskLayout.insertWidget(
|
self.ALAddTimerTaskLayout.insertWidget(
|
||||||
self.ALAddTimerTaskLayout.indexOf(self.TaskConfigGroupBox) + 1,
|
self.ALAddTimerTaskLayout.indexOf(self.TaskConfigGroupBox) + 1,
|
||||||
self.AutoScriptGroupBox
|
self.AutoScriptGroupBox
|
||||||
@@ -291,18 +303,18 @@ class ALTimerTaskAddDialog(QDialog, Ui_ALTimerTaskAddDialog):
|
|||||||
@Slot()
|
@Slot()
|
||||||
def onPreviewAutoScript(self):
|
def onPreviewAutoScript(self):
|
||||||
from gui.ALAutoScriptEditDialog import ALAutoScriptEditDialog
|
from gui.ALAutoScriptEditDialog import ALAutoScriptEditDialog
|
||||||
dlg = ALAutoScriptEditDialog(self, self.__auto_script, self.__mock_target_data)
|
Dlg = ALAutoScriptEditDialog(self, self.__auto_script, self.__mock_target_data)
|
||||||
if dlg.exec() == QDialog.DialogCode.Accepted:
|
if Dlg.exec() == QDialog.DialogCode.Accepted:
|
||||||
script = dlg.getScript()
|
script = Dlg.getScript()
|
||||||
self.__auto_script = script
|
self.__auto_script = script
|
||||||
self.__mock_target_data = dlg.getMockData()
|
self.__mock_target_data = Dlg.getMockData()
|
||||||
if script:
|
if script:
|
||||||
self.AutoScriptStatusLabel.setText("已设置")
|
self.AutoScriptStatusLabel.setText("已设置")
|
||||||
self.AutoScriptStatusLabel.setStyleSheet("color: #4CAF50;")
|
self.AutoScriptStatusLabel.setStyleSheet("color: #4CAF50;")
|
||||||
else:
|
else:
|
||||||
self.AutoScriptStatusLabel.setText("未设置")
|
self.AutoScriptStatusLabel.setText("未设置")
|
||||||
self.AutoScriptStatusLabel.setStyleSheet("color: #969696;")
|
self.AutoScriptStatusLabel.setStyleSheet("color: #969696;")
|
||||||
dlg.deleteLater()
|
Dlg.deleteLater()
|
||||||
|
|
||||||
@Slot()
|
@Slot()
|
||||||
def onAutoScriptHelp(
|
def onAutoScriptHelp(
|
||||||
|
|||||||
@@ -41,7 +41,6 @@ class ALTimerTaskHistoryDialog(QDialog):
|
|||||||
self.setWindowTitle("定时任务执行历史 - AutoLibrary")
|
self.setWindowTitle("定时任务执行历史 - AutoLibrary")
|
||||||
self.setMinimumSize(300, 300)
|
self.setMinimumSize(300, 300)
|
||||||
self.setMaximumSize(500, 400)
|
self.setMaximumSize(500, 400)
|
||||||
|
|
||||||
MainLayout = QVBoxLayout(self)
|
MainLayout = QVBoxLayout(self)
|
||||||
InfoLayout = QGridLayout()
|
InfoLayout = QGridLayout()
|
||||||
TaskNameLabel = QLabel(f"任务: {self.__task_data.get('name', '未命名')}")
|
TaskNameLabel = QLabel(f"任务: {self.__task_data.get('name', '未命名')}")
|
||||||
@@ -51,7 +50,6 @@ class ALTimerTaskHistoryDialog(QDialog):
|
|||||||
TaskUUIDLabel.setStyleSheet("color: #969696; font-size: 11px;")
|
TaskUUIDLabel.setStyleSheet("color: #969696; font-size: 11px;")
|
||||||
InfoLayout.addWidget(TaskUUIDLabel, 1, 0)
|
InfoLayout.addWidget(TaskUUIDLabel, 1, 0)
|
||||||
InfoLayout.setColumnStretch(0, 1)
|
InfoLayout.setColumnStretch(0, 1)
|
||||||
|
|
||||||
if self.__task_data.get("repeat", False):
|
if self.__task_data.get("repeat", False):
|
||||||
RepeatLabel = QLabel("可重复性任务")
|
RepeatLabel = QLabel("可重复性任务")
|
||||||
RepeatLabel.setStyleSheet("color: #2294FF; font-size: 12px;")
|
RepeatLabel.setStyleSheet("color: #2294FF; font-size: 12px;")
|
||||||
@@ -68,7 +66,6 @@ class ALTimerTaskHistoryDialog(QDialog):
|
|||||||
self.HistoryTableWidget.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectRows)
|
self.HistoryTableWidget.setSelectionBehavior(QTableWidget.SelectionBehavior.SelectRows)
|
||||||
self.loadHistory()
|
self.loadHistory()
|
||||||
MainLayout.addWidget(self.HistoryTableWidget)
|
MainLayout.addWidget(self.HistoryTableWidget)
|
||||||
|
|
||||||
ButtonLayout = QHBoxLayout()
|
ButtonLayout = QHBoxLayout()
|
||||||
ButtonLayout.addStretch()
|
ButtonLayout.addStretch()
|
||||||
self.CloseButton = QPushButton("关闭")
|
self.CloseButton = QPushButton("关闭")
|
||||||
|
|||||||
@@ -173,20 +173,20 @@ class ALTimerTaskItemWidget(QWidget):
|
|||||||
pos
|
pos
|
||||||
):
|
):
|
||||||
|
|
||||||
menu = QMenu(self)
|
Menu = QMenu(self)
|
||||||
edit_action = QAction("编辑", self)
|
EditAction = QAction("编辑", self)
|
||||||
edit_action.triggered.connect(
|
EditAction.triggered.connect(
|
||||||
lambda: self.editRequested.emit(self.__timer_task)
|
lambda: self.editRequested.emit(self.__timer_task)
|
||||||
)
|
)
|
||||||
menu.addAction(edit_action)
|
Menu.addAction(EditAction)
|
||||||
if self.__timer_task["status"] != ALTimerTaskStatus.RUNNING\
|
if self.__timer_task["status"] != ALTimerTaskStatus.RUNNING\
|
||||||
and self.__timer_task["status"] != ALTimerTaskStatus.READY:
|
and self.__timer_task["status"] != ALTimerTaskStatus.READY:
|
||||||
delete_action = QAction("删除", self)
|
DeleteAction = QAction("删除", self)
|
||||||
delete_action.triggered.connect(
|
DeleteAction.triggered.connect(
|
||||||
lambda: self.__manage_widget.deleteTask(self.__timer_task)
|
lambda: self.__manage_widget.deleteTask(self.__timer_task)
|
||||||
)
|
)
|
||||||
menu.addAction(delete_action)
|
Menu.addAction(DeleteAction)
|
||||||
menu.exec(self.mapToGlobal(pos))
|
Menu.exec(self.mapToGlobal(pos))
|
||||||
|
|
||||||
|
|
||||||
class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
||||||
@@ -209,7 +209,7 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
super().__init__(parent)
|
super().__init__(parent)
|
||||||
self.__cfg_mgr: ConfigProvider = ConfigManager.instance()
|
self.__cfg_mgr: ConfigProvider = ConfigManager.instance()
|
||||||
self.__timer_tasks = []
|
self.__timer_tasks = []
|
||||||
self.__check_timer = None
|
self.__CheckTimer = None
|
||||||
self.__sort_policy = self.SortPolicy.BY_EXECUTE_TIME
|
self.__sort_policy = self.SortPolicy.BY_EXECUTE_TIME
|
||||||
self.__sort_order = Qt.SortOrder.AscendingOrder
|
self.__sort_order = Qt.SortOrder.AscendingOrder
|
||||||
|
|
||||||
@@ -233,9 +233,9 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
self.__check_timer = QTimer(self)
|
self.__CheckTimer = QTimer(self)
|
||||||
self.__check_timer.timeout.connect(self.checkTasks)
|
self.__CheckTimer.timeout.connect(self.checkTasks)
|
||||||
self.__check_timer.start(500)
|
self.__CheckTimer.start(500)
|
||||||
|
|
||||||
def initializeTimerTasks(
|
def initializeTimerTasks(
|
||||||
self
|
self
|
||||||
@@ -386,28 +386,28 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
self.TimerTasksListWidget.clear()
|
self.TimerTasksListWidget.clear()
|
||||||
self.sortTimerTasks(self.__sort_policy, self.__sort_order)
|
self.sortTimerTasks(self.__sort_policy, self.__sort_order)
|
||||||
for timer_task in self.__timer_tasks:
|
for timer_task in self.__timer_tasks:
|
||||||
item = QListWidgetItem()
|
Item = QListWidgetItem()
|
||||||
item.setData(Qt.UserRole, timer_task)
|
Item.setData(Qt.UserRole, timer_task)
|
||||||
widget = ALTimerTaskItemWidget(self, timer_task)
|
Widget = ALTimerTaskItemWidget(self, timer_task)
|
||||||
widget.DeleteButton.clicked.connect(
|
Widget.DeleteButton.clicked.connect(
|
||||||
lambda _, task = timer_task: self.deleteTask(task)
|
lambda _, task = timer_task: self.deleteTask(task)
|
||||||
)
|
)
|
||||||
if timer_task.get("repeat", False) and hasattr(widget, "HistoryButton"):
|
if timer_task.get("repeat", False) and hasattr(Widget, "HistoryButton"):
|
||||||
widget.HistoryButton.clicked.connect(
|
Widget.HistoryButton.clicked.connect(
|
||||||
lambda _, task = timer_task: self.showTaskHistory(task)
|
lambda _, task = timer_task: self.showTaskHistory(task)
|
||||||
)
|
)
|
||||||
widget.editRequested.connect(self.editTask)
|
Widget.editRequested.connect(self.editTask)
|
||||||
item.setSizeHint(widget.size())
|
Item.setSizeHint(Widget.size())
|
||||||
self.TimerTasksListWidget.addItem(item)
|
self.TimerTasksListWidget.addItem(Item)
|
||||||
self.TimerTasksListWidget.setItemWidget(item, widget)
|
self.TimerTasksListWidget.setItemWidget(Item, Widget)
|
||||||
|
|
||||||
def addTask(
|
def addTask(
|
||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
dialog = ALTimerTaskAddDialog(self)
|
Dialog = ALTimerTaskAddDialog(self)
|
||||||
if dialog.exec() == QDialog.DialogCode.Accepted:
|
if Dialog.exec() == QDialog.DialogCode.Accepted:
|
||||||
timer_task = dialog.getTimerTask()
|
timer_task = Dialog.getTimerTask()
|
||||||
self.__timer_tasks.append(timer_task)
|
self.__timer_tasks.append(timer_task)
|
||||||
self.timerTasksChanged.emit()
|
self.timerTasksChanged.emit()
|
||||||
|
|
||||||
@@ -416,9 +416,9 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
timer_task: dict
|
timer_task: dict
|
||||||
):
|
):
|
||||||
|
|
||||||
dialog = ALTimerTaskAddDialog(self, timer_task)
|
Dialog = ALTimerTaskAddDialog(self, timer_task)
|
||||||
if dialog.exec() == QDialog.DialogCode.Accepted:
|
if Dialog.exec() == QDialog.DialogCode.Accepted:
|
||||||
updated = dialog.getTimerTask()
|
updated = Dialog.getTimerTask()
|
||||||
for i, task in enumerate(self.__timer_tasks):
|
for i, task in enumerate(self.__timer_tasks):
|
||||||
if task["uuid"] == updated["uuid"]:
|
if task["uuid"] == updated["uuid"]:
|
||||||
self.__timer_tasks[i] = updated
|
self.__timer_tasks[i] = updated
|
||||||
@@ -449,19 +449,19 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
):
|
):
|
||||||
|
|
||||||
if timer_task["repeat"]: # when delete a repeat task
|
if timer_task["repeat"]: # when delete a repeat task
|
||||||
msgbox = QMessageBox(self)
|
MsgBox = QMessageBox(self)
|
||||||
msgbox.setIcon(QMessageBox.Icon.Question)
|
MsgBox.setIcon(QMessageBox.Icon.Question)
|
||||||
msgbox.setWindowTitle("警告 - AutoLibrary")
|
MsgBox.setWindowTitle("警告 - AutoLibrary")
|
||||||
msgbox.setStandardButtons(
|
MsgBox.setStandardButtons(
|
||||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
||||||
)
|
)
|
||||||
msgbox.setText("删除可重复性任务将同时删除所有已执行的记录 !\n是否继续 ?")
|
MsgBox.setText("删除可重复性任务将同时删除所有已执行的记录 !\n是否继续 ?")
|
||||||
msgbox.setDetailedText(
|
MsgBox.setDetailedText(
|
||||||
"以下可重复性任务将被删除:\n"\
|
"以下可重复性任务将被删除:\n"\
|
||||||
"\n"
|
"\n"
|
||||||
f"{self.getTimerTaskDetailMessage(timer_task)}"
|
f"{self.getTimerTaskDetailMessage(timer_task)}"
|
||||||
)
|
)
|
||||||
result = msgbox.exec()
|
result = MsgBox.exec()
|
||||||
if result != QMessageBox.StandardButton.Yes:
|
if result != QMessageBox.StandardButton.Yes:
|
||||||
return
|
return
|
||||||
task_uuid = timer_task["uuid"]
|
task_uuid = timer_task["uuid"]
|
||||||
@@ -506,13 +506,13 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
]
|
]
|
||||||
repeat_tasks_count = len(repeat_tasks)
|
repeat_tasks_count = len(repeat_tasks)
|
||||||
if repeat_tasks_count > 0:
|
if repeat_tasks_count > 0:
|
||||||
msgbox = QMessageBox(self)
|
MsgBox = QMessageBox(self)
|
||||||
msgbox.setIcon(QMessageBox.Icon.Question)
|
MsgBox.setIcon(QMessageBox.Icon.Question)
|
||||||
msgbox.setWindowTitle("警告 - AutoLibrary")
|
MsgBox.setWindowTitle("警告 - AutoLibrary")
|
||||||
msgbox.setStandardButtons(
|
MsgBox.setStandardButtons(
|
||||||
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No
|
||||||
)
|
)
|
||||||
msgbox.setText(
|
MsgBox.setText(
|
||||||
f"存在 {repeat_tasks_count} 个可重复性任务,\n"
|
f"存在 {repeat_tasks_count} 个可重复性任务,\n"
|
||||||
"删除可重复性任务将同时删除所有已执行的记录 !\n"
|
"删除可重复性任务将同时删除所有已执行的记录 !\n"
|
||||||
"是否继续 ?"
|
"是否继续 ?"
|
||||||
@@ -520,12 +520,12 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
delete_msgs = [
|
delete_msgs = [
|
||||||
self.getTimerTaskDetailMessage(x) for x in repeat_tasks
|
self.getTimerTaskDetailMessage(x) for x in repeat_tasks
|
||||||
]
|
]
|
||||||
msgbox.setDetailedText(
|
MsgBox.setDetailedText(
|
||||||
"以下可重复性任务将被删除:\n"\
|
"以下可重复性任务将被删除:\n"\
|
||||||
"\n"
|
"\n"
|
||||||
f"{"\n\n".join(delete_msgs)}"
|
f"{"\n\n".join(delete_msgs)}"
|
||||||
)
|
)
|
||||||
result = msgbox.exec()
|
result = MsgBox.exec()
|
||||||
if result != QMessageBox.StandardButton.Yes:
|
if result != QMessageBox.StandardButton.Yes:
|
||||||
return
|
return
|
||||||
# clear all tasks
|
# clear all tasks
|
||||||
@@ -537,8 +537,8 @@ class ALTimerTaskManageWidget(QWidget, Ui_ALTimerTaskManageWidget):
|
|||||||
task: dict
|
task: dict
|
||||||
):
|
):
|
||||||
|
|
||||||
dialog = ALTimerTaskHistoryDialog(self, task)
|
Dialog = ALTimerTaskHistoryDialog(self, task)
|
||||||
if dialog.exec() == QDialog.DialogCode.Accepted:
|
if Dialog.exec() == QDialog.DialogCode.Accepted:
|
||||||
self.timerTasksChanged.emit()
|
self.timerTasksChanged.emit()
|
||||||
|
|
||||||
def checkTasks(
|
def checkTasks(
|
||||||
|
|||||||
+15
-15
@@ -51,9 +51,9 @@ class ALUserTreeWidget(QTreeWidget):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
__qtreewidgetitem = QTreeWidgetItem()
|
__QTreeWidgetItem = QTreeWidgetItem()
|
||||||
__qtreewidgetitem.setText(0, u"\u5206\u7ec4/\u7528\u6237");
|
__QTreeWidgetItem.setText(0, u"\u5206\u7ec4/\u7528\u6237");
|
||||||
self.setHeaderItem(__qtreewidgetitem)
|
self.setHeaderItem(__QTreeWidgetItem)
|
||||||
self.setObjectName(u"UserTreeWidget")
|
self.setObjectName(u"UserTreeWidget")
|
||||||
self.setMinimumSize(QSize(230, 0))
|
self.setMinimumSize(QSize(230, 0))
|
||||||
self.setMaximumSize(QSize(250, 16777215))
|
self.setMaximumSize(QSize(250, 16777215))
|
||||||
@@ -81,8 +81,8 @@ class ALUserTreeWidget(QTreeWidget):
|
|||||||
self
|
self
|
||||||
):
|
):
|
||||||
|
|
||||||
___qtreewidgetitem = self.headerItem()
|
___QTreeWidgetItem = self.headerItem()
|
||||||
___qtreewidgetitem.setText(1, QCoreApplication.translate("ALConfigWidget", u"\u72b6\u6001", None));
|
___QTreeWidgetItem.setText(1, QCoreApplication.translate("ALConfigWidget", u"\u72b6\u6001", None));
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def isDragPositionValid(
|
def isDragPositionValid(
|
||||||
@@ -109,27 +109,27 @@ class ALUserTreeWidget(QTreeWidget):
|
|||||||
|
|
||||||
super().dragMoveEvent(event)
|
super().dragMoveEvent(event)
|
||||||
|
|
||||||
source_item = self.currentItem()
|
SourceItem = self.currentItem()
|
||||||
target_item = self.itemAt(event.position().toPoint())
|
TargetItem = self.itemAt(event.position().toPoint())
|
||||||
if source_item is None:
|
if SourceItem is None:
|
||||||
event.ignore()
|
event.ignore()
|
||||||
return
|
return
|
||||||
if source_item.type() == ALUserTreeItemType.GROUP.value:
|
if SourceItem.type() == ALUserTreeItemType.GROUP.value:
|
||||||
if target_item is not None:
|
if TargetItem is not None:
|
||||||
event.ignore()
|
event.ignore()
|
||||||
return
|
return
|
||||||
elif source_item.type() == ALUserTreeItemType.USER.value:
|
elif SourceItem.type() == ALUserTreeItemType.USER.value:
|
||||||
if target_item is None:
|
if TargetItem is None:
|
||||||
event.ignore()
|
event.ignore()
|
||||||
return
|
return
|
||||||
if target_item.type() != ALUserTreeItemType.GROUP.value:
|
if TargetItem.type() != ALUserTreeItemType.GROUP.value:
|
||||||
event.ignore()
|
event.ignore()
|
||||||
return
|
return
|
||||||
if target_item.checkState(1) == Qt.CheckState.Unchecked:
|
if TargetItem.checkState(1) == Qt.CheckState.Unchecked:
|
||||||
event.ignore()
|
event.ignore()
|
||||||
return
|
return
|
||||||
if not self.isDragPositionValid(
|
if not self.isDragPositionValid(
|
||||||
self.visualItemRect(target_item),
|
self.visualItemRect(TargetItem),
|
||||||
event.position().toPoint()
|
event.position().toPoint()
|
||||||
):
|
):
|
||||||
event.ignore()
|
event.ignore()
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ from PySide6.QtWidgets import (
|
|||||||
from PySide6.QtGui import QCloseEvent
|
from PySide6.QtGui import QCloseEvent
|
||||||
|
|
||||||
from managers.driver.WebDriverManager import (
|
from managers.driver.WebDriverManager import (
|
||||||
instance as webdriverManagerInstance,
|
instance as webdriverInstance,
|
||||||
WebDriverManager,
|
WebDriverManager,
|
||||||
WebDriverInfo,
|
WebDriverInfo,
|
||||||
WebDriverType,
|
WebDriverType,
|
||||||
@@ -182,14 +182,11 @@ class ALWebDriverDownloadDialog(QDialog):
|
|||||||
self.setMaximumHeight(240)
|
self.setMaximumHeight(240)
|
||||||
self.setMinimumHeight(240)
|
self.setMinimumHeight(240)
|
||||||
self.setWindowTitle("浏览器驱动下载 - AutoLibrary")
|
self.setWindowTitle("浏览器驱动下载 - AutoLibrary")
|
||||||
|
|
||||||
self.MainLayout = QVBoxLayout(self)
|
self.MainLayout = QVBoxLayout(self)
|
||||||
self.MainLayout.setContentsMargins(5, 5, 5, 5)
|
self.MainLayout.setContentsMargins(5, 5, 5, 5)
|
||||||
self.MainLayout.setSpacing(5)
|
self.MainLayout.setSpacing(5)
|
||||||
|
|
||||||
self.BrowserCountLabel = QLabel("检测到 0 个可用浏览器:")
|
self.BrowserCountLabel = QLabel("检测到 0 个可用浏览器:")
|
||||||
self.MainLayout.addWidget(self.BrowserCountLabel)
|
self.MainLayout.addWidget(self.BrowserCountLabel)
|
||||||
|
|
||||||
self.DriverInfoLayout = QHBoxLayout()
|
self.DriverInfoLayout = QHBoxLayout()
|
||||||
self.DriverInfoLayout.setSpacing(5)
|
self.DriverInfoLayout.setSpacing(5)
|
||||||
self.DriverComboBox = QComboBox()
|
self.DriverComboBox = QComboBox()
|
||||||
@@ -198,7 +195,6 @@ class ALWebDriverDownloadDialog(QDialog):
|
|||||||
self.StatusLabel.setFixedSize(32, 32)
|
self.StatusLabel.setFixedSize(32, 32)
|
||||||
self.DriverInfoLayout.addWidget(self.StatusLabel)
|
self.DriverInfoLayout.addWidget(self.StatusLabel)
|
||||||
self.MainLayout.addLayout(self.DriverInfoLayout)
|
self.MainLayout.addLayout(self.DriverInfoLayout)
|
||||||
|
|
||||||
self.DetailLayout = QVBoxLayout()
|
self.DetailLayout = QVBoxLayout()
|
||||||
self.DetailLayout.setSpacing(5)
|
self.DetailLayout.setSpacing(5)
|
||||||
self.DetailLayout.setContentsMargins(5, 5, 5, 5)
|
self.DetailLayout.setContentsMargins(5, 5, 5, 5)
|
||||||
@@ -211,7 +207,6 @@ class ALWebDriverDownloadDialog(QDialog):
|
|||||||
self.PathLabel.setText("路径:未安装")
|
self.PathLabel.setText("路径:未安装")
|
||||||
self.DetailLayout.addWidget(self.PathLabel)
|
self.DetailLayout.addWidget(self.PathLabel)
|
||||||
self.MainLayout.addLayout(self.DetailLayout)
|
self.MainLayout.addLayout(self.DetailLayout)
|
||||||
|
|
||||||
self.Line = QFrame()
|
self.Line = QFrame()
|
||||||
self.Line.setFrameShape(QFrame.Shape.HLine)
|
self.Line.setFrameShape(QFrame.Shape.HLine)
|
||||||
self.Line.setFrameShadow(QFrame.Shadow.Sunken)
|
self.Line.setFrameShadow(QFrame.Shadow.Sunken)
|
||||||
@@ -237,7 +232,6 @@ class ALWebDriverDownloadDialog(QDialog):
|
|||||||
self.ConfirmButton = QPushButton("确认")
|
self.ConfirmButton = QPushButton("确认")
|
||||||
self.ConfirmButton.setFixedSize(80, 25)
|
self.ConfirmButton.setFixedSize(80, 25)
|
||||||
self.ConfirmButton.setEnabled(False)
|
self.ConfirmButton.setEnabled(False)
|
||||||
|
|
||||||
self.ControlLayout.addWidget(self.RefreshButton)
|
self.ControlLayout.addWidget(self.RefreshButton)
|
||||||
self.ControlLayout.addWidget(self.DownloadButton)
|
self.ControlLayout.addWidget(self.DownloadButton)
|
||||||
self.ControlLayout.addWidget(self.DeleteButton)
|
self.ControlLayout.addWidget(self.DeleteButton)
|
||||||
@@ -261,7 +255,7 @@ class ALWebDriverDownloadDialog(QDialog):
|
|||||||
):
|
):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.__driver_manager = webdriverManagerInstance(self.__driver_dir)
|
self.__driver_manager = webdriverInstance(self.__driver_dir)
|
||||||
except ValueError as e:
|
except ValueError as e:
|
||||||
QMessageBox.warning(self, "初始化失败", f"WebDriverManager 初始化失败:\n{str(e)}")
|
QMessageBox.warning(self, "初始化失败", f"WebDriverManager 初始化失败:\n{str(e)}")
|
||||||
self.reject()
|
self.reject()
|
||||||
|
|||||||
+6
-16
@@ -1,19 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
GUI module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- ALMainWindow: Main window class.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
- ALAboutDialog: About dialog class.
|
See the LICENSE file for details.
|
||||||
- ALConfigWidget: Configuration widget class.
|
|
||||||
- ALSeatFrame: Seat frame class.
|
|
||||||
- ALSeatMapView: Seat map view class.
|
|
||||||
- ALSeatMapTable: Seat map table class.
|
|
||||||
- ALSeatMapSelectDialog: Seat map select dialog class.
|
|
||||||
- ALTimerTaskAddDialog: Timer task add dialog class.
|
|
||||||
- ALAutoScriptOrchDialog: AutoScript orchestration dialog class.
|
|
||||||
- ALTimerTaskHistoryDialog: Timer task history dialog class.
|
|
||||||
- ALTimerTaskManageWidget: Timer task manage widget class.
|
|
||||||
- ALUserTreeWidget: User tree widget class.
|
|
||||||
- ALMainWorkers: Main workers class.
|
|
||||||
- ALVersionInfo: Version info class.
|
|
||||||
"""
|
"""
|
||||||
@@ -3,6 +3,9 @@
|
|||||||
<file>icons/AutoLibrary_Logo_64.svg</file>
|
<file>icons/AutoLibrary_Logo_64.svg</file>
|
||||||
<file>icons/AutoLibrary_Logo_128.svg</file>
|
<file>icons/AutoLibrary_Logo_128.svg</file>
|
||||||
|
|
||||||
|
<file>icons/Copy.svg</file>
|
||||||
|
<file>icons/Reset.svg</file>
|
||||||
|
|
||||||
<file>translators/qtbase_zh_CN.qm</file>
|
<file>translators/qtbase_zh_CN.qm</file>
|
||||||
</qresource>
|
</qresource>
|
||||||
</RCC>
|
</RCC>
|
||||||
|
|||||||
@@ -1,3 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
GUI resources module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
Binary file not shown.
Binary file not shown.
|
After Width: | Height: | Size: 173 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 178 KiB |
@@ -0,0 +1,6 @@
|
|||||||
|
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||||
|
<path d="M13 7H7V5H13V7Z" fill="currentColor"/>
|
||||||
|
<path d="M13 11H7V9H13V11Z" fill="currentColor"/>
|
||||||
|
<path d="M7 15H13V13H7V15Z" fill="currentColor"/>
|
||||||
|
<path fill-rule="evenodd" clip-rule="evenodd" d="M3 19V1H17V5H21V23H7V19H3ZM15 17V3H5V17H15ZM17 7V19H9V21H19V7H17Z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 396 B |
@@ -0,0 +1,3 @@
|
|||||||
|
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="currentColor">
|
||||||
|
<path d="M20.817 11.186a8.94 8.94 0 0 0-1.355-3.219 9.053 9.053 0 0 0-2.43-2.43 8.95 8.95 0 0 0-3.219-1.355 9.028 9.028 0 0 0-1.838-.18V2L8 5l3.975 3V6.002c.484-.002.968.044 1.435.14a6.961 6.961 0 0 1 2.502 1.053 7.005 7.005 0 0 1 1.892 1.892A6.967 6.967 0 0 1 19 13a7.032 7.032 0 0 1-.55 2.725 7.11 7.11 0 0 1-.644 1.188 7.2 7.2 0 0 1-.858 1.039 7.028 7.028 0 0 1-3.536 1.907 7.13 7.13 0 0 1-2.822 0 6.961 6.961 0 0 1-2.503-1.054 7.002 7.002 0 0 1-1.89-1.89A6.996 6.996 0 0 1 5 13H3a9.02 9.02 0 0 0 1.539 5.034 9.096 9.096 0 0 0 2.428 2.428A8.95 8.95 0 0 0 12 22a9.09 9.09 0 0 0 1.814-.183 9.014 9.014 0 0 0 3.218-1.355 8.886 8.886 0 0 0 1.331-1.099 9.228 9.228 0 0 0 1.1-1.332A8.952 8.952 0 0 0 21 13a9.09 9.09 0 0 0-.183-1.814z" fill="currentColor"/>
|
||||||
|
</svg>
|
||||||
|
After Width: | Height: | Size: 866 B |
@@ -19,7 +19,7 @@
|
|||||||
<property name="maximumSize">
|
<property name="maximumSize">
|
||||||
<size>
|
<size>
|
||||||
<width>800</width>
|
<width>800</width>
|
||||||
<height>400</height>
|
<height>600</height>
|
||||||
</size>
|
</size>
|
||||||
</property>
|
</property>
|
||||||
<property name="windowTitle">
|
<property name="windowTitle">
|
||||||
@@ -103,53 +103,6 @@
|
|||||||
<property name="spacing">
|
<property name="spacing">
|
||||||
<number>0</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
|
||||||
<widget class="QFrame" name="AboutInfoSpaceFrame">
|
|
||||||
<property name="minimumSize">
|
|
||||||
<size>
|
|
||||||
<width>56</width>
|
|
||||||
<height>0</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<property name="maximumSize">
|
|
||||||
<size>
|
|
||||||
<width>56</width>
|
|
||||||
<height>16777215</height>
|
|
||||||
</size>
|
|
||||||
</property>
|
|
||||||
<property name="frameShape">
|
|
||||||
<enum>QFrame::Shape::NoFrame</enum>
|
|
||||||
</property>
|
|
||||||
<property name="frameShadow">
|
|
||||||
<enum>QFrame::Shadow::Plain</enum>
|
|
||||||
</property>
|
|
||||||
<property name="lineWidth">
|
|
||||||
<number>0</number>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item>
|
|
||||||
<widget class="QTextBrowser" name="AboutInfoBrowser">
|
|
||||||
<property name="frameShadow">
|
|
||||||
<enum>QFrame::Shadow::Plain</enum>
|
|
||||||
</property>
|
|
||||||
<property name="verticalScrollBarPolicy">
|
|
||||||
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
|
|
||||||
</property>
|
|
||||||
<property name="horizontalScrollBarPolicy">
|
|
||||||
<enum>Qt::ScrollBarPolicy::ScrollBarAlwaysOff</enum>
|
|
||||||
</property>
|
|
||||||
<property name="lineWrapMode">
|
|
||||||
<enum>QTextEdit::LineWrapMode::NoWrap</enum>
|
|
||||||
</property>
|
|
||||||
<property name="openExternalLinks">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
<property name="openLinks">
|
|
||||||
<bool>true</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
</layout>
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
|
|||||||
@@ -1,11 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Interfaces module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Defines abstract interfaces (Protocols) and shared type definitions
|
This software is provided "as is", without any warranty of any kind.
|
||||||
used across layers to decouple consumers from concrete implementations.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
Key components:
|
|
||||||
- ConfigProvider: Abstract interface for configuration access.
|
|
||||||
- ConfigType: Enumeration of configuration file types.
|
|
||||||
- ConfigKey: Type-safe hierarchical key constants for config lookups.
|
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Managers module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- ConfigManager: Config manager for managing configuration files.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
- LogManager: Log manager for logging.
|
See the LICENSE file for details.
|
||||||
- WebDriverManager: Web driver manager for managing web drivers.
|
|
||||||
"""
|
"""
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Config managers module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- ConfigManager: Config manager for managing configuration files.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
@@ -360,6 +360,10 @@ class WebDriverDownloader:
|
|||||||
break
|
break
|
||||||
if not driver_file:
|
if not driver_file:
|
||||||
raise FileNotFoundError(f"未找到 web driver 文件 : {expected_name}")
|
raise FileNotFoundError(f"未找到 web driver 文件 : {expected_name}")
|
||||||
|
# Ensure executable permissions on Unix systems (zipfile
|
||||||
|
# extraction does not preserve the execute bit).
|
||||||
|
if os.name != 'nt':
|
||||||
|
os.chmod(driver_file, 0o755)
|
||||||
progress_callback(100, 100, 0.0, "解压完成")
|
progress_callback(100, 100, 0.0, "解压完成")
|
||||||
self.download_path.unlink()
|
self.download_path.unlink()
|
||||||
self._cleanup(driver_file)
|
self._cleanup(driver_file)
|
||||||
|
|||||||
@@ -111,6 +111,10 @@ class WebDriverManager:
|
|||||||
for driver_info in self.__driver_infos:
|
for driver_info in self.__driver_infos:
|
||||||
driver_path = self._getDriverPath(driver_info)
|
driver_path = self._getDriverPath(driver_info)
|
||||||
if driver_path and driver_path.exists() and driver_path.is_file():
|
if driver_path and driver_path.exists() and driver_path.is_file():
|
||||||
|
# Repair missing execute permission on Unix
|
||||||
|
# (zip-extracted drivers from older versions).
|
||||||
|
if os.name != 'nt' and not os.access(str(driver_path), os.X_OK):
|
||||||
|
os.chmod(str(driver_path), 0o755)
|
||||||
driver_info.driver_path = driver_path
|
driver_info.driver_path = driver_path
|
||||||
driver_info.driver_status = WebDriverStatus.INSTALLED
|
driver_info.driver_status = WebDriverStatus.INSTALLED
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Driver managers module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- WebBrowserDetector: Web browser detector class.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
- WebDriverDownloader: Web driver downloader class.
|
See the LICENSE file for details.
|
||||||
- WebDriverManager: Web driver manager class.
|
|
||||||
"""
|
"""
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Log managers module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- LogManager: Log manager for logging.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
"""
|
"""
|
||||||
@@ -1,352 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import os
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from selenium import webdriver
|
|
||||||
from selenium.common.exceptions import TimeoutException
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
from selenium.webdriver.edge.service import Service as EdgeService
|
|
||||||
from selenium.webdriver.chrome.service import Service as ChromeService
|
|
||||||
from selenium.webdriver.firefox.service import Service as FirefoxService
|
|
||||||
|
|
||||||
from base.MsgBase import MsgBase
|
|
||||||
from operators.LibChecker import LibChecker
|
|
||||||
from operators.LibLogin import LibLogin
|
|
||||||
from operators.LibLogout import LibLogout
|
|
||||||
from operators.LibReserve import LibReserve
|
|
||||||
from operators.LibCheckin import LibCheckin
|
|
||||||
from operators.LibRenew import LibRenew
|
|
||||||
|
|
||||||
|
|
||||||
class AutoLib(MsgBase):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
run_config: dict
|
|
||||||
):
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__run_config = run_config
|
|
||||||
self.__user_config = None
|
|
||||||
self.__driver = None
|
|
||||||
if not self.__initBrowserDriver():
|
|
||||||
raise Exception("浏览器驱动初始化失败 !")
|
|
||||||
else:
|
|
||||||
if not self.__initDriverUrl():
|
|
||||||
self.close()
|
|
||||||
raise Exception("浏览器驱动URL初始化失败 !")
|
|
||||||
self.__initLibOperators()
|
|
||||||
|
|
||||||
def __initBrowserDriver(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
self._showTrace("正在初始化浏览器驱动......", no_log=True)
|
|
||||||
|
|
||||||
web_driver_config = self.__run_config.get("web_driver", None)
|
|
||||||
self.__driver_type = web_driver_config.get("driver_type")
|
|
||||||
match self.__driver_type.lower():
|
|
||||||
case "edge":
|
|
||||||
driver_options = webdriver.EdgeOptions()
|
|
||||||
case "chrome":
|
|
||||||
driver_options = webdriver.ChromeOptions()
|
|
||||||
case "firefox":
|
|
||||||
driver_options = webdriver.FirefoxOptions()
|
|
||||||
case _:
|
|
||||||
self._showTrace(
|
|
||||||
f"不支持的浏览器驱动类型: {self.__driver_type} !",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
if not web_driver_config:
|
|
||||||
self._showTrace("未配置浏览器驱动参数 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
if web_driver_config.get("headless"):
|
|
||||||
driver_options.add_argument("--headless")
|
|
||||||
driver_options.add_argument("--disable-gpu")
|
|
||||||
driver_options.add_argument("--no-sandbox")
|
|
||||||
driver_options.add_argument("--disable-dev-shm-usage")
|
|
||||||
|
|
||||||
# must be 1920x1080, otherwise the page will cause some elements not accessible
|
|
||||||
driver_options.add_argument("--window-size=1920,1080")
|
|
||||||
|
|
||||||
# omit ssl errors and verbose log level
|
|
||||||
driver_options.add_argument("--ignore-certificate-errors")
|
|
||||||
driver_options.add_argument("--ignore-ssl-errors")
|
|
||||||
driver_options.add_argument("--log-level=OFF")
|
|
||||||
driver_options.add_argument("--silent")
|
|
||||||
|
|
||||||
# set options for chrome and edge
|
|
||||||
if self.__driver_type.lower() in ["edge", "chrome"]:
|
|
||||||
driver_options.add_argument("--remote-allow-origins=*")
|
|
||||||
driver_options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
|
||||||
driver_options.add_experimental_option("useAutomationExtension", False)
|
|
||||||
driver_options.add_argument("--disable-blink-features=AutomationControlled")
|
|
||||||
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "\
|
|
||||||
"AppleWebKit/537.36 (KHTML, like Gecko) "\
|
|
||||||
"Chrome/120.0.0.0 "\
|
|
||||||
"Safari/537.36"
|
|
||||||
if self.__driver_type.lower() == "edge":
|
|
||||||
user_agent += " Edg/120.0.0.0"
|
|
||||||
# set options for firefox
|
|
||||||
elif self.__driver_type.lower() == "firefox":
|
|
||||||
driver_options.set_preference("dom.webdriver.enabled", False)
|
|
||||||
driver_options.set_preference("useAutomationExtension", False)
|
|
||||||
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) "\
|
|
||||||
"Gecko/20100101 Firefox/120.0"
|
|
||||||
driver_options.add_argument(f"user-agent={user_agent}")
|
|
||||||
|
|
||||||
# init browser driver
|
|
||||||
self.__driver_path = web_driver_config.get("driver_path")
|
|
||||||
if not self.__driver_path:
|
|
||||||
self._showTrace("未配置浏览器驱动路径 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
self.__driver_path = os.path.abspath(self.__driver_path)
|
|
||||||
try:
|
|
||||||
service = None
|
|
||||||
match self.__driver_type.lower():
|
|
||||||
case "edge":
|
|
||||||
service = EdgeService(executable_path=self.__driver_path)
|
|
||||||
self.__driver = webdriver.Edge(service=service, options=driver_options)
|
|
||||||
case "chrome":
|
|
||||||
service = ChromeService(executable_path=self.__driver_path)
|
|
||||||
self.__driver = webdriver.Chrome(service=service, options=driver_options)
|
|
||||||
case "firefox":
|
|
||||||
self._showTrace(f"Firefox 浏览器驱动初始化略慢, 请耐心等待...", no_log=True)
|
|
||||||
service = FirefoxService(executable_path=self.__driver_path)
|
|
||||||
self.__driver = webdriver.Firefox(service=service, options=driver_options)
|
|
||||||
case _: # actually will not happen, beacuse we have checked it at the initlization
|
|
||||||
# of 'driver_options'
|
|
||||||
raise Exception(f"不支持的浏览器驱动类型: {self.__driver_type} !")
|
|
||||||
self.__driver.implicitly_wait(1)
|
|
||||||
self.__driver.execute_script(
|
|
||||||
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
|
||||||
)
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"浏览器驱动初始化失败: {e}", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
self._showTrace(f"浏览器驱动已初始化, 类型: {self.__driver_type}, 路径: {self.__driver_path}")
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __initLibOperators(
|
|
||||||
self
|
|
||||||
):
|
|
||||||
|
|
||||||
if not self.__driver:
|
|
||||||
self._showTrace(f"浏览器驱动未初始化, 请先初始化浏览器驱动 !", self.TraceLevel.WARNING)
|
|
||||||
return
|
|
||||||
self.__lib_checker = LibChecker(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
self.__lib_login = LibLogin(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
self.__lib_logout = LibLogout(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
self.__lib_reserve = LibReserve(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
self.__lib_checkin = LibCheckin(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
self.__lib_renew = LibRenew(self._input_queue, self._output_queue, self.__driver)
|
|
||||||
|
|
||||||
def __waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# wait for page load
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until( # title contains "首页"
|
|
||||||
EC.title_contains("首页")
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # username field presence
|
|
||||||
EC.presence_of_element_located((By.NAME, "username"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # password field presence
|
|
||||||
EC.presence_of_element_located((By.NAME, "password"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # captcha field presence
|
|
||||||
EC.presence_of_element_located((By.NAME, "answer"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # captcha image presence
|
|
||||||
EC.presence_of_element_located((By.ID, "loadImgId"))
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace(f"登录页面加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __initDriverUrl(
|
|
||||||
self,
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
lib_config = self.__run_config.get("library", None)
|
|
||||||
if not lib_config:
|
|
||||||
self._showTrace("未配置图书馆参数 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
url = lib_config.get("host_url") + lib_config.get("login_url")
|
|
||||||
self.__driver.set_page_load_timeout(5)
|
|
||||||
try:
|
|
||||||
self.__driver.get(url)
|
|
||||||
except TimeoutException:
|
|
||||||
self.__driver.execute_script("window.stop();")
|
|
||||||
self._showTrace(
|
|
||||||
f"图书馆登录页面加载超时 ! 请检查网络环境是否正常", self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
if not self.__waitResponseLoad():
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __run(
|
|
||||||
self,
|
|
||||||
username: str,
|
|
||||||
password: str,
|
|
||||||
login_config: dict,
|
|
||||||
run_mode_config: dict,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> int:
|
|
||||||
|
|
||||||
# result : -1 - terminate, 0 - success, 1 - failed, 2 - passed
|
|
||||||
result = 2
|
|
||||||
|
|
||||||
# login
|
|
||||||
if not self.__lib_login.login(
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
login_config.get("max_attempt", 3),
|
|
||||||
login_config.get("auto_captcha", True),
|
|
||||||
):
|
|
||||||
return 1
|
|
||||||
# Here, we collect the run mode from the run config.
|
|
||||||
run_mode = run_mode_config.get("run_mode", 0)
|
|
||||||
run_mode = {
|
|
||||||
"auto_reserve": run_mode&0x1,
|
|
||||||
"auto_checkin": run_mode&0x2,
|
|
||||||
"auto_renewal": run_mode&0x4,
|
|
||||||
}
|
|
||||||
# reserve
|
|
||||||
if run_mode["auto_reserve"]:
|
|
||||||
if self.__lib_checker.canReserve(reserve_info.get("date")):
|
|
||||||
if self.__lib_reserve.reserve(username, reserve_info):
|
|
||||||
result = 0
|
|
||||||
else:
|
|
||||||
result = 1
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 无法预约, 已跳过")
|
|
||||||
result = 2
|
|
||||||
|
|
||||||
# checkin
|
|
||||||
last_result = result
|
|
||||||
if run_mode["auto_checkin"] and last_result != 1:
|
|
||||||
if self.__lib_checker.canCheckin():
|
|
||||||
if self.__lib_checkin.checkin(username):
|
|
||||||
result = 0
|
|
||||||
else:
|
|
||||||
result = 1
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 无法签到, 已跳过")
|
|
||||||
result = 2
|
|
||||||
if last_result == 0: # partly success
|
|
||||||
result = 0
|
|
||||||
|
|
||||||
# renewal
|
|
||||||
last_result = result
|
|
||||||
if run_mode["auto_renewal"] and last_result != 1:
|
|
||||||
can_renew, record = self.__lib_checker.canRenew()
|
|
||||||
if can_renew:
|
|
||||||
if self.__lib_renew.renew(username, record, reserve_info):
|
|
||||||
if self.__lib_checker.postRenewCheck(record):
|
|
||||||
self._showTrace(f"用户 {username} 续约成功 !")
|
|
||||||
result = 0
|
|
||||||
else:
|
|
||||||
if result != 1: # partly success
|
|
||||||
result = 0
|
|
||||||
else:
|
|
||||||
result = 1
|
|
||||||
else:
|
|
||||||
result = 1
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 无法续约, 已跳过")
|
|
||||||
result = 2
|
|
||||||
if last_result == 0: # partly success
|
|
||||||
result = 0
|
|
||||||
|
|
||||||
# logout
|
|
||||||
if not self.__lib_logout.logout(
|
|
||||||
username
|
|
||||||
):
|
|
||||||
# if logout is failed, we must make sure the host to be reloaded
|
|
||||||
# otherwise, the next login may fail
|
|
||||||
if not self.__initDriverUrl():
|
|
||||||
return -1
|
|
||||||
return result
|
|
||||||
|
|
||||||
def run(
|
|
||||||
self,
|
|
||||||
user_config: dict
|
|
||||||
):
|
|
||||||
|
|
||||||
self.__user_config = user_config
|
|
||||||
|
|
||||||
user_counter = {"current": 0, "success": 0, "failed": 0, "passed": 0}
|
|
||||||
users = self.__user_config["users"]
|
|
||||||
self._showTrace(f"共发现 {len(users)} 个用户")
|
|
||||||
for user in users:
|
|
||||||
user_counter["current"] += 1
|
|
||||||
self._showTrace(
|
|
||||||
f"正在处理第 {user_counter["current"]}/{len(users)} 个用户: {user["username"]}......",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
if not user["enabled"]:
|
|
||||||
self._showTrace(f"用户 {user["username"]} 已跳过")
|
|
||||||
user_counter["passed"] += 1
|
|
||||||
continue
|
|
||||||
r = self.__run(
|
|
||||||
username=user["username"],
|
|
||||||
password=user["password"],
|
|
||||||
login_config=self.__run_config["login"],
|
|
||||||
run_mode_config=self.__run_config["mode"],
|
|
||||||
reserve_info=user["reserve_info"],
|
|
||||||
)
|
|
||||||
if r == -1:
|
|
||||||
self._showTrace(
|
|
||||||
f"用户 {user["username"]} 处理过程中页面发生异常, 无法继续操作, 任务已终止 !",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
break
|
|
||||||
elif r == 0:
|
|
||||||
user_counter["success"] += 1
|
|
||||||
elif r == 1:
|
|
||||||
user_counter["failed"] += 1
|
|
||||||
elif r == 2:
|
|
||||||
user_counter["passed"] += 1
|
|
||||||
self._showTrace(f"处理完成, 共计 {user_counter["current"]} 个用户, "\
|
|
||||||
f"成功 {user_counter["success"]} 个用户, "\
|
|
||||||
f"失败 {user_counter["failed"]} 个用户, "\
|
|
||||||
f"跳过 {user_counter["passed"]} 个用户"
|
|
||||||
)
|
|
||||||
return
|
|
||||||
|
|
||||||
def close(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__driver:
|
|
||||||
if self.__driver_type.lower() == "firefox":
|
|
||||||
self._showTrace(
|
|
||||||
f"Firefox 浏览器驱动关闭略慢, 请耐心等待...",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
self.__driver.quit()
|
|
||||||
self.__driver = None
|
|
||||||
self._showTrace(f"浏览器驱动已关闭")
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self._showTrace(f"浏览器驱动未初始化, 无需关闭", no_log=True)
|
|
||||||
return False
|
|
||||||
@@ -1,365 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibChecker(LibOperator):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __formatDiffTime(
|
|
||||||
seconds: float
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
hours = int(seconds//3600)
|
|
||||||
minutes = int(seconds%3600//60)
|
|
||||||
seconds = int(seconds%60)
|
|
||||||
return f"{hours} 时 {minutes} 分 {seconds} 秒"
|
|
||||||
|
|
||||||
def __navigateToReserveRecordPage(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.XPATH, "//a[@href='/history?type=SEAT']"))
|
|
||||||
).click()
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CLASS_NAME, "myReserveList"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace("加载预约记录页面失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __decodeReserveTime(
|
|
||||||
self,
|
|
||||||
time_element
|
|
||||||
) -> dict:
|
|
||||||
|
|
||||||
time_str = time_element.text.strip()
|
|
||||||
today = datetime.now().date()
|
|
||||||
if "明天" in time_str:
|
|
||||||
target_date = today + timedelta(days=1)
|
|
||||||
date = target_date.strftime("%Y-%m-%d")
|
|
||||||
elif "今天" in time_str:
|
|
||||||
target_date = today
|
|
||||||
date = target_date.strftime("%Y-%m-%d")
|
|
||||||
elif "昨天" in time_str:
|
|
||||||
target_date = today - timedelta(days=1)
|
|
||||||
date = target_date.strftime("%Y-%m-%d")
|
|
||||||
else:
|
|
||||||
date_match = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", time_str)
|
|
||||||
if date_match:
|
|
||||||
date = date_match.group(1)
|
|
||||||
else:
|
|
||||||
date = ""
|
|
||||||
time_match = re.search(r"(\d{1,2}:\d{2}) -- (\d{1,2}:\d{2})", time_str)
|
|
||||||
if time_match:
|
|
||||||
begin_time = time_match.group(1)
|
|
||||||
end_time = time_match.group(2)
|
|
||||||
else:
|
|
||||||
begin_time = ""
|
|
||||||
end_time = ""
|
|
||||||
return {
|
|
||||||
"date": date,
|
|
||||||
"time": {
|
|
||||||
"begin": begin_time,
|
|
||||||
"end": end_time
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
def __decodeReserveInfo(
|
|
||||||
self,
|
|
||||||
info_elements
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
location = ""
|
|
||||||
status = ""
|
|
||||||
for info in info_elements:
|
|
||||||
if "已预约" in info.text:
|
|
||||||
status = "已预约"
|
|
||||||
elif "使用中" in info.text:
|
|
||||||
status = "使用中"
|
|
||||||
elif "已完成" in info.text:
|
|
||||||
status = "已完成"
|
|
||||||
elif "已结束使用" in info.text:
|
|
||||||
status = "已结束使用"
|
|
||||||
elif "已取消" in info.text:
|
|
||||||
status = "已取消"
|
|
||||||
elif "失约" in info.text:
|
|
||||||
status = "失约"
|
|
||||||
elif "图书馆" in info.text:
|
|
||||||
location = info.text.strip()
|
|
||||||
return {
|
|
||||||
"location": location,
|
|
||||||
"status": status,
|
|
||||||
}
|
|
||||||
|
|
||||||
def __decodeReserveRecord(
|
|
||||||
self,
|
|
||||||
reservation
|
|
||||||
) -> dict:
|
|
||||||
|
|
||||||
try:
|
|
||||||
time_element = reservation.find_element(
|
|
||||||
By.CSS_SELECTOR, "dt"
|
|
||||||
)
|
|
||||||
info_elements = reservation.find_elements(
|
|
||||||
By.CSS_SELECTOR, "a"
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
return {
|
|
||||||
"date": "",
|
|
||||||
"time": {"begin": "", "end": ""},
|
|
||||||
"info": {"location": "", "status": ""}
|
|
||||||
}
|
|
||||||
time = self.__decodeReserveTime(time_element)
|
|
||||||
info = self.__decodeReserveInfo(info_elements)
|
|
||||||
return {
|
|
||||||
"date": time["date"],
|
|
||||||
"time": time["time"],
|
|
||||||
"info": info
|
|
||||||
}
|
|
||||||
|
|
||||||
def __loadReserveRecords(
|
|
||||||
self
|
|
||||||
) -> list:
|
|
||||||
try:
|
|
||||||
# check if there's any reservation on the date
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CSS_SELECTOR, ".myReserveList > dl"))
|
|
||||||
)
|
|
||||||
reservations = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR, ".myReserveList > dl:not(#moreBlock)"
|
|
||||||
)
|
|
||||||
return reservations
|
|
||||||
except:
|
|
||||||
self._showTrace("加载预约记录失败 !", self.TraceLevel.ERROR)
|
|
||||||
return None
|
|
||||||
|
|
||||||
def __showMoreReserveRecords(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# load new reservations if still not sure
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 0.1).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, "moreBtn"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
# the reservation is the last one
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
more_btn = self.__driver.find_element(By.ID, "moreBtn")
|
|
||||||
if more_btn.is_displayed() and more_btn.is_enabled():
|
|
||||||
self.__driver.execute_script("arguments[0].scrollIntoView(true);", more_btn)
|
|
||||||
self.__driver.execute_script("arguments[0].click();", more_btn)
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self._showTrace("用户无法加载更多预约记录", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
except:
|
|
||||||
self._showTrace("加载更多预约记录失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __getReserveRecord(
|
|
||||||
self,
|
|
||||||
wanted_date: str,
|
|
||||||
wanted_status: str
|
|
||||||
) -> dict:
|
|
||||||
|
|
||||||
if wanted_date is None:
|
|
||||||
self._showTrace("日期未指定, 无法检查当前预约状态", self.TraceLevel.WARNING)
|
|
||||||
return None
|
|
||||||
self._showTrace(f"正在检查用户在 {wanted_date} 是否有预约状态为 {wanted_status} 的预约记录......", no_log=True)
|
|
||||||
|
|
||||||
checked_count = 0
|
|
||||||
max_check_times = 6 # we only check (4*(6-1)=)20 reservations, the last time cant be checked
|
|
||||||
|
|
||||||
if not self.__navigateToReserveRecordPage():
|
|
||||||
return None
|
|
||||||
for _ in range(max_check_times):
|
|
||||||
reservations = self.__loadReserveRecords()
|
|
||||||
if reservations is None:
|
|
||||||
return None
|
|
||||||
for reservation in reservations[checked_count:]:
|
|
||||||
record = self.__decodeReserveRecord(reservation)
|
|
||||||
checked_count += 1
|
|
||||||
if record is None:
|
|
||||||
continue
|
|
||||||
if record["date"] == "":
|
|
||||||
continue
|
|
||||||
if record["time"] == {"begin": "", "end": ""}:
|
|
||||||
continue
|
|
||||||
# record date is later than the given date, check the next one
|
|
||||||
if datetime.strptime(record["date"], "%Y-%m-%d").date() >\
|
|
||||||
datetime.strptime(wanted_date, "%Y-%m-%d").date():
|
|
||||||
continue
|
|
||||||
# record date is earlier than the given date, so there is no wanted record
|
|
||||||
if datetime.strptime(record["date"], "%Y-%m-%d").date() <\
|
|
||||||
datetime.strptime(wanted_date, "%Y-%m-%d").date():
|
|
||||||
return None
|
|
||||||
if record["info"]["status"] == wanted_status:
|
|
||||||
self._showTrace(
|
|
||||||
f"寻找到用户第 {checked_count} 条状态为 {wanted_status} 的预约记录, "
|
|
||||||
f"详细信息: {record["date"]} "
|
|
||||||
f"{record["time"]["begin"]} - {record["time"]["end"]} {record["info"]["location"]}",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
return record
|
|
||||||
if not self.__showMoreReserveRecords():
|
|
||||||
break
|
|
||||||
return None
|
|
||||||
|
|
||||||
def canReserve(
|
|
||||||
self,
|
|
||||||
date: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# no reserved or using record in the given date
|
|
||||||
# then can reserve
|
|
||||||
if self.__getReserveRecord(date, "已预约") is None:
|
|
||||||
if self.__getReserveRecord(date, "使用中") is None:
|
|
||||||
self._showTrace(f"用户在 {date} 可以预约")
|
|
||||||
return True
|
|
||||||
self._showTrace(f"用户在 {date} 有使用中的预约, 无法预约")
|
|
||||||
return False
|
|
||||||
self._showTrace(f"用户在 {date} 已存在有效预约, 无法预约")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def canCheckin(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# only check the current date
|
|
||||||
date = time.strftime("%Y-%m-%d", time.localtime())
|
|
||||||
record = self.__getReserveRecord(date, "已预约")
|
|
||||||
if record is not None:
|
|
||||||
begin_time = record["time"]["begin"]
|
|
||||||
begin_time = datetime.strptime(f"{date} {begin_time}", "%Y-%m-%d %H:%M")
|
|
||||||
time_diff = datetime.now() - begin_time
|
|
||||||
time_diff_seconds = time_diff.total_seconds()
|
|
||||||
# before 30 minutes, cant checkin
|
|
||||||
if time_diff_seconds < -30*60:
|
|
||||||
self._showTrace(
|
|
||||||
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
|
||||||
f"当前距离预约开始时间还有 {self.__formatDiffTime(abs(time_diff_seconds))}, 无法签到"
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
# before in 30 minutes, can checkin
|
|
||||||
elif -30*60 <= time_diff_seconds < 0:
|
|
||||||
self._showTrace(
|
|
||||||
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
|
||||||
f"当前距离预约开始时间还有 {self.__formatDiffTime(abs(time_diff_seconds))}, 可以签到"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
# past less than 30 minutes, can checkin
|
|
||||||
elif 0 <= time_diff_seconds < 30*60 - 5: # spare 5 seconds for the checkin process
|
|
||||||
self._showTrace(
|
|
||||||
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
|
||||||
f"当前距离预约开始时间已经过去 {self.__formatDiffTime(abs(time_diff_seconds))}, 可以签到"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法签到")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def canRenew(
|
|
||||||
self
|
|
||||||
) -> tuple[bool, dict]:
|
|
||||||
|
|
||||||
# only check the current date
|
|
||||||
date = time.strftime("%Y-%m-%d", time.localtime())
|
|
||||||
record = self.__getReserveRecord(date, "使用中")
|
|
||||||
if record is not None:
|
|
||||||
end_time = record["time"]["end"]
|
|
||||||
end_time = datetime.strptime(f"{date} {end_time}", "%Y-%m-%d %H:%M")
|
|
||||||
time_diff = end_time - datetime.now()
|
|
||||||
time_diff_seconds = time_diff.total_seconds()
|
|
||||||
# a using record is definitely after the begin time
|
|
||||||
trace_msg = (
|
|
||||||
f"用户在 {date} 的预约结束时间为 {end_time}, "
|
|
||||||
f"当前距离预约结束时间还有 {self.__formatDiffTime(abs(time_diff_seconds))}"
|
|
||||||
)
|
|
||||||
if abs(time_diff_seconds) < 120*60:
|
|
||||||
self._showTrace(f"{trace_msg}, 可以续约")
|
|
||||||
return True, record
|
|
||||||
else:
|
|
||||||
self._showTrace(f"{trace_msg}, 无法续约")
|
|
||||||
return False, None # we do not need to return the record, because if current
|
|
||||||
# time is not available for renewal, the record is not required
|
|
||||||
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法续约")
|
|
||||||
return False, None
|
|
||||||
|
|
||||||
def postRenewCheck(
|
|
||||||
self,
|
|
||||||
record: dict
|
|
||||||
) -> bool:
|
|
||||||
"""
|
|
||||||
Check if the renew operation is successful
|
|
||||||
|
|
||||||
Args:
|
|
||||||
record (dict): The expected record after renewal
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
bool: True if the renew operation is successful, False otherwise
|
|
||||||
"""
|
|
||||||
# because the special circumstance that the renew operation
|
|
||||||
# do not show the success message or anything else,
|
|
||||||
# we need to check the record data to make sure the renew operation is successful.
|
|
||||||
|
|
||||||
# only check the given record date
|
|
||||||
date = record["date"]
|
|
||||||
act_record = self.__getReserveRecord(date, "使用中")
|
|
||||||
if act_record is not None:
|
|
||||||
if act_record["time"]["begin"] == record["time"]["begin"] and\
|
|
||||||
act_record["time"]["end"] == record["time"]["end"]:
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
f" 续约成功 !\n"\
|
|
||||||
f" 日 期 :{date}\n"\
|
|
||||||
f" 时 间 :{act_record["time"]["begin"]} - {act_record["time"]["end"]}\n"\
|
|
||||||
f" 位 置 :{act_record["info"]["location"]}\n"
|
|
||||||
f" 状 态 :{act_record["info"]["status"]}"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
f" 续约失败 !\n"\
|
|
||||||
f" 续约后结束时间为 {act_record["time"]["end"]},与预期结束时间 {record["time"]["end"]} 不符 !"
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法检查续约结果")
|
|
||||||
return False
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import time
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibCheckin(LibOperator):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CLASS_NAME, "ui_dialog"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CLASS_NAME, "resultMessage"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.CLASS_NAME, "btnOK"))
|
|
||||||
)
|
|
||||||
result_message_element = self.__driver.find_element(
|
|
||||||
By.CLASS_NAME, "resultMessage"
|
|
||||||
)
|
|
||||||
ok_btn = self.__driver.find_element(By.CLASS_NAME, "btnOK")
|
|
||||||
except:
|
|
||||||
self._showTrace("签到时发生未知错误 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
result_message = result_message_element.text
|
|
||||||
if "签到成功" in result_message:
|
|
||||||
try:
|
|
||||||
detail_elements = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR, ".resultMessage dd"
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
if detail_elements:
|
|
||||||
details = [element.text for element in detail_elements if element.text.strip()]
|
|
||||||
if len(details) >= 5:
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
f" 签到成功 !\n"\
|
|
||||||
f" {details[1]}\n"\
|
|
||||||
f" {details[2]}\n"\
|
|
||||||
f" {details[3]}\n"\
|
|
||||||
f" {details[4]}"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
" 签到成功 !\n"\
|
|
||||||
" 未获取到签到详情 !"
|
|
||||||
)
|
|
||||||
ok_btn.click()
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
failure_reason = result_message.replace("签到失败", "").strip()
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
" 签到失败 !\n"\
|
|
||||||
f" {failure_reason}"
|
|
||||||
)
|
|
||||||
ok_btn.click()
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __enableCheckinBtn(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
script = """
|
|
||||||
try {
|
|
||||||
var checkin_btn = document.getElementById('btnCheckIn');
|
|
||||||
if (checkin_btn) {
|
|
||||||
checkin_btn.classList.remove('disabled');
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
} catch (e) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
"""
|
|
||||||
result = self.__driver.execute_script(script)
|
|
||||||
time.sleep(0.1)
|
|
||||||
if result:
|
|
||||||
self._showTrace("签到按钮已启用", no_log=True)
|
|
||||||
else:
|
|
||||||
self._showTrace("签到按钮启用失败", self.TraceLevel.WARNING)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def checkin(
|
|
||||||
self,
|
|
||||||
username: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__driver is None:
|
|
||||||
self._showTrace("未提供有效 WebDriver 实例 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
checkin_btn = WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, "btnCheckIn"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"用户 {username} 签到界面加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
if "disabled" in checkin_btn.get_attribute("class"):
|
|
||||||
self._showTrace("签到按钮不可用, 可能不在场馆内, 正在尝试启用......", no_log=True)
|
|
||||||
if not self.__enableCheckinBtn():
|
|
||||||
self._showTrace(f"签到按钮启用失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
checkin_btn.click()
|
|
||||||
if self._waitResponseLoad():
|
|
||||||
self._showTrace(f"用户 {username} 签到成功 !", no_log=True)
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 签到失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
@@ -1,40 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import re
|
|
||||||
import time
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibCheckout(LibOperator):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
pass
|
|
||||||
@@ -1,207 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import queue
|
|
||||||
import base64
|
|
||||||
|
|
||||||
import ddddocr
|
|
||||||
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibLogin(LibOperator):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
self.__ddddocr = ddddocr.DdddOcr()
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# wait to verify login success
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until( # title contains "自选座位 :: 座位预约系统"
|
|
||||||
EC.title_contains("自选座位 :: 座位预约系统")
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # search button presence
|
|
||||||
EC.presence_of_element_located((By.ID, "search"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until( # select content presence
|
|
||||||
EC.presence_of_element_located((By.CLASS_NAME, "selectContent"))
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace(
|
|
||||||
f"登录页面加载失败 ! : 用户账号或者密码错误/验证码错误, 具体以页面提示为准",
|
|
||||||
self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __fillLogInElements(
|
|
||||||
self,
|
|
||||||
username: str,
|
|
||||||
password: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
# ensure elements presence and fill them
|
|
||||||
try:
|
|
||||||
username_element = self.__driver.find_element(By.NAME, "username")
|
|
||||||
username_element.clear()
|
|
||||||
username_element.send_keys(username)
|
|
||||||
password_element = self.__driver.find_element(By.NAME, "password")
|
|
||||||
password_element.clear()
|
|
||||||
password_element.send_keys(password)
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"用户名或密码填写失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __autoRecognizeCaptcha(
|
|
||||||
self
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
# auto recognize captcha
|
|
||||||
try:
|
|
||||||
captcha_img = self.__driver.find_element(By.ID, "loadImgId")
|
|
||||||
img_src = captcha_img.get_attribute("src")
|
|
||||||
base64_str = img_src.split(',', 1)[1]
|
|
||||||
captcha_img = base64.b64decode(base64_str)
|
|
||||||
captcha_text = self.__ddddocr.classification(captcha_img)
|
|
||||||
captcha_text = ''.join(filter(str.isalnum, captcha_text)).lower()
|
|
||||||
self._showTrace(f"识别到验证码为 : '{captcha_text}'", no_log=True)
|
|
||||||
if len(captcha_text) != 4:
|
|
||||||
self._showLog("识别到的验证码长度不等于 4 个字符 !", self.TraceLevel.WARNING)
|
|
||||||
raise Exception("识别到的验证码长度不等于 4 个字符 !")
|
|
||||||
return captcha_text
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"验证码识别失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def __manualRecognizeCaptcha(
|
|
||||||
self
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
# manual recognize captcha
|
|
||||||
try:
|
|
||||||
self._showMsg("请输入验证码:")
|
|
||||||
captcha_text = self._waitMsg(timeout=15)
|
|
||||||
self._showTrace(f"输入的验证码为 : '{captcha_text}'", no_log=True)
|
|
||||||
if len(captcha_text) != 4:
|
|
||||||
self._showLog("输入的验证码长度不等于 4 个字符 !", self.TraceLevel.WARNING)
|
|
||||||
raise Exception("输入的验证码长度不等于 4 个字符 !")
|
|
||||||
return captcha_text
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"输入验证码失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def __refreshCaptcha(
|
|
||||||
self
|
|
||||||
):
|
|
||||||
|
|
||||||
# refresh captcha
|
|
||||||
try:
|
|
||||||
self._showTrace("刷新验证码......", no_log=True)
|
|
||||||
self.__driver.find_element(
|
|
||||||
By.ID, "loadImgId"
|
|
||||||
).click()
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"刷新验证码失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __solveCaptcha(
|
|
||||||
self,
|
|
||||||
auto_captcha: bool = True
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
max_attempts = 3 # the possibility of 3 times failed is less than (10%^3)
|
|
||||||
for _ in range(max_attempts):
|
|
||||||
if auto_captcha:
|
|
||||||
captcha_text = self.__autoRecognizeCaptcha()
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户未配置自动识别验证码, 请手动输入验证码 !", no_log=True)
|
|
||||||
captcha_text = self.__manualRecognizeCaptcha()
|
|
||||||
if captcha_text:
|
|
||||||
return captcha_text
|
|
||||||
else:
|
|
||||||
if not self.__refreshCaptcha():
|
|
||||||
return ""
|
|
||||||
self._showTrace(
|
|
||||||
f"验证码识别失败 {max_attempts} 次, 达到最大尝试次数 !",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def __fillCaptchaElement(
|
|
||||||
self,
|
|
||||||
captcha_text: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
captcha_element = self.__driver.find_element(By.NAME, "answer")
|
|
||||||
captcha_element.clear()
|
|
||||||
captcha_element.send_keys(captcha_text)
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"验证码填写失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def login(
|
|
||||||
self,
|
|
||||||
username: str,
|
|
||||||
password: str,
|
|
||||||
max_attempts: int = 5,
|
|
||||||
auto_captcha: bool = True
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__driver is None:
|
|
||||||
self._showTrace("未提供有效 WebDriver 实例 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
# begin login process
|
|
||||||
for attempt in range(max_attempts):
|
|
||||||
self._showTrace(f"用户 {username} 第 {attempt + 1} 次尝试登录......", no_log=True)
|
|
||||||
if not self.__fillLogInElements(
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
):
|
|
||||||
continue
|
|
||||||
captcha_text = self.__solveCaptcha(auto_captcha)
|
|
||||||
if not captcha_text:
|
|
||||||
continue
|
|
||||||
if not self.__fillCaptchaElement(captcha_text):
|
|
||||||
continue
|
|
||||||
self._showTrace("尝试登录...", no_log=True)
|
|
||||||
try:
|
|
||||||
self.__driver.find_element(
|
|
||||||
By.XPATH,
|
|
||||||
"//input[@type='button' and @value='登录']"
|
|
||||||
).click()
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"尝试登录失败 ! : {e}")
|
|
||||||
continue
|
|
||||||
if self._waitResponseLoad():
|
|
||||||
self._showTrace(f"用户 {username} 第 {attempt + 1} 次登录成功 !")
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 第 {attempt + 1} 次登录失败 !",self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
@@ -1,53 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibLogout(LibOperator):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
return True
|
|
||||||
|
|
||||||
def logout(
|
|
||||||
self,
|
|
||||||
username: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__driver is None:
|
|
||||||
self._showTrace("未提供有效 WebDriver 实例 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
self.__driver.find_element(
|
|
||||||
By.XPATH, "//a[@href='/logout']"
|
|
||||||
).click()
|
|
||||||
self._showTrace(f"用户 {username} 注销成功 !")
|
|
||||||
return True
|
|
||||||
except Exception as e:
|
|
||||||
self._showTrace(f"用户 {username} 注销失败 ! : {e}", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
@@ -1,199 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from operators.abs.LibTimeSelector import LibTimeSelector
|
|
||||||
|
|
||||||
|
|
||||||
class LibRenew(LibTimeSelector):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
self.__driver.refresh()
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __waitRenewDialog(
|
|
||||||
self
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.visibility_of_element_located((By.ID, "extendDiv"))
|
|
||||||
)
|
|
||||||
head_message = WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CSS_SELECTOR, "#extendDiv p.messageHead"))
|
|
||||||
)
|
|
||||||
result_message = WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CSS_SELECTOR, "#extendDiv div.resultMessage"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace("续约时间选择界面加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
head_message = head_message.text.strip()
|
|
||||||
if "警告" in head_message:
|
|
||||||
result_message = result_message.text.strip()
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
f" 续约失败 !\n"\
|
|
||||||
f" {result_message}", no_log=True)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_all_elements_located(
|
|
||||||
(By.CSS_SELECTOR, "#extendDiv .renewal_List li")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CSS_SELECTOR, "#extendDiv .btnOK"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace("续约时间选择界面加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __selectNearestTime(
|
|
||||||
self,
|
|
||||||
record: dict,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Select the nearest available renewal time.
|
|
||||||
"""
|
|
||||||
end_time = record["time"]["end"]
|
|
||||||
renew_info = reserve_info["renew_time"]
|
|
||||||
max_diff = renew_info["max_diff"]
|
|
||||||
prefer_earlier = renew_info["prefer_early"]
|
|
||||||
target_renew_mins = self._timeStrToMins(end_time) + renew_info["expect_duration"]*60
|
|
||||||
|
|
||||||
# Validate and adjust target renew time to library closing time
|
|
||||||
if not self.__validateAndAdjustRenewTime(end_time, target_renew_mins):
|
|
||||||
return False
|
|
||||||
renew_ok_btn = self.__driver.find_element(By.CSS_SELECTOR, "#extendDiv .btnOK")
|
|
||||||
renew_time_opts = self.__driver.find_elements(By.CSS_SELECTOR, "#extendDiv .renewal_List li")
|
|
||||||
if not renew_time_opts:
|
|
||||||
self._showTrace("当前未查询到可用续约时间 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
|
|
||||||
# Find best renewal time option
|
|
||||||
best_opt, best_text, actual_diff, free_times = self._findBestTimeOption(
|
|
||||||
renew_time_opts, target_renew_mins, max_diff, prefer_earlier, is_reserve=False
|
|
||||||
)
|
|
||||||
if best_opt is not None:
|
|
||||||
return self.__confirmRenewal(best_opt, best_text, actual_diff, record, renew_ok_btn)
|
|
||||||
self._showTrace(
|
|
||||||
"无法选择最近的可用续约时间 ! "
|
|
||||||
f"所有可选时间与目标时间相差都超过了 {max_diff} 分钟 !",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
self._showTrace(f"当前可供续约的时间有: {free_times}")
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __validateAndAdjustRenewTime(
|
|
||||||
self,
|
|
||||||
end_time: str,
|
|
||||||
target_renew_mins: int
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Validate and adjust renewal time to library closing time if needed.
|
|
||||||
"""
|
|
||||||
LIBRARY_CLOSE_TIME = 1410 # 23:30 in minutes
|
|
||||||
if target_renew_mins > LIBRARY_CLOSE_TIME:
|
|
||||||
actual_renew_duration = LIBRARY_CLOSE_TIME - self._timeStrToMins(end_time)
|
|
||||||
if actual_renew_duration <= 0:
|
|
||||||
self._showTrace(f"当前结束时间 {end_time} 已接近闭馆时间,无法续约 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
self._showTrace(
|
|
||||||
f"续约时间已调整至闭馆时间 {self._minsToTimeStr(LIBRARY_CLOSE_TIME)},"
|
|
||||||
f"实际续约时长为 {actual_renew_duration//60} 小时 {actual_renew_duration%60} 分钟"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __confirmRenewal(
|
|
||||||
self,
|
|
||||||
best_opt,
|
|
||||||
best_text: str,
|
|
||||||
actual_diff: int,
|
|
||||||
record: dict,
|
|
||||||
ok_btn
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Confirm the selected renewal time.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
best_opt.click()
|
|
||||||
abs_diff = abs(actual_diff)
|
|
||||||
time_relation = self._formatTimeRelation(abs_diff, actual_diff, "续约时间")
|
|
||||||
self._showTrace(
|
|
||||||
f"选择距离期望续约时间最近的 {best_text}, "
|
|
||||||
f"与期望续约时间相比 {time_relation}"
|
|
||||||
)
|
|
||||||
record["time"]["end"] = best_text.strip()
|
|
||||||
ok_btn.click()
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace("确认续约时发生错误 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def renew(
|
|
||||||
self,
|
|
||||||
username: str,
|
|
||||||
record: dict,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__driver is None:
|
|
||||||
self._showTrace("未提供有效 WebDriver 实例 !", self.TraceLevel.WARNING)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
renew_btn = WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, "btnExtend"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"用户 {username} 续约界面加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
if "disabled" in renew_btn.get_attribute("class"):
|
|
||||||
self._showLog(f"用户 {username} 续约按钮不可用, 可能不在场馆内")
|
|
||||||
self._showTrace(f"用户 {username} 续约按钮不可用, 可能不在场馆内, 请连接图书馆网络后重试", no_log=True)
|
|
||||||
return False
|
|
||||||
renew_btn.click()
|
|
||||||
if not self.__waitRenewDialog():
|
|
||||||
self._showTrace(f"用户 {username} 续约失败 !", self.TraceLevel.ERROR)
|
|
||||||
|
|
||||||
# After the renewal, the webpage will display a mask overlay,
|
|
||||||
# so we need to refresh the page for subsequent operations.
|
|
||||||
self.__driver.refresh()
|
|
||||||
return False
|
|
||||||
if not self.__selectNearestTime(record, reserve_info):
|
|
||||||
self._showTrace(f"用户 {username} 续约失败 !", self.TraceLevel.ERROR)
|
|
||||||
self.__driver.refresh()
|
|
||||||
return False
|
|
||||||
if self._waitResponseLoad():
|
|
||||||
return True
|
|
||||||
@@ -1,674 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2025 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import time
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from selenium.webdriver.common.by import By
|
|
||||||
from selenium.webdriver.chrome.webdriver import WebDriver
|
|
||||||
from selenium.webdriver.support.ui import WebDriverWait
|
|
||||||
from selenium.webdriver.support import expected_conditions as EC
|
|
||||||
|
|
||||||
from operators.abs.LibTimeSelector import LibTimeSelector
|
|
||||||
|
|
||||||
|
|
||||||
class LibReserve(LibTimeSelector):
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue,
|
|
||||||
driver: WebDriver
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
self.__driver = driver
|
|
||||||
# library floor and room mapping in website
|
|
||||||
self.__floor_map = {
|
|
||||||
"2": "二层",
|
|
||||||
"3": "三层",
|
|
||||||
"4": "四层",
|
|
||||||
"5": "五层"
|
|
||||||
}
|
|
||||||
self.__room_map = {
|
|
||||||
"1": "二层内环",
|
|
||||||
"2": "二层西区",
|
|
||||||
"3": "三层内环",
|
|
||||||
"4": "三层外环",
|
|
||||||
"5": "四层内环",
|
|
||||||
"6": "四层外环",
|
|
||||||
"7": "四层期刊",
|
|
||||||
"8": "五层考研"
|
|
||||||
}
|
|
||||||
|
|
||||||
def _waitResponseLoad(
|
|
||||||
self,
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CLASS_NAME, "layoutSeat"))
|
|
||||||
)
|
|
||||||
title_elements = []
|
|
||||||
# reserve failed without title elements, so we need to try
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.CSS_SELECTOR, ".layoutSeat dt"))
|
|
||||||
)
|
|
||||||
title_elements = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR, ".layoutSeat dt"
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
content_elements = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR, ".layoutSeat dd"
|
|
||||||
)
|
|
||||||
if not content_elements:
|
|
||||||
self._showTrace("未找到预约结果", self.TraceLevel.WARNING)
|
|
||||||
raise
|
|
||||||
title = title_elements[0].text if title_elements else ""
|
|
||||||
contents = [element.text for element in content_elements if element.text.strip()]
|
|
||||||
for message in contents:
|
|
||||||
if "预约失败" in message or "已有1个有效预约" in message:
|
|
||||||
self._showTrace(f"预约失败 - {"".join(contents)}", self.TraceLevel.ERROR)
|
|
||||||
raise
|
|
||||||
if "预定好了" in title or "预约成功" in title or "操作成功" in title:
|
|
||||||
if len(contents) >= 6:
|
|
||||||
self._showTrace(f"\n"\
|
|
||||||
f" 预约成功 !\n"\
|
|
||||||
f" {contents[1]}\n"\
|
|
||||||
f" {contents[2]}\n"\
|
|
||||||
f" {contents[3]}\n"\
|
|
||||||
f" 签到时间 :{contents[5]}"
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
self._showTrace("\n"\
|
|
||||||
" 预约成功 !\n"\
|
|
||||||
" 未找获取到详细信息"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace(f"预约结果加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __containRequiredInfo(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
# must contain the required infomation
|
|
||||||
# key 'place' is no need to check
|
|
||||||
# because 'place' is only has one possible value '1' or '图书馆'
|
|
||||||
if reserve_info.get("floor") is None: # if existence ?
|
|
||||||
raise ValueError("未指定楼层")
|
|
||||||
if reserve_info["floor"] not in self.__floor_map: # if in the mao ?
|
|
||||||
raise ValueError(f"该楼层 '{reserve_info['floor']}' 不存在")
|
|
||||||
if reserve_info.get("room") is None:
|
|
||||||
raise ValueError("未指定房间")
|
|
||||||
if reserve_info["room"] not in self.__room_map:
|
|
||||||
raise ValueError(f"该房间 '{reserve_info['room']}' 不存在")
|
|
||||||
if reserve_info.get("seat_id") is None:
|
|
||||||
raise ValueError("未指定座位")
|
|
||||||
if reserve_info["seat_id"] == "":
|
|
||||||
raise ValueError("未指定座位号")
|
|
||||||
return True
|
|
||||||
except ValueError as e:
|
|
||||||
self._showTrace(
|
|
||||||
f"预约信息错误 ! : {e}, "\
|
|
||||||
f"由于缺少必要的预约信息, 无法开始预约流程",
|
|
||||||
self.TraceLevel.ERROR
|
|
||||||
)
|
|
||||||
self._showTrace(
|
|
||||||
f"预约信息错误 ! : {e}, "\
|
|
||||||
f"由于缺少必要的预约信息, 无法开始预约流程, 请检查预约信息是否完整",
|
|
||||||
no_log=True
|
|
||||||
)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __isValidDate(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
cur_date_str = time.strftime("%Y-%m-%d", time.localtime())
|
|
||||||
cur_timestamp = time.mktime(time.strptime(cur_date_str, "%Y-%m-%d"))
|
|
||||||
if reserve_info.get("date") is None:
|
|
||||||
reserve_info["date"] = cur_date_str
|
|
||||||
self._showTrace(f"预约日期未指定, 自动设置为当前日期: {cur_date_str}")
|
|
||||||
else:
|
|
||||||
res_timestamp = time.mktime(time.strptime(reserve_info["date"], "%Y-%m-%d"))
|
|
||||||
if res_timestamp < cur_timestamp:
|
|
||||||
self._showTrace(
|
|
||||||
f"预约日期错误 ! :"\
|
|
||||||
f"{reserve_info['date']} 早于当前日期 {cur_date_str}, 自动设置为当前日期",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
reserve_info["date"] = cur_date_str
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __isValidBeginTime(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
cur_time = time.strftime("%H:%M", time.localtime())
|
|
||||||
if reserve_info.get("begin_time") is None:
|
|
||||||
reserve_info["begin_time"] = {}
|
|
||||||
if "time" not in reserve_info["begin_time"]:
|
|
||||||
reserve_info["begin_time"]["time"] = cur_time
|
|
||||||
self._showTrace(f"开始时间未指定, 自动设置为当前时间: {cur_time}")
|
|
||||||
if "max_diff" not in reserve_info["begin_time"]:
|
|
||||||
reserve_info["begin_time"]["max_diff"] = 30
|
|
||||||
self._showTrace(f"开始时间最大时间差未指定, 自动设置为 30 分钟")
|
|
||||||
if "prefer_early" not in reserve_info["begin_time"]:
|
|
||||||
reserve_info["begin_time"]["prefer_early"] = True
|
|
||||||
self._showTrace(f"是否优先选择更早开始时间未指定, 自动设置为 True")
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __isValidExpectDuration(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if reserve_info.get("satisfy_duration") is None:
|
|
||||||
reserve_info["satisfy_duration"] = True
|
|
||||||
self._showTrace("预约满足时长要求未指定, 默认满足")
|
|
||||||
if reserve_info["satisfy_duration"]:
|
|
||||||
if reserve_info.get("expect_duration") is None:
|
|
||||||
reserve_info["expect_duration"] = 4
|
|
||||||
self._showTrace("需要满足预约持续时间, 但未指定, 使用默认时长为 4 小时")
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __isValidEndTime(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if reserve_info.get("end_time") is None:
|
|
||||||
reserve_info["end_time"] = {}
|
|
||||||
if "time" not in reserve_info["end_time"]:
|
|
||||||
# here we add the expect duration to the begin time first,
|
|
||||||
# the edge case that the end time is later than 23:30 will
|
|
||||||
# be handled in __finalCheck. so no need to concern about it.
|
|
||||||
end_mins = self._timeStrToMins(reserve_info["begin_time"]["time"])
|
|
||||||
end_mins = end_mins + int(reserve_info["expect_duration"]*60)
|
|
||||||
reserve_info["end_time"] = {
|
|
||||||
"time": self._minsToTimeStr(end_mins),
|
|
||||||
"max_diff": 30,
|
|
||||||
"prefer_early": False
|
|
||||||
}
|
|
||||||
self._showTrace(
|
|
||||||
f"结束时间未指定, 自动设置为开始时间加上期望时长: {reserve_info['end_time']['time']}"
|
|
||||||
)
|
|
||||||
if "max_diff" not in reserve_info["end_time"]:
|
|
||||||
reserve_info["end_time"]["max_diff"] = 30
|
|
||||||
self._showTrace(f"结束时间最大时间差未指定, 自动设置为 30 分钟")
|
|
||||||
if "prefer_early" not in reserve_info["end_time"]:
|
|
||||||
reserve_info["end_time"]["prefer_early"] = False
|
|
||||||
self._showTrace(f"是否优先选择较晚结束时间未指定, 自动设置为 True")
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __finalCheck(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
):
|
|
||||||
|
|
||||||
begin_time, end_time = reserve_info["begin_time"], reserve_info["end_time"]
|
|
||||||
begin_mins = self._timeStrToMins(begin_time["time"])
|
|
||||||
end_mins = self._timeStrToMins(end_time["time"])
|
|
||||||
|
|
||||||
# if end time is earlier than begin_time, exchange them
|
|
||||||
# except that the user has set the satisfy_duration to True
|
|
||||||
if end_mins < begin_mins and reserve_info["satisfy_duration"] is False:
|
|
||||||
self._showTrace(
|
|
||||||
f"结束时间 {end_time['time']} 早于开始时间 {begin_time['time']}, 尝试交换时间",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
reserve_info["end_time"], reserve_info["begin_time"] = begin_time, end_time
|
|
||||||
begin_time, end_time = end_time, begin_time
|
|
||||||
begin_mins = self._timeStrToMins(begin_time["time"])
|
|
||||||
end_mins = self._timeStrToMins(end_time["time"])
|
|
||||||
|
|
||||||
# ensure the end time is not later than 23:30
|
|
||||||
max_end_mins = self._timeStrToMins("23:30")
|
|
||||||
if end_mins > max_end_mins:
|
|
||||||
self._showTrace(
|
|
||||||
f"结束时间 {end_time['time']} 晚于 23:30, 自动设置为 23:30",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
reserve_info["end_time"]["time"] = "23:30"
|
|
||||||
end_mins = max_end_mins
|
|
||||||
|
|
||||||
# ensure the duration is not longer than 8 hours
|
|
||||||
if reserve_info["satisfy_duration"]:
|
|
||||||
if reserve_info["expect_duration"] > 8:
|
|
||||||
self._showTrace(
|
|
||||||
f"该用户设置了优先满足时长要求, 但是预约期望持续时间 "
|
|
||||||
f"{reserve_info['expect_duration']} 小时 "
|
|
||||||
f"超出最大时长 8 小时, 自动设置为 8 小时",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
reserve_info["expect_duration"] = 8
|
|
||||||
else:
|
|
||||||
if end_mins - begin_mins > 8*60:
|
|
||||||
self._showTrace(
|
|
||||||
f"该用户未设置优先满足时长要求, 但是检查到预约持续时间 "
|
|
||||||
f"{float((end_mins - begin_mins)/60)} 小时 "
|
|
||||||
f"超出最大时长 8 小时, 自动设置为 8 小时",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
reserve_info["end_time"]["time"] = self._minsToTimeStr(begin_mins + 8*60)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __checkReserveInfo(
|
|
||||||
self,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if not self.__containRequiredInfo(reserve_info):
|
|
||||||
return False
|
|
||||||
if not self.__isValidDate(reserve_info):
|
|
||||||
return False
|
|
||||||
if not self.__isValidBeginTime(reserve_info):
|
|
||||||
return False
|
|
||||||
if not self.__isValidExpectDuration(reserve_info):
|
|
||||||
return False
|
|
||||||
if not self.__isValidEndTime(reserve_info):
|
|
||||||
return False
|
|
||||||
if not self.__finalCheck(reserve_info):
|
|
||||||
return False
|
|
||||||
self._showTrace(
|
|
||||||
f"预约信息检查完成, 准备预约 "
|
|
||||||
f"{reserve_info['date']} "
|
|
||||||
f"{reserve_info['begin_time']['time']} - "
|
|
||||||
f"{reserve_info['end_time']['time']} "
|
|
||||||
f"图书馆 "
|
|
||||||
f"{self.__floor_map[reserve_info['floor']]} "
|
|
||||||
f"{self.__room_map[reserve_info['room']]} "
|
|
||||||
f"的座位 {reserve_info['seat_id']}"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __clickElement(
|
|
||||||
self,
|
|
||||||
trigger_locator: tuple,
|
|
||||||
fail_msg: str,
|
|
||||||
success_msg: str,
|
|
||||||
option_locator: tuple = None
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
# click the trigger element
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable(trigger_locator)
|
|
||||||
).click()
|
|
||||||
if option_locator:
|
|
||||||
# select the option element if specified
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable(option_locator)
|
|
||||||
).click()
|
|
||||||
self._showTrace(success_msg)
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace(fail_msg)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __clickElementByJS(
|
|
||||||
self,
|
|
||||||
trigger_locator_id: str,
|
|
||||||
option_query_selector: str,
|
|
||||||
fail_msg: str,
|
|
||||||
success_msg: str,
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
script = f"""
|
|
||||||
try {{
|
|
||||||
var trigger = document.getElementById('{trigger_locator_id}');
|
|
||||||
if (trigger) {{
|
|
||||||
trigger.click();
|
|
||||||
var option = document.querySelector("{option_query_selector}");
|
|
||||||
if (option) {{
|
|
||||||
option.click();
|
|
||||||
return true;
|
|
||||||
}}
|
|
||||||
return false;
|
|
||||||
}}
|
|
||||||
return false;
|
|
||||||
}} catch (e) {{
|
|
||||||
return false;
|
|
||||||
}}
|
|
||||||
"""
|
|
||||||
result = self.__driver.execute_script(script)
|
|
||||||
time.sleep(0.1)
|
|
||||||
if result:
|
|
||||||
self._showTrace(success_msg)
|
|
||||||
else:
|
|
||||||
self._showTrace(fail_msg)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def __selectDate(
|
|
||||||
self,
|
|
||||||
date_str: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
if self.__clickElementByJS(
|
|
||||||
trigger_locator_id="onDate_select",
|
|
||||||
option_query_selector=f"p#options_onDate a[value='{date_str}']",
|
|
||||||
success_msg=f"日期 {date_str} 选择成功 !",
|
|
||||||
fail_msg=f"选择日期失败 ! : {date_str} 不可用"
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
return self.__clickElement(
|
|
||||||
trigger_locator=(By.ID, "onDate_select"),
|
|
||||||
option_locator=(By.XPATH, f"//p[@id='options_onDate']/a[@value='{date_str}']"),
|
|
||||||
success_msg=f"日期 {date_str} 选择成功 !",
|
|
||||||
fail_msg=f"选择日期失败 ! : {date_str} 不可用"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __selectPlace(
|
|
||||||
self,
|
|
||||||
place: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
place = "1" # the library only have this place :)
|
|
||||||
display_place = "图书馆"
|
|
||||||
if self.__clickElementByJS(
|
|
||||||
trigger_locator_id="display_building",
|
|
||||||
option_query_selector=f"p#options_building a[value='{place}']",
|
|
||||||
success_msg=f"预约场所 {display_place} 选择成功 !",
|
|
||||||
fail_msg=f"选择预约场所失败 ! : {display_place} 不可用"
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
return self.__clickElement(
|
|
||||||
trigger_locator=(By.ID, "display_building"),
|
|
||||||
option_locator=(By.XPATH, f"//p[@id='options_building']/a[@value='{place}']"),
|
|
||||||
success_msg=f"预约场所 {display_place} 选择成功 !",
|
|
||||||
fail_msg=f"选择预约场所失败 ! : {display_place} 不可用"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __selectFloor(
|
|
||||||
self,
|
|
||||||
floor: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
display_floor = self.__floor_map.get(floor)
|
|
||||||
if self.__clickElementByJS(
|
|
||||||
trigger_locator_id="floor_select",
|
|
||||||
option_query_selector=f"p#options_floor a[value='{floor}']",
|
|
||||||
success_msg=f"楼层 {display_floor} 选择成功 !",
|
|
||||||
fail_msg=f"选择楼层失败 ! : {display_floor} 不可用"
|
|
||||||
):
|
|
||||||
return True
|
|
||||||
return self.__clickElement(
|
|
||||||
trigger_locator=(By.ID, "floor_select"),
|
|
||||||
option_locator=(By.XPATH, f"//p[@id='options_floor']/a[@value='{floor}']"),
|
|
||||||
success_msg=f"楼层 {display_floor} 选择成功 !",
|
|
||||||
fail_msg=f"选择楼层失败 ! : {display_floor} 不可用"
|
|
||||||
)
|
|
||||||
|
|
||||||
def __selectRoom(
|
|
||||||
self,
|
|
||||||
room: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
display_room = self.__room_map.get(room)
|
|
||||||
# find room
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, "findRoom"))
|
|
||||||
).click()
|
|
||||||
except:
|
|
||||||
self._showTrace("加载房间/区域失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
# select room
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, f"room_{room}"))
|
|
||||||
).click()
|
|
||||||
self._showTrace(f"房间 {display_room} 选择成功 !")
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
self._showTrace(f"选择房间失败 ! : {display_room} 不可用", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __selectSeat(
|
|
||||||
self,
|
|
||||||
seat_id: str
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
try:
|
|
||||||
# wait fot seat layout element to load
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.ID, "seatLayout"))
|
|
||||||
)
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_all_elements_located((By.CSS_SELECTOR, "li[id^='seat_']"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"座位加载失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
try:
|
|
||||||
all_seats = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR, "li[id^='seat_']"
|
|
||||||
)
|
|
||||||
seat_id_upper = seat_id.lstrip('0').upper()
|
|
||||||
for seat in all_seats:
|
|
||||||
if not seat_id_upper == seat.text.lstrip('0'):
|
|
||||||
continue
|
|
||||||
seat_link = seat.find_element(By.TAG_NAME, "a")
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable(seat_link)
|
|
||||||
)
|
|
||||||
seat_link.click()
|
|
||||||
seat_status = seat_link.get_attribute("title")
|
|
||||||
self._showTrace(f"座位 {seat_id} 选择成功 ! : 当前状态 - '{seat_status}'")
|
|
||||||
return True
|
|
||||||
self._showLog(f"座位 {seat_id} 在该楼层区域中不存在", self.TraceLevel.WARNING)
|
|
||||||
self._showTrace(f"座位 {seat_id} 在该楼层区域中不存在, 请检查座位号是否正确", no_log=True)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"座位选择失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
|
|
||||||
def __selectNearestTime(
|
|
||||||
self,
|
|
||||||
time_id: str,
|
|
||||||
time_type: str,
|
|
||||||
target_time: int,
|
|
||||||
max_time_diff: int = 30,
|
|
||||||
prefer_earlier: bool = True
|
|
||||||
) -> int:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Select the nearest available time option.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
int: The actual selected time value in minutes.
|
|
||||||
"""
|
|
||||||
# Wait for time options to load
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_all_elements_located(
|
|
||||||
(By.CSS_SELECTOR, f"#{time_id} ul li a")
|
|
||||||
)
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"{time_type} 选择失败 ! : 当前未查询到可用时间", self.TraceLevel.ERROR)
|
|
||||||
return -1
|
|
||||||
|
|
||||||
# Find best time option
|
|
||||||
all_time_opts = self.__driver.find_elements(
|
|
||||||
By.CSS_SELECTOR,
|
|
||||||
f"#{time_id} ul li a"
|
|
||||||
)
|
|
||||||
if not all_time_opts:
|
|
||||||
self._showTrace(f"{time_type} 选择失败 ! : 当前未查询到可用时间", self.TraceLevel.ERROR)
|
|
||||||
return -1
|
|
||||||
best_opt, best_text, actual_diff, free_times = self._findBestTimeOption(
|
|
||||||
all_time_opts, target_time, max_time_diff, prefer_earlier, is_reserve=True
|
|
||||||
)
|
|
||||||
if best_opt is not None:
|
|
||||||
best_opt.click()
|
|
||||||
abs_diff = abs(actual_diff)
|
|
||||||
time_relation = self._formatTimeRelation(abs_diff, actual_diff, time_type)
|
|
||||||
target_time += actual_diff
|
|
||||||
self._showTrace(
|
|
||||||
f"选择距离期望 {time_type} 最近的 {best_text}, "
|
|
||||||
f"与期望 {time_type} 相比 {time_relation}"
|
|
||||||
)
|
|
||||||
return target_time
|
|
||||||
self._showTrace(
|
|
||||||
f"无法选择最近的 {time_type} {self._minsToTimeStr(target_time)}, "
|
|
||||||
f"所有可选时间与目标时间相差都超过 {max_time_diff} 分钟", self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
self._showTrace(f"当前可供预约的 {time_type} 有: {free_times}")
|
|
||||||
return -1
|
|
||||||
|
|
||||||
def __selectSeatTime(
|
|
||||||
self,
|
|
||||||
begin_time: dict,
|
|
||||||
end_time: dict,
|
|
||||||
expect_duration: int = 4,
|
|
||||||
satisfy_duration: bool = True
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Select seat begin and end time.
|
|
||||||
"""
|
|
||||||
exp_beg_tm_str = begin_time["time"]
|
|
||||||
exp_end_tm_str = end_time["time"]
|
|
||||||
# Initialize actual time strings for logging
|
|
||||||
act_beg_tm_str = exp_beg_tm_str
|
|
||||||
act_end_tm_str = exp_end_tm_str
|
|
||||||
exp_beg_mins = self._timeStrToMins(exp_beg_tm_str)
|
|
||||||
act_beg_mins = exp_beg_mins
|
|
||||||
exp_end_mins = self._timeStrToMins(exp_end_tm_str)
|
|
||||||
act_end_mins = exp_end_mins
|
|
||||||
|
|
||||||
# Select begin time
|
|
||||||
act_beg_mins = self.__selectNearestTime(
|
|
||||||
time_id="startTime",
|
|
||||||
time_type="开始时间",
|
|
||||||
target_time=exp_beg_mins,
|
|
||||||
max_time_diff=begin_time["max_diff"],
|
|
||||||
prefer_earlier=begin_time["prefer_early"]
|
|
||||||
)
|
|
||||||
if act_beg_mins == -1:
|
|
||||||
return False
|
|
||||||
act_beg_tm_str = self._minsToTimeStr(act_beg_mins)
|
|
||||||
|
|
||||||
# If 'satisfy_duration' is True, select end time based on actual begin time
|
|
||||||
if satisfy_duration:
|
|
||||||
exp_end_mins = int(self.__validateAndAdjustEndTime(act_beg_mins, expect_duration))
|
|
||||||
exp_end_tm_str = self._minsToTimeStr(exp_end_mins)
|
|
||||||
self._showTrace(
|
|
||||||
f"需要满足期望预约持续时间: {expect_duration} 小时, "
|
|
||||||
f"根据开始时间 {act_beg_tm_str} 计算结束时间: {exp_end_tm_str}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Select end time
|
|
||||||
act_end_mins = self.__selectNearestTime(
|
|
||||||
time_id="endTime",
|
|
||||||
time_type="结束时间",
|
|
||||||
target_time=exp_end_mins,
|
|
||||||
max_time_diff=end_time["max_diff"],
|
|
||||||
prefer_earlier=end_time["prefer_early"]
|
|
||||||
)
|
|
||||||
if act_end_mins == -1:
|
|
||||||
return False
|
|
||||||
act_end_tm_str = self._minsToTimeStr(act_end_mins)
|
|
||||||
self._showTrace(
|
|
||||||
f"期望预约时间段: {exp_beg_tm_str} - {exp_end_tm_str}, "
|
|
||||||
f"实际预约时间段: {act_beg_tm_str} - {act_end_tm_str}"
|
|
||||||
)
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __validateAndAdjustEndTime(
|
|
||||||
self,
|
|
||||||
begin_mins: int,
|
|
||||||
duration: int
|
|
||||||
) -> int:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Validate and adjust reserve end time to library closing time if needed.
|
|
||||||
"""
|
|
||||||
LIBRARY_CLOSE_TIME = self._timeStrToMins("23:30")
|
|
||||||
expect_end_mins = int(begin_mins + duration*60)
|
|
||||||
if expect_end_mins > LIBRARY_CLOSE_TIME:
|
|
||||||
expect_end_mins = LIBRARY_CLOSE_TIME
|
|
||||||
self._showTrace(
|
|
||||||
f"预约持续时间 {duration} 小时, 超过最大预约时间 23:30, 自动调整为 23:30",
|
|
||||||
self.TraceLevel.WARNING
|
|
||||||
)
|
|
||||||
return expect_end_mins
|
|
||||||
|
|
||||||
def reserve(
|
|
||||||
self,
|
|
||||||
username: str,
|
|
||||||
reserve_info: dict
|
|
||||||
) -> bool:
|
|
||||||
|
|
||||||
submit_reserve = False
|
|
||||||
reserve_success = False
|
|
||||||
have_hover_on_page = False
|
|
||||||
|
|
||||||
# reserve info
|
|
||||||
if not self.__checkReserveInfo(reserve_info):
|
|
||||||
return False
|
|
||||||
# map page
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.XPATH, "//a[@href='/map']"))
|
|
||||||
).click()
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.presence_of_element_located((By.ID, "seatLayout"))
|
|
||||||
)
|
|
||||||
except:
|
|
||||||
self._showTrace(f"加载预约选座页面失败 !", self.TraceLevel.ERROR)
|
|
||||||
return False
|
|
||||||
# date, place, floor, room
|
|
||||||
if not self.__selectDate(reserve_info["date"]):
|
|
||||||
return False
|
|
||||||
if not self.__selectPlace(reserve_info["place"]):
|
|
||||||
return False
|
|
||||||
if not self.__selectFloor(reserve_info["floor"]):
|
|
||||||
return False
|
|
||||||
if not self.__selectRoom(reserve_info["room"]):
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
have_hover_on_page = True
|
|
||||||
# seat selections
|
|
||||||
if not self.__selectSeat(reserve_info["seat_id"]):
|
|
||||||
pass
|
|
||||||
elif not self.__selectSeatTime(
|
|
||||||
begin_time=reserve_info["begin_time"],
|
|
||||||
end_time=reserve_info["end_time"],
|
|
||||||
expect_duration=reserve_info["expect_duration"],
|
|
||||||
satisfy_duration=reserve_info["satisfy_duration"]
|
|
||||||
):
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
try:
|
|
||||||
WebDriverWait(self.__driver, 2).until(
|
|
||||||
EC.element_to_be_clickable((By.ID, "reserveBtn"))
|
|
||||||
).click()
|
|
||||||
submit_reserve = True
|
|
||||||
if not self._waitResponseLoad():
|
|
||||||
raise
|
|
||||||
reserve_success = True
|
|
||||||
except:
|
|
||||||
self._showTrace(f"预约提交失败 !", self.TraceLevel.ERROR)
|
|
||||||
if not submit_reserve and have_hover_on_page:
|
|
||||||
self.__driver.refresh()
|
|
||||||
if reserve_success:
|
|
||||||
self._showTrace(f"用户 {username} 预约成功 !")
|
|
||||||
else:
|
|
||||||
self._showTrace(f"用户 {username} 预约失败 !", self.TraceLevel.ERROR)
|
|
||||||
return reserve_success
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
"""
|
|
||||||
Operators module for the AutoLibrary project.
|
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
|
||||||
- AutoLib: AutoLibrary operator.
|
|
||||||
- LibLogin: Library operator for logging in.
|
|
||||||
- LibLogout: Library operator for logging out.
|
|
||||||
- LibReserve: Library operator for reserving seat.
|
|
||||||
- LibCheckin: Library operator for checking in seat.
|
|
||||||
- LibCheckout: Library operator for checking out seat.
|
|
||||||
- LibChecker: Library operator for checking record status.
|
|
||||||
- LibRenew: Library operator for renewing seat.
|
|
||||||
"""
|
|
||||||
@@ -1,139 +0,0 @@
|
|||||||
# -*- coding: utf-8 -*-
|
|
||||||
"""
|
|
||||||
Copyright (c) 2026 KenanZhu.
|
|
||||||
All rights reserved.
|
|
||||||
|
|
||||||
This software is provided "as is", without any warranty of any kind.
|
|
||||||
You may use, modify, and distribute this file under the terms of the MIT License.
|
|
||||||
See the LICENSE file for details.
|
|
||||||
"""
|
|
||||||
import queue
|
|
||||||
|
|
||||||
from datetime import datetime
|
|
||||||
|
|
||||||
from base.LibOperator import LibOperator
|
|
||||||
|
|
||||||
|
|
||||||
class LibTimeSelector(LibOperator):
|
|
||||||
"""
|
|
||||||
Abstract base class for time selection operations.
|
|
||||||
|
|
||||||
This class provides common time selection logic for reservation and renewal
|
|
||||||
operations, including time conversion utilities and best time option finding.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
input_queue: queue.Queue,
|
|
||||||
output_queue: queue.Queue
|
|
||||||
):
|
|
||||||
|
|
||||||
super().__init__(input_queue, output_queue)
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _timeStrToMins(
|
|
||||||
time_str: str
|
|
||||||
) -> int:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Convert time string "HH:MM" to minutes since midnight.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
"10:00" -> 600
|
|
||||||
"13:30" -> 810
|
|
||||||
"""
|
|
||||||
hour, minute = map(int, time_str.split(":"))
|
|
||||||
return hour*60 + minute
|
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def _minsToTimeStr(
|
|
||||||
mins: int
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Convert minutes since midnight to time string "HH:MM".
|
|
||||||
|
|
||||||
Example:
|
|
||||||
600 -> "10:00"
|
|
||||||
810 -> "13:30"
|
|
||||||
"""
|
|
||||||
hour, minute = divmod(int(mins), 60)
|
|
||||||
return f"{hour:02d}:{minute:02d}"
|
|
||||||
|
|
||||||
def _formatTimeRelation(
|
|
||||||
self,
|
|
||||||
abs_diff: int,
|
|
||||||
actual_diff: int,
|
|
||||||
time_type: str
|
|
||||||
) -> str:
|
|
||||||
|
|
||||||
"""
|
|
||||||
Format time difference relation string.
|
|
||||||
"""
|
|
||||||
if actual_diff < 0:
|
|
||||||
return f"早了 {abs_diff} 分钟"
|
|
||||||
elif actual_diff > 0:
|
|
||||||
return f"晚了 {abs_diff} 分钟"
|
|
||||||
else:
|
|
||||||
return f"正好等于 {time_type}"
|
|
||||||
|
|
||||||
def _findBestTimeOption(
|
|
||||||
self,
|
|
||||||
time_options: list,
|
|
||||||
target_time: int,
|
|
||||||
max_time_diff: int,
|
|
||||||
prefer_earlier: bool,
|
|
||||||
is_reserve: bool = True
|
|
||||||
) -> tuple:
|
|
||||||
"""
|
|
||||||
Find the best time option from available times.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
time_options: List of WebElement time options
|
|
||||||
target_time: Target time in minutes
|
|
||||||
max_time_diff: Maximum acceptable time difference in minutes
|
|
||||||
prefer_earlier: If True, prefer earlier times when diffs are equal
|
|
||||||
is_reserve: If True, parse 'time' attribute; if False, parse 'id' attribute
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
Tuple of (best_time_element, best_time_text, actual_diff, free_times_list)
|
|
||||||
or (None, None, None, []) if no suitable option found
|
|
||||||
"""
|
|
||||||
free_times = []
|
|
||||||
best_time_diff = max_time_diff
|
|
||||||
best_actual_diff = None
|
|
||||||
best_time_opt = None
|
|
||||||
|
|
||||||
for time_opt in time_options:
|
|
||||||
# Parse time value based on context
|
|
||||||
if is_reserve:
|
|
||||||
# Reservation context: parse 'time' attribute
|
|
||||||
time_attr = time_opt.get_attribute("time")
|
|
||||||
if time_attr == "now":
|
|
||||||
now = datetime.now()
|
|
||||||
time_val = now.hour*60 + now.minute
|
|
||||||
elif time_attr and time_attr.isdigit():
|
|
||||||
time_val = int(time_attr)
|
|
||||||
else:
|
|
||||||
continue
|
|
||||||
else:
|
|
||||||
# Renewal context: parse 'id' attribute
|
|
||||||
time_attr = time_opt.get_attribute("id")
|
|
||||||
if not (time_attr and time_attr.isdigit()):
|
|
||||||
continue
|
|
||||||
time_val = int(time_attr)
|
|
||||||
free_times.append(time_opt.text.strip() if not is_reserve else self._minsToTimeStr(time_val))
|
|
||||||
actual_diff = time_val - target_time
|
|
||||||
abs_diff = abs(actual_diff)
|
|
||||||
|
|
||||||
# Update best option if current is better
|
|
||||||
if (abs_diff < best_time_diff or
|
|
||||||
(abs_diff == best_time_diff and
|
|
||||||
((prefer_earlier and actual_diff <= 0) or
|
|
||||||
(not prefer_earlier and actual_diff >= 0)))):
|
|
||||||
best_time_diff = abs_diff
|
|
||||||
best_actual_diff = actual_diff
|
|
||||||
best_time_opt = time_opt
|
|
||||||
if best_time_opt is not None:
|
|
||||||
return (best_time_opt, best_time_opt.text.strip(), best_actual_diff, free_times)
|
|
||||||
return (None, None, None, free_times)
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
"""
|
|
||||||
Abstract layer class of the LibOperator
|
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
|
||||||
- LibTimeSelector: Abstract base class for time selection operations.
|
|
||||||
"""
|
|
||||||
@@ -0,0 +1,396 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2025 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import os
|
||||||
|
import queue
|
||||||
|
from selenium import webdriver
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
TimeoutException,
|
||||||
|
WebDriverException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.edge.service import Service as EdgeService
|
||||||
|
from selenium.webdriver.chrome.service import Service as ChromeService
|
||||||
|
from selenium.webdriver.firefox.service import Service as FirefoxService
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.LoginPage import LoginPage
|
||||||
|
from pages.MainShell import MainShell
|
||||||
|
from pages.flows.ReserveFlow import ReserveFlow, ReserveContext
|
||||||
|
from pages.flows.CheckinFlow import CheckinFlow
|
||||||
|
from pages.flows.RenewFlow import RenewFlow
|
||||||
|
from pages.services.CaptchaSolver import CaptchaSolver
|
||||||
|
from pages.services.ReserveChecker import ReserveChecker
|
||||||
|
from pages.services.RecordChecker import RecordChecker
|
||||||
|
|
||||||
|
|
||||||
|
class AutoLib(MsgBase):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
run_config: dict,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self.__run_config: dict = run_config
|
||||||
|
self.__user_config: dict | None = None
|
||||||
|
self.__driver: WebDriver | None = None
|
||||||
|
self.__driver_type: str = ""
|
||||||
|
self.__driver_path: str = ""
|
||||||
|
self.__login_page: LoginPage = None
|
||||||
|
self.__shell: MainShell = None
|
||||||
|
self.__captcha_solver: CaptchaSolver = None
|
||||||
|
self.__record_checker: RecordChecker = None
|
||||||
|
self.__reserve_checker: ReserveChecker = None
|
||||||
|
self.__reserve_flow: ReserveFlow = None
|
||||||
|
self.__checkin_flow: CheckinFlow = None
|
||||||
|
self.__renew_flow: RenewFlow = None
|
||||||
|
|
||||||
|
if not self.__initBrowserDriver():
|
||||||
|
raise Exception("浏览器驱动初始化失败 !")
|
||||||
|
else:
|
||||||
|
if not self.__initDriverUrl():
|
||||||
|
self.close()
|
||||||
|
raise Exception("浏览器驱动 URL 初始化失败 !")
|
||||||
|
self.__initPagesServices()
|
||||||
|
self.__initPagesFlows()
|
||||||
|
|
||||||
|
def __initBrowserDriver(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
self._showTrace("正在初始化浏览器驱动......", no_log=True)
|
||||||
|
driver_config: dict = self.__run_config.get("web_driver", None)
|
||||||
|
self.__driver_type = driver_config.get("driver_type", "none")
|
||||||
|
self.__driver_type = self.__driver_type.lower()
|
||||||
|
match self.__driver_type:
|
||||||
|
case "edge":
|
||||||
|
driver_options = webdriver.EdgeOptions()
|
||||||
|
case "chrome":
|
||||||
|
driver_options = webdriver.ChromeOptions()
|
||||||
|
case "firefox":
|
||||||
|
driver_options = webdriver.FirefoxOptions()
|
||||||
|
case _:
|
||||||
|
self._showTrace(
|
||||||
|
f"不支持的浏览器驱动类型: {self.__driver_type} !",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
if not driver_config:
|
||||||
|
self._showTrace("未配置浏览器驱动参数 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
if driver_config.get("headless", False):
|
||||||
|
driver_options.add_argument("--headless")
|
||||||
|
driver_options.add_argument("--disable-gpu")
|
||||||
|
driver_options.add_argument("--no-sandbox")
|
||||||
|
driver_options.add_argument("--disable-dev-shm-usage")
|
||||||
|
|
||||||
|
# must be 1920x1080, otherwise the page will cause some elements not accessible
|
||||||
|
driver_options.add_argument("--window-size=1920,1080")
|
||||||
|
|
||||||
|
# omit ssl errors and verbose log level
|
||||||
|
driver_options.add_argument("--ignore-certificate-errors")
|
||||||
|
driver_options.add_argument("--ignore-ssl-errors")
|
||||||
|
driver_options.add_argument("--log-level=OFF")
|
||||||
|
driver_options.add_argument("--silent")
|
||||||
|
|
||||||
|
# set options for chrome and edge
|
||||||
|
if self.__driver_type.lower() in ["edge", "chrome"]:
|
||||||
|
driver_options.add_argument("--remote-allow-origins=*")
|
||||||
|
driver_options.add_experimental_option("excludeSwitches", ["enable-automation"])
|
||||||
|
driver_options.add_experimental_option("useAutomationExtension", False)
|
||||||
|
driver_options.add_argument("--disable-blink-features=AutomationControlled")
|
||||||
|
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "\
|
||||||
|
"AppleWebKit/537.36 (KHTML, like Gecko) "\
|
||||||
|
"Chrome/120.0.0.0 "\
|
||||||
|
"Safari/537.36"
|
||||||
|
if self.__driver_type == "edge":
|
||||||
|
user_agent += " Edg/120.0.0.0"
|
||||||
|
|
||||||
|
# set options for firefox
|
||||||
|
elif self.__driver_type == "firefox":
|
||||||
|
driver_options.set_preference("dom.webdriver.enabled", False)
|
||||||
|
driver_options.set_preference("useAutomationExtension", False)
|
||||||
|
user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:120.0) "\
|
||||||
|
"Gecko/20100101 Firefox/120.0"
|
||||||
|
driver_options.add_argument(f"user-agent={user_agent}")
|
||||||
|
|
||||||
|
# init browser driver
|
||||||
|
self.__driver_path = driver_config.get("driver_path", "")
|
||||||
|
if not self.__driver_path:
|
||||||
|
self._showTrace("未配置浏览器驱动路径 !", self.TraceLevel.WARNING)
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
self.__driver_path = os.path.abspath(self.__driver_path)
|
||||||
|
service = None
|
||||||
|
match self.__driver_type:
|
||||||
|
case "edge":
|
||||||
|
service = EdgeService(executable_path=self.__driver_path)
|
||||||
|
self.__driver = webdriver.Edge(service=service, options=driver_options)
|
||||||
|
case "chrome":
|
||||||
|
service = ChromeService(executable_path=self.__driver_path)
|
||||||
|
self.__driver = webdriver.Chrome(service=service, options=driver_options)
|
||||||
|
case "firefox":
|
||||||
|
self._showTrace("Firefox 浏览器驱动初始化略慢, 请耐心等待...", no_log=True)
|
||||||
|
service = FirefoxService(executable_path=self.__driver_path)
|
||||||
|
self.__driver = webdriver.Firefox(service=service, options=driver_options)
|
||||||
|
case _:
|
||||||
|
raise Exception(f"不支持的浏览器驱动类型: {self.__driver_type} !")
|
||||||
|
self.__driver.implicitly_wait(1)
|
||||||
|
self.__driver.execute_script(
|
||||||
|
"Object.defineProperty(navigator, 'webdriver', {get: () => undefined})"
|
||||||
|
)
|
||||||
|
except WebDriverException as e:
|
||||||
|
self._showTrace(f"浏览器驱动初始化失败: {e}", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
self._showTrace(f"浏览器驱动已初始化, 类型: {self.__driver_type}, 路径: {self.__driver_path}")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __initDriverUrl(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
lib_config: dict = self.__run_config.get("library", None)
|
||||||
|
if not lib_config:
|
||||||
|
self._showTrace("未配置图书馆参数 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
url: str = lib_config.get("host_url") + lib_config.get("login_url")
|
||||||
|
self.__login_page = LoginPage(self._input_queue, self._output_queue, self.__driver)
|
||||||
|
self.__driver.set_page_load_timeout(5)
|
||||||
|
try:
|
||||||
|
self.__driver.get(url)
|
||||||
|
except TimeoutException:
|
||||||
|
self.__login_page.stopPageLoad()
|
||||||
|
self._showTrace(
|
||||||
|
"图书馆登录页面加载超时 ! 请检查网络环境是否正常", self.TraceLevel.ERROR
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
except WebDriverException as e:
|
||||||
|
self._showTrace(f"图书馆页面加载失败: {e}", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
if not self.__login_page.waitUntilLoaded():
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __initPagesServices(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if not self.__driver:
|
||||||
|
self._showTrace("浏览器驱动未初始化, 请先初始化浏览器驱动 !", self.TraceLevel.WARNING)
|
||||||
|
return
|
||||||
|
self.__shell = MainShell(self.__driver)
|
||||||
|
self.__captcha_solver = CaptchaSolver(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
)
|
||||||
|
self.__record_checker = RecordChecker(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
)
|
||||||
|
self.__reserve_checker = ReserveChecker(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __initPagesFlows(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self.__reserve_flow = ReserveFlow(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
driver=self.__driver,
|
||||||
|
shell=self.__shell,
|
||||||
|
)
|
||||||
|
self.__checkin_flow = CheckinFlow(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
driver=self.__driver,
|
||||||
|
shell=self.__shell,
|
||||||
|
)
|
||||||
|
self.__renew_flow = RenewFlow(
|
||||||
|
input_queue=self._input_queue,
|
||||||
|
output_queue=self._output_queue,
|
||||||
|
driver=self.__driver,
|
||||||
|
shell=self.__shell,
|
||||||
|
)
|
||||||
|
|
||||||
|
def __run(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
password: str,
|
||||||
|
login_config: dict,
|
||||||
|
run_mode_config: dict,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> int:
|
||||||
|
|
||||||
|
# result : -1 - terminate, 0 - success, 1 - failed, 2 - passed
|
||||||
|
result: int = 2
|
||||||
|
|
||||||
|
# login
|
||||||
|
auto_captcha: bool = login_config.get("auto_captcha", True)
|
||||||
|
if not self.__login_page.login(
|
||||||
|
username,
|
||||||
|
password,
|
||||||
|
captcha_solver=self.__captcha_solver.solveCaptcha,
|
||||||
|
auto_captcha=auto_captcha,
|
||||||
|
max_attempts=login_config.get("max_attempt", 3),
|
||||||
|
):
|
||||||
|
return 1
|
||||||
|
run_mode_raw: int = run_mode_config.get("run_mode", 0)
|
||||||
|
run_mode: dict[str, bool] = {
|
||||||
|
"auto_reserve": run_mode_raw & 0x1,
|
||||||
|
"auto_checkin": run_mode_raw & 0x2,
|
||||||
|
"auto_renewal": run_mode_raw & 0x4,
|
||||||
|
}
|
||||||
|
|
||||||
|
# reserve
|
||||||
|
if run_mode["auto_reserve"]:
|
||||||
|
if self.__reserve_checker.check(reserve_info):
|
||||||
|
if self.__record_checker.canReserve(self.__shell, reserve_info["date"]):
|
||||||
|
ctx = ReserveContext(
|
||||||
|
username=username,
|
||||||
|
date=reserve_info["date"],
|
||||||
|
floor=reserve_info["floor"],
|
||||||
|
room=reserve_info["room"],
|
||||||
|
seat_id=reserve_info["seat_id"],
|
||||||
|
begin_time=reserve_info["begin_time"]["time"],
|
||||||
|
end_time=reserve_info["end_time"]["time"],
|
||||||
|
begin_max_diff=reserve_info["begin_time"]["max_diff"],
|
||||||
|
end_max_diff=reserve_info["end_time"]["max_diff"],
|
||||||
|
begin_prefer_early=reserve_info["begin_time"]["prefer_early"],
|
||||||
|
end_prefer_early=reserve_info["end_time"]["prefer_early"],
|
||||||
|
expect_duration=reserve_info["expect_duration"],
|
||||||
|
satisfy_duration=reserve_info["satisfy_duration"],
|
||||||
|
)
|
||||||
|
if self.__reserve_flow.execute(ctx):
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
result = 1
|
||||||
|
else:
|
||||||
|
self._showTrace(f"用户 {username} 无法预约, 已跳过")
|
||||||
|
result = 2
|
||||||
|
else:
|
||||||
|
result = 1
|
||||||
|
|
||||||
|
# checkin
|
||||||
|
last_result: int = result
|
||||||
|
if run_mode["auto_checkin"] and last_result != 1:
|
||||||
|
if self.__record_checker.canCheckin(self.__shell):
|
||||||
|
if self.__checkin_flow.execute(username):
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
result = 1
|
||||||
|
else:
|
||||||
|
self._showTrace(f"用户 {username} 无法签到, 已跳过")
|
||||||
|
result = 2
|
||||||
|
if last_result == 0: # partly success
|
||||||
|
result = 0
|
||||||
|
|
||||||
|
# renewal
|
||||||
|
last_result = result
|
||||||
|
if run_mode["auto_renewal"] and last_result != 1:
|
||||||
|
can_renew, record = self.__record_checker.canRenew(self.__shell)
|
||||||
|
if can_renew:
|
||||||
|
renew_info: dict = reserve_info.get("renew_time", {})
|
||||||
|
if self.__renew_flow.execute(username, record, renew_info):
|
||||||
|
if self.__record_checker.postRenewCheck(self.__shell, record):
|
||||||
|
self._showTrace(f"用户 {username} 续约成功 !")
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
if result != 1: # partly success
|
||||||
|
result = 0
|
||||||
|
else:
|
||||||
|
result = 1
|
||||||
|
else:
|
||||||
|
result = 1
|
||||||
|
else:
|
||||||
|
self._showTrace(f"用户 {username} 无法续约, 已跳过")
|
||||||
|
result = 2
|
||||||
|
if last_result == 0: # partly success
|
||||||
|
result = 0
|
||||||
|
|
||||||
|
# logout
|
||||||
|
if not self.__shell.logout():
|
||||||
|
self._showTrace(f"用户 {username} 退出登录失败, 尝试直接重载页面")
|
||||||
|
if not self.__initDriverUrl():
|
||||||
|
self._showTrace(f"用户 {username} 重载页面失败, 无法继续操作, 该任务已终止 !")
|
||||||
|
return -1
|
||||||
|
self._showTrace(f"用户 {username} 已退出登录")
|
||||||
|
return result
|
||||||
|
|
||||||
|
def run(
|
||||||
|
self,
|
||||||
|
user_config: dict,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self.__user_config = user_config
|
||||||
|
user_counter: dict[str, int] = {"current": 0, "success": 0, "failed": 0, "passed": 0}
|
||||||
|
users: list = self.__user_config.get("users", [])
|
||||||
|
self._showTrace(f"共发现 {len(users)} 个用户")
|
||||||
|
for user in users:
|
||||||
|
user_counter["current"] += 1
|
||||||
|
self._showTrace(
|
||||||
|
f"正在处理第 {user_counter["current"]}/{len(users)} 个用户: {user.get("username", "未知")}......",
|
||||||
|
no_log=True,
|
||||||
|
)
|
||||||
|
if not user.get("enabled", False):
|
||||||
|
self._showTrace(f"用户 {user.get("username", "未知")} 已跳过")
|
||||||
|
user_counter["passed"] += 1
|
||||||
|
continue
|
||||||
|
r: int = self.__run(
|
||||||
|
username=user.get("username", ""),
|
||||||
|
password=user.get("password", ""),
|
||||||
|
login_config=self.__run_config.get("login", {}),
|
||||||
|
run_mode_config=self.__run_config.get("mode", {}),
|
||||||
|
reserve_info=user.get("reserve_info", {}),
|
||||||
|
)
|
||||||
|
if r == -1:
|
||||||
|
self._showTrace(
|
||||||
|
f"用户 {user.get("username", "未知")} 处理过程中页面发生异常, 无法继续操作, 任务已终止 !",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
break
|
||||||
|
elif r == 0:
|
||||||
|
user_counter["success"] += 1
|
||||||
|
elif r == 1:
|
||||||
|
user_counter["failed"] += 1
|
||||||
|
elif r == 2:
|
||||||
|
user_counter["passed"] += 1
|
||||||
|
self._showTrace(
|
||||||
|
f"处理完成, 共计 {user_counter["current"]} 个用户, "
|
||||||
|
f"成功 {user_counter["success"]} 个用户, "
|
||||||
|
f"失败 {user_counter["failed"]} 个用户, "
|
||||||
|
f"跳过 {user_counter["passed"]} 个用户"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
def close(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if self.__driver:
|
||||||
|
if self.__driver_type.lower() == "firefox":
|
||||||
|
self._showTrace(
|
||||||
|
"Firefox 浏览器驱动关闭略慢, 请耐心等待...",
|
||||||
|
no_log=True,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
self.__driver.quit()
|
||||||
|
except WebDriverException as e:
|
||||||
|
self._showTrace(f"浏览器驱动关闭时发生异常: {e}", self.TraceLevel.WARNING)
|
||||||
|
self.__driver = None
|
||||||
|
self._showTrace("浏览器驱动已关闭")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._showTrace("浏览器驱动未初始化, 无需关闭", no_log=True)
|
||||||
|
return False
|
||||||
@@ -0,0 +1,205 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
from typing import Callable
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
|
||||||
|
|
||||||
|
class LoginPage(MsgBase):
|
||||||
|
|
||||||
|
USERNAME_INPUT = (By.NAME, "username")
|
||||||
|
PASSWORD_INPUT = (By.NAME, "password")
|
||||||
|
CAPTCHA_INPUT = (By.NAME, "answer")
|
||||||
|
CAPTCHA_IMG = (By.ID, "loadImgId")
|
||||||
|
LOGIN_BUTTON = (By.XPATH, "//input[@type='button' and @value='登录']")
|
||||||
|
|
||||||
|
SUCCESS_INDICATOR_SEARCH = (By.ID, "search")
|
||||||
|
SUCCESS_INDICATOR_CONTENT = (By.CLASS_NAME, "selectContent")
|
||||||
|
SUCCESS_TITLE_KEYWORD = "自选座位 :: 座位预约系统"
|
||||||
|
|
||||||
|
PAGE_LOAD_TIMEOUT = 5
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self._driver: WebDriver = driver
|
||||||
|
|
||||||
|
def navigate(
|
||||||
|
self,
|
||||||
|
url: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
self._driver.set_page_load_timeout(self.PAGE_LOAD_TIMEOUT)
|
||||||
|
self._driver.get(url)
|
||||||
|
if not self.waitUntilLoaded():
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def waitUntilLoaded(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.title_contains("首页")
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.USERNAME_INPUT)
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.PASSWORD_INPUT)
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.CAPTCHA_INPUT)
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.CAPTCHA_IMG)
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def fillCredentials(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
password: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
el = self._driver.find_element(*self.USERNAME_INPUT)
|
||||||
|
el.clear()
|
||||||
|
el.send_keys(username)
|
||||||
|
el = self._driver.find_element(*self.PASSWORD_INPUT)
|
||||||
|
el.clear()
|
||||||
|
el.send_keys(password)
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getCaptchaImageSrc(
|
||||||
|
self,
|
||||||
|
) -> str | None:
|
||||||
|
|
||||||
|
# return 'None' if captcha image element is not found.
|
||||||
|
# But the 'get_attribute("src")' also return 'None' if there's no attribute with
|
||||||
|
# that name, which is not what we want.
|
||||||
|
try:
|
||||||
|
captcha_el = self._driver.find_element(*self.CAPTCHA_IMG)
|
||||||
|
return captcha_el.get_attribute("src")
|
||||||
|
except NoSuchElementException:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def refreshCaptcha(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._driver.find_element(*self.CAPTCHA_IMG).click()
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def fillCaptcha(
|
||||||
|
self,
|
||||||
|
captcha_text: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
el = self._driver.find_element(*self.CAPTCHA_INPUT)
|
||||||
|
el.clear()
|
||||||
|
el.send_keys(captcha_text)
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def clickLogin(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._driver.find_element(*self.LOGIN_BUTTON).click()
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def waitLoginSuccess(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.title_contains(self.SUCCESS_TITLE_KEYWORD)
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.SUCCESS_INDICATOR_SEARCH)
|
||||||
|
)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.SUCCESS_INDICATOR_CONTENT)
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def stopPageLoad(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._driver.execute_script("window.stop();")
|
||||||
|
|
||||||
|
def login(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
password: str,
|
||||||
|
captcha_solver: Callable[["LoginPage", bool], str],
|
||||||
|
auto_captcha: bool,
|
||||||
|
max_attempts: int = 5,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
for attempt in range(max_attempts):
|
||||||
|
self._showTrace(
|
||||||
|
f"用户 {username} 第 {attempt + 1} 次尝试登录......",
|
||||||
|
no_log=True,
|
||||||
|
)
|
||||||
|
if not self.fillCredentials(username, password):
|
||||||
|
continue
|
||||||
|
captcha_text = captcha_solver(self, auto_captcha)
|
||||||
|
if not captcha_text:
|
||||||
|
continue
|
||||||
|
if not self.fillCaptcha(captcha_text):
|
||||||
|
continue
|
||||||
|
self._showTrace("尝试登录...", no_log=True)
|
||||||
|
if not self.clickLogin():
|
||||||
|
continue
|
||||||
|
if self.waitLoginSuccess():
|
||||||
|
self._showTrace(f"用户 {username} 第 {attempt + 1} 次登录成功 !")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._showTrace(
|
||||||
|
"登录页面加载失败 ! : "
|
||||||
|
"用户账号或者密码错误/验证码错误, 具体以页面提示为准",
|
||||||
|
level=self.TraceLevel.ERROR,
|
||||||
|
)
|
||||||
|
return False
|
||||||
@@ -0,0 +1,177 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
TimeoutException,
|
||||||
|
WebDriverException,
|
||||||
|
)
|
||||||
|
|
||||||
|
from pages.ReserveView import ReserveView
|
||||||
|
from pages.RecordsView import RecordsView
|
||||||
|
|
||||||
|
|
||||||
|
class MainShell:
|
||||||
|
|
||||||
|
TAB_RESERVE = (By.XPATH, "//a[@href='/map']")
|
||||||
|
TAB_HISTORY = (By.XPATH, "//a[@href='/history?type=SEAT']")
|
||||||
|
TAB_LOGOUT = (By.XPATH, "//a[@href='/logout']")
|
||||||
|
|
||||||
|
BTN_CHECKIN = (By.ID, "btnCheckIn")
|
||||||
|
BTN_EXTEND = (By.ID, "btnExtend")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._driver = driver
|
||||||
|
|
||||||
|
def _clickTab(
|
||||||
|
self,
|
||||||
|
locator: tuple,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(locator)
|
||||||
|
).click()
|
||||||
|
|
||||||
|
def gotoReserveView(
|
||||||
|
self,
|
||||||
|
) -> ReserveView:
|
||||||
|
|
||||||
|
self._clickTab(self.TAB_RESERVE)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located((By.ID, "seatLayout"))
|
||||||
|
)
|
||||||
|
return ReserveView(self._driver)
|
||||||
|
|
||||||
|
def gotoRecordsView(
|
||||||
|
self,
|
||||||
|
) -> RecordsView:
|
||||||
|
|
||||||
|
self._clickTab(self.TAB_HISTORY)
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located((By.CLASS_NAME, "myReserveList"))
|
||||||
|
)
|
||||||
|
return RecordsView(self._driver)
|
||||||
|
|
||||||
|
def logout(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._driver.find_element(*self.TAB_LOGOUT).click()
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def waitCheckinButton(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.BTN_CHECKIN)
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def waitExtendButton(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.BTN_EXTEND)
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def isCheckinButtonDisabled(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
btn = self._driver.find_element(*self.BTN_CHECKIN)
|
||||||
|
return "disabled" in btn.get_attribute("class")
|
||||||
|
except NoSuchElementException:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def isExtendButtonDisabled(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
btn = self._driver.find_element(*self.BTN_EXTEND)
|
||||||
|
return "disabled" in btn.get_attribute("class")
|
||||||
|
except NoSuchElementException:
|
||||||
|
return True
|
||||||
|
|
||||||
|
def clickCheckinButton(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
btn = WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.BTN_CHECKIN)
|
||||||
|
)
|
||||||
|
btn.click()
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return
|
||||||
|
|
||||||
|
def clickExtendButton(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
btn = WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.BTN_EXTEND)
|
||||||
|
)
|
||||||
|
btn.click()
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return
|
||||||
|
|
||||||
|
def enableCheckinButtonByJS(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
script = """
|
||||||
|
try {
|
||||||
|
var checkin_btn = document.getElementById('btnCheckIn');
|
||||||
|
if (checkin_btn) {
|
||||||
|
checkin_btn.classList.remove('disabled');
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
} catch (e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
result = self._driver.execute_script(script)
|
||||||
|
time.sleep(0.1)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def refresh(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._driver.refresh()
|
||||||
|
except (TimeoutException, WebDriverException):
|
||||||
|
return
|
||||||
@@ -0,0 +1,87 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RecordsView:
|
||||||
|
|
||||||
|
RECORDS_LIST = (By.CSS_SELECTOR, ".myReserveList > dl:not(#moreBlock)")
|
||||||
|
MORE_BTN = (By.ID, "moreBtn")
|
||||||
|
RECORD_TIME = (By.CSS_SELECTOR, "dt")
|
||||||
|
RECORD_INFO = (By.CSS_SELECTOR, "a")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._driver = driver
|
||||||
|
|
||||||
|
def loadRecords(
|
||||||
|
self,
|
||||||
|
) -> list | None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.presence_of_element_located(self.RECORDS_LIST)
|
||||||
|
)
|
||||||
|
return self._driver.find_elements(*self.RECORDS_LIST)
|
||||||
|
except TimeoutException:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def getRecordTimeElement(
|
||||||
|
self,
|
||||||
|
record: WebElement,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return record.find_element(*self.RECORD_TIME)
|
||||||
|
|
||||||
|
def getRecordInfoElements(
|
||||||
|
self,
|
||||||
|
record: WebElement,
|
||||||
|
) -> list[WebElement]:
|
||||||
|
|
||||||
|
return record.find_elements(*self.RECORD_INFO)
|
||||||
|
|
||||||
|
def showMoreRecords(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.MORE_BTN)
|
||||||
|
)
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
more_btn = self._driver.find_element(*self.MORE_BTN)
|
||||||
|
if more_btn.is_displayed() and more_btn.is_enabled():
|
||||||
|
self._driver.execute_script("arguments[0].scrollIntoView(true);", more_btn)
|
||||||
|
self._driver.execute_script("arguments[0].click();", more_btn)
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getRecordText(
|
||||||
|
self,
|
||||||
|
record: WebElement,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
return record.text.strip()
|
||||||
@@ -0,0 +1,186 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import time
|
||||||
|
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
|
||||||
|
from pages.components.SeatMapDialog import SeatMapDialog
|
||||||
|
|
||||||
|
|
||||||
|
class ReserveView:
|
||||||
|
|
||||||
|
DATE_SELECT = (By.ID, "onDate_select")
|
||||||
|
DATE_OPTION_FMT = "p#options_onDate a[value='{value}']"
|
||||||
|
DATE_XPATH_FMT = "//p[@id='options_onDate']/a[@value='{value}']"
|
||||||
|
|
||||||
|
PLACE_SELECT = (By.ID, "display_building")
|
||||||
|
PLACE_OPTION_FMT = "p#options_building a[value='{value}']"
|
||||||
|
PLACE_XPATH_FMT = "//p[@id='options_building']/a[@value='{value}']"
|
||||||
|
|
||||||
|
FLOOR_SELECT = (By.ID, "floor_select")
|
||||||
|
FLOOR_OPTION_FMT = "p#options_floor a[value='{value}']"
|
||||||
|
FLOOR_XPATH_FMT = "//p[@id='options_floor']/a[@value='{value}']"
|
||||||
|
|
||||||
|
FIND_ROOM_BTN = (By.ID, "findRoom")
|
||||||
|
ROOM_BTN_FMT = "room_{room}"
|
||||||
|
|
||||||
|
RESERVE_BTN = (By.ID, "reserveBtn")
|
||||||
|
|
||||||
|
FLOOR_MAP = {"2": "二层", "3": "三层", "4": "四层", "5": "五层"}
|
||||||
|
ROOM_MAP = {
|
||||||
|
"1": "二层内环", "2": "二层西区", "3": "三层内环", "4": "三层外环",
|
||||||
|
"5": "四层内环", "6": "四层外环", "7": "四层期刊", "8": "五层考研",
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._driver = driver
|
||||||
|
|
||||||
|
def _clickOptionByJS(
|
||||||
|
self,
|
||||||
|
trigger_id: str,
|
||||||
|
option_css: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
script = f"""
|
||||||
|
try {{
|
||||||
|
var trigger = document.getElementById('{trigger_id}');
|
||||||
|
if (trigger) {{
|
||||||
|
trigger.click();
|
||||||
|
var option = document.querySelector("{option_css}");
|
||||||
|
if (option) {{
|
||||||
|
option.click();
|
||||||
|
return true;
|
||||||
|
}}
|
||||||
|
return false;
|
||||||
|
}}
|
||||||
|
return false;
|
||||||
|
}} catch (e) {{
|
||||||
|
return false;
|
||||||
|
}}
|
||||||
|
"""
|
||||||
|
result = self._driver.execute_script(script)
|
||||||
|
time.sleep(0.1)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def _clickOption(
|
||||||
|
self,
|
||||||
|
trigger: tuple,
|
||||||
|
option: tuple,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(trigger)
|
||||||
|
).click()
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(option)
|
||||||
|
).click()
|
||||||
|
return True
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def selectDate(
|
||||||
|
self,
|
||||||
|
date_str: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if self._clickOptionByJS(
|
||||||
|
trigger_id="onDate_select",
|
||||||
|
option_css=self.DATE_OPTION_FMT.format(value=date_str),
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
return self._clickOption(
|
||||||
|
trigger=self.DATE_SELECT,
|
||||||
|
option=(By.XPATH, self.DATE_XPATH_FMT.format(value=date_str)),
|
||||||
|
)
|
||||||
|
|
||||||
|
def selectPlace(
|
||||||
|
self,
|
||||||
|
place: str = "1",
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if self._clickOptionByJS(
|
||||||
|
trigger_id="display_building",
|
||||||
|
option_css=self.PLACE_OPTION_FMT.format(value=place),
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
return self._clickOption(
|
||||||
|
trigger=self.PLACE_SELECT,
|
||||||
|
option=(By.XPATH, self.PLACE_XPATH_FMT.format(value=place)),
|
||||||
|
)
|
||||||
|
|
||||||
|
def selectFloor(
|
||||||
|
self,
|
||||||
|
floor: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if self._clickOptionByJS(
|
||||||
|
trigger_id="floor_select",
|
||||||
|
option_css=self.FLOOR_OPTION_FMT.format(value=floor),
|
||||||
|
):
|
||||||
|
return True
|
||||||
|
return self._clickOption(
|
||||||
|
trigger=self.FLOOR_SELECT,
|
||||||
|
option=(By.XPATH, self.FLOOR_XPATH_FMT.format(value=floor)),
|
||||||
|
)
|
||||||
|
|
||||||
|
def selectRoom(
|
||||||
|
self,
|
||||||
|
room: str,
|
||||||
|
) -> SeatMapDialog | None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.FIND_ROOM_BTN)
|
||||||
|
).click()
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable((By.ID, self.ROOM_BTN_FMT.format(room=room)))
|
||||||
|
).click()
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
return SeatMapDialog(self._driver)
|
||||||
|
except TimeoutException:
|
||||||
|
return None
|
||||||
|
|
||||||
|
def submitReserve(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(self.RESERVE_BTN)
|
||||||
|
).click()
|
||||||
|
return True
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
|
|
||||||
|
def refresh(
|
||||||
|
self,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._driver.refresh()
|
||||||
|
except TimeoutException:
|
||||||
|
return
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from .AutoLib import AutoLib
|
||||||
|
from .LoginPage import LoginPage
|
||||||
|
from .MainShell import MainShell
|
||||||
|
from .ReserveView import ReserveView
|
||||||
|
from .RecordsView import RecordsView
|
||||||
|
from .components.SeatMapDialog import SeatMapDialog
|
||||||
|
from .components.TimeSelectDialog import TimeSelectDialog
|
||||||
|
from .components.ReserveResultDialog import ReserveResultDialog
|
||||||
|
from .components.CheckinResultDialog import CheckinResultDialog
|
||||||
|
from .components.RenewDialog import RenewDialog
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
|
from pages.components.Dialog import Dialog
|
||||||
|
|
||||||
|
|
||||||
|
class CheckinResultDialog(Dialog):
|
||||||
|
"""
|
||||||
|
Check-in result dialog.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ROOT = (By.CLASS_NAME, "ui_dialog")
|
||||||
|
|
||||||
|
RESULT_MSG = (By.CLASS_NAME, "resultMessage")
|
||||||
|
OK_BTN = (By.CLASS_NAME, "btnOK")
|
||||||
|
DETAIL_DD = (By.CSS_SELECTOR, ".resultMessage dd")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(driver, self.ROOT, auto_close_on_exit=False)
|
||||||
|
|
||||||
|
def getResultMessage(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._waitPresence(self.RESULT_MSG)
|
||||||
|
el = self._find(*self.RESULT_MSG)
|
||||||
|
return el.text
|
||||||
|
except (TimeoutException, NoSuchElementException,
|
||||||
|
StaleElementReferenceException):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def getDetails(
|
||||||
|
self,
|
||||||
|
) -> list[str]:
|
||||||
|
|
||||||
|
try:
|
||||||
|
elements = self._findAll(*self.DETAIL_DD)
|
||||||
|
return [el.text for el in elements if el.text.strip()]
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return []
|
||||||
|
|
||||||
|
def clickOk(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._waitClickable(self.OK_BTN).click()
|
||||||
|
return True
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
|
||||||
|
|
||||||
|
class Dialog:
|
||||||
|
"""
|
||||||
|
Context-managed overlay / modal / dialog on a page.
|
||||||
|
|
||||||
|
The constructor verifies that the root element is visible — if not,
|
||||||
|
the dialog is not on screen and a :exc:`TimeoutException` is raised.
|
||||||
|
|
||||||
|
Automates the lifecycle: wait for appearance on enter,
|
||||||
|
optionally wait for disappearance on exit.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
root_locator: tuple,
|
||||||
|
auto_close_on_exit: bool = True,
|
||||||
|
wait_timeout: float = 3.0,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._driver: WebDriver = driver
|
||||||
|
self._root_locator: tuple = root_locator
|
||||||
|
self._auto_close: bool = auto_close_on_exit
|
||||||
|
self._timeout: float = wait_timeout
|
||||||
|
|
||||||
|
WebDriverWait(self._driver, self._timeout).until(
|
||||||
|
EC.visibility_of_element_located(self._root_locator)
|
||||||
|
)
|
||||||
|
|
||||||
|
def __enter__(
|
||||||
|
self,
|
||||||
|
) -> "Dialog":
|
||||||
|
|
||||||
|
return self
|
||||||
|
|
||||||
|
def __exit__(
|
||||||
|
self,
|
||||||
|
*args: object,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if self._auto_close:
|
||||||
|
WebDriverWait(self._driver, self._timeout).until(
|
||||||
|
EC.invisibility_of_element_located(self._root_locator)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _find(
|
||||||
|
self,
|
||||||
|
by: str,
|
||||||
|
value: str,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return self._driver.find_element(by, value)
|
||||||
|
|
||||||
|
def _findAll(
|
||||||
|
self,
|
||||||
|
by: str,
|
||||||
|
value: str,
|
||||||
|
) -> list[WebElement]:
|
||||||
|
|
||||||
|
return self._driver.find_elements(by, value)
|
||||||
|
|
||||||
|
def _waitClickable(
|
||||||
|
self,
|
||||||
|
locator: tuple,
|
||||||
|
timeout: float = 2.0,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return WebDriverWait(self._driver, timeout).until(
|
||||||
|
EC.element_to_be_clickable(locator)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _waitPresence(
|
||||||
|
self,
|
||||||
|
locator: tuple,
|
||||||
|
timeout: float = 2.0,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return WebDriverWait(self._driver, timeout).until(
|
||||||
|
EC.presence_of_element_located(locator)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _waitVisible(
|
||||||
|
self,
|
||||||
|
locator: tuple,
|
||||||
|
timeout: float = 2.0,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return WebDriverWait(self._driver, timeout).until(
|
||||||
|
EC.visibility_of_element_located(locator)
|
||||||
|
)
|
||||||
|
|
||||||
|
def _waitAllPresence(
|
||||||
|
self,
|
||||||
|
locator: tuple,
|
||||||
|
timeout: float = 2.0,
|
||||||
|
) -> list[WebElement]:
|
||||||
|
|
||||||
|
return WebDriverWait(self._driver, timeout).until(
|
||||||
|
EC.presence_of_all_elements_located(locator)
|
||||||
|
)
|
||||||
@@ -0,0 +1,127 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
|
||||||
|
from pages.components.Dialog import Dialog
|
||||||
|
from pages.strategies.TimeSelectMaker import (
|
||||||
|
TimeSelectionResult,
|
||||||
|
TimeSelectMaker,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class RenewDialog(Dialog):
|
||||||
|
"""
|
||||||
|
Renewal time selection dialog.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ROOT = (By.ID, "extendDiv")
|
||||||
|
|
||||||
|
MESSAGE_HEAD = (By.CSS_SELECTOR, "#extendDiv p.messageHead")
|
||||||
|
RESULT_MSG = (By.CSS_SELECTOR, "#extendDiv div.resultMessage")
|
||||||
|
TIME_OPTS = (By.CSS_SELECTOR, "#extendDiv .renewal_List li")
|
||||||
|
OK_BTN = (By.CSS_SELECTOR, "#extendDiv .btnOK")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(driver, self.ROOT, auto_close_on_exit=False)
|
||||||
|
|
||||||
|
def waitUntilReady(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._waitVisible(self.ROOT)
|
||||||
|
self._waitPresence(self.MESSAGE_HEAD)
|
||||||
|
self._waitPresence(self.RESULT_MSG)
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
head_msg = self._find(*self.MESSAGE_HEAD).text.strip()
|
||||||
|
if "警告" in head_msg:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
self._waitAllPresence(self.TIME_OPTS)
|
||||||
|
self._waitPresence(self.OK_BTN)
|
||||||
|
except TimeoutException:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def getHeadMessage(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self._find(*self.MESSAGE_HEAD).text.strip()
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def getResultMessage(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self._find(*self.RESULT_MSG).text.strip()
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def getTimeOptions(
|
||||||
|
self,
|
||||||
|
) -> list[WebElement]:
|
||||||
|
|
||||||
|
return self._findAll(*self.TIME_OPTS)
|
||||||
|
|
||||||
|
def selectBestTime(
|
||||||
|
self,
|
||||||
|
target_time: int,
|
||||||
|
max_time_diff: int,
|
||||||
|
prefer_earlier: bool,
|
||||||
|
) -> TimeSelectionResult:
|
||||||
|
|
||||||
|
all_time_opts = self.getTimeOptions()
|
||||||
|
if not all_time_opts:
|
||||||
|
return TimeSelectionResult()
|
||||||
|
result = TimeSelectMaker.forRenew().decide(
|
||||||
|
all_time_opts,
|
||||||
|
target_time,
|
||||||
|
max_time_diff,
|
||||||
|
prefer_earlier,
|
||||||
|
)
|
||||||
|
if result.selected_index >= 0:
|
||||||
|
try:
|
||||||
|
all_time_opts[result.selected_index].click()
|
||||||
|
except (ElementNotInteractableException, StaleElementReferenceException):
|
||||||
|
return TimeSelectionResult(free_times=result.free_times)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def getOkButton(
|
||||||
|
self,
|
||||||
|
) -> WebElement:
|
||||||
|
|
||||||
|
return self._find(*self.OK_BTN)
|
||||||
|
|
||||||
|
def clickOk(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._find(*self.OK_BTN).click()
|
||||||
|
return True
|
||||||
|
except (NoSuchElementException, ElementNotInteractableException):
|
||||||
|
return False
|
||||||
@@ -0,0 +1,77 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
|
from pages.components.Dialog import Dialog
|
||||||
|
|
||||||
|
|
||||||
|
class ReserveResultDialog(Dialog):
|
||||||
|
"""
|
||||||
|
Reservation result dialog shown after submitting a reserve request.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ROOT = (By.CLASS_NAME, "layoutSeat")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(driver, self.ROOT, auto_close_on_exit=False)
|
||||||
|
|
||||||
|
def _titleLocator(
|
||||||
|
self,
|
||||||
|
) -> tuple:
|
||||||
|
|
||||||
|
return (By.CSS_SELECTOR, ".layoutSeat dt")
|
||||||
|
|
||||||
|
def getTitle(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self._find(*self._titleLocator()).text
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def isSuccess(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
title = self.getTitle()
|
||||||
|
return any(
|
||||||
|
kw in title
|
||||||
|
for kw in ("预定好了", "预约成功", "操作成功")
|
||||||
|
)
|
||||||
|
|
||||||
|
def isFailure(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
contents = self.getDetailTexts()
|
||||||
|
return any(
|
||||||
|
"预约失败" in msg or "已有1个有效预约" in msg
|
||||||
|
for msg in contents
|
||||||
|
)
|
||||||
|
|
||||||
|
def getDetailTexts(
|
||||||
|
self,
|
||||||
|
) -> list[str]:
|
||||||
|
|
||||||
|
try:
|
||||||
|
elements = self._findAll(By.CSS_SELECTOR, ".layoutSeat dd")
|
||||||
|
return [el.text for el in elements if el.text.strip()]
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return []
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.support.ui import WebDriverWait
|
||||||
|
from selenium.webdriver.support import expected_conditions as EC
|
||||||
|
|
||||||
|
from pages.components.Dialog import Dialog
|
||||||
|
|
||||||
|
|
||||||
|
class SeatMapDialog(Dialog):
|
||||||
|
"""
|
||||||
|
Seat selection overlay that opens after choosing a floor and room.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ROOT = (By.ID, "seatLayout")
|
||||||
|
SEAT_ITEMS = (By.CSS_SELECTOR, "li[id^='seat_']")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(driver, self.ROOT)
|
||||||
|
|
||||||
|
def selectSeat(
|
||||||
|
self,
|
||||||
|
seat_id: str,
|
||||||
|
) -> str | None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._waitAllPresence(self.SEAT_ITEMS)
|
||||||
|
except TimeoutException:
|
||||||
|
return None
|
||||||
|
try:
|
||||||
|
seat_el = self._find(By.ID, f"seat_{int(seat_id):03d}")
|
||||||
|
seat_link = seat_el.find_element(By.TAG_NAME, "a")
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(seat_link)
|
||||||
|
)
|
||||||
|
seat_link.click()
|
||||||
|
return seat_link.get_attribute("title")
|
||||||
|
except (NoSuchElementException, ValueError, TimeoutException,
|
||||||
|
ElementNotInteractableException, StaleElementReferenceException):
|
||||||
|
pass
|
||||||
|
try:
|
||||||
|
all_seats = self._findAll(*self.SEAT_ITEMS)
|
||||||
|
seat_id_upper = seat_id.lstrip('0').upper()
|
||||||
|
for seat in all_seats:
|
||||||
|
if not seat_id_upper == seat.text.lstrip('0'):
|
||||||
|
continue
|
||||||
|
seat_link = seat.find_element(By.TAG_NAME, "a")
|
||||||
|
WebDriverWait(self._driver, 2).until(
|
||||||
|
EC.element_to_be_clickable(seat_link)
|
||||||
|
)
|
||||||
|
seat_link.click()
|
||||||
|
return seat_link.get_attribute("title")
|
||||||
|
return None
|
||||||
|
except (NoSuchElementException, TimeoutException,
|
||||||
|
ElementNotInteractableException, StaleElementReferenceException):
|
||||||
|
return None
|
||||||
@@ -0,0 +1,234 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from typing import TYPE_CHECKING, Callable, Optional
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.common.by import By
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
from selenium.webdriver.remote.webelement import WebElement
|
||||||
|
|
||||||
|
from pages.components.Dialog import Dialog
|
||||||
|
from pages.strategies.TimeSelectMaker import (
|
||||||
|
TimeRangeResult,
|
||||||
|
TimeSelectionResult,
|
||||||
|
TimeSelectMaker,
|
||||||
|
minsToTimeStr,
|
||||||
|
timeStrToMins,
|
||||||
|
)
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from pages.flows.ReserveFlow import ReserveContext
|
||||||
|
|
||||||
|
|
||||||
|
class TimeSelectDialog(Dialog):
|
||||||
|
"""
|
||||||
|
Time selection panel that appears after selecting a seat.
|
||||||
|
|
||||||
|
Contains start-time and end-time option lists.
|
||||||
|
Does NOT auto-close — the reserve submission handles cleanup.
|
||||||
|
"""
|
||||||
|
|
||||||
|
ROOT = (By.CSS_SELECTOR, "#startTime ul")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
driver: WebDriver,
|
||||||
|
tracer: Optional[Callable[[str, int], None]] = None,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(driver, self.ROOT, auto_close_on_exit=False)
|
||||||
|
self._tracer = tracer
|
||||||
|
|
||||||
|
def _trace(
|
||||||
|
self,
|
||||||
|
msg: str,
|
||||||
|
level: int = logging.INFO,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if self._tracer is not None:
|
||||||
|
self._tracer(msg, level)
|
||||||
|
|
||||||
|
def _logTimeStep(
|
||||||
|
self,
|
||||||
|
time_type: str,
|
||||||
|
target_mins: int,
|
||||||
|
max_diff: int,
|
||||||
|
step_result: TimeSelectionResult,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if step_result.selected_index >= 0:
|
||||||
|
abs_diff = abs(step_result.actual_diff)
|
||||||
|
if step_result.actual_diff < 0:
|
||||||
|
relation = f"早了 {abs_diff} 分钟"
|
||||||
|
elif step_result.actual_diff > 0:
|
||||||
|
relation = f"晚了 {abs_diff} 分钟"
|
||||||
|
else:
|
||||||
|
relation = f"正好等于 {time_type}"
|
||||||
|
self._trace(
|
||||||
|
f"选择距离期望 {time_type} 最近的 {step_result.display_text}, "
|
||||||
|
f"与期望 {time_type} 相比 {relation}"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
if not step_result.free_times:
|
||||||
|
self._trace(
|
||||||
|
f"{time_type} 选择失败 ! : 当前未查询到可用时间",
|
||||||
|
logging.ERROR,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
target_str = minsToTimeStr(target_mins)
|
||||||
|
self._trace(
|
||||||
|
f"无法选择最近的 {time_type} {target_str}, "
|
||||||
|
f"所有可选时间与目标时间相差都超过 {max_diff} 分钟",
|
||||||
|
logging.WARNING,
|
||||||
|
)
|
||||||
|
self._trace(f"当前可供预约的 {time_type} 有: {step_result.free_times}")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def getTimeOptions(
|
||||||
|
self,
|
||||||
|
time_id: str,
|
||||||
|
) -> list[WebElement]:
|
||||||
|
|
||||||
|
try:
|
||||||
|
self._waitAllPresence(
|
||||||
|
(By.CSS_SELECTOR, f"#{time_id} ul li a")
|
||||||
|
)
|
||||||
|
except TimeoutException:
|
||||||
|
return []
|
||||||
|
return self._findAll(
|
||||||
|
By.CSS_SELECTOR,
|
||||||
|
f"#{time_id} ul li a",
|
||||||
|
)
|
||||||
|
|
||||||
|
def selectNearestTime(
|
||||||
|
self,
|
||||||
|
time_id: str,
|
||||||
|
target_time: int,
|
||||||
|
max_time_diff: int,
|
||||||
|
prefer_earlier: bool,
|
||||||
|
) -> TimeSelectionResult:
|
||||||
|
|
||||||
|
all_time_opts = self.getTimeOptions(time_id)
|
||||||
|
if not all_time_opts:
|
||||||
|
return TimeSelectionResult()
|
||||||
|
result = TimeSelectMaker.forReserve().decide(
|
||||||
|
all_time_opts,
|
||||||
|
target_time,
|
||||||
|
max_time_diff,
|
||||||
|
prefer_earlier,
|
||||||
|
)
|
||||||
|
if result.selected_index >= 0:
|
||||||
|
try:
|
||||||
|
all_time_opts[result.selected_index].click()
|
||||||
|
except (ElementNotInteractableException, StaleElementReferenceException):
|
||||||
|
return TimeSelectionResult(free_times=result.free_times)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def selectTimeRange(
|
||||||
|
self,
|
||||||
|
begin_target: int,
|
||||||
|
end_target: int,
|
||||||
|
begin_max_diff: int = 30,
|
||||||
|
end_max_diff: int = 30,
|
||||||
|
begin_prefer_early: bool = True,
|
||||||
|
end_prefer_early: bool = False,
|
||||||
|
satisfy_duration: bool = True,
|
||||||
|
expect_duration: int = 4,
|
||||||
|
library_close_mins: int = TimeSelectMaker.LIBRARY_CLOSE_MINS,
|
||||||
|
) -> TimeRangeResult:
|
||||||
|
|
||||||
|
begin_result = self.selectNearestTime(
|
||||||
|
"startTime",
|
||||||
|
begin_target,
|
||||||
|
begin_max_diff,
|
||||||
|
begin_prefer_early,
|
||||||
|
)
|
||||||
|
if begin_result.selected_index < 0:
|
||||||
|
return TimeRangeResult(begin_result=begin_result)
|
||||||
|
actual_begin = begin_result.selected_value
|
||||||
|
if satisfy_duration:
|
||||||
|
end_target = TimeSelectMaker.calcEndTime(
|
||||||
|
actual_begin,
|
||||||
|
expect_duration,
|
||||||
|
library_close_mins,
|
||||||
|
)
|
||||||
|
end_result = self.selectNearestTime(
|
||||||
|
"endTime",
|
||||||
|
end_target,
|
||||||
|
end_max_diff,
|
||||||
|
end_prefer_early,
|
||||||
|
)
|
||||||
|
if end_result.selected_index < 0:
|
||||||
|
return TimeRangeResult(
|
||||||
|
begin_result=begin_result,
|
||||||
|
actual_begin_mins=actual_begin,
|
||||||
|
end_result=end_result,
|
||||||
|
expect_end_mins=end_target,
|
||||||
|
)
|
||||||
|
return TimeRangeResult(
|
||||||
|
begin_result=begin_result,
|
||||||
|
end_result=end_result,
|
||||||
|
actual_begin_mins=actual_begin,
|
||||||
|
actual_end_mins=end_result.selected_value,
|
||||||
|
expect_end_mins=end_target,
|
||||||
|
)
|
||||||
|
|
||||||
|
def selectSeatTime(
|
||||||
|
self,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
library_close_mins: int = TimeSelectMaker.LIBRARY_CLOSE_MINS,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
exp_beg_mins = timeStrToMins(ctx.begin_time)
|
||||||
|
exp_end_mins = timeStrToMins(ctx.end_time)
|
||||||
|
result = self.selectTimeRange(
|
||||||
|
begin_target=exp_beg_mins,
|
||||||
|
end_target=exp_end_mins,
|
||||||
|
begin_max_diff=ctx.begin_max_diff,
|
||||||
|
end_max_diff=ctx.end_max_diff,
|
||||||
|
begin_prefer_early=ctx.begin_prefer_early,
|
||||||
|
end_prefer_early=ctx.end_prefer_early,
|
||||||
|
satisfy_duration=ctx.satisfy_duration,
|
||||||
|
expect_duration=ctx.expect_duration,
|
||||||
|
library_close_mins=library_close_mins,
|
||||||
|
)
|
||||||
|
if not self._logTimeStep("开始时间", exp_beg_mins, ctx.begin_max_diff, result.begin_result):
|
||||||
|
return False
|
||||||
|
if ctx.satisfy_duration:
|
||||||
|
unclipped = result.actual_begin_mins + ctx.expect_duration*60
|
||||||
|
if unclipped > library_close_mins:
|
||||||
|
self._trace(
|
||||||
|
f"预约持续时间 {ctx.expect_duration} 小时, 超过最大预约时间 {minsToTimeStr(library_close_mins)}, "
|
||||||
|
f"自动调整为 {minsToTimeStr(library_close_mins)}",
|
||||||
|
logging.WARNING,
|
||||||
|
)
|
||||||
|
act_beg_str = minsToTimeStr(result.actual_begin_mins)
|
||||||
|
exp_end_str = minsToTimeStr(result.expect_end_mins)
|
||||||
|
self._trace(
|
||||||
|
f"需要满足期望预约持续时间: {ctx.expect_duration} 小时, "
|
||||||
|
f"根据开始时间 {act_beg_str} 计算结束时间: {exp_end_str}"
|
||||||
|
)
|
||||||
|
if not self._logTimeStep("结束时间", result.expect_end_mins, ctx.end_max_diff, result.end_result):
|
||||||
|
return False
|
||||||
|
act_beg_str = minsToTimeStr(result.actual_begin_mins)
|
||||||
|
act_end_str = minsToTimeStr(result.actual_end_mins)
|
||||||
|
exp_end_str = minsToTimeStr(result.expect_end_mins)
|
||||||
|
self._trace(
|
||||||
|
f"期望预约时间段: {ctx.begin_time} - {exp_end_str}, "
|
||||||
|
f"实际预约时间段: {act_beg_str} - {act_end_str}"
|
||||||
|
)
|
||||||
|
return True
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from .SeatMapDialog import SeatMapDialog
|
||||||
|
from .TimeSelectDialog import TimeSelectDialog
|
||||||
|
from .ReserveResultDialog import ReserveResultDialog
|
||||||
|
from .CheckinResultDialog import CheckinResultDialog
|
||||||
|
from .RenewDialog import RenewDialog
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.MainShell import MainShell
|
||||||
|
from pages.components.CheckinResultDialog import CheckinResultDialog
|
||||||
|
|
||||||
|
|
||||||
|
class CheckinFlow(MsgBase):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
driver: WebDriver,
|
||||||
|
shell: MainShell,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self._driver: WebDriver = driver
|
||||||
|
self._shell: MainShell = shell
|
||||||
|
|
||||||
|
def _ensureCheckinButton(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not self._shell.waitCheckinButton():
|
||||||
|
self._showTrace(f"用户 {username} 签到界面加载失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
if self._shell.isCheckinButtonDisabled():
|
||||||
|
self._showTrace("签到按钮不可用, 可能不在场馆内, 正在尝试启用......")
|
||||||
|
if not self._shell.enableCheckinButtonByJS():
|
||||||
|
self._showTrace(f"签到按钮启用失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
self._showTrace("签到按钮已启用")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _processCheckinDialog(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
with CheckinResultDialog(self._driver) as dialog:
|
||||||
|
result_msg = dialog.getResultMessage()
|
||||||
|
if "签到成功" in result_msg:
|
||||||
|
details = dialog.getDetails()
|
||||||
|
if details:
|
||||||
|
if len(details) >= 5:
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
f" 签到成功 !\n"
|
||||||
|
f" {details[1]}\n"
|
||||||
|
f" {details[2]}\n"
|
||||||
|
f" {details[3]}\n"
|
||||||
|
f" {details[4]}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._showTrace(
|
||||||
|
"\n"
|
||||||
|
" 签到成功 !\n"
|
||||||
|
" 未获取到签到详情 !"
|
||||||
|
)
|
||||||
|
dialog.clickOk()
|
||||||
|
self._showTrace(f"用户 {username} 签到成功 !")
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
failure_reason = result_msg.replace("签到失败", "").strip()
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
" 签到失败 !\n"
|
||||||
|
f" {failure_reason}"
|
||||||
|
)
|
||||||
|
dialog.clickOk()
|
||||||
|
self._showTrace(f"用户 {username} 签到失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
except (TimeoutException, NoSuchElementException, ElementNotInteractableException):
|
||||||
|
self._showTrace("签到时发生未知错误 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def execute(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not self._ensureCheckinButton(username):
|
||||||
|
return False
|
||||||
|
self._shell.clickCheckinButton()
|
||||||
|
return self._processCheckinDialog(username)
|
||||||
@@ -0,0 +1,167 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
NoSuchElementException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.MainShell import MainShell
|
||||||
|
from pages.components.RenewDialog import RenewDialog
|
||||||
|
from pages.flows._helpers import timeStrToMins, minsToTimeStr
|
||||||
|
from pages.strategies.TimeSelectMaker import TimeSelectMaker
|
||||||
|
|
||||||
|
|
||||||
|
class RenewFlow(MsgBase):
|
||||||
|
|
||||||
|
LIBRARY_CLOSE_MINS = TimeSelectMaker.LIBRARY_CLOSE_MINS
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
driver: WebDriver,
|
||||||
|
shell: MainShell,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self._driver: WebDriver = driver
|
||||||
|
self._shell: MainShell = shell
|
||||||
|
|
||||||
|
def _validateRenewTime(
|
||||||
|
self,
|
||||||
|
end_time: str,
|
||||||
|
target_renew_mins: int,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if target_renew_mins > self.LIBRARY_CLOSE_MINS:
|
||||||
|
actual_renew_duration = self.LIBRARY_CLOSE_MINS - timeStrToMins(end_time)
|
||||||
|
if actual_renew_duration <= 0:
|
||||||
|
self._showTrace(
|
||||||
|
f"当前结束时间 {end_time} 已接近闭馆时间,无法续约 !", self.TraceLevel.ERROR
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
self._showTrace(
|
||||||
|
f"续约时间已调整至闭馆时间 "
|
||||||
|
f"{minsToTimeStr(self.LIBRARY_CLOSE_MINS)},"
|
||||||
|
f"实际续约时长为 "
|
||||||
|
f"{actual_renew_duration // 60} 小时 "
|
||||||
|
f"{actual_renew_duration % 60} 分钟"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _computeRenewTarget(
|
||||||
|
self,
|
||||||
|
record: dict,
|
||||||
|
renew_info: dict,
|
||||||
|
):
|
||||||
|
|
||||||
|
end_time = record["time"]["end"]
|
||||||
|
target_renew_mins = timeStrToMins(end_time) + renew_info.get("expect_duration", 2) * 60
|
||||||
|
if not self._validateRenewTime(end_time, target_renew_mins):
|
||||||
|
return None
|
||||||
|
return target_renew_mins
|
||||||
|
|
||||||
|
def _ensureExtendButton(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not self._shell.waitExtendButton():
|
||||||
|
self._showTrace(f"用户 {username} 续约界面加载失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
if self._shell.isExtendButtonDisabled():
|
||||||
|
self._showTrace(
|
||||||
|
f"用户 {username} 续约按钮不可用, 可能不在场馆内, "
|
||||||
|
f"请连接图书馆网络后重试"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _processRenewDialog(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
record: dict,
|
||||||
|
target_renew_mins: int,
|
||||||
|
max_diff: int,
|
||||||
|
prefer_earlier: bool,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
try:
|
||||||
|
with RenewDialog(self._driver) as dialog:
|
||||||
|
if not dialog.waitUntilReady():
|
||||||
|
result_msg = dialog.getResultMessage()
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
f" 续约失败 !\n"
|
||||||
|
f" {result_msg}"
|
||||||
|
)
|
||||||
|
self._shell.refresh()
|
||||||
|
self._showTrace(f"用户 {username} 续约失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
result = dialog.selectBestTime(
|
||||||
|
target_renew_mins,
|
||||||
|
max_diff,
|
||||||
|
prefer_earlier,
|
||||||
|
)
|
||||||
|
if result.selected_index >= 0:
|
||||||
|
abs_diff = abs(result.actual_diff)
|
||||||
|
if result.actual_diff < 0:
|
||||||
|
relation = f"早了 {abs_diff} 分钟"
|
||||||
|
elif result.actual_diff > 0:
|
||||||
|
relation = f"晚了 {abs_diff} 分钟"
|
||||||
|
else:
|
||||||
|
relation = "正好等于 续约时间"
|
||||||
|
self._showTrace(
|
||||||
|
f"选择距离期望续约时间最近的 {result.display_text}, "
|
||||||
|
f"与期望续约时间相比 {relation}"
|
||||||
|
)
|
||||||
|
record["time"]["end"] = result.display_text.strip()
|
||||||
|
dialog.clickOk()
|
||||||
|
self._shell.refresh()
|
||||||
|
return True
|
||||||
|
if not result.free_times:
|
||||||
|
self._showTrace("当前未查询到可用续约时间 !", self.TraceLevel.WARNING)
|
||||||
|
else:
|
||||||
|
self._showTrace(
|
||||||
|
"无法选择最近的可用续约时间 ! "
|
||||||
|
f"所有可选时间与目标时间相差都超过了 {max_diff} 分钟 !",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
self._showTrace(f"当前可供续约的时间有: {result.free_times}")
|
||||||
|
self._shell.refresh()
|
||||||
|
return False
|
||||||
|
except (NoSuchElementException, TimeoutException, ElementNotInteractableException) as e:
|
||||||
|
self._showTrace(f"用户 {username} 续约失败 ! : {e}", self.TraceLevel.ERROR)
|
||||||
|
self._shell.refresh()
|
||||||
|
return False
|
||||||
|
|
||||||
|
def execute(
|
||||||
|
self,
|
||||||
|
username: str,
|
||||||
|
record: dict,
|
||||||
|
renew_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
max_diff = renew_info.get("max_diff", 30)
|
||||||
|
prefer_earlier = renew_info.get("prefer_early", True)
|
||||||
|
target_renew_mins = self._computeRenewTarget(record, renew_info)
|
||||||
|
if target_renew_mins is None:
|
||||||
|
return False
|
||||||
|
if not self._ensureExtendButton(username):
|
||||||
|
return False
|
||||||
|
self._shell.clickExtendButton()
|
||||||
|
return self._processRenewDialog(
|
||||||
|
username, record, target_renew_mins, max_diff, prefer_earlier,
|
||||||
|
)
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
ElementNotInteractableException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
from selenium.webdriver.remote.webdriver import WebDriver
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.MainShell import MainShell
|
||||||
|
from pages.strategies.TimeSelectMaker import TimeSelectMaker
|
||||||
|
from pages.ReserveView import ReserveView
|
||||||
|
from pages.components.ReserveResultDialog import ReserveResultDialog
|
||||||
|
from pages.components.TimeSelectDialog import TimeSelectDialog
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class ReserveContext:
|
||||||
|
|
||||||
|
username: str
|
||||||
|
date: str
|
||||||
|
floor: str
|
||||||
|
room: str
|
||||||
|
seat_id: str
|
||||||
|
begin_time: str
|
||||||
|
end_time: str
|
||||||
|
begin_max_diff: int = 30
|
||||||
|
end_max_diff: int = 30
|
||||||
|
begin_prefer_early: bool = True
|
||||||
|
end_prefer_early: bool = False
|
||||||
|
expect_duration: int = 4
|
||||||
|
satisfy_duration: bool = True
|
||||||
|
|
||||||
|
|
||||||
|
class ReserveFlow(MsgBase):
|
||||||
|
|
||||||
|
LIBRARY_CLOSE_MINS = TimeSelectMaker.LIBRARY_CLOSE_MINS
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
driver: WebDriver,
|
||||||
|
shell: MainShell,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self._driver: WebDriver = driver
|
||||||
|
self._shell: MainShell = shell
|
||||||
|
|
||||||
|
def _loadReserveView(
|
||||||
|
self,
|
||||||
|
) -> ReserveView | None:
|
||||||
|
|
||||||
|
try:
|
||||||
|
return self._shell.gotoReserveView()
|
||||||
|
except (TimeoutException, ElementNotInteractableException) as e:
|
||||||
|
self._showTrace(f"加载预约选座页面失败 ! : {e}", self.TraceLevel.ERROR)
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _selectDate(
|
||||||
|
self,
|
||||||
|
view: ReserveView,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not view.selectDate(ctx.date):
|
||||||
|
self._showTrace(f"选择日期失败 ! : {ctx.date} 不可用", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
self._showTrace(f"日期 {ctx.date} 选择成功 !")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _selectPlace(
|
||||||
|
self,
|
||||||
|
view: ReserveView,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not view.selectPlace("1"):
|
||||||
|
self._showTrace("选择预约场所失败 ! : 图书馆 不可用", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
self._showTrace("预约场所 图书馆 选择成功 !")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _selectFloor(
|
||||||
|
self,
|
||||||
|
view: ReserveView,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not view.selectFloor(ctx.floor):
|
||||||
|
display_floor = ReserveView.FLOOR_MAP.get(ctx.floor, ctx.floor)
|
||||||
|
self._showTrace(f"选择楼层失败 ! : {display_floor} 不可用", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
self._showTrace(f"楼层 {ReserveView.FLOOR_MAP.get(ctx.floor)} 选择成功 !")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _selectRoom(
|
||||||
|
self,
|
||||||
|
view: ReserveView,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
):
|
||||||
|
|
||||||
|
seat_map = view.selectRoom(ctx.room)
|
||||||
|
if seat_map is None:
|
||||||
|
display_room = ReserveView.ROOM_MAP.get(ctx.room, ctx.room)
|
||||||
|
self._showTrace(f"选择房间失败 ! : {display_room} 不可用", self.TraceLevel.ERROR)
|
||||||
|
return None
|
||||||
|
self._showTrace(f"房间 {ReserveView.ROOM_MAP.get(ctx.room)} 选择成功 !")
|
||||||
|
return seat_map
|
||||||
|
|
||||||
|
def _processReserveResult(
|
||||||
|
self,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
with ReserveResultDialog(self._driver) as result:
|
||||||
|
if result.isFailure():
|
||||||
|
self._showTrace("预约失败", self.TraceLevel.ERROR)
|
||||||
|
elif result.isSuccess():
|
||||||
|
details = result.getDetailTexts()
|
||||||
|
if len(details) >= 6:
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
f" 预约成功 !\n"
|
||||||
|
f" {details[1]}\n"
|
||||||
|
f" {details[2]}\n"
|
||||||
|
f" {details[3]}\n"
|
||||||
|
f" 签到时间 :{details[5]}"
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._showTrace(
|
||||||
|
"\n"
|
||||||
|
" 预约成功 !\n"
|
||||||
|
" 未找获取到详细信息"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._showTrace("预约结果加载失败 !", self.TraceLevel.ERROR)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _selectSeatAndSubmit(
|
||||||
|
self,
|
||||||
|
view: ReserveView,
|
||||||
|
seat_map,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
) -> tuple[bool, bool]:
|
||||||
|
|
||||||
|
submit_reserve = False
|
||||||
|
reserve_success = False
|
||||||
|
|
||||||
|
seat_status = seat_map.selectSeat(ctx.seat_id)
|
||||||
|
if seat_status is None:
|
||||||
|
self._showTrace(
|
||||||
|
f"座位 {ctx.seat_id} 在该楼层区域中不存在, 请检查座位号是否正确",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
self._showTrace(f"座位 {ctx.seat_id} 选择成功 ! : 当前状态 - '{seat_status}'")
|
||||||
|
try:
|
||||||
|
time_dialog = TimeSelectDialog(self._driver, tracer=self._showTrace)
|
||||||
|
except TimeoutException:
|
||||||
|
self._showTrace("时间选择面板未出现 !", self.TraceLevel.ERROR)
|
||||||
|
else:
|
||||||
|
if not time_dialog.selectSeatTime(ctx):
|
||||||
|
self._showTrace("选择时间失败 !", self.TraceLevel.ERROR)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
view.submitReserve()
|
||||||
|
submit_reserve = True
|
||||||
|
reserve_success = self._processReserveResult()
|
||||||
|
except (TimeoutException, ElementNotInteractableException):
|
||||||
|
self._showTrace("预约提交失败 !", self.TraceLevel.ERROR)
|
||||||
|
return submit_reserve, reserve_success
|
||||||
|
|
||||||
|
def execute(
|
||||||
|
self,
|
||||||
|
ctx: ReserveContext,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
# reserve flow pipeline:
|
||||||
|
# date > place > floor > room > seat (begin/end time) > submit > result
|
||||||
|
view = self._loadReserveView()
|
||||||
|
if view is None:
|
||||||
|
return False
|
||||||
|
if not self._selectDate(view, ctx):
|
||||||
|
return False
|
||||||
|
if not self._selectPlace(view):
|
||||||
|
return False
|
||||||
|
if not self._selectFloor(view, ctx):
|
||||||
|
return False
|
||||||
|
seat_map = self._selectRoom(view, ctx)
|
||||||
|
if seat_map is None:
|
||||||
|
return False
|
||||||
|
have_hover_on_page = True
|
||||||
|
submit_reserve, reserve_success = self._selectSeatAndSubmit(
|
||||||
|
view, seat_map, ctx,
|
||||||
|
)
|
||||||
|
if not submit_reserve and have_hover_on_page:
|
||||||
|
view.refresh()
|
||||||
|
if reserve_success:
|
||||||
|
self._showTrace(f"用户 {ctx.username} 预约成功 !")
|
||||||
|
else:
|
||||||
|
self._showTrace(f"用户 {ctx.username} 预约失败 !", self.TraceLevel.ERROR)
|
||||||
|
return reserve_success
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from .ReserveFlow import ReserveFlow
|
||||||
|
from .CheckinFlow import CheckinFlow
|
||||||
|
from .RenewFlow import RenewFlow
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from pages.strategies.TimeSelectMaker import (
|
||||||
|
minsToTimeStr,
|
||||||
|
timeStrToMins,
|
||||||
|
)
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import base64
|
||||||
|
import queue
|
||||||
|
|
||||||
|
import ddddocr
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.LoginPage import LoginPage
|
||||||
|
|
||||||
|
|
||||||
|
class CaptchaSolver(MsgBase):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
self._ocr = ddddocr.DdddOcr()
|
||||||
|
|
||||||
|
def _autoRecognize(
|
||||||
|
self,
|
||||||
|
login_page: LoginPage,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
try:
|
||||||
|
img_src = login_page.getCaptchaImageSrc()
|
||||||
|
if img_src is None:
|
||||||
|
self._showTrace("验证码图片元素定位时发生错误 !", self.TraceLevel.ERROR)
|
||||||
|
return ""
|
||||||
|
base64_str = img_src.split(',', 1)[1]
|
||||||
|
captcha_img = base64.b64decode(base64_str)
|
||||||
|
captcha_text = self._ocr.classification(captcha_img)
|
||||||
|
captcha_text = ''.join(filter(str.isalnum, captcha_text)).lower()
|
||||||
|
self._showTrace(f"识别到验证码为 : '{captcha_text}'", 20, no_log=True)
|
||||||
|
if len(captcha_text) != 4:
|
||||||
|
self._showLog("识别到的验证码长度不等于 4 个字符 !", self.TraceLevel.WARNING)
|
||||||
|
return ""
|
||||||
|
return captcha_text
|
||||||
|
except ValueError as e:
|
||||||
|
self._showTrace(f"验证码识别失败 ! : {e}", self.TraceLevel.ERROR)
|
||||||
|
return ""
|
||||||
|
|
||||||
|
def _manualRecognize(
|
||||||
|
self,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
self._showMsg("请输入验证码:")
|
||||||
|
captcha_text = self._waitMsg(timeout=15)
|
||||||
|
self._showTrace(f"输入的验证码为 : '{captcha_text}'", 20, no_log=True)
|
||||||
|
if len(captcha_text) != 4:
|
||||||
|
self._showLog("输入的验证码长度不等于 4 个字符 !", self.TraceLevel.WARNING)
|
||||||
|
return ""
|
||||||
|
return captcha_text
|
||||||
|
|
||||||
|
def solveCaptcha(
|
||||||
|
self,
|
||||||
|
login_page: LoginPage,
|
||||||
|
auto_captcha: bool = True,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
max_attempts = 3
|
||||||
|
for _ in range(max_attempts):
|
||||||
|
if auto_captcha:
|
||||||
|
captcha_text = self._autoRecognize(login_page)
|
||||||
|
else:
|
||||||
|
self._showTrace("用户未配置自动识别验证码, 请手动输入验证码 !", 20, no_log=True)
|
||||||
|
captcha_text = self._manualRecognize()
|
||||||
|
if captcha_text:
|
||||||
|
return captcha_text
|
||||||
|
else:
|
||||||
|
if not login_page.refreshCaptcha():
|
||||||
|
return ""
|
||||||
|
self._showTrace(
|
||||||
|
f"验证码识别失败 {max_attempts} 次, 达到最大尝试次数 !",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
return ""
|
||||||
@@ -0,0 +1,309 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
import re
|
||||||
|
import time
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
|
from selenium.common.exceptions import (
|
||||||
|
NoSuchElementException,
|
||||||
|
StaleElementReferenceException,
|
||||||
|
TimeoutException,
|
||||||
|
)
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.MainShell import MainShell
|
||||||
|
from pages.RecordsView import RecordsView
|
||||||
|
|
||||||
|
|
||||||
|
class RecordChecker(MsgBase):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _formatDiffTime(
|
||||||
|
seconds: float,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
hours = int(seconds//3600)
|
||||||
|
minutes = int(seconds%3600//60)
|
||||||
|
seconds = int(seconds%60)
|
||||||
|
return f"{hours} 时 {minutes} 分 {seconds} 秒"
|
||||||
|
|
||||||
|
def _decodeReserveTime(
|
||||||
|
self,
|
||||||
|
time_element,
|
||||||
|
) -> dict:
|
||||||
|
|
||||||
|
time_str = time_element.text.strip()
|
||||||
|
today = datetime.now().date()
|
||||||
|
if "明天" in time_str:
|
||||||
|
target_date = today + timedelta(days=1)
|
||||||
|
date = target_date.strftime("%Y-%m-%d")
|
||||||
|
elif "今天" in time_str:
|
||||||
|
target_date = today
|
||||||
|
date = target_date.strftime("%Y-%m-%d")
|
||||||
|
elif "昨天" in time_str:
|
||||||
|
target_date = today - timedelta(days=1)
|
||||||
|
date = target_date.strftime("%Y-%m-%d")
|
||||||
|
else:
|
||||||
|
date_match = re.search(r"(\d{4}-\d{1,2}-\d{1,2})", time_str)
|
||||||
|
if date_match:
|
||||||
|
date = date_match.group(1)
|
||||||
|
else:
|
||||||
|
date = ""
|
||||||
|
time_match = re.search(
|
||||||
|
r"(\d{1,2}:\d{2}) -- (\d{1,2}:\d{2})", time_str
|
||||||
|
)
|
||||||
|
if time_match:
|
||||||
|
begin_time = time_match.group(1)
|
||||||
|
end_time = time_match.group(2)
|
||||||
|
else:
|
||||||
|
begin_time = ""
|
||||||
|
end_time = ""
|
||||||
|
return {
|
||||||
|
"date": date,
|
||||||
|
"time": {"begin": begin_time, "end": end_time},
|
||||||
|
}
|
||||||
|
|
||||||
|
def _decodeReserveInfo(
|
||||||
|
self,
|
||||||
|
info_elements,
|
||||||
|
) -> dict:
|
||||||
|
|
||||||
|
location = ""
|
||||||
|
status = ""
|
||||||
|
for info in info_elements:
|
||||||
|
if "已预约" in info.text:
|
||||||
|
status = "已预约"
|
||||||
|
elif "使用中" in info.text:
|
||||||
|
status = "使用中"
|
||||||
|
elif "已完成" in info.text:
|
||||||
|
status = "已完成"
|
||||||
|
elif "已结束使用" in info.text:
|
||||||
|
status = "已结束使用"
|
||||||
|
elif "已取消" in info.text:
|
||||||
|
status = "已取消"
|
||||||
|
elif "失约" in info.text:
|
||||||
|
status = "失约"
|
||||||
|
elif "图书馆" in info.text:
|
||||||
|
location = info.text.strip()
|
||||||
|
return {"location": location, "status": status}
|
||||||
|
|
||||||
|
def _decodeReserveRecord(
|
||||||
|
self,
|
||||||
|
reservation,
|
||||||
|
records_view: RecordsView,
|
||||||
|
) -> dict:
|
||||||
|
|
||||||
|
try:
|
||||||
|
time_element = records_view.getRecordTimeElement(reservation)
|
||||||
|
info_elements = records_view.getRecordInfoElements(reservation)
|
||||||
|
except (NoSuchElementException, StaleElementReferenceException):
|
||||||
|
return {
|
||||||
|
"date": "",
|
||||||
|
"time": {"begin": "", "end": ""},
|
||||||
|
"info": {"location": "", "status": ""},
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
time_data = self._decodeReserveTime(time_element)
|
||||||
|
info_data = self._decodeReserveInfo(info_elements)
|
||||||
|
except StaleElementReferenceException:
|
||||||
|
return {
|
||||||
|
"date": "",
|
||||||
|
"time": {"begin": "", "end": ""},
|
||||||
|
"info": {"location": "", "status": ""},
|
||||||
|
}
|
||||||
|
return {
|
||||||
|
"date": time_data["date"],
|
||||||
|
"time": time_data["time"],
|
||||||
|
"info": info_data,
|
||||||
|
}
|
||||||
|
|
||||||
|
def _getReserveRecord(
|
||||||
|
self,
|
||||||
|
shell: MainShell,
|
||||||
|
wanted_date: str,
|
||||||
|
wanted_status: str,
|
||||||
|
) -> dict | None:
|
||||||
|
|
||||||
|
if wanted_date is None:
|
||||||
|
self._showTrace("日期未指定, 无法检查当前预约状态", self.TraceLevel.WARNING)
|
||||||
|
return None
|
||||||
|
self._showTrace(
|
||||||
|
f"正在检查用户在 {wanted_date} 是否有预约状态为 "
|
||||||
|
f"{wanted_status} 的预约记录......", 20, no_log=True
|
||||||
|
)
|
||||||
|
|
||||||
|
checked_count = 0
|
||||||
|
max_check_times = 6
|
||||||
|
|
||||||
|
records_view = shell.gotoRecordsView()
|
||||||
|
for _ in range(max_check_times):
|
||||||
|
try:
|
||||||
|
reservations = records_view.loadRecords()
|
||||||
|
except TimeoutException:
|
||||||
|
reservations = None
|
||||||
|
if reservations is None:
|
||||||
|
return None
|
||||||
|
for reservation in reservations[checked_count:]:
|
||||||
|
record = self._decodeReserveRecord(reservation, records_view)
|
||||||
|
checked_count += 1
|
||||||
|
if record is None:
|
||||||
|
continue
|
||||||
|
if record["date"] == "":
|
||||||
|
continue
|
||||||
|
if record["time"] == {"begin": "", "end": ""}:
|
||||||
|
continue
|
||||||
|
if (
|
||||||
|
datetime.strptime(record["date"], "%Y-%m-%d").date()
|
||||||
|
> datetime.strptime(wanted_date, "%Y-%m-%d").date()
|
||||||
|
):
|
||||||
|
continue
|
||||||
|
if (
|
||||||
|
datetime.strptime(record["date"], "%Y-%m-%d").date()
|
||||||
|
< datetime.strptime(wanted_date, "%Y-%m-%d").date()
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
if record["info"]["status"] == wanted_status:
|
||||||
|
self._showTrace(
|
||||||
|
f"寻找到用户第 {checked_count} 条状态为 "
|
||||||
|
f"{wanted_status} 的预约记录, "
|
||||||
|
f"详细信息: {record["date"]} "
|
||||||
|
f"{record["time"]["begin"]} - "
|
||||||
|
f"{record["time"]["end"]} "
|
||||||
|
f"{record["info"]["location"]}",
|
||||||
|
20, no_log=True,
|
||||||
|
)
|
||||||
|
return record
|
||||||
|
if not records_view.showMoreRecords():
|
||||||
|
break
|
||||||
|
return None
|
||||||
|
|
||||||
|
def canReserve(
|
||||||
|
self,
|
||||||
|
shell: MainShell,
|
||||||
|
date: str,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if self._getReserveRecord(shell, date, "已预约") is None:
|
||||||
|
if self._getReserveRecord(shell, date, "使用中") is None:
|
||||||
|
self._showTrace(f"用户在 {date} 可以预约")
|
||||||
|
return True
|
||||||
|
self._showTrace(f"用户在 {date} 有使用中的预约, 无法预约")
|
||||||
|
return False
|
||||||
|
self._showTrace(f"用户在 {date} 已存在有效预约, 无法预约")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def canCheckin(
|
||||||
|
self,
|
||||||
|
shell: MainShell,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
date = time.strftime("%Y-%m-%d", time.localtime())
|
||||||
|
record = self._getReserveRecord(shell, date, "已预约")
|
||||||
|
if record is not None:
|
||||||
|
begin_time = record["time"]["begin"]
|
||||||
|
begin_time = datetime.strptime(
|
||||||
|
f"{date} {begin_time}", "%Y-%m-%d %H:%M"
|
||||||
|
)
|
||||||
|
time_diff = datetime.now() - begin_time
|
||||||
|
time_diff_seconds = time_diff.total_seconds()
|
||||||
|
if time_diff_seconds < -30 * 60:
|
||||||
|
self._showTrace(
|
||||||
|
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
||||||
|
f"当前距离预约开始时间还有 "
|
||||||
|
f"{self._formatDiffTime(abs(time_diff_seconds))}, 无法签到"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
elif -30 * 60 <= time_diff_seconds < 0:
|
||||||
|
self._showTrace(
|
||||||
|
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
||||||
|
f"当前距离预约开始时间还有 "
|
||||||
|
f"{self._formatDiffTime(abs(time_diff_seconds))}, 可以签到"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
elif 0 <= time_diff_seconds < 30*60 - 5:
|
||||||
|
self._showTrace(
|
||||||
|
f"用户在 {date} 的预约开始时间为 {begin_time}, "
|
||||||
|
f"当前距离预约开始时间已经过去 "
|
||||||
|
f"{self._formatDiffTime(abs(time_diff_seconds))}, 可以签到"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法签到")
|
||||||
|
return False
|
||||||
|
|
||||||
|
def canRenew(
|
||||||
|
self,
|
||||||
|
shell: MainShell,
|
||||||
|
) -> tuple[bool, dict]:
|
||||||
|
|
||||||
|
date = time.strftime("%Y-%m-%d", time.localtime())
|
||||||
|
record = self._getReserveRecord(shell, date, "使用中")
|
||||||
|
if record is not None:
|
||||||
|
end_time = record["time"]["end"]
|
||||||
|
end_time = datetime.strptime(
|
||||||
|
f"{date} {end_time}", "%Y-%m-%d %H:%M"
|
||||||
|
)
|
||||||
|
time_diff = end_time - datetime.now()
|
||||||
|
time_diff_seconds = time_diff.total_seconds()
|
||||||
|
trace_msg = (
|
||||||
|
f"用户在 {date} 的预约结束时间为 {end_time}, "
|
||||||
|
f"当前距离预约结束时间还有 "
|
||||||
|
f"{self._formatDiffTime(abs(time_diff_seconds))}"
|
||||||
|
)
|
||||||
|
if abs(time_diff_seconds) < 120 * 60:
|
||||||
|
self._showTrace(f"{trace_msg}, 可以续约")
|
||||||
|
return True, record
|
||||||
|
else:
|
||||||
|
self._showTrace(f"{trace_msg}, 无法续约")
|
||||||
|
return False, None
|
||||||
|
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法续约")
|
||||||
|
return False, None
|
||||||
|
|
||||||
|
def postRenewCheck(
|
||||||
|
self,
|
||||||
|
shell: MainShell,
|
||||||
|
record: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
date = record["date"]
|
||||||
|
act_record = self._getReserveRecord(shell, date, "使用中")
|
||||||
|
if act_record is not None:
|
||||||
|
if (
|
||||||
|
act_record["time"]["begin"] == record["time"]["begin"]
|
||||||
|
and act_record["time"]["end"] == record["time"]["end"]
|
||||||
|
):
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
f" 续约成功 !\n"
|
||||||
|
f" 日 期 :{date}\n"
|
||||||
|
f" 时 间 :{act_record["time"]["begin"]}"
|
||||||
|
f" - {act_record["time"]["end"]}\n"
|
||||||
|
f" 位 置 :{act_record["info"]["location"]}\n"
|
||||||
|
f" 状 态 :{act_record["info"]["status"]}"
|
||||||
|
)
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
self._showTrace(
|
||||||
|
f"\n"
|
||||||
|
f" 续约失败 !\n"
|
||||||
|
f" 续约后结束时间为 {act_record["time"]["end"]},"
|
||||||
|
f"与预期结束时间 {record["time"]["end"]} 不符 !"
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
self._showTrace(f"用户在 {date} 没有有效预约记录, 无法检查续约结果")
|
||||||
|
return False
|
||||||
@@ -0,0 +1,232 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
import queue
|
||||||
|
import time
|
||||||
|
|
||||||
|
from base.MsgBase import MsgBase
|
||||||
|
from pages.ReserveView import ReserveView
|
||||||
|
from pages.flows._helpers import timeStrToMins, minsToTimeStr
|
||||||
|
from pages.strategies.TimeSelectMaker import TimeSelectMaker
|
||||||
|
|
||||||
|
|
||||||
|
class ReserveChecker(MsgBase):
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
input_queue: queue.Queue,
|
||||||
|
output_queue: queue.Queue,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
super().__init__(input_queue, output_queue)
|
||||||
|
|
||||||
|
def _containRequiredInfo(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
floor_map = ReserveView.FLOOR_MAP
|
||||||
|
room_map = ReserveView.ROOM_MAP
|
||||||
|
try:
|
||||||
|
if reserve_info.get("floor") is None:
|
||||||
|
raise ValueError("未指定楼层")
|
||||||
|
if reserve_info["floor"] not in floor_map:
|
||||||
|
raise ValueError(f"该楼层 '{reserve_info["floor"]}' 不存在")
|
||||||
|
if reserve_info.get("room") is None:
|
||||||
|
raise ValueError("未指定房间")
|
||||||
|
if reserve_info["room"] not in room_map:
|
||||||
|
raise ValueError(f"该房间 '{reserve_info["room"]}' 不存在")
|
||||||
|
if reserve_info.get("seat_id") is None:
|
||||||
|
raise ValueError("未指定座位")
|
||||||
|
if reserve_info["seat_id"] == "":
|
||||||
|
raise ValueError("未指定座位号")
|
||||||
|
return True
|
||||||
|
except ValueError as e:
|
||||||
|
msg = (
|
||||||
|
f"预约信息错误 ! : {e}, "
|
||||||
|
f"由于缺少必要的预约信息, 无法开始预约流程"
|
||||||
|
)
|
||||||
|
self._showTrace(msg, self.TraceLevel.ERROR)
|
||||||
|
self._showTrace(
|
||||||
|
f"预约信息错误 ! : {e}, "
|
||||||
|
f"由于缺少必要的预约信息, 无法开始预约流程, 请检查预约信息是否完整",
|
||||||
|
20,
|
||||||
|
no_log=True,
|
||||||
|
)
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _isValidDate(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
cur_date_str = time.strftime("%Y-%m-%d", time.localtime())
|
||||||
|
cur_timestamp = time.mktime(time.strptime(cur_date_str, "%Y-%m-%d"))
|
||||||
|
if reserve_info.get("date") is None:
|
||||||
|
reserve_info["date"] = cur_date_str
|
||||||
|
self._showTrace(f"预约日期未指定, 自动设置为当前日期: {cur_date_str}")
|
||||||
|
else:
|
||||||
|
res_timestamp = time.mktime(time.strptime(reserve_info["date"], "%Y-%m-%d"))
|
||||||
|
if res_timestamp < cur_timestamp:
|
||||||
|
self._showTrace(
|
||||||
|
f"预约日期错误 ! :"
|
||||||
|
f"{reserve_info["date"]} 早于当前日期 {cur_date_str}, 自动设置为当前日期",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["date"] = cur_date_str
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _isValidBeginTime(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
cur_time = time.strftime("%H:%M", time.localtime())
|
||||||
|
cur_date = time.strftime("%Y-%m-%d", time.localtime())
|
||||||
|
if reserve_info.get("begin_time") is None:
|
||||||
|
reserve_info["begin_time"] = {}
|
||||||
|
if "time" not in reserve_info["begin_time"]:
|
||||||
|
reserve_info["begin_time"]["time"] = cur_time
|
||||||
|
self._showTrace(f"开始时间未指定, 自动设置为当前时间: {cur_time}")
|
||||||
|
elif reserve_info.get("date") == cur_date:
|
||||||
|
begin_mins = timeStrToMins(reserve_info["begin_time"]["time"])
|
||||||
|
cur_mins = timeStrToMins(cur_time)
|
||||||
|
if begin_mins < cur_mins:
|
||||||
|
self._showTrace(
|
||||||
|
f"开始时间 {reserve_info['begin_time']['time']} 已过当前时间 {cur_time}, "
|
||||||
|
f"自动调整为当前时间",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["begin_time"]["time"] = cur_time
|
||||||
|
if "max_diff" not in reserve_info["begin_time"]:
|
||||||
|
reserve_info["begin_time"]["max_diff"] = 30
|
||||||
|
self._showTrace("开始时间最大时间差未指定, 自动设置为 30 分钟")
|
||||||
|
if "prefer_early" not in reserve_info["begin_time"]:
|
||||||
|
reserve_info["begin_time"]["prefer_early"] = True
|
||||||
|
self._showTrace("是否优先选择更早开始时间未指定, 自动设置为 True")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _isValidExpectDuration(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if reserve_info.get("satisfy_duration") is None:
|
||||||
|
reserve_info["satisfy_duration"] = True
|
||||||
|
self._showTrace("预约满足时长要求未指定, 默认满足")
|
||||||
|
if reserve_info["satisfy_duration"]:
|
||||||
|
if reserve_info.get("expect_duration") is None:
|
||||||
|
reserve_info["expect_duration"] = 4
|
||||||
|
self._showTrace("需要满足预约持续时间, 但未指定, 使用默认时长为 4 小时")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _isValidEndTime(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if reserve_info.get("end_time") is None:
|
||||||
|
reserve_info["end_time"] = {}
|
||||||
|
if "time" not in reserve_info["end_time"]:
|
||||||
|
end_mins = timeStrToMins(reserve_info["begin_time"]["time"])
|
||||||
|
end_mins = end_mins + int(reserve_info["expect_duration"] * 60)
|
||||||
|
reserve_info["end_time"] = {
|
||||||
|
"time": minsToTimeStr(end_mins),
|
||||||
|
"max_diff": 30,
|
||||||
|
"prefer_early": False,
|
||||||
|
}
|
||||||
|
self._showTrace(
|
||||||
|
f"结束时间未指定, 自动设置为开始时间加上期望时长: "
|
||||||
|
f"{reserve_info["end_time"]["time"]}"
|
||||||
|
)
|
||||||
|
if "max_diff" not in reserve_info["end_time"]:
|
||||||
|
reserve_info["end_time"]["max_diff"] = 30
|
||||||
|
self._showTrace("结束时间最大时间差未指定, 自动设置为 30 分钟")
|
||||||
|
if "prefer_early" not in reserve_info["end_time"]:
|
||||||
|
reserve_info["end_time"]["prefer_early"] = False
|
||||||
|
self._showTrace("是否优先选择较晚结束时间未指定, 自动设置为 True")
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _finalCheck(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
begin_time = reserve_info["begin_time"]
|
||||||
|
end_time = reserve_info["end_time"]
|
||||||
|
begin_mins = timeStrToMins(begin_time["time"])
|
||||||
|
end_mins = timeStrToMins(end_time["time"])
|
||||||
|
if end_mins < begin_mins and reserve_info["satisfy_duration"] is False:
|
||||||
|
self._showTrace(
|
||||||
|
f"结束时间 {end_time["time"]} 早于开始时间 {begin_time["time"]}, "
|
||||||
|
f"尝试交换时间",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["end_time"], reserve_info["begin_time"] = begin_time, end_time
|
||||||
|
begin_time, end_time = end_time, begin_time
|
||||||
|
begin_mins = timeStrToMins(begin_time["time"])
|
||||||
|
end_mins = timeStrToMins(end_time["time"])
|
||||||
|
max_end_mins = TimeSelectMaker.LIBRARY_CLOSE_MINS
|
||||||
|
if end_mins > max_end_mins:
|
||||||
|
close_time_str = minsToTimeStr(TimeSelectMaker.LIBRARY_CLOSE_MINS)
|
||||||
|
self._showTrace(
|
||||||
|
f"结束时间 {end_time["time"]} 晚于 {close_time_str}, "
|
||||||
|
f"自动设置为 {close_time_str}",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["end_time"]["time"] = close_time_str
|
||||||
|
end_mins = max_end_mins
|
||||||
|
if reserve_info["satisfy_duration"]:
|
||||||
|
if reserve_info["expect_duration"] > 8:
|
||||||
|
self._showTrace(
|
||||||
|
f"该用户设置了优先满足时长要求, 但是预约期望持续时间 "
|
||||||
|
f"{reserve_info["expect_duration"]} 小时 "
|
||||||
|
f"超出最大时长 8 小时, 自动设置为 8 小时",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["expect_duration"] = 8
|
||||||
|
else:
|
||||||
|
if end_mins - begin_mins > 8*60:
|
||||||
|
self._showTrace(
|
||||||
|
f"该用户未设置优先满足时长要求, 但是检查到预约持续时间 "
|
||||||
|
f"{float((end_mins - begin_mins) / 60)} 小时 "
|
||||||
|
f"超出最大时长 8 小时, 自动设置为 8 小时",
|
||||||
|
self.TraceLevel.WARNING,
|
||||||
|
)
|
||||||
|
reserve_info["end_time"]["time"] = minsToTimeStr(begin_mins + 8*60)
|
||||||
|
return True
|
||||||
|
|
||||||
|
def check(
|
||||||
|
self,
|
||||||
|
reserve_info: dict,
|
||||||
|
) -> bool:
|
||||||
|
|
||||||
|
if not self._containRequiredInfo(reserve_info):
|
||||||
|
return False
|
||||||
|
if not self._isValidDate(reserve_info):
|
||||||
|
return False
|
||||||
|
if not self._isValidBeginTime(reserve_info):
|
||||||
|
return False
|
||||||
|
if not self._isValidExpectDuration(reserve_info):
|
||||||
|
return False
|
||||||
|
if not self._isValidEndTime(reserve_info):
|
||||||
|
return False
|
||||||
|
if not self._finalCheck(reserve_info):
|
||||||
|
return False
|
||||||
|
self._showTrace(
|
||||||
|
f"预约信息检查完成, 准备预约 "
|
||||||
|
f"{reserve_info["date"]} "
|
||||||
|
f"{reserve_info["begin_time"]["time"]} - "
|
||||||
|
f"{reserve_info["end_time"]["time"]} "
|
||||||
|
f"图书馆 "
|
||||||
|
f"{ReserveView.FLOOR_MAP[reserve_info["floor"]]} "
|
||||||
|
f"{ReserveView.ROOM_MAP[reserve_info["room"]]} "
|
||||||
|
f"的座位 {reserve_info["seat_id"]}"
|
||||||
|
)
|
||||||
|
return True
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from .CaptchaSolver import CaptchaSolver
|
||||||
|
from .ReserveChecker import ReserveChecker
|
||||||
|
from .RecordChecker import RecordChecker
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from abc import ABC, abstractmethod
|
||||||
|
from dataclasses import dataclass, field
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
def timeStrToMins(
|
||||||
|
time_str: str,
|
||||||
|
) -> int:
|
||||||
|
|
||||||
|
hour, minute = map(int, time_str.split(":"))
|
||||||
|
return hour*60 + minute
|
||||||
|
|
||||||
|
def minsToTimeStr(
|
||||||
|
mins: int,
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
hour, minute = divmod(int(mins), 60)
|
||||||
|
return f"{hour:02d}:{minute:02d}"
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TimeOption:
|
||||||
|
|
||||||
|
value: int
|
||||||
|
element_text: str
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TimeSelectionResult:
|
||||||
|
|
||||||
|
selected_index: int = -1
|
||||||
|
selected_value: int = 0
|
||||||
|
display_text: str = ""
|
||||||
|
actual_diff: int = 0
|
||||||
|
free_times: list[str] = field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class TimeRangeResult:
|
||||||
|
|
||||||
|
begin_result: TimeSelectionResult = field(default_factory=TimeSelectionResult)
|
||||||
|
end_result: TimeSelectionResult = field(default_factory=TimeSelectionResult)
|
||||||
|
actual_begin_mins: int = -1
|
||||||
|
actual_end_mins: int = -1
|
||||||
|
expect_end_mins: int = 0
|
||||||
|
|
||||||
|
|
||||||
|
class TimeOptionReader(ABC):
|
||||||
|
|
||||||
|
@abstractmethod
|
||||||
|
def readOptions(
|
||||||
|
self,
|
||||||
|
elements: list
|
||||||
|
) -> list[TimeOption]:
|
||||||
|
...
|
||||||
|
|
||||||
|
def formatFreeTime(
|
||||||
|
self,
|
||||||
|
opt: TimeOption
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
return opt.element_text
|
||||||
|
|
||||||
|
|
||||||
|
class ReserveTimeReader(TimeOptionReader):
|
||||||
|
"""
|
||||||
|
Reads the ``time`` HTML attribute for the reserve flow.
|
||||||
|
Special value ``"now"`` is resolved to the current wall-clock minute.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def readOptions(
|
||||||
|
self,
|
||||||
|
elements: list
|
||||||
|
) -> list[TimeOption]:
|
||||||
|
|
||||||
|
options: list[TimeOption] = []
|
||||||
|
for el in elements:
|
||||||
|
time_attr = el.get_attribute("time")
|
||||||
|
if time_attr == "now":
|
||||||
|
now = datetime.now()
|
||||||
|
value = now.hour * 60 + now.minute
|
||||||
|
elif time_attr and time_attr.isdigit():
|
||||||
|
value = int(time_attr)
|
||||||
|
else:
|
||||||
|
continue
|
||||||
|
options.append(TimeOption(value=value, element_text=el.text.strip()))
|
||||||
|
return options
|
||||||
|
|
||||||
|
def formatFreeTime(
|
||||||
|
self,
|
||||||
|
opt: TimeOption
|
||||||
|
) -> str:
|
||||||
|
|
||||||
|
return minsToTimeStr(opt.value)
|
||||||
|
|
||||||
|
|
||||||
|
class RenewTimeReader(TimeOptionReader):
|
||||||
|
"""
|
||||||
|
Reads the ``id`` HTML attribute for the renewal flow.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def readOptions(
|
||||||
|
self,
|
||||||
|
elements: list
|
||||||
|
) -> list[TimeOption]:
|
||||||
|
|
||||||
|
options: list[TimeOption] = []
|
||||||
|
for el in elements:
|
||||||
|
time_attr = el.get_attribute("id")
|
||||||
|
if not (time_attr and time_attr.isdigit()):
|
||||||
|
continue
|
||||||
|
options.append(TimeOption(value=int(time_attr), element_text=el.text.strip()))
|
||||||
|
return options
|
||||||
|
|
||||||
|
|
||||||
|
class TimeDecisionMaker:
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
reader: TimeOptionReader
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self._reader = reader
|
||||||
|
|
||||||
|
def decide(
|
||||||
|
self,
|
||||||
|
elements: list,
|
||||||
|
target_time: int,
|
||||||
|
max_time_diff: int,
|
||||||
|
prefer_earlier: bool
|
||||||
|
) -> TimeSelectionResult:
|
||||||
|
|
||||||
|
options = self._reader.readOptions(elements)
|
||||||
|
free_times = [self._reader.formatFreeTime(o) for o in options]
|
||||||
|
best_diff = max_time_diff
|
||||||
|
best_actual_diff = None
|
||||||
|
best_index = -1
|
||||||
|
for i, opt in enumerate(options):
|
||||||
|
actual_diff = opt.value - target_time
|
||||||
|
abs_diff = abs(actual_diff)
|
||||||
|
if abs_diff < best_diff or (
|
||||||
|
abs_diff == best_diff
|
||||||
|
and (
|
||||||
|
(prefer_earlier and actual_diff <= 0)
|
||||||
|
or (not prefer_earlier and actual_diff >= 0)
|
||||||
|
)
|
||||||
|
):
|
||||||
|
best_diff = abs_diff
|
||||||
|
best_actual_diff = actual_diff
|
||||||
|
best_index = i
|
||||||
|
if best_index == -1:
|
||||||
|
return TimeSelectionResult(free_times=free_times)
|
||||||
|
chosen = options[best_index]
|
||||||
|
return TimeSelectionResult(
|
||||||
|
selected_index=best_index,
|
||||||
|
selected_value=chosen.value,
|
||||||
|
display_text=chosen.element_text,
|
||||||
|
actual_diff=best_actual_diff or 0,
|
||||||
|
free_times=free_times,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TimeSelectMaker:
|
||||||
|
|
||||||
|
LIBRARY_CLOSE_MINS = 1350 # 22:30
|
||||||
|
MAX_DURATION_HOURS = 8
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def calcEndTime(
|
||||||
|
begin_mins: int,
|
||||||
|
duration: int,
|
||||||
|
library_close_mins: int = LIBRARY_CLOSE_MINS
|
||||||
|
) -> int:
|
||||||
|
|
||||||
|
expect_end_mins = int(begin_mins + duration*60)
|
||||||
|
if expect_end_mins > library_close_mins:
|
||||||
|
return library_close_mins
|
||||||
|
return expect_end_mins
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def calcRemainingDuration(
|
||||||
|
end_time_str: str,
|
||||||
|
target_mins: int,
|
||||||
|
library_close_mins: int = LIBRARY_CLOSE_MINS
|
||||||
|
) -> int:
|
||||||
|
|
||||||
|
return library_close_mins - timeStrToMins(end_time_str)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def forReserve(
|
||||||
|
) -> TimeDecisionMaker:
|
||||||
|
|
||||||
|
return TimeDecisionMaker(ReserveTimeReader())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def forRenew(
|
||||||
|
) -> TimeDecisionMaker:
|
||||||
|
|
||||||
|
return TimeDecisionMaker(RenewTimeReader())
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
This software is provided "as is", without any warranty of any kind.
|
||||||
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
|
See the LICENSE file for details.
|
||||||
|
"""
|
||||||
|
from .TimeSelectMaker import (
|
||||||
|
TimeSelectMaker,
|
||||||
|
TimeDecisionMaker,
|
||||||
|
TimeOptionReader,
|
||||||
|
ReserveTimeReader,
|
||||||
|
RenewTimeReader,
|
||||||
|
TimeOption,
|
||||||
|
TimeSelectionResult,
|
||||||
|
TimeRangeResult,
|
||||||
|
)
|
||||||
@@ -1,8 +1,9 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
"""
|
"""
|
||||||
Utils module for the AutoLibrary project.
|
Copyright (c) 2026 KenanZhu.
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
Here are the classes and modules in this package:
|
This software is provided "as is", without any warranty of any kind.
|
||||||
- TimerUtils: Timer utils class for the AutoLibrary project.
|
You may use, modify, and distribute this file under the terms of the MIT License.
|
||||||
- JSONReader: JSON reader class for the AutoLibrary project.
|
See the LICENSE file for details.
|
||||||
- JSONWriter: JSON writer class for the AutoLibrary project.
|
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in New Issue
Block a user