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