|
| 1 | +import json |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +from models import Group, Label |
| 5 | + |
| 6 | + |
| 7 | +def get_standard_labels(): |
| 8 | + """ |
| 9 | + Get the list of standard labels that apply to every repository. |
| 10 | + @return: the list of standard labels |
| 11 | + """ |
| 12 | + |
| 13 | + labels_dict = load_json_from_file("labels") |
| 14 | + standard_labels = [] |
| 15 | + for group_info in labels_dict["groups"]: |
| 16 | + label_names = group_info.pop("labels", []) |
| 17 | + group = Group(**group_info) |
| 18 | + for label_info in label_names: |
| 19 | + label = Label(**label_info, group=group) |
| 20 | + standard_labels.append(label) |
| 21 | + for label_info in labels_dict["standalone"]: |
| 22 | + label = Label(**label_info) |
| 23 | + standard_labels.append(label) |
| 24 | + return standard_labels |
| 25 | + |
| 26 | + |
| 27 | +def get_repo_specific_labels(): |
| 28 | + """ |
| 29 | + Get the dict mapping each repository to a list of skill labels. For each |
| 30 | + repository, skill levels will be added on top of the standard labels. |
| 31 | + @return: the dict mapping repo names to skill labels |
| 32 | + """ |
| 33 | + |
| 34 | + skill_group = Group(color="5ff1f5", name="skill") |
| 35 | + labels_dict = load_json_from_file("skills") |
| 36 | + repo_specific_labels = {} |
| 37 | + for repo_name, skill_names in labels_dict.items(): |
| 38 | + skill_labels = [ |
| 39 | + get_skill_label_from_name(skill_group, skill_name) |
| 40 | + for skill_name in skill_names |
| 41 | + ] |
| 42 | + repo_specific_labels[repo_name] = skill_labels |
| 43 | + return repo_specific_labels |
| 44 | + |
| 45 | + |
| 46 | +def get_skill_label_from_name(skill_group, skill_name): |
| 47 | + """ |
| 48 | + Generate the skill label purely from the name of the skill. The name of the |
| 49 | + skill is plugged into the description and the lower-cased version is used as |
| 50 | + the label name. |
| 51 | + @param skill_group: the logical parent group of all skill labels |
| 52 | + @param skill_name: the name of the skill to convert into a label |
| 53 | + @return: an instance of Label derived from the skill name |
| 54 | + """ |
| 55 | + |
| 56 | + return Label( |
| 57 | + name=skill_name.lower(), |
| 58 | + description=f"Requires proficiency in '{skill_name}'", |
| 59 | + emoji="💪", |
| 60 | + group=skill_group, |
| 61 | + ) |
| 62 | + |
| 63 | + |
| 64 | +def load_json_from_file(file_name): |
| 65 | + """ |
| 66 | + Load the JSON file into a Python list or dict. The extension '.json' is |
| 67 | + appended to the file name and only the current directory is scanned for the |
| 68 | + matching file. |
| 69 | + @param file_name: the name of the file to load |
| 70 | + @return: the contents of the JSON file as a Python object |
| 71 | + """ |
| 72 | + |
| 73 | + file_path = get_datafile_path(file_name) |
| 74 | + with open(file_path, "r") as file: |
| 75 | + data = json.load(file) |
| 76 | + return data |
| 77 | + |
| 78 | + |
| 79 | +def get_datafile_path(file_name): |
| 80 | + """ |
| 81 | + Get the path to the datafile by searching the current directory for a file |
| 82 | + with the given name and the '.json' extension. |
| 83 | + @param file_name: the name of the file whose path is required |
| 84 | + @return: the path to the file |
| 85 | + """ |
| 86 | + |
| 87 | + current_file = Path(__file__).resolve() |
| 88 | + data_file = current_file.parent.joinpath(f"{file_name}.json") |
| 89 | + return data_file |
| 90 | + |
| 91 | + |
| 92 | +def get_labels(): |
| 93 | + """ |
| 94 | + Get the standard list of packages and the repository-specific labels. |
| 95 | + @return: the standard list of packages and the repository-specific labels |
| 96 | + """ |
| 97 | + |
| 98 | + standard_labels = get_standard_labels() |
| 99 | + repo_specific_labels = get_repo_specific_labels() |
| 100 | + return standard_labels, repo_specific_labels |
| 101 | + |
| 102 | + |
| 103 | +__all__ = [get_labels] |
0 commit comments