def __init__(self, source): self.rules = [] for s in source: for p in pathlib.Path(s).glob("*.py"): filename = os.path.splitext(os.path.basename(p))[0] if not re.match(r"^[A-Za-z]+$", filename): continue spec = importlib.util.spec_from_file_location(filename, p) module = importlib.util.module_from_spec(spec) try: spec.loader.exec_module(module) except (ImportError, NameError) as e: sysexit_with_message( "Failed to load roles file {module}: \n {msg}".format( msg=str(e), module=filename)) try: for name, obj in inspect.getmembers(module): if self._is_plugin(obj): self.rules.append(obj()) except TypeError as e: sysexit_with_message( "Failed to load roles file: \n {msg}".format( msg=str(e))) self.validate()
def validate(self): normalized_std = (list(toolz.remove(lambda x: x.sid == "", self.rules))) unique_std = len(list(toolz.unique(normalized_std, key=lambda x: x.sid))) all_std = len(normalized_std) if not all_std == unique_std: sysexit_with_message( "Detect duplicate ID's in standards definition. Please use unique ID's only." )
def _validate(self, config): try: anyconfig.validate(config, self.schema, ac_schema_safe=False) return True except jsonschema.exceptions.ValidationError as e: schema_error = ( "Error while loading configuration:\n" "Failed validating '{validator}' in schema{schema}").format( validator=e.validator, schema=format_as_index(list(e.relative_schema_path)[:-1])) utils.sysexit_with_message("{schema}: {msg}".format( schema=schema_error, msg=e.message))