def run(self) -> int: self.tests = loadyaml(open('tests.yaml', 'r', encoding='UTF-8'), CSafeLoader) for test in self.tests: self.checkTest(test) err_num = 0 warn_num = 0 suc = 0 fail = 0 self.start_server() for test in self.tests: if self.run_test(test): suc += 1 else: fail += 1 if test['level'] == 'warning': warn_num += 1 else: err_num += 1 self.kill_server() pt = f'Run {suc + fail} tests, {suc} successed, {fail} failed.' if fail != 0: pt += f' ({warn_num} Warnings, {err_num} Errors)' print(pt) return 0 if err_num == 0 else 1
def loadConfig(path) -> Dict: with open(path, encoding='utf-8') as file: config = loadyaml(file, SafeLoader) if 'classifier' in config: checkCompleteness(config['classifier']) if 'aliaser' in config: aliaser = config['aliaser'] for _, data in aliaser.items(): indexList = data['indexList'] data['indexList'] = expandAliaser(indexList) # 展开省略式 return config
def loadkey(): from yaml import safe_load as loadyaml from yaml.scanner import ScannerError global anserv try: apikey = {n.lower(): k for n, k in loadyaml(open(meta.APIKEY_PATH)).items() if n.lower() in anserv} except IOError as e: out.error(f"cannot open API keys file \"{meta.APIKEY_PATH}\": {e}") except ScannerError as e: out.error(f"cannot load API keys file \"{meta.APIKEY_PATH}\": {e}") else: for n, k in apikey.items(): if type(k) is not dict or \ k.get("apikey") is None or k.get("apikey") == "": out.error(f"service \"{anserv[n].name}\" missing " f"a valid API key \"{k}\"") else: anserv[n].set_apikey(k) out.debug("apikey", obj=apikey)
def loadReference(path): with open(path, encoding='utf-8') as file: return loadyaml(file, BaseLoader)
def main(): args = parser.parse_args() config = { 'settings': { 'loglevel': 'WARNING', }, 'output': { 'format': 'xml', 'location': 'stats.xml', }, 'plugins': {} } if not args.configfile: parser.print_help() raise SystemExit() try: with open(args.configfile, 'r') as stream: config.update(loadyaml(stream)) except IOError as e: print "error: '%s' cannot be read\n" % e.filename parser.print_help() raise SystemExit() numeric_level = getattr(logging, config['settings']['loglevel'].upper(), None) if not isinstance(numeric_level, int): raise ValueError('Invalid log level: %s' % loglevel) logging.basicConfig(level=numeric_level) writer_name = config['output']['format'] module = __import__( "collector.formatters.%s" % writer_name, locals(), globals(), ['collector', 'formatters'] ) writer = getattr(module, writer_name)() del module logging.info('loaded output formatter: %s' % writer_name) results = [] for plugin_details in config['plugins']: for plugin_name, settings in plugin_details.iteritems(): try: logging.info( 'initialising plugin "%s" with parameters: %s', plugin_name, ', '.join(['%s=%s' % (k, v) for k, v in settings.iteritems()]) ) module = __import__(plugin_name, locals(), globals()) plugin = getattr(module, plugin_name)(**settings) del module except ImportError: continue logging.info( 'executing plugin "%s"', plugin_name ) results.append({plugin_name: plugin()}) try: logging.info( 'writing to file "%s"', config['output']['location'], ) with open(config['output']['location'], 'w') as f: f.write(writer(results)) except IOError as e: logging.error( "error: '%s' cannot be written to", e.filename, ) parser.print_help() raise SystemExit() logging.info('file "%s" created', config['output']['location'])