style: format all Python files with Black

Apply Black formatter to the entire codebase for consistent code style.

Co-Authored-By: Claude (glm-5) <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-26 22:44:03 +08:00
parent 1b16842a2c
commit 3b7c00377f
46 changed files with 1488 additions and 974 deletions

View File

@@ -27,22 +27,24 @@ except ImportError:
# --- 全局日志配置 ---
# 调整格式:增加 [] 使其与 UI 控件的默认风格保持一致
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
LOG_FORMAT = "[%(asctime)s] [%(levelname)s] %(message)s"
DATE_FORMAT = "%Y-%m-%d %H:%M:%S"
logging.basicConfig(
level=logging.INFO,
format=LOG_FORMAT,
datefmt=DATE_FORMAT
)
logging.basicConfig(level=logging.INFO, format=LOG_FORMAT, datefmt=DATE_FORMAT)
logger = logging.getLogger(__name__)
class DiscreteMaterialPlanExtractor:
"""离散备料计划维护数据提取器"""
def __init__(
self, username, password, headless=False, verbose=True, batch_size=100,
enable_db_persistence=False
self,
username,
password,
headless=False,
verbose=True,
batch_size=100,
enable_db_persistence=False,
):
self.username = username
self.password = password
@@ -53,10 +55,11 @@ class DiscreteMaterialPlanExtractor:
self.converter = ExcelConverter(verbose=verbose)
self.enable_db_persistence = enable_db_persistence
self.dao = None
if self.enable_db_persistence:
try:
from db.discrete_material_plan_dao import DiscreteMaterialPlanDAO
self.dao = DiscreteMaterialPlanDAO()
except ImportError:
self._log("无法加载数据库 DAO 模块,持久化功能将不可用", "error")
@@ -67,11 +70,7 @@ class DiscreteMaterialPlanExtractor:
"""
level = level.lower()
# 1. 记录到标准控制台
log_map = {
"info": logger.info,
"warn": logger.warning,
"error": logger.error
}
log_map = {"info": logger.info, "warn": logger.warning, "error": logger.error}
log_func = log_map.get(level, logger.info)
log_func(message)
@@ -80,7 +79,9 @@ class DiscreteMaterialPlanExtractor:
if self.progress_callback:
self._report_progress("log", 0, 0, message, log_level=level.upper())
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
):
"""标准化进度汇报"""
if self.progress_callback and ProgressInfo:
try:
@@ -98,20 +99,30 @@ class DiscreteMaterialPlanExtractor:
def get_production_order_numbers(self, production_id_file, report_progress=False):
"""读取总排号并查询数据库获取生产订单号"""
if report_progress:
self._report_progress("query", 1, 3, "正在读取总排号文件...", action="read_file")
self._report_progress(
"query", 1, 3, "正在读取总排号文件...", action="read_file"
)
production_ids = read_production_ids(production_id_file)
self._log(f"文件读取完成: 找到 {len(production_ids)} 个 Production ID")
if report_progress:
self._report_progress("query", 2, 3, "正在查询数据库获取生产订单号...", action="query_database")
self._report_progress(
"query",
2,
3,
"正在查询数据库获取生产订单号...",
action="query_database",
)
order_ids = query_production_order_numbers(production_ids)
self._log(f"数据库查询完成: 共匹配到 {len(order_ids)} 条生产订单号")
if report_progress:
self._report_progress("query", 3, 3, "订单号查询阶段结束", action="query_complete")
self._report_progress(
"query", 3, 3, "订单号查询阶段结束", action="query_complete"
)
return order_ids
def group_order_ids(self, order_ids, group_size=100):
@@ -121,9 +132,14 @@ class DiscreteMaterialPlanExtractor:
def download_batch(self, inner_frame, order_ids, batch_index, total_batches, page1):
"""执行单批次数据的下载流程"""
self._report_progress("download", batch_index * 7 + 1, total_batches * 7,
f"{batch_index + 1} 批: 正在填充订单号", action="fill_orders")
self._report_progress(
"download",
batch_index * 7 + 1,
total_batches * 7,
f"{batch_index + 1} 批: 正在填充订单号",
action="fill_orders",
)
textbox = inner_frame.get_by_role("textbox", name="来源生产订单号")
textbox.fill("")
textbox.fill(",".join(order_ids))
@@ -139,8 +155,12 @@ class DiscreteMaterialPlanExtractor:
inner_frame.get_by_role("row", name="序号").get_by_label("").click()
inner_frame.get_by_role("button", name="更多").hover()
inner_frame.get_by_text("输出", exact=True).click()
threshold_box = inner_frame.locator("div").filter(has_text=re.compile(r"^行数阈值$")).locator("input[type='text']")
threshold_box = (
inner_frame.locator("div")
.filter(has_text=re.compile(r"^行数阈值$"))
.locator("input[type='text']")
)
threshold_box.fill("300000")
download_path = f"D:/python/playwrite/data/temp_batch_{batch_index + 1}.xlsx"
@@ -165,36 +185,46 @@ class DiscreteMaterialPlanExtractor:
total_steps = len(file_paths) * 2 + 3
for i, path in enumerate(file_paths, 1):
self._report_progress("convert", 1 + (i-1)*2 + 1, total_steps, f"正在转换 Excel {i}/{len(file_paths)}")
self._report_progress(
"convert",
1 + (i - 1) * 2 + 1,
total_steps,
f"正在转换 Excel {i}/{len(file_paths)}",
)
df = self.converter.convert(path, output_file=None)
all_dfs.append(df)
self._log(f"文件 {i} 转换完成: 提取到 {len(df)} 条记录")
if all_dfs:
self._report_progress("convert", total_steps - 1, total_steps, "正在进行最终数据合并...")
self._report_progress(
"convert", total_steps - 1, total_steps, "正在进行最终数据合并..."
)
merged_df = pd.concat(all_dfs, ignore_index=True)
merged_df.to_excel(output_path, index=False)
for p in file_paths:
try: os.remove(p)
except: pass
try:
os.remove(p)
except:
pass
return output_path, merged_df
return None, None
def _save_to_database(self, df: pd.DataFrame):
"""将结果存入数据库并打印详细统计信息"""
if not self.dao: return
if not self.dao:
return
try:
self._report_progress("database", 1, 3, "正在将数据同步至数据库...")
# 使用 with 关键字确保资源安全释放
with self.dao as db:
stats = db.save_dataframe_with_replace(df)
# 保留并输出完整的处理细节:删除条数和新增条数
msg = f"数据库保存完成: 删除 {stats.get('deleted', 0)} 条, 新增 {stats.get('inserted', 0)}"
self._log(msg, "info")
except Exception as e:
self._log(f"数据库保存失败: {str(e)}", "error")
@@ -209,8 +239,10 @@ class DiscreteMaterialPlanExtractor:
input_box.press("Enter")
def extract(
self, production_id_file, output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
progress_callback=None
self,
production_id_file,
output_file="D:/python/playwrite/data/离散备料计划维护_合并.xlsx",
progress_callback=None,
):
"""主入口:执行全流程数据提取任务"""
self.progress_callback = progress_callback
@@ -220,15 +252,22 @@ class DiscreteMaterialPlanExtractor:
with sync_playwright() as playwright:
self._report_progress("login", 1, 3, "启动浏览器并尝试登录 ERP...")
browser, context, page, main_frame = login(
playwright=playwright, username=self.username, password=self.password,
headless=self.headless, ignore_https_errors=True
playwright=playwright,
username=self.username,
password=self.password,
headless=self.headless,
ignore_https_errors=True,
)
self._log(
"======================================== 开始执行数据提取任务 ========================================"
)
self._log("======================================== 开始执行数据提取任务 ========================================")
main_frame.locator("i").first.click()
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
f_frame = page1.locator("#forwardFrame").content_frame
@@ -237,16 +276,22 @@ class DiscreteMaterialPlanExtractor:
work_frame = inner_frame_locator.content_frame
self.setup_query_interface(work_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
)
batch_list = list(self.group_order_ids(order_ids, self.batch_size))
for i, batch_ids in enumerate(batch_list):
self._log(f"正在处理第 {i+1} 批次 (共 {len(batch_list)} 批)")
try:
f_path = self.download_batch(work_frame, batch_ids, i, len(batch_list), page1)
f_path = self.download_batch(
work_frame, batch_ids, i, len(batch_list), page1
)
downloaded_files.append(f_path)
except Exception as e:
self._log(f"批次 {i+1} 处理异常,已跳过。详细错误: {e}", "error")
self._log(
f"批次 {i+1} 处理异常,已跳过。详细错误: {e}", "error"
)
continue
self._log("正在注销并关闭浏览器环境...")
@@ -255,28 +300,32 @@ class DiscreteMaterialPlanExtractor:
browser.close()
if downloaded_files:
final_path, final_df = self.convert_and_merge_files(downloaded_files, output_file)
final_path, final_df = self.convert_and_merge_files(
downloaded_files, output_file
)
if self.enable_db_persistence and final_df is not None:
self._save_to_database(final_df)
self._log(f"所有流程已顺利结束,结果文件: {final_path}")
self._report_progress("complete", 1, 1, "任务完成")
return final_path
self._log("未获得任何有效数据,任务终止", "warn")
return None
finally:
self.progress_callback = None
def main():
extractor = DiscreteMaterialPlanExtractor(
username="BLDpengqiangqiang",
password="your_password",
enable_db_persistence=True
enable_db_persistence=True,
)
id_file = os.path.join(os.path.dirname(__file__), "productionID.txt")
extractor.extract(id_file)
if __name__ == "__main__":
main()
main()