def init_logger(debug=False): log_root = get_dir_path(LOG) now = datetime.now() timestamp = ( '%04d%02d%02d%02d%02d%02d' % (now.year, now.month, now.day, now.hour, now.minute, now.second)) if not exists(log_root): mkdir(log_root) else: file_dict = dict() log_files = [f for f in listdir(log_root) if isfile(join(log_root, f))] for lf in log_files: ts = int(lf.replace('RG_', '').replace('.log', '')) file_dict[ts] = lf keys = sorted(file_dict, reverse=True) if len(keys) > 9: i = 0 for k in keys: if i >= 9: remove(join(log_root, file_dict[k])) i += 1 if debug: log_lvl = logging.DEBUG else: log_lvl = logging.INFO logging.basicConfig( level=log_lvl, format= '%(asctime)s [%(threadName)-12.12s] [%(levelname)-5.5s] %(message)s', handlers=[ logging.FileHandler(join(log_root, 'RG_{}.log'.format(timestamp))), logging.StreamHandler() ])
def __get_valid_config_tree(s_xml): try: with open(join(get_dir_path(RESOURCES), 'config_schema.xsd'), 'r') as ff: s_schema = ''.join(ff.readlines()) if s_schema is None: raise RGUIError( 'Reporter could not find a valid configuration file') schema_root = l_etree.XML(s_schema.encode(encoding='UTF-8')) schema = l_etree.XMLSchema(schema_root) parser = l_etree.XMLParser(schema=schema) tree = l_etree.fromstring(s_xml.encode(encoding='UTF-8'), parser) except l_etree.XMLSyntaxError as ex: logging.error( 'Configuration contains invalid tags/data [{}]'.format( str(ex))) raise RGUIError( CONFIGURATION_FILE_CONTAINS_INVALID_XML_TAGS_OR_DATA) except Exception as ex: logging.error( 'Reporter could not validate configuration file [{}]'.format( str(ex))) raise RGUIError(REPORTER_COULD_NOT_VALIDATE_CONFIGURATION_FILE) return tree
def __arg_parser(): parser = ArgumentParser() parser.add_argument('-d', '--debug', action='store_true', default=False, help='Enable debug logging', dest='debug') parser.add_argument('-y', '--year', default=datetime.now().year, action='store', type=int, help='Current fiscal year of the report to be generated (valid integer in YYYY format). ' 'Default value is current fiscal year.', dest='year') parser.add_argument('-m', '--month', default=get_prev_fiscal_month(), action='store', type=str, help='Current month of the report to be generated (valid 3-character string in MMM format). ' 'Default value is current date previous month.', dest='month') parser.add_argument('-p', '--path', default=get_dir_path(TEMPLATES), action='store', type=str, help='Base directory of report templates', dest='templates_root') parser.add_argument('-e', '--exclusions', default=list(), action='store', type=list, help='List of measure IDs to be excluded from the report', dest='exclusions') parser.add_argument('-o', '--output', default=get_dir_path(OUTPUT), action='store', type=str, help='Report output directory', dest='out_dir') parser.add_argument('--orca', default=None, action='store', type=str, help='Path to orca executable file', dest='orca_path') return parser.parse_args()
def read_config(): try: with open(join(get_dir_path(ROOT), 'config.xml'), 'r') as ff: s_xml = ''.join(ff.readlines()) tree = RGConfigManager.__get_valid_config_tree(s_xml) config = RGConfigManager.__parse_config_from_xml(tree) except RGUIError as ex: raise ex except Exception as ex: logging.error( 'Configuration manager could not read reporter configuration file [{}]' .format(str(ex))) raise RGUIError(REPORTER_COULD_NOT_READ_THE_CONFIGURATION_FILE) return config
def save_config(config): if not isinstance(config, RGConfig): raise RGUIError(CONFIGURATION_COULD_NOT_BE_SAVED) try: tree = RGConfigManager.__parse_config_to_xml(config=config) s_config = l_etree.tostring(tree, encoding='UTF-8', pretty_print=True, xml_declaration=True) \ .decode(encoding='UTF-8') # validate config before saving RGConfigManager.__get_valid_config_tree(s_config) with open(join(get_dir_path(ROOT), 'config.xml'), 'w') as ff: ff.write(s_config) except Exception as ex: logging.error( 'Configuration manager could not save reporter configuration file [{}]' .format(str(ex))) raise RGUIError(CONFIGURATION_COULD_NOT_BE_SAVED)
def do_clean(): tmp_dir = get_dir_path(TEMP) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir)
def do_init(self): # TODO resolve issue with temp directory failing to delete tmp_dir = get_dir_path(TEMP) if os.path.exists(tmp_dir): shutil.rmtree(tmp_dir) os.mkdir(tmp_dir)