def do_prepare_export(self, out_dir=None): if self.report is None: raise RGError('Report document has not been initialized') if out_dir is None: raise RGError( 'Invalid report output directory provided [{}]'.format( out_dir)) if not os.path.exists(out_dir): os.makedirs(out_dir)
def get_fiscal_month_id(s_month): if s_month is None or s_month.upper() not in list(FISCAL_MONTHS.values()): raise RGError('Unknown fiscal month provided [{}]'.format(s_month)) for k, v in FISCAL_MONTHS.items(): if v == s_month.upper(): return k raise RGError( 'Provided fiscal month [{}] cannot be parsed. Please double check the correctness of the 3-character ' 'month name'.format(s_month))
def __init__(self, files=()): object.__init__(self) for f in files: if not isinstance(f, RGFile): raise RGError('RGFileContainer cannot contain files of unsupported type [{}]'.format(type(f))) self.files = files self.files.sort(key=lambda x: (x.f_type, x.fym))
def create_entity(m_type=None, data_cfy=None, data_lfy=None, measure_cfy=None, measure_lfy=None): """ Create measure by measure type :param m_type: Measure type :param data_cfy: Current fiscal year data list :param data_lfy: Last fiscal year data list :param measure_cfy: Current fiscal year measure object :param measure_lfy: Last fiscal year measure object :return: """ __TYPE_CREATION_MAP = { CPM: RGEntityFactory.__create_cpm_entity, SDM: RGEntityFactory.__create_sdm_entity, SSG: RGEntityFactory.__create_ssg_entity, PMT_ADDITIONAL: RGEntityFactory.__create_pmt_additional_entity, HR_SCORECARD: RGEntityFactory.__create_hr_scorecard_entity, HR_ABSENCES: RGEntityFactory.__create_hr_absences_entity, HR_SICKNESS: RGEntityFactory.__create_hr_sickness_entity, HR_TRAINING: RGEntityFactory.__create_hr_training_entity, DCS_COMPLAINTS: RGEntityFactory.__create_dcs_complaints_entity } try: fun = __TYPE_CREATION_MAP[m_type] return fun(data_cfy=data_cfy, data_lfy=data_lfy, measure_cfy=measure_cfy, measure_lfy=measure_lfy) except AttributeError: raise RGError("Unknown measure type {}".format(m_type))
def __create_measures(self): df_cym, df_cyd, df_pmt, df_hr_scorecard, df_hr_absences, df_hr_sickness, df_hr_training, df_dcs_complaints = self.get_data_frames() if df_cym is None: raise RGError('Unable to create measures from invalid data frame object [{}]'.format(df_cym)) self.__entities = list() d_list = list() m_list = list() m_ids = set() for idx, line in df_cym.iterrows(): m = RGMeasureFactory.create_measure(m_type=get_val(line, MEASURE_TYPE).upper(), df=line) m_ids.add(m.m_id) m_list.append(m) for idx, line in df_cyd.iterrows(): d_list.append(RGDataFactory.create_data(m_type=get_val(line, MEASURE_TYPE).upper(), df=line)) m_ids = sorted(m_ids) m_list.sort(key=lambda x: x.m_id) d_list.sort(key=lambda x: x.m_id) for m_id in m_ids: m_cfy = None m_lfy = None d_cfy = None d_lfy = None for m in m_list: if m.m_id == m_id: if m.f_year == get_cfy_prefix(cfy=self.year): m_cfy = m continue if m.f_year == get_lfy_prefix(cfy=self.year): m_lfy = m continue if m_cfy is not None: d_cfy = self.__get_data_for_measure(m_cfy, d_list) if m_lfy is not None: d_lfy = self.__get_data_for_measure(m_lfy, d_list) if m_cfy is not None and m_lfy is not None and \ d_cfy is not None and len(d_cfy) > 0 and \ d_lfy is not None and len(d_lfy) > 0: self.__entities.append( RGEntityFactory.create_entity(m_type=m_cfy.m_type, data_cfy=d_cfy, data_lfy=d_lfy, measure_cfy=m_cfy, measure_lfy=m_lfy)) # parse unknown data frame self.__parse_abnormal_data(df_pmt, PMT_ADDITIONAL) self.__parse_abnormal_data(df_hr_scorecard, HR_SCORECARD) self.__parse_abnormal_data(df_hr_absences, HR_ABSENCES) self.__parse_abnormal_data(df_hr_sickness, HR_SICKNESS) self.__parse_abnormal_data(df_hr_training, HR_TRAINING) self.__parse_abnormal_data(df_dcs_complaints, DCS_COMPLAINTS) logging.debug('[{}] entities has been parsed'.format(len(self.__entities)))
def generate(self): if self.options is None or not isinstance(self.options, RGReporterOptions): raise RGError('Invalid report options provided') logging.info('Generating report...') self.do_init() self.do_compose(options=self.options) self.do_prepare_export(out_dir=self.options.out_dir) self.do_export(out_dir=self.options.out_dir) self.do_clean() logging.info('Report successfully generated! [{}]'.format( self.report_name))
def __validate_and_clean_templates(self, templates=None): if templates is None: raise RGError('Templates could not be found under provided path [{}]'.format(self.path)) to_remove = list() for name in templates.keys(): if len(templates[name]) < 2: to_remove.append(name) for name in to_remove: del templates[name] logging.debug( 'Template file [{}] has been removed, because current or last fiscal year report is not present'.format( name)) return templates
def get_templates(self): if self.path is None: raise RGError('Invalid template root directory provided [{}]'.format(self.path)) templates = dict() for ff in self.search_in_root(path=self.path, pattern='*.xlsx'): if ff.name.startswith(self.current_prefix) or ff.name.startswith(self.last_prefix): t_name = ff.name.replace('{}_'.format(self.current_prefix), '').replace('{}_'.format(self.last_prefix), '').replace('.xlsx', '') if t_name not in templates.keys(): templates[t_name] = list() templates[t_name].append(ff) return templates
def get_files(self): if self.path is None: raise RGError('Invalid image file root directory provided [{}]'.format(self.path)) images = list() ff_paths = self.search_in_root(path=self.path, pattern='*.png') ff_paths.sort(key=lambda x: (x.parent.name, x.name.upper().replace(' ', '_'))) for ff in ff_paths: name = ff.name.upper().replace(' ', '_') f_type = self.__get_image_type(name) if f_type is not None: images.append(self.create_file(f_type, str(ff), name)) else: logging.error('Could not get image file type from name [{}]'.format(name)) return RGFileContainer(images)
def __init__(self, m_type=None, df=None): if m_type is None or df is None: raise RGError( 'Invalid measure type [{}] or data frame [{}] provided'.format( m_type, df)) self.m_type = m_type self.f_year = get_val(df, FISCAL_YEAR) self.m_id = get_val(df, MEASURE_ID) self.m_ref_no = get_val(df, MEASURE_REF_NO) self.m_title = get_val(df, MEASURE_TITLE) self.month = get_val(df, MONTH) if self.m_title is None: self.m_title = get_val(df, MEASURE) self.year = get_val(df, YEAR) self.year_month = get_val(df, YEAR_MONTH)
def create_measure(m_type=None, df=None): """ Create measure by measure type :param m_type: Measure type :param df: Year Measure :return: """ __TYPE_CREATION_MAP = { CPM: RGMeasureFactory.__create_cpm_measure, SDM: RGMeasureFactory.__create_sdm_measure, SSG: RGMeasureFactory.__create_ssg_measure } try: fun = __TYPE_CREATION_MAP[m_type] return fun(df=df) except AttributeError: raise RGError("Unknown measure type {}".format(m_type))
def __parse_abnormal_data(self, df, m_type): if df is None or df.size == 0: return if m_type is None: raise RGError('Cannot parse abnormal data as unknown measure type provided [{}]'.format(m_type)) d_list = list() for idx, line in df.iterrows(): d_list.append(RGDataFactory.create_data(m_type=m_type, df=line)) values = sorted(set(map(lambda x: x.m_id, d_list))) d_list = [[y for y in d_list if y.m_id == x] for x in values] for d_group in d_list: d_cfy, d_lfy = list(), list() for d in d_group: if d.f_year == get_cfy_prefix(cfy=self.year): d_cfy.append(d) elif d.f_year == get_lfy_prefix(cfy=self.year): d_lfy.append(d) self.__entities.append(RGEntityFactory.create_entity(m_type=m_type, data_lfy=d_lfy, data_cfy=d_cfy))
def create_data(m_type=None, df=None): """ Create measure by measure type :param m_type: Measure type :param df: Year data :return: """ __TYPE_CREATION_MAP = { CPM: RGDataFactory.__create_cpm_data, SDM: RGDataFactory.__create_sdm_data, SSG: RGDataFactory.__create_ssg_data, PMT_ADDITIONAL: RGDataFactory.__create_pmt_additional_data, HR_SCORECARD: RGDataFactory.__create_hr_scorecard_data, HR_ABSENCES: RGDataFactory.__create_hr_absences_data, HR_SICKNESS: RGDataFactory.__create_hr_sickness_data, HR_TRAINING: RGDataFactory.__create_hr_training_data, DCS_COMPLAINTS: RGDataFactory.__create_dcs_complaints_data } try: fun = __TYPE_CREATION_MAP[m_type] return fun(df=df) except AttributeError: raise RGError("Unknown data type {}".format(m_type))
def __init__(self): raise RGError('Factory classes cannot be initialized')
def do_compose(self, options=None): if self.report is None: raise RGError('PDF document has not been initialized') if options.entities is None or len(options.entities) < 1: raise RGError('No measures provided to PDF report generator')
def create_file(self, f_type, f_path, name): if f_type is None or f_path is None or name is None: raise RGError( 'One of the file fields does not contain valid information. Type: [{}] Path [{}] Name [{}]'.format( f_type, f_path, name)) return RGFile(f_type=f_type, name=name, path=f_path, fym=self.get_fym_from_name(name))
def get_files(self): raise RGError('Unimplemented method RGDaoBase.get_files')
def __new__(cls): raise RGError('Factory classes cannot be initialized')