|
| 1 | +import os |
| 2 | +import typing as t |
| 3 | +import urllib |
| 4 | +from io import BytesIO |
| 5 | + |
| 6 | +from openpyxl import load_workbook |
| 7 | +from openpyxl.workbook.workbook import Workbook |
| 8 | + |
| 9 | +from ..storing import get_all_jobs, get_all_profiles |
| 10 | +from .job import TEMPLATE_URL as JOB_TEMPLATE_URL |
| 11 | +from .job import fill_work_sheet as fill_job_work_sheet |
| 12 | +from .job import parsing_evaluator as job_parsing_evaluator |
| 13 | +from .profile import TEMPLATE_URL as PROFILE_TEMPLATE_URL |
| 14 | +from .profile import fill_work_sheet as fill_profile_work_sheet |
| 15 | +from .profile import parsing_evaluator as profile_parsing_evaluator |
| 16 | + |
| 17 | +STATISTICS_SHEET_NAME = "1. Statistics" |
| 18 | + |
| 19 | + |
| 20 | +def load_workbook_from_url(url: str) -> Workbook: |
| 21 | + """ |
| 22 | + Load an excel file from a url |
| 23 | +
|
| 24 | + Args: |
| 25 | + url: <str> |
| 26 | + The url of the excel file |
| 27 | +
|
| 28 | + Returns: |
| 29 | + <Workbook> |
| 30 | + The loaded workbook |
| 31 | + """ |
| 32 | + file = urllib.request.urlopen(url).read() |
| 33 | + return load_workbook(filename=BytesIO(file)) |
| 34 | + |
| 35 | + |
| 36 | +def prepare_report_path(path: str) -> str: |
| 37 | + """ |
| 38 | + Prepare the report path |
| 39 | +
|
| 40 | + Args: |
| 41 | + path: <str> |
| 42 | + The path of the report |
| 43 | + """ |
| 44 | + if os.path.isdir(path): |
| 45 | + return os.path.join(path, "parsing_evaluation.xlsx") |
| 46 | + if not path.endswith(".xlsx"): |
| 47 | + return f"{path}.xlsx" |
| 48 | + return path |
| 49 | + |
| 50 | + |
| 51 | +def generate_parsing_evaluation_report( |
| 52 | + client: "Hrflow", # noqa: F821 |
| 53 | + report_path: str, |
| 54 | + source_key: t.Optional[str] = None, |
| 55 | + board_key: t.Optional[str] = None, |
| 56 | + show_progress: bool = False, |
| 57 | +): |
| 58 | + """ |
| 59 | + Generate a parsing evaluation report |
| 60 | +
|
| 61 | + If you want to generate a parsing evaluation report for jobs, you must |
| 62 | + provide the board_key. |
| 63 | + If you want to generate a parsing evaluation report for profiles, you must |
| 64 | + provide the source_key. |
| 65 | +
|
| 66 | + board_key and source_key are optional string, you must provide only one of them. |
| 67 | +
|
| 68 | + Args: |
| 69 | + client: <Hrflow> |
| 70 | + The client to use |
| 71 | + source_key: <Optional[str]> |
| 72 | + The source key where the profiles are |
| 73 | + board_key: <Optional[str]> |
| 74 | + The board key where the jobs are |
| 75 | + report_path: <str> |
| 76 | + The path of the report |
| 77 | + This can be a already existing directory where |
| 78 | + the report will be saved as parsing_evaluation.xlsx |
| 79 | + This can be directly the path of the report. |
| 80 | + If the path is not an excel file (xlsx), |
| 81 | + the report will be saved as {path}.xlsx |
| 82 | + show_progress: <bool> |
| 83 | + Show the progress bar |
| 84 | + """ |
| 85 | + |
| 86 | + if not source_key and not board_key: |
| 87 | + raise ValueError("You must provide either source_key or board_key") |
| 88 | + if source_key and board_key: |
| 89 | + raise ValueError("You must provide only one of source_key or board_key") |
| 90 | + |
| 91 | + if source_key: |
| 92 | + profile_list = get_all_profiles(client, source_key, show_progress) |
| 93 | + evaluation_list = profile_parsing_evaluator(profile_list, show_progress) |
| 94 | + |
| 95 | + work_book = load_workbook_from_url(PROFILE_TEMPLATE_URL) |
| 96 | + work_sheet = work_book[STATISTICS_SHEET_NAME] |
| 97 | + fill_profile_work_sheet(work_sheet, evaluation_list, show_progress) |
| 98 | + else: |
| 99 | + assert board_key is not None |
| 100 | + job_list = get_all_jobs(client, board_key, show_progress) |
| 101 | + evaluation_list = job_parsing_evaluator(job_list, show_progress) |
| 102 | + |
| 103 | + work_book = load_workbook_from_url(JOB_TEMPLATE_URL) |
| 104 | + work_sheet = work_book[STATISTICS_SHEET_NAME] |
| 105 | + fill_job_work_sheet(work_sheet, evaluation_list, show_progress) |
| 106 | + |
| 107 | + report_path = prepare_report_path(report_path) |
| 108 | + work_book.save(report_path) |
| 109 | + work_book.close() |
0 commit comments