feat(config): execute all json
in config/
This commit is contained in:
@@ -13,7 +13,7 @@ logging.basicConfig(
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def load_config(config_path="trading_config.json"):
|
||||
def load_config(config_path="config/trading_config.json"):
|
||||
"""加载交易配置文件。
|
||||
|
||||
Args:
|
||||
@@ -100,7 +100,9 @@ def generate_ics(config):
|
||||
date_obj = datetime.strptime(date_str, "%Y-%m-%d").date()
|
||||
event = create_event(symbol, comment, date_obj, description)
|
||||
cal.add_component(event)
|
||||
logger.info("已创建项目:%s 交易 (%s) %s", symbol, comment, date_str)
|
||||
logger.info(
|
||||
"已创建项目:%s 交易 (%s) %s", symbol, comment, date_str
|
||||
)
|
||||
except ValueError as ex:
|
||||
logger.warning("跳过无效日期 %s: %s", date_str, ex)
|
||||
|
||||
@@ -121,13 +123,33 @@ def save_ics(calendar, output_path="public/trading.ics"):
|
||||
|
||||
|
||||
def main():
|
||||
"""主执行函数。"""
|
||||
"""主执行函数,遍历config目录下的所有json文件并生成对应的ics文件。"""
|
||||
|
||||
try:
|
||||
config = load_config()
|
||||
calendar = generate_ics(config)
|
||||
save_ics(calendar)
|
||||
except Exception as ex: # pylint: disable=broad-except
|
||||
logger.error("生成失败: %s", ex, exc_info=True)
|
||||
# 遍历config目录下的所有json文件
|
||||
for filename in os.listdir("config"):
|
||||
if filename.endswith(".json"):
|
||||
config_path = os.path.join("config", filename)
|
||||
try:
|
||||
# 加载配置
|
||||
config = load_config(config_path)
|
||||
|
||||
# 生成ics文件名(保留原文件名,只改扩展名)
|
||||
ics_filename = os.path.splitext(filename)[0] + ".ics"
|
||||
ics_path = os.path.join("public", ics_filename)
|
||||
|
||||
# 生成并保存ics文件
|
||||
calendar = generate_ics(config)
|
||||
save_ics(calendar, ics_path)
|
||||
|
||||
logger.info("成功生成: %s → %s", config_path, ics_path)
|
||||
except Exception as ex:
|
||||
logger.error("处理文件 %s 失败: %s", config_path, ex, exc_info=True)
|
||||
continue # 继续处理下一个文件
|
||||
|
||||
logger.info("所有文件处理完成")
|
||||
except Exception as ex:
|
||||
logger.error("程序执行失败: %s", ex, exc_info=True)
|
||||
raise
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user