-
Notifications
You must be signed in to change notification settings - Fork 1
feat(cli): add repo command to lookup OpenAuto repository IDs #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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 | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+33
to
+43
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The logic for finding the repository ID can be simplified and made more concise by using the
Suggested change
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| def run(args): | ||||||||||||||||||||||||||||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For better code clarity and maintainability, it's good practice to add type hints to function arguments. The
Suggested change
|
||||||||||||||||||||||||||||||||||
| """Entry point for the repo command""" | ||||||||||||||||||||||||||||||||||
| repo_id = get_repo_id(args.plan_id, args.course_code, args.data_dir) | ||||||||||||||||||||||||||||||||||
| print(repo_id) | ||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Catching a broad
Exceptioncan hide unexpected errors and make debugging harder. It's better to catch more specific exceptions that you expect to handle. In this case,tomllib.loadcan raisetomllib.TOMLDecodeError, and file operations can raiseOSError.