style: normalize code formatting across the codebase
- Standardize quote style (single to double quotes) - Improve code formatting consistency - Apply formatting to utilities, GUI components, and tools - Update imports and docstrings for consistency Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -2,19 +2,25 @@
|
||||
离散备料计划维护数据提取工具
|
||||
负责登录、批量下载、转换数据
|
||||
"""
|
||||
|
||||
import os
|
||||
import pandas as pd
|
||||
from playwright.sync_api import sync_playwright
|
||||
from utils.excel_converter import ExcelConverter
|
||||
from utils.auth import login, logout
|
||||
from db.production_order_query import read_production_ids, query_production_order_numbers
|
||||
from db.production_order_query import (
|
||||
read_production_ids,
|
||||
query_production_order_numbers,
|
||||
)
|
||||
from typing import Callable, Optional
|
||||
|
||||
|
||||
class DiscreteMaterialPlanExtractor:
|
||||
"""离散备料计划维护数据提取器"""
|
||||
|
||||
def __init__(self, username, password, headless=False, verbose=True, batch_size=100):
|
||||
def __init__(
|
||||
self, username, password, headless=False, verbose=True, batch_size=100
|
||||
):
|
||||
"""
|
||||
初始化提取器
|
||||
|
||||
@@ -38,7 +44,9 @@ class DiscreteMaterialPlanExtractor:
|
||||
if self.verbose:
|
||||
print(*args, **kwargs)
|
||||
|
||||
def _report_progress(self, stage: str, current: int, total: int, message: str, **detail):
|
||||
def _report_progress(
|
||||
self, stage: str, current: int, total: int, message: str, **detail
|
||||
):
|
||||
"""
|
||||
报告进度
|
||||
|
||||
@@ -52,12 +60,13 @@ class DiscreteMaterialPlanExtractor:
|
||||
if self.progress_callback:
|
||||
try:
|
||||
from gui.progress import ProgressInfo
|
||||
|
||||
progress_info = ProgressInfo(
|
||||
stage=stage,
|
||||
current=current,
|
||||
total=total,
|
||||
message=message,
|
||||
detail=detail
|
||||
detail=detail,
|
||||
)
|
||||
self.progress_callback(progress_info)
|
||||
except Exception:
|
||||
@@ -84,16 +93,31 @@ class DiscreteMaterialPlanExtractor:
|
||||
self._print(f"查询到 {len(order_ids)} 个生产订单号")
|
||||
|
||||
if report_progress:
|
||||
self._report_progress('query', 1, 1, f"查询到 {len(order_ids)} 个生产订单号", count=len(order_ids))
|
||||
self._report_progress(
|
||||
"query",
|
||||
1,
|
||||
1,
|
||||
f"查询到 {len(order_ids)} 个生产订单号",
|
||||
count=len(order_ids),
|
||||
)
|
||||
|
||||
return order_ids
|
||||
|
||||
def group_order_ids(self, order_ids, group_size=100):
|
||||
"""将订单号分组"""
|
||||
for i in range(0, len(order_ids), group_size):
|
||||
yield order_ids[i:i + group_size]
|
||||
yield order_ids[i : i + group_size]
|
||||
|
||||
def download_batch(self, inner_frame, order_ids, batch_index, total_batches, page1, debug_mode=False, debug_batch=None):
|
||||
def download_batch(
|
||||
self,
|
||||
inner_frame,
|
||||
order_ids,
|
||||
batch_index,
|
||||
total_batches,
|
||||
page1,
|
||||
debug_mode=False,
|
||||
debug_batch=None,
|
||||
):
|
||||
"""下载一批订单号的数据"""
|
||||
from playwright.sync_api import TimeoutError
|
||||
import re
|
||||
@@ -133,7 +157,11 @@ class DiscreteMaterialPlanExtractor:
|
||||
inner_frame.get_by_text("输出", exact=True).click()
|
||||
|
||||
# 设置行数阈值
|
||||
input_box = inner_frame.locator("div").filter(has_text=re.compile(r"^行数阈值$")).locator("input[type='text']")
|
||||
input_box = (
|
||||
inner_frame.locator("div")
|
||||
.filter(has_text=re.compile(r"^行数阈值$"))
|
||||
.locator("input[type='text']")
|
||||
)
|
||||
input_box.fill("300000")
|
||||
|
||||
# 下载文件
|
||||
@@ -147,11 +175,11 @@ class DiscreteMaterialPlanExtractor:
|
||||
|
||||
# 报告进度
|
||||
self._report_progress(
|
||||
'download',
|
||||
"download",
|
||||
batch_index + 1,
|
||||
total_batches,
|
||||
f"第 {batch_index + 1}/{total_batches} 批下载完成",
|
||||
batch_index=batch_index + 1
|
||||
batch_index=batch_index + 1,
|
||||
)
|
||||
|
||||
# 关闭输出对话框(如果有的话)
|
||||
@@ -188,12 +216,12 @@ class DiscreteMaterialPlanExtractor:
|
||||
|
||||
# 报告进度
|
||||
self._report_progress(
|
||||
'convert',
|
||||
"convert",
|
||||
i,
|
||||
len(file_paths),
|
||||
f"转换第 {i}/{len(file_paths)} 个文件",
|
||||
file_index=i,
|
||||
file_path=file_path
|
||||
file_path=file_path,
|
||||
)
|
||||
|
||||
df = self.converter.convert(file_path, output_file=None) # 只转换,不保存
|
||||
@@ -235,13 +263,23 @@ class DiscreteMaterialPlanExtractor:
|
||||
self._print(f"文本框填充成功: {expected_value}")
|
||||
break
|
||||
else:
|
||||
self._print(f"第 {attempt + 1} 次填充失败,实际值: {actual_value},重试...")
|
||||
self._print(
|
||||
f"第 {attempt + 1} 次填充失败,实际值: {actual_value},重试..."
|
||||
)
|
||||
if attempt == max_retries - 1:
|
||||
self._print(f"警告: {max_retries} 次尝试后仍未成功填充,继续执行...")
|
||||
self._print(
|
||||
f"警告: {max_retries} 次尝试后仍未成功填充,继续执行..."
|
||||
)
|
||||
|
||||
def extract(self, production_id_file, data_dir="D:/python/playwrite/data",
|
||||
output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
|
||||
debug_mode=False, debug_batch=None, progress_callback=None):
|
||||
def extract(
|
||||
self,
|
||||
production_id_file,
|
||||
data_dir="D:/python/playwrite/data",
|
||||
output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
|
||||
debug_mode=False,
|
||||
debug_batch=None,
|
||||
progress_callback=None,
|
||||
):
|
||||
"""
|
||||
执行完整的数据提取流程
|
||||
|
||||
@@ -269,11 +307,11 @@ class DiscreteMaterialPlanExtractor:
|
||||
username=self.username,
|
||||
password=self.password,
|
||||
headless=self.headless,
|
||||
ignore_https_errors=True
|
||||
ignore_https_errors=True,
|
||||
)
|
||||
|
||||
# 登录完成
|
||||
self._report_progress('login', 1, 1, "登录成功")
|
||||
self._report_progress("login", 1, 1, "登录成功")
|
||||
|
||||
self._print("=" * 80)
|
||||
self._print("开始执行离散备料计划维护数据提取")
|
||||
@@ -285,7 +323,9 @@ class DiscreteMaterialPlanExtractor:
|
||||
|
||||
# 点击打开"离散备料计划维护"
|
||||
with page.expect_popup() as page1_info:
|
||||
main_frame.get_by_title("离散备料计划维护", exact=True).first.click()
|
||||
main_frame.get_by_title(
|
||||
"离散备料计划维护", exact=True
|
||||
).first.click()
|
||||
page1 = page1_info.value
|
||||
|
||||
# 获取 nested iframe
|
||||
@@ -298,47 +338,62 @@ class DiscreteMaterialPlanExtractor:
|
||||
self.setup_query_interface(inner_frame)
|
||||
|
||||
# 读取总排号并查询生产订单号
|
||||
order_ids = self.get_production_order_numbers(production_id_file, report_progress=True)
|
||||
order_ids = self.get_production_order_numbers(
|
||||
production_id_file, report_progress=True
|
||||
)
|
||||
|
||||
# 按批次下载
|
||||
downloaded_files = []
|
||||
# 计算总批次数
|
||||
total_batches = sum(1 for _ in self.group_order_ids(order_ids, self.batch_size))
|
||||
total_batches = sum(
|
||||
1 for _ in self.group_order_ids(order_ids, self.batch_size)
|
||||
)
|
||||
|
||||
for batch_index, order_ids_batch in enumerate(self.group_order_ids(order_ids, self.batch_size)):
|
||||
self._print(f"\n=== 开始处理第 {batch_index + 1} 批,共 {len(order_ids_batch)} 个订单号 ===")
|
||||
for batch_index, order_ids_batch in enumerate(
|
||||
self.group_order_ids(order_ids, self.batch_size)
|
||||
):
|
||||
self._print(
|
||||
f"\n=== 开始处理第 {batch_index + 1} 批,共 {len(order_ids_batch)} 个订单号 ==="
|
||||
)
|
||||
|
||||
# 报告开始下载批次
|
||||
self._report_progress(
|
||||
'download',
|
||||
"download",
|
||||
batch_index,
|
||||
total_batches,
|
||||
f"正在下载第 {batch_index + 1}/{total_batches} 批...",
|
||||
batch_index=batch_index + 1,
|
||||
batch_size=len(order_ids_batch)
|
||||
batch_size=len(order_ids_batch),
|
||||
)
|
||||
|
||||
downloaded_file = self.download_batch(
|
||||
inner_frame, order_ids_batch, batch_index, total_batches, page1,
|
||||
debug_mode=debug_mode, debug_batch=debug_batch
|
||||
inner_frame,
|
||||
order_ids_batch,
|
||||
batch_index,
|
||||
total_batches,
|
||||
page1,
|
||||
debug_mode=debug_mode,
|
||||
debug_batch=debug_batch,
|
||||
)
|
||||
downloaded_files.append(downloaded_file)
|
||||
|
||||
# 执行账号注销
|
||||
self._print("\n开始执行账号注销...")
|
||||
self._report_progress('logout', 1, 1, "正在注销账号...")
|
||||
self._report_progress("logout", 1, 1, "正在注销账号...")
|
||||
logout(main_frame, verbose=self.verbose)
|
||||
|
||||
# 转换并合并文件
|
||||
if downloaded_files:
|
||||
self._report_progress(
|
||||
'convert',
|
||||
"convert",
|
||||
0,
|
||||
len(downloaded_files),
|
||||
f"开始转换并合并 {len(downloaded_files)} 个文件",
|
||||
file_count=len(downloaded_files)
|
||||
file_count=len(downloaded_files),
|
||||
)
|
||||
self._print(
|
||||
f"\n=== 开始转换并合并 {len(downloaded_files)} 个文件 ==="
|
||||
)
|
||||
self._print(f"\n=== 开始转换并合并 {len(downloaded_files)} 个文件 ===")
|
||||
self.convert_and_merge_files(downloaded_files, output_file)
|
||||
else:
|
||||
self._print("\n没有下载到任何文件")
|
||||
@@ -347,7 +402,9 @@ class DiscreteMaterialPlanExtractor:
|
||||
self._print(f"最终文件: {output_file}")
|
||||
|
||||
# 报告完成
|
||||
self._report_progress('complete', 1, 1, "提取完成", output_file=output_file)
|
||||
self._report_progress(
|
||||
"complete", 1, 1, "提取完成", output_file=output_file
|
||||
)
|
||||
|
||||
# 关闭浏览器
|
||||
context.close()
|
||||
@@ -359,13 +416,14 @@ class DiscreteMaterialPlanExtractor:
|
||||
# 恢复原始回调
|
||||
self.progress_callback = original_callback
|
||||
|
||||
|
||||
def main():
|
||||
"""测试函数"""
|
||||
extractor = DiscreteMaterialPlanExtractor(
|
||||
username="BLDpengqiangqiang",
|
||||
password="Cqbld123456.",
|
||||
headless=False,
|
||||
verbose=True
|
||||
verbose=True,
|
||||
)
|
||||
|
||||
production_id_file = os.path.join(os.path.dirname(__file__), "productionID.txt")
|
||||
|
||||
Reference in New Issue
Block a user