def parse_config(): """ Parses the config.cfg file and outputs it to the global python dictionary variable config. """ parser = argparse.ArgumentParser() g = parser.add_mutually_exclusive_group(required=True) g.add_argument('-c', '--clean', action='store_true', help='Clean output folder of all files that are not dependencies.') g.add_argument('YYQQ', nargs='?', type=int, action='store', help='Report month in YYQQ format') h = parser.add_mutually_exclusive_group() h.add_argument('-l', '--latex-only', action='store_true', help='Only compiles the latex report without replotting charts. Note: Requires the charts to be generated previously.') h.add_argument('-g', '--graph-only', action='store_true', help='Only generates the charts for the report without compiling the LaTeX and PDF.') args = parser.parse_args(sys.argv[1:]) cfg = ConfigParser.ConfigParser(allow_no_value=True) cfg.read('config.cfg') config = {key : cfg.get('quarterly',key) for key in ['input','output']} config.update({k:map(lambda x:x.replace('-',','), cfg.get('quarterly',k).split(',')) for k in ['defce_color','phish_color','malwr_color','other_color']}) rutil.set_bar_deflt_colors(config['other_color']) # Clean output folder if --clean flag if args.clean: clean(config['output']) config['trim_config'] = (cfg.get('quarterly', 'bar_chart').replace('-','='), cfg.get('quarterly', 'pie_chart').replace('-','=')) if args.latex_only: config['only'] = 'latex' elif args.graph_only: config['only'] = 'graph' else: config['only'] = 'all' config['YYQQ'] = args.YYQQ return config
def parse_config(): """ Parses the config.cfg file and outputs it to the global python dictionary variable config. """ parser = argparse.ArgumentParser() g = parser.add_mutually_exclusive_group(required=True) g.add_argument('-c', '--clean', action='store_true', help='Clean output folder of all files that are not dependencies.') g.add_argument('YYMM', nargs='?', type=int, action='store', help='Report month in YYMM format') h = parser.add_mutually_exclusive_group() h.add_argument('-l', '--latex-only', action='store_true', help='Only compiles the latex report without replotting charts. Note: Requires the charts to be generated previously.') h.add_argument('-g', '--graph-only', action='store_true', help='Only generates the charts for the report without compiling the LaTeX and PDF.') args = parser.parse_args(sys.argv[1:]) # File dependencies required_files = ['footer.tex', 'header.tex', 'HKCERT.png'] cfg = ConfigParser.ConfigParser(allow_no_value=True) cfg.read('config.cfg') ltx_output = cfg.get('monthly','output') data_folder = cfg.get('monthly','input') # Clean output folder if --clean flag if args.clean: clean(ltx_output) # Check arguments if args.YYMM < 1000 or args.YYMM > 10000 or args.YYMM % 100 > 12: print('[FATAL] Error: Argument should be in format YYMM (e.g. 1403 for 2014 March)') sys.exit(-1) try: yymm = args.YYMM month = yymm % 100 year = (yymm-month) / 100 except: print('[FATAL] Error: Invalid argument. Expected format: YYMM (e.g. 1403 for 2014 March') sys.exit(1) file_paths = [data_folder + month_format(year, month - 2) + '/report/', data_folder + month_format(year, month - 1) + '/report/', data_folder + month_format(year, month) + '/report/' ] plotly_cred = (cfg.get('plotly','username'), cfg.get('plotly','api_key')) rutil.plotly_init(plotly_cred) # Check file dependencies file_missing = False for req in map(lambda x: ltx_output + x, required_files): if not os.path.isfile(req): print('[FATAL] Missing file: ' + req) file_missing = True if file_missing: sys.exit('[FATAL] Please check the data path is correct in config.cfg') # Set global config config = {'yymm': yymm, 'year': year, 'month': month, 'file_paths':file_paths, 'output_dir': ltx_output, 'plotly_cred': plotly_cred} config.update({k:map(lambda x:x.replace('-',','), cfg.get('monthly',k).split(',')) for k in ['defce_color','phish_color','malwr_color','other_color']}) rutil.set_bar_deflt_colors(config['other_color']) config['trim_config'] = (cfg.get('monthly', 'bar_chart').replace('-','='), cfg.get('monthly', 'pie_chart').replace('-','=')) if args.latex_only: config['only'] = 'latex' elif args.graph_only: config['only'] = 'graph' else: config['only'] = 'all' return config