Beispiel #1
0
    def test_load_empty_yaml_file(self, tmpdir):
        """
        Loading empty yaml files should raise an exception
        """
        # Load empty source file
        source_f = tmpdir.join("foo.yaml")
        source_f.write("")

        schema_f = tmpdir.join("bar.yaml")
        schema_f.write("")

        # TODO: This is abit buggy because wrong exception is raised...
        with pytest.raises(CoreError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        # assert "Unable to load any data from source yaml file" in str(ex.value)

        # Load empty schema files
        source_f = tmpdir.join("foo.yaml")
        source_f.write("3.14159")

        schema_f = tmpdir.join("bar.yaml")
        schema_f.write("")

        with pytest.raises(CoreError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        assert "No data loaded from file" in str(ex.value)
Beispiel #2
0
    def __init__(self, config_path, schema_path, config_changes):
        with open(config_path, 'rt') as src:
            config = read_config(src)
        make_config_changes(config, config_changes)

        self.multi_stage = 'stages' in config
        if self.multi_stage:
            ordered_changes = OrderedDict(
                sorted(
                    config['stages'].items(),
                    key=lambda (k, v): v['number'],
                ))
            self.ordered_stages = OrderedDict()
            for name, changes in ordered_changes.items():
                current_config = copy.deepcopy(config)
                del current_config['stages']
                del changes['number']
                merge_recursively(current_config, changes)
                self.ordered_stages[name] = current_config

        # Validate the configuration and the training stages
        with open(os.path.expandvars(schema_path)) as schema_file:
            schema = yaml.safe_load(schema_file)
            core = Core(source_data=config, schema_data=schema)
            core.validate(raise_exception=True)
            if self.multi_stage:
                for stage in self.ordered_stages.values():
                    core = Core(source_data=config, schema_data=schema)
                    core.validate(raise_exception=True)
        super(Configuration, self).__init__(config)
Beispiel #3
0
    def test_load_empty_json_file(self, tmpdir):
        """
        Loading an empty json files should raise an exception
        """
        # Load empty source file
        source_f = tmpdir.join("foo.json")
        source_f.write("")

        schema_f = tmpdir.join("bar.json")
        schema_f.write("")

        with pytest.raises(ValueError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        # Python 2.7 and Python 3.5 JSON parsers return different exception
        # strings for the same data file, so check for both errors strings.
        assert ("No JSON object could be decoded" in str(ex.value)
                or "Expecting value:" in str(ex.value))

        # Load empty schema files
        source_f = tmpdir.join("foo.json")
        source_f.write("3.14159")

        schema_f = tmpdir.join("bar.json")
        schema_f.write("")

        with pytest.raises(ValueError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        assert ("No JSON object could be decoded" in str(ex.value)
                or "Expecting value:" in str(ex.value))
Beispiel #4
0
    def test_load_empty_json_file(self, tmpdir):
        """
        Loading an empty json files should raise an exception
        """
        # Load empty source file
        source_f = tmpdir.join("foo.json")
        source_f.write("")

        schema_f = tmpdir.join("bar.json")
        schema_f.write("")

        with pytest.raises(CoreError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        assert "Unable to load any data from source json file" in str(ex.value)

        # Load empty schema files
        source_f = tmpdir.join("foo.json")
        source_f.write("3.14159")

        schema_f = tmpdir.join("bar.json")
        schema_f.write("")

        with pytest.raises(CoreError) as ex:
            Core(source_file=str(source_f), schema_files=[str(schema_f)])
        assert "No data loaded from file" in str(ex.value)
Beispiel #5
0
def parse_data(site):
    base = op.join(op.dirname(__file__), 'games')

    originals = []
    for fn in sorted(os.listdir(op.join(base, 'originals'))):
        if fn.endswith('.yaml'):
            originals.extend(yaml.load(open(op.join(base, 'originals', fn))))
    print(str(len(originals)) + ' games in total')

    try:
        core = Core(source_data=originals,
                    schema_files=['schema_originals.yaml'])
        core.validate(raise_exception=True)
    except Exception as error:
        if len(core.errors) > 0:
            show_validation_errors(originals, core.errors)
        else:
            raise error

    clones = []
    for fn in sorted(os.listdir(op.join(base, 'clones'))):
        if fn.endswith('.yaml'):
            clones.extend(yaml.load(open(op.join(base, 'clones', fn))))
    print(str(len(clones)) + ' clones in total')

    try:
        core = Core(source_data=clones, schema_files=['schema_clones.yaml'])
        core.validate(raise_exception=True)
    except Exception as error:
        if len(core.errors) > 0:
            show_validation_errors(clones, core.errors)
        else:
            raise error

    for item in originals:
        parse_global_tags(site, item.get('meta', {}), 'genre')
        # Recombine originals and clones
        combined = copy.deepcopy(item)
        name = combined['name']
        if isinstance(name, list):
            name = name[0]
        combined_remakes = [
            clone for clone in clones
            if 'remakes' in clone and name in clone['remakes']
        ]
        if len(combined_remakes) > 0:
            combined['remakes'] = combined_remakes
        combined_clones = [
            clone for clone in clones
            if 'clones' in clone and name in clone['clones']
        ]
        if len(combined_clones) > 0:
            combined['clones'] = combined_clones
        parse_items(site, combined, 'remakes')
        parse_items(site, combined, 'clones')
Beispiel #6
0
    def test_create_empty_core_object(self, tmpdir):
        """
        If createing a core object without any source or schema file an exception should be raised.
        """
        with pytest.raises(CoreError) as ex:
            Core()
        assert "No source file/data was loaded" in str(ex.value)

        # To trigger schema exception we must pass in a source file
        source_f = tmpdir.join("bar.json")
        source_f.write("3.14159")

        with pytest.raises(CoreError) as ex:
            Core(source_file=str(source_f))
        assert "No schema file/data was loaded" in str(ex.value)
Beispiel #7
0
def load_config(file_name):
    """
    Load the file, verify that it conforms to the schema,
    and return the configuration.
    """
    try:
        with open(file_name, 'r') as conf_file:
            data = yaml.safe_load(conf_file)
            validator = Core(
                source_data=data,
                schema_files=[dirname(__file__) + "/rebench-schema.yml"])
            try:
                validator.validate(raise_exception=True)
            except SchemaError as err:
                errors = [
                    escape_braces(val_err)
                    for val_err in validator.validation_errors
                ]
                raise UIError(
                    "Validation of " + file_name + " failed.\n{ind}" +
                    "\n{ind}".join(errors) + "\n", err)
            return data
    except IOError as err:
        if err.errno == 2:
            assert err.strerror == "No such file or directory"
            raise UIError(
                "The requested config file (%s) could not be opened. %s.\n" %
                (file_name, err.strerror), err)
        raise UIError(str(err) + "\n", err)
    except yaml.YAMLError as err:
        raise UIError(
            "Parsing of the config file " + file_name + " failed.\nError " +
            str(err) + "\n", err)
Beispiel #8
0
    def acquireYAMLConfig(configFile: str, 
                      validationFile: str, 
                      domains: List[str]) -> _ValRetType:
        """
        Parse YAML config file and return dictionary.
        """
        # Validate the input file before reading it from file with `yaml`.
        c = Core(source_file=configFile, schema_files=[validationFile],
                strict_rule_validation=True)

        # If there's an exception, then call it out to the user.
        c.validate(raise_exception=True)

        # Proceed with opening and parsing the file, now that its structure /
        # schema has been validated.
        with open(configFile, 'r') as y:
            parsed = yaml.safe_load(y)

        class InvalidDomainRequest(KeyError):
            pass

        # Validate the requested domains against a master list.
        for pool in parsed:
            if pool['name'] not in domains:
                raise InvalidDomainRequest(f'Invalid domain: {pool["name"]}')

        return parsed
Beispiel #9
0
    def test_python_obj_loading(self, tmp_path):
        # in latest pytest version, tmp_path is a PosixPath
        tmp_path = str(tmp_path)

        schema = """
        allowempty: True
        mapping:
          intents:
            type: !!python/str "seq"
            sequence:
            - type: !!python/str "str"
        """
        data = """
        intents:
         - greet
         - default
         - goodbye
        """
        schema_path = os.path.join(tmp_path, 'schema.yaml')
        with open(schema_path, 'w') as stream:
            stream.write(schema)
        data_path = os.path.join(tmp_path, 'data.yaml')
        with open(data_path, 'w') as stream:
            stream.write(data)

        c = Core(source_file=data_path, schema_files=[schema_path])
        c.validate()
def load_config(config_name):
    with open(config_name) as config_file:
        c = Core(data_file_obj=config_file, schema_files=[
            "schema.yaml",
            "resolvers_schema.yaml"])
        app_config = c.validate(raise_exception=True)
        return app_config
def create_validator(source_data):
    """ Generate validator from PyKwalify """
    version = source_data.get('schema_version', '3.1.0')
    schema = get_schema(version)
    validator = Core(source_data={}, schema_data=schema)
    validator.source = source_data
    return validator
Beispiel #12
0
def load(testfile):
    """
    It loads a test file.

    :param testfile: the file path of the test
    :type testfile: str
    :returns: dict representing the test file
    """
    if not testfile:
        raise ValueError("'testfile' is empty")

    logger = logging.getLogger(__name__)
    logger.debug("loading file '%s'", testfile)

    file_def = {}
    file_name, file_ext = os.path.splitext(testfile)

    logger.debug("filename=%s, extension=%s", file_name, file_ext)

    if file_ext == '.yml' or file_ext == '.yaml':
        try:
            currdir = os.path.abspath(os.path.dirname(__file__))
            schemafile = os.path.join(currdir, "files", "schema.yml")
            validator = Core(source_file=testfile, schema_files=[schemafile])
            validator.validate(raise_exception=True)
        except PyKwalifyException as ex:
            raise FileParseError(ex.msg)

        with open(testfile, 'r') as stream:
            file_def = yaml.load(stream)
    else:
        raise FileNotSupported("'%s' file type is not supported"%file_ext)

    return file_def
Beispiel #13
0
    def validate_config(config_file):
        ''' Validates config against the schema
        And also validates the lambdas in the subscriptions that they are defined
        Raises ConfigValidationError if not valid.
        '''
        schema_path = resource_filename("xflow", "schema.yaml")
        c = Core(source_file=config_file, schema_files=[schema_path])
        try:
            config = c.validate(raise_exception=True)
        except pykwalify.errors.SchemaError as ex:
            raise ConfigValidationError(str(ex))

        subscriptions = config.get('subscriptions') or []
        lambdas = config.get('lambdas') or []
        lambda_names = [l['name'] for l in lambdas]
        subscription_events = []
        for ss in subscriptions:
            event_name = ss['event']
            subscription_events.append(event_name)
            subscribers = ss.get('subscribers') or []
            for s in subscribers:
                if s not in lambda_names:
                    raise ConfigValidationError("Lambda not defined for subscriber %s" % s)

        workflows = config.get('workflows') or []
        for w in workflows:
            workflow_id = w['id']
            events = w.get('flow') or []
            for e in events:
                if e not in subscription_events:
                    raise ConfigValidationError("Event %s not defined in workflow %s" % (e, workflow_id))
Beispiel #14
0
 def run_check(queue, file):
     # print(file)
     c = Core(source_file=file, schema_files=[DICTIONARY_SCHEMA_FILE])
     try:
         c.validate(raise_exception=True)
     except SchemaError as e:
         queue.put([file, e.msg])
Beispiel #15
0
    def is_valid_scheme(self):
        # type: () -> bool
        """Validate the file scheme according to the scheme we have saved in SCHEMAS_PATH.

        Returns:
            bool. Whether the scheme is valid on self.file_path.
        """
        if self.scheme_name in [None, 'image', 'readme', 'changelog']:
            return True
        # ignore reputations.json
        if checked_type(self.file_path,
                        JSON_ALL_REPUTATIONS_INDICATOR_TYPES_REGEXES):
            return True
        try:
            # disabling massages of level INFO and beneath of pykwalify such as: INFO:pykwalify.core:validation.valid
            log = logging.getLogger('pykwalify.core')
            log.setLevel(logging.WARNING)
            path = os.path.normpath(
                os.path.join(__file__, "..", "..", self.SCHEMAS_PATH,
                             '{}.yml'.format(self.scheme_name)))
            core = Core(source_file=self.file_path, schema_files=[path])
            core.validate(raise_exception=True)
        except Exception as err:
            try:
                print_error(self.parse_error_msg(err))
                print_error(Errors.suggest_fix(self.file_path))
            except Exception:
                print_error('Failed: {} failed.\nin {}'.format(
                    self.file_path, str(err)))
            self.is_valid = False
            return False
        return True
Beispiel #16
0
    def _validate_cfg(self):
        """
        Open and parse the YAML configuration file and ensure it matches
        our Schema for a Dogen configuration.
        """
        # Fail early if descriptor file is not found
        if not os.path.exists(self.descriptor):
            raise Error(
                "Descriptor file '%s' could not be found. Please make sure you specified correct path."
                % self.descriptor)

        schema_path = os.path.join(self.pwd, "schema", "kwalify_schema.yaml")
        schema = {}
        with open(schema_path, 'r') as fh:
            schema = yaml.safe_load(fh)

        if schema is None:
            raise Error("couldn't read a valid schema at %s" % schema_path)

        for plugin in self.plugins:
            plugin.extend_schema(schema)

        with open(self.descriptor, 'r') as stream:
            self.cfg = yaml.safe_load(stream)

        c = Core(source_data=self.cfg, schema_data=schema)
        try:
            c.validate(raise_exception=True)
        except SchemaError as e:
            raise Error(e)
Beispiel #17
0
def main(argv=None):

    parser = argparse.ArgumentParser()
    parser.add_argument('filenames', nargs='*', help='filenames to check')
    args = parser.parse_args(argv)
    filenames = args.filenames
    settings = load_settings()
    rules = settings["rules"]
    checked_files = set()
    errors = False
    for rule in rules:
        schema_file, data_dir = rule["schema_file"], rule["data_dir"]
        for filename in filenames:
            if filename not in checked_files:
                file_path = Path(filename)
                parent = file_path.parent
                if str(parent) == data_dir:
                    try:
                        print("\n" + filename)
                        file = Core(source_file=filename,
                                    schema_files=[schema_file])
                        file.validate(raise_exception=True)
                        checked_files.add(filename)
                    except SchemaError:
                        errors = True
                    else:
                        print("Validated")
    return 0 if not errors else 1
Beispiel #18
0
def verify_generic(to_verify, schema_filename):
    """Verify a generic file against a given schema file

    Args:
        to_verify (str): filename of file to check
        schema_filename (str): filename of schema

    Raises:
        BadSchemaError: Schema did not match
    """
    logger.debug("Verifying %s against %s", to_verify, schema_filename)

    here = os.path.dirname(os.path.abspath(__file__))
    extension_module_filename = os.path.join(here, "extensions.py")

    verifier = Core(
        to_verify,
        [schema_filename],
        extensions=[extension_module_filename],
    )

    try:
        verifier.validate()
    except pykwalify.errors.PyKwalifyException as e:
        logger.exception("Error validating %s", to_verify)
        raise_from(BadSchemaError(), e)
Beispiel #19
0
 def test_load_wrong_schema_files_type(self):
     """
     It should only be possible to send in a list type as 'schema_files' object
     """
     with pytest.raises(CoreError) as ex:
         Core(source_file=None, schema_files={})
     assert "schema_files must be of list type" in str(ex.value)
def validate_schema(file_path, matching_regex=None):
    if matching_regex is None:
        for regex in CHECKED_TYPES_REGEXES:
            if re.match(regex, file_path, re.IGNORECASE):
                matching_regex = regex
                break

    if matching_regex in SKIPPED_SCHEMAS:
        return True

    if not os.path.isfile(file_path):
        return True

    if matching_regex is not None and REGEXES_TO_SCHEMA_DIC.get(
            matching_regex):
        c = Core(source_file=file_path,
                 schema_files=[
                     SCHEMAS_PATH + REGEXES_TO_SCHEMA_DIC.get(matching_regex) +
                     '.yml'
                 ])
        try:
            c.validate(raise_exception=True)
            return True
        except Exception as err:
            print_error('Failed: %s failed' % (file_path, ))
            print_error(err)
            return False

    print file_path + " doesn't match any of the known supported file prefix/suffix," \
                      " please make sure that its naming is correct."
    return True
    def validate(self, cloudformation_string):
        """
        Validating the schema for a cloudformation segment
        :param cloudformation_string:
        :return:
        """
        if self.debug:
            print("\n\n##################################################")
            print(
                'CloudformationValidator - validate - validating following string'
                + lineno())

            print(str(cloudformation_string) + lineno())
            print(
                "#######################################################\n\n")

        schema = SchemaGenerator.SchemaGenerator(debug=self.debug)
        main_schema = schema.generate(cloudformation_string)

        if self.debug:
            print('main_schema: ' + str(main_schema) + lineno())

        ## create validator
        c = Core(source_data=cloudformation_string, schema_data=main_schema)
        c.validate(raise_exception=False)

        if len(c.errors) > 0:
            if self.debug:
                print('errors: ' + str(c.errors) + lineno())
            #raise ParserError.new('Basic CloudFormation syntax error', errors)

        # Return any validation error
        return c.errors
Beispiel #22
0
def _validate(args, app_desc):
    path = _find_config(args, app_desc)
    # Allow empty mapping (not allowed by pykawlify)
    raw_config = _order_load_path(path)
    if raw_config.get(app_desc.app_name, None) is None:
        raw_config[app_desc.app_name] = {}
        config_p = tempfile.NamedTemporaryFile('w',
                                               delete=False,
                                               suffix=".yml")
        ordered_dump(raw_config, config_p)
        config_p.flush()
        path = config_p.name

    fp = tempfile.NamedTemporaryFile('w', delete=False, suffix=".yml")

    def _clean(p, k, v):
        return k != 'reloadable'

    clean_schema = remap(app_desc.schema.raw_schema, _clean)
    ordered_dump(clean_schema, fp)
    fp.flush()
    name = fp.name
    if Core is None:
        raise Exception("Cannot validate file, pykwalify is not installed.")
    c = Core(
        source_file=path,
        schema_files=[name],
    )
    c.validate()
Beispiel #23
0
    def validate_domain_yaml(cls, yaml):
        """Validate domain yaml."""
        from pykwalify.core import Core

        log = logging.getLogger("pykwalify")
        log.setLevel(logging.WARN)

        try:
            schema_file = pkg_resources.resource_filename(
                __name__, "schemas/domain.yml")
            source_data = rasa.utils.io.read_yaml(yaml)
        except YAMLError:
            raise InvalidDomain(
                "The provided domain file is invalid. You can use "
                "http://www.yamllint.com/ to validate the yaml syntax "
                "of your domain file.")
        except DuplicateKeyError as e:
            raise InvalidDomain(
                "The provided domain file contains a duplicated key: {}".
                format(str(e)))

        try:
            c = Core(source_data=source_data, schema_files=[schema_file])
            c.validate(raise_exception=True)
        except SchemaError:
            raise InvalidDomain(
                "Failed to validate your domain yaml. "
                "Please make sure the file is correct; to do so, "
                "take a look at the errors logged during "
                "validation previous to this exception. "
                "You can also validate your domain file's yaml "
                "syntax using http://www.yamllint.com/.")
Beispiel #24
0
    def get_group_config(self, group_dir):
        with Dir(group_dir):

            group_schema_path = os.path.join(
                os.path.dirname(os.path.realpath(__file__)),
                "schema_group.yml")
            c = Core(source_file="group.yml", schema_files=[group_schema_path])
            c.validate(raise_exception=True)

            with open("group.yml", "r") as f:
                group_yml = f.read()

            # group.yml can contain a `vars` section which should be a
            # single level dict containing keys to str.format(**dict) replace
            # into the YAML content. If `vars` found, the format will be
            # preformed and the YAML model will reloaded from that result
            tmp_config = Model(yaml.load(group_yml))
            replace_vars = tmp_config.vars
            if replace_vars is not Missing:
                try:
                    tmp_config = Model(
                        yaml.load(group_yml.format(**replace_vars)))
                except KeyError as e:
                    raise ValueError(
                        'group.yml contains template key `{}` but no value was provided'
                        .format(e.args[0]))
            return tmp_config
Beispiel #25
0
    def is_valid_scheme(self):
        # type: () -> bool
        """Validate the file scheme according to the scheme we have saved in SCHEMAS_PATH.

        Returns:
            bool. Whether the scheme is valid on self.file_path.
        """
        if self.scheme_name in [None, FileType.IMAGE, FileType.README, FileType.RELEASE_NOTES, FileType.TEST_PLAYBOOK]:
            return True
        # ignore reputations.json
        if checked_type(self.file_path, JSON_ALL_REPUTATIONS_INDICATOR_TYPES_REGEXES):
            return True
        try:
            # disabling massages of level INFO and beneath of pykwalify such as: INFO:pykwalify.core:validation.valid
            log = logging.getLogger('pykwalify.core')
            log.setLevel(logging.WARNING)
            scheme_file_name = 'integration' if self.scheme_name.value == 'betaintegration' else self.scheme_name.value
            path = os.path.normpath(
                os.path.join(__file__, "..", "..", self.SCHEMAS_PATH, '{}.yml'.format(scheme_file_name)))
            core = Core(source_file=self.file_path,
                        schema_files=[path])
            core.validate(raise_exception=True)
        except Exception as err:
            try:
                error_message, error_code = self.parse_error_msg(err)
                if self.handle_error(error_message, error_code, self.file_path,
                                     suggested_fix=Errors.suggest_fix(self.file_path)):
                    self.is_valid = False
                    return False
            except Exception:
                error_message, error_code = Errors.pykwalify_general_error(err)
                if self.handle_error(error_message, error_code, self.file_path):
                    self.is_valid = False
                    return False
        return True
def validate_file_schema(file_url: str, info) -> bool:
    """Validate if a file has the correct schema."""
    schema_dir = os.path.dirname(os.path.abspath(__file__))
    schema_dir = os.path.join(schema_dir, '../entity/schema.yaml')
    core = Core(source_file=file_url, schema_files=[schema_dir])
    is_valid = False
    try:
        core.validate(raise_exception=True)
        is_valid = True
    except SchemaError:
        lines_of_exceptions = core.errors
        errors_values = \
            [x.value for x in lines_of_exceptions if not hasattr(x, 'key')]
        errors_keys = \
            [x for x in lines_of_exceptions if hasattr(x, 'key')]
        errors_values_formated = \
            ['"{val}"'.format(val=x) for x in errors_values]
        errors_keys_formated = \
            ['"{val}"'.format(val=x.key) for x in errors_keys
                if x.msg and str(x.msg).find('was not defined') >= 0]
        errors_keys_joined = ','.join(errors_keys_formated)
        errors_values_joined = ','.join(errors_values_formated)
        error_value = '"values": [{values}], "keys": [{keys}]'.format(
            values=errors_values_joined, keys=errors_keys_joined)
        util.cloudwatch_log(
            info.context,
            'Error: An error occurred validating vulnerabilities file')
        raise InvalidSchema(expr=error_value)
    except CoreError:
        raise InvalidSchema()
    finally:
        os.unlink(file_url)
    return is_valid
Beispiel #27
0
def _validate(args, app_desc):
    if Core is None:
        raise Exception("Cannot validate file, pykwalify is not installed.")
    path = _find_config(args, app_desc)
    # Allow empty mapping (not allowed by pykwalify)
    raw_config = _order_load_path(path)
    if raw_config.get(app_desc.app_name) is None:
        raw_config[app_desc.app_name] = {}
    # Rewrite the file any way to merge any duplicate keys
    with tempfile.NamedTemporaryFile('w', delete=False,
                                     suffix=".yml") as config_p:
        ordered_dump(raw_config, config_p)

    def _clean(p, k, v):
        return k not in ['reloadable', 'path_resolves_to']

    clean_schema = remap(app_desc.schema.raw_schema, _clean)
    with tempfile.NamedTemporaryFile('w', suffix=".yml") as fp:
        ordered_dump(clean_schema, fp)
        fp.flush()
        c = Core(
            source_file=config_p.name,
            schema_files=[fp.name],
        )
    os.remove(config_p.name)
    c.validate()
Beispiel #28
0
def validate_yaml_schema(yaml_file_content: Text, schema_path: Text) -> None:
    """
    Validate yaml content.

    Args:
        yaml_file_content: the content of the yaml file to be validated
        schema_path: the schema of the yaml file
    """
    from pykwalify.core import Core
    from pykwalify.errors import SchemaError
    from ruamel.yaml import YAMLError
    import pkg_resources
    import logging

    log = logging.getLogger("pykwalify")
    log.setLevel(logging.CRITICAL)

    try:
        # we need "rt" since
        # it will add meta information to the parsed output. this meta information
        # will include e.g. at which line an object was parsed. this is very
        # helpful when we validate files later on and want to point the user to the
        # right line
        source_data = rasa.shared.utils.io.read_yaml(
            yaml_file_content, reader_type=["safe", "rt"]
        )
    except (YAMLError, DuplicateKeyError) as e:
        raise YamlSyntaxException(underlying_yaml_exception=e)

    schema_file = pkg_resources.resource_filename(PACKAGE_NAME, schema_path)
    schema_utils_file = pkg_resources.resource_filename(
        PACKAGE_NAME, RESPONSES_SCHEMA_FILE
    )
    schema_extensions = pkg_resources.resource_filename(
        PACKAGE_NAME, SCHEMA_EXTENSIONS_FILE
    )

    # Load schema content using our YAML loader as `pykwalify` uses a global instance
    # which can fail when used concurrently
    schema_content = rasa.shared.utils.io.read_yaml_file(schema_file)
    schema_utils_content = rasa.shared.utils.io.read_yaml_file(schema_utils_file)
    schema_content = dict(schema_content, **schema_utils_content)

    c = Core(
        source_data=source_data,
        schema_data=schema_content,
        extensions=[schema_extensions],
    )

    try:
        c.validate(raise_exception=True)
    except SchemaError:
        raise YamlValidationException(
            "Please make sure the file is correct and all "
            "mandatory parameters are specified. Here are the errors "
            "found during validation",
            c.errors,
            content=source_data,
        )
Beispiel #29
0
    def test_files_with_unicode_content_success(self, tmpdir):
        """
        These tests should pass with no exception raised
        """
        fail_data_2s_yaml = {
            'schema': {
                'type': 'map',
                'mapping': {
                    'msg': {
                        'type': 'int',
                    },
                }
            },
            'data': {
                'msg': 123,
            },
            'errors': []
        }

        source_f = tmpdir.join(u"2så.json")
        source_f.write(yaml.safe_dump(fail_data_2s_yaml, allow_unicode=True))

        _pass_tests = [
            # Test mapping with unicode key and value
            u"1s.yaml",
            # # Test unicode filename.
            # It is not possible to package a file with unicode characters
            # like åäö in the filename in some python versions.
            # Mock a file with åäö during testing to properly simulate this again.
            unicode(source_f),
            # Test sequence with unicode keys
            u"3s.yaml",
        ]

        for passing_test_files in _pass_tests:
            f = self.f(passing_test_files)

            with open(f, "r") as stream:
                yaml_data = yaml.safe_load(stream)
                data = yaml_data["data"]
                schema = yaml_data["schema"]

            try:
                print(u"Running test files: {0}".format(f))
                c = Core(source_data=data, schema_data=schema)
                c.validate()
                compare(c.validation_errors, [],
                        prefix="No validation errors should exist...")
            except Exception as e:
                print(u"ERROR RUNNING FILES: {0}".format(f))
                raise e

            # This serve as an extra schema validation that tests more complex structures then testrule.py do
            compare(
                c.root_rule.schema_str,
                schema,
                prefix=
                u"Parsed rules is not correct, something have changed... files : {0}"
                .format(f))
Beispiel #30
0
    def test_files_with_unicode_content_failing(self, tmpdir):
        """
        These tests should fail with the specified exception
        """
        # To trigger schema exception we must pass in a source file
        fail_data_2f_yaml = {
            'schema': {
                'type': 'map',
                'mapping': {
                    'msg': {
                        'type': 'int',
                    },
                }
            },
            'data': {
                'msg': 'Foobar',
            },
            'errors': ["Value 'Foobar' is not of type 'int'. Path: '/msg'"]
        }

        source_f = tmpdir.join(u"2få.json")
        source_f.write(yaml.safe_dump(fail_data_2f_yaml, allow_unicode=True))

        _fail_tests = [
            # Test mapping with unicode key and value but wrong type
            (u"1f.yaml", SchemaError),
            # Test unicode filename with validation errors.
            # It is not possible to package a file with unicode characters
            # like åäö in the filename in some python versions.
            # Mock a file with åäö during testing to properly simulate this again.
            (unicode(source_f), SchemaError),
            # Test unicode data inside seq but wrong type
            (u"3f.yaml", SchemaError),
        ]

        for failing_test, exception_type in _fail_tests:
            f = self.f(failing_test)

            with open(f, "r") as stream:
                yaml_data = yaml.safe_load(stream)
                data = yaml_data["data"]
                schema = yaml_data["schema"]
                errors = yaml_data["errors"]

            try:
                print(u"Running test files: {0}".format(f))
                c = Core(source_data=data, schema_data=schema)
                c.validate()
            except exception_type:
                pass  # OK
            else:
                raise AssertionError(
                    u"Exception {0} not raised as expected... FILES: {1} : {2}"
                    .format(exception_type, exception_type))

            compare(sorted(c.validation_errors),
                    sorted(errors),
                    prefix=u"Wrong validation errors when parsing files : {0}".
                    format(f))