From 05969b9aac9aed5908d17c66d6690eb9d1a2b632 Mon Sep 17 00:00:00 2001 From: Kowyo Date: Thu, 22 Jan 2026 13:39:32 +0800 Subject: [PATCH] feat(cli): add repo command to lookup OpenAuto repository IDs --- src/hoa_majors/cli/main.py | 10 +++++++- src/hoa_majors/cli/repo.py | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/hoa_majors/cli/repo.py diff --git a/src/hoa_majors/cli/main.py b/src/hoa_majors/cli/main.py index 627142a..4ceb650 100644 --- a/src/hoa_majors/cli/main.py +++ b/src/hoa_majors/cli/main.py @@ -3,7 +3,7 @@ from pathlib import Path from hoa_majors import __version__ -from hoa_majors.cli import audit, courses, crawl, info, plans, search +from hoa_majors.cli import audit, courses, crawl, info, plans, repo, search from hoa_majors.config import DEFAULT_DATA_DIR, logger @@ -61,6 +61,12 @@ def main(): info_parser.add_argument("course_code", help="课程代码") info_parser.add_argument("--data-dir", type=Path, default=DEFAULT_DATA_DIR, help="数据存储目录") + # repo + repo_parser = subparsers.add_parser("repo", help="获取课程对应的 OpenAuto 仓库 ID") + repo_parser.add_argument("plan_id", help="培养方案 ID (fah)") + repo_parser.add_argument("course_code", help="课程代码") + repo_parser.add_argument("--data-dir", type=Path, default=DEFAULT_DATA_DIR, help="数据存储目录") + if len(sys.argv) == 1: parser.print_help() sys.exit(0) @@ -100,6 +106,8 @@ def main(): courses.list_courses(args.plan_id, args.data_dir) elif args.command == "info": info.get_course_info(args.plan_id, args.course_code, args.data_dir) + elif args.command == "repo": + repo.run(args) else: parser.print_help() diff --git a/src/hoa_majors/cli/repo.py b/src/hoa_majors/cli/repo.py new file mode 100644 index 0000000..2cc9bea --- /dev/null +++ b/src/hoa_majors/cli/repo.py @@ -0,0 +1,49 @@ +import tomllib +from pathlib import Path + +from hoa_majors.config import logger + + +def load_lookup_table(data_dir: Path) -> dict: + """Load the lookup_table.toml file""" + lookup_path = data_dir / "lookup_table.toml" + if not lookup_path.exists(): + logger.warning(f"Lookup table not found at {lookup_path}") + return {} + try: + with open(lookup_path, "rb") as f: + return tomllib.load(f) + except Exception as e: + logger.error(f"Failed to load lookup table: {e}") + return {} + + +def get_repo_id(plan_id: str, course_code: str, data_dir: Path) -> str: + """ + 获取课程对应的 OpenAuto 仓库 ID。 + + 逻辑: + 1. 如果 course_code 不在 lookup table 中 -> 返回 course_code + 2. 如果该 course_code 下存在 plan_id 对应的 key -> 返回该 value + 3. 如果该 course_code 下存在 DEFAULT key -> 返回 DEFAULT 对应的 value + 4. 否则 -> 返回 course_code + """ + lookup = load_lookup_table(data_dir) + + if course_code not in lookup: + return course_code + + mapping = lookup[course_code] + + if plan_id in mapping: + return mapping[plan_id] + elif "DEFAULT" in mapping: + return mapping["DEFAULT"] + else: + return course_code + + +def run(args): + """Entry point for the repo command""" + repo_id = get_repo_id(args.plan_id, args.course_code, args.data_dir) + print(repo_id)