Python schema.SchemaError() Examples

The following are 30 code examples of schema.SchemaError(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module schema , or try the search function .
Example #1
Source File: repository.py    From skelebot with MIT License 6 votes vote down vote up
def load(cls, config):
        """Instantiate the Repository Class Object based on a config dict"""

        cls.validate(config)

        s3 = None
        artifactory = None
        if ("s3" in config):
            s3 = S3Repo.load(config["s3"])
        elif ("artifactory" in config):
            artifactory = ArtifactoryRepo.load(config["artifactory"])
        else:
            raise SchemaError(None, "Repository must contain 's3' or 'artifactory' config")

        artifactDicts = config["artifacts"]
        artifacts = []
        for artifact in artifactDicts:
            newArtifact = Artifact.load(artifact)
            artifacts.append(newArtifact)

        return cls(artifacts, s3, artifactory) 
Example #2
Source File: hard.py    From CO2MPAS-TA with European Union Public License 1.1 6 votes vote down vote up
def hard_validation(inputs, errors):
    """
    Parse input data for the hard validation.

    :param inputs:
        Input data.
    :type inputs: dict

    :param errors:
        Errors container.
    :type errors: dict

    :return:
        Parsed input data and errors container.
    :rtype: dict, dict
    """
    from schema import SchemaError
    for k, v in sh.stack_nested_keys(inputs, depth=3):
        for c, msg in _hard_validation(v, *k):
            sh.get_nested_dicts(errors, *k)[c] = SchemaError([], [msg])
    return inputs, errors 
Example #3
Source File: schema.py    From CO2MPAS-TA with European Union Public License 1.1 6 votes vote down vote up
def validate(data):
        if isinstance(data, str) and data == 'EMPTY':
            return sh.EMPTY

        try:
            empty = not (data or data == 0)
        except ValueError:
            empty = np.isnan(data).all()

        if empty:
            return sh.NONE
        else:
            raise SchemaError('%r is not empty' % data)


# noinspection PyUnusedLocal 
Example #4
Source File: convert.py    From CO2MPAS-TA with European Union Public License 1.1 6 votes vote down vote up
def _parameters2df(data, data_descriptions, write_schema):
    import schema
    validate, df = write_schema.validate, []
    for k, v in data.items():
        try:
            for param_id, vl in validate({_param_parts(k)['param']: v}).items():
                if vl is not sh.NONE:
                    df.append({
                        'Parameter': _parse_name(param_id, data_descriptions),
                        'Model Name': k,
                        'Value': vl
                    })
        except schema.SchemaError as ex:
            raise ValueError(k, v, ex)

    if df:
        import pandas as pd
        df = pd.DataFrame(df)
        df.set_index(['Parameter', 'Model Name'], inplace=True)
        return df
    else:
        return None 
Example #5
Source File: sns.py    From hoaxy-backend with GNU General Public License v3.0 6 votes vote down vote up
def run(cls, args):
        """Overriding method as the entry point of this command."""
        try:
            args = cls.args_schema.validate(args)
        except SchemaError as e:
            raise SystemExit('\n' + e + '\n')
        session = Session(expire_on_commit=False)
        if args['--twitter-streaming'] is True:
            configure_logging('twitter.streaming')
            cls.twitter_stream(session, args)
        elif args['--load-tweets'] is True:
            configure_logging('twitter.load-tweets')
            cls.load_tweets(session, args)
        elif args['--reparse-db-tweets'] is True:
            configure_logging('twitter.reparse-db', file_level='WARNING')
            cls._test_table_names(session, args)
            cls.reparse_db(session, args) 
Example #6
Source File: validate.py    From policy_sentry with MIT License 6 votes vote down vote up
def validate_condition_block(condition_block):
    """
    Validates the format of the condition block that should be supplied in the template.

    Arguments:
        condition_block: {"condition_key_string": "ec2:ResourceTag/purpose", "condition_type_string": "StringEquals", "condition_value": "test"}
    Returns:
        Boolean: The decision
    """

    # TODO: Validate that the values are legit somehow
    CONDITION_BLOCK_SCHEMA = Schema(
        {
            "condition_key_string": And(Use(str)),
            "condition_type_string": And(Use(str)),
            "condition_value": And(Use(str)),
        }
    )
    try:
        CONDITION_BLOCK_SCHEMA.validate(condition_block)
        # TODO: Try to validate whether or not the condition keys are legit
        return True
    except SchemaError as s_e:
        logger.warning(s_e)
        return False 
Example #7
Source File: test_components_artifactory.py    From skelebot with MIT License 6 votes vote down vote up
def test_validate_mising(self):
        artifactoryDict = copy.deepcopy(self.artifactoryDict)
        del artifactoryDict['url']
        del artifactoryDict['repo']
        del artifactoryDict['path']

        try:
            sb.components.artifactory.Artifactory.validate(artifactoryDict)
        except SchemaError as error:
            self.assertEqual(error.code, "Missing keys: 'path', 'repo', 'url'")

        artifactDict = copy.deepcopy(self.artifactDict)
        del artifactDict['name']
        del artifactDict['file']

        try:
            sb.components.artifactory.Artifact.validate(artifactDict)
        except SchemaError as error:
            self.assertEqual(error.code, "Missing keys: 'file', 'name'") 
Example #8
Source File: validate.py    From policy_sentry with MIT License 6 votes vote down vote up
def check(conf_schema, conf):
    """
    Validates a user-supplied JSON vs a defined schema.

    Arguments:
        conf_schema: The Schema object that defines the required structure.
        conf: The user-supplied schema to validate against the required structure.
    Returns:
        Boolean: The decision about whether the JSON meets expected Schema requirements
    """
    try:
        conf_schema.validate(conf)
        return True
    except SchemaError as schema_error:
        try:
            # workarounds for Schema's logging approach
            print(schema_error.autos[0])
            detailed_error_message = schema_error.autos[2]
            print(detailed_error_message.split(" in {'")[0])
            # for error in schema_error.autos:
        except:  # pylint: disable=bare-except
            logger.critical(schema_error)
        return False 
Example #9
Source File: config.py    From tsrc with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def parse_config(
    file_path: Path, config_schema: Optional[schema.Schema] = None
) -> Config:
    try:
        contents = file_path.read_text()
    except OSError as os_error:
        raise tsrc.InvalidConfig(file_path, os_error)
    try:
        yaml = ruamel.yaml.YAML(typ="safe", pure=True)
        parsed = yaml.load(contents)
    except ruamel.yaml.error.YAMLError as yaml_error:
        raise tsrc.InvalidConfig(file_path, yaml_error)
    if config_schema:
        try:
            config_schema.validate(parsed)
        except schema.SchemaError as schema_error:
            raise tsrc.InvalidConfig(file_path, schema_error)
    return Config(parsed) 
Example #10
Source File: config_schema.py    From nni with MIT License 6 votes vote down vote up
def validate_annotation_content(self, experiment_config, spec_key, builtin_name):
        '''
        Valid whether useAnnotation and searchSpacePath is coexist
        spec_key: 'advisor' or 'tuner'
        builtin_name: 'builtinAdvisorName' or 'builtinTunerName'
        '''
        if experiment_config.get('useAnnotation'):
            if experiment_config.get('searchSpacePath'):
                raise SchemaError('If you set useAnnotation=true, please leave searchSpacePath empty')
        else:
            # validate searchSpaceFile
            if experiment_config[spec_key].get(builtin_name) == 'NetworkMorphism':
                return
            if experiment_config[spec_key].get(builtin_name):
                if experiment_config.get('searchSpacePath') is None:
                    raise SchemaError('Please set searchSpacePath!')
                self.validate_search_space_content(experiment_config) 
Example #11
Source File: config_schema.py    From nni with MIT License 6 votes vote down vote up
def validate_kubeflow_operators(self, experiment_config):
        '''Validate whether the kubeflow operators are valid'''
        if experiment_config.get('kubeflowConfig'):
            if experiment_config.get('kubeflowConfig').get('operator') == 'tf-operator':
                if experiment_config.get('trial').get('master') is not None:
                    raise SchemaError('kubeflow with tf-operator can not set master')
                if experiment_config.get('trial').get('worker') is None:
                    raise SchemaError('kubeflow with tf-operator must set worker')
            elif experiment_config.get('kubeflowConfig').get('operator') == 'pytorch-operator':
                if experiment_config.get('trial').get('ps') is not None:
                    raise SchemaError('kubeflow with pytorch-operator can not set ps')
                if experiment_config.get('trial').get('master') is None:
                    raise SchemaError('kubeflow with pytorch-operator must set master')

            if experiment_config.get('kubeflowConfig').get('storage') == 'nfs':
                if experiment_config.get('kubeflowConfig').get('nfs') is None:
                    raise SchemaError('please set nfs configuration!')
            elif experiment_config.get('kubeflowConfig').get('storage') == 'azureStorage':
                if experiment_config.get('kubeflowConfig').get('azureStorage') is None:
                    raise SchemaError('please set azureStorage configuration!')
            elif experiment_config.get('kubeflowConfig').get('storage') is None:
                if experiment_config.get('kubeflowConfig').get('azureStorage'):
                    raise SchemaError('please set storage type!') 
Example #12
Source File: config_schema.py    From nni with MIT License 6 votes vote down vote up
def validate_extras(self, data, algo_type):
        builtin_key = self.builtin_keys[algo_type]
        if (builtin_key in data) and (set(data.keys()) & self.customized_keys):
            raise SchemaError('{} and {} cannot be specified at the same time.'.format(
                builtin_key, set(data.keys()) & self.customized_keys
            ))

        if self.missing_customized_keys(data) and builtin_key not in data:
            raise SchemaError('Either customized {} ({}) or builtin {} ({}) must be set.'.format(
                algo_type, self.customized_keys, algo_type, builtin_key))

        if not self.missing_customized_keys(data):
            class_file_name = os.path.join(data['codeDir'], data['classFileName'])
            if not os.path.isfile(class_file_name):
                raise SchemaError('classFileName {} not found.'.format(class_file_name))

        builtin_name = data.get(builtin_key)
        class_args = data.get('classArgs')
        self.validate_class_args(class_args, algo_type, builtin_name) 
Example #13
Source File: input.py    From bob with GNU General Public License v3.0 6 votes vote down vote up
def Scm(spec, env, overrides, recipeSet):
    # resolve with environment
    spec = { k : ( env.substitute(v, "checkoutSCM::"+k) if isinstance(v, str) else v)
        for (k, v) in spec.items() }

    # apply overrides before creating scm instances. It's possible to switch the Scm type with an override..
    matchedOverrides = []
    for override in overrides:
        matched, spec = override.mangle(spec, env)
        if matched:
            matchedOverrides.append(override)

    # check schema again if any SCM override matched
    if matchedOverrides:
        try:
            recipeSet.SCM_SCHEMA.validate({ k:v for k,v in spec.items()
                if k != '__source' and k != 'recipe' })
        except schema.SchemaError as e:
            raise ParseError("Error validating SCM after applying scmOverrides: {}".format(str(e)))

    # create scm instance
    return getScm(spec, matchedOverrides, recipeSet) 
Example #14
Source File: __init__.py    From bob with GNU General Public License v3.0 6 votes vote down vote up
def auditFromData(data):
    typ = data.get("type")
    if typ == "git":
        scm = GitAudit
    elif typ == "import":
        scm = ImportAudit
    elif typ == "url":
        scm = UrlAudit
    elif typ == "svn":
        scm = SvnAudit
    else:
        from ..errors import ParseError
        raise ParseError("Cannot handle SCM: " + str(typ))

    try:
        data = scm.SCHEMA.validate(data)
        return scm.fromData(data)
    except schema.SchemaError as e:
        from ..errors import ParseError
        raise ParseError("Error while validating audit: {} {}".format(str(e), str(data))) 
Example #15
Source File: test_parametrization.py    From testplan with Apache License 2.0 6 votes vote down vote up
def test_same_name_func():
    """`name_func` should be used for generating method names."""

    @testsuite
    class MySuite(object):
        @testcase(
            parameters=(("foo", "bar"), ("alpha", "beta")),
            name_func=lambda func_name, kwargs: "same_name",
        )
        def sample_test(self, env, result, a, b):
            pass

        @testcase(
            parameters=(("foo", "bar"), ("alpha", "beta")),
            name_func=lambda func_name, kwargs: "same_name",
        )
        def other_test(self, env, result, a, b):
            pass

    with pytest.raises(SchemaError):
        MultiTest(name="abc", suites=[MySuite()]) 
Example #16
Source File: skelebot.py    From skelebot with MIT License 6 votes vote down vote up
def main():
    """
    The main function for Skelebot CLI where the config is loaded,
    arguments are parsed, and commands are executed
    """

    try:
        env = get_env()
        config = yaml.loadConfig(env)
        parser = skeleParser.SkeleParser(config, env)
        executor.execute(config, parser)
    except SchemaError as error:
        print(SCHEMA_ERROR.format(error))
        sys.exit(1)
    except RuntimeError as error:
        print(ERROR.format(error))
        sys.exit(1) 
Example #17
Source File: validator.py    From git-webhook with MIT License 6 votes vote down vote up
def param(self, schema):
        """A decorator for validate request data"""
        if not isinstance(schema, collections.Mapping):
            raise TypeError('schema must be Mapping')
        # add error message
        schema = {k: And(v, error='%s invalid' % k)
                  for k, v in schema.items()}
        validate = Schema(schema).validate

        def decorator(f):
            @functools.wraps(f)
            def wrapper(*args, **kwargs):
                data = self.get_data()
                try:
                    data = validate(data)
                except SchemaError as ex:
                    self.handle_error(str(ex))
                kwargs.update(data)
                return f(*args, **kwargs)
            return wrapper
        return decorator 
Example #18
Source File: yaml.py    From sismic with GNU Lesser General Public License v3.0 5 votes vote down vote up
def import_from_yaml(text: str=None, filepath: str=None, *, ignore_schema: bool=False, ignore_validation: bool=False) -> Statechart:
    """
    Import a statechart from a YAML representation (first argument) or a YAML file (filepath argument).

    Unless specified, the structure contained in the YAML is validated against a predefined
    schema (see *sismic.io.SCHEMA*), and the resulting statechart is validated using its *validate()* method.

    :param text: A YAML text. If not provided, filepath argument has to be provided.
    :param filepath: A path to a YAML file.
    :param ignore_schema: set to *True* to disable yaml validation.
    :param ignore_validation: set to *True* to disable statechart validation.
    :return: a *Statechart* instance
    """
    if not text and not filepath:
        raise TypeError('A YAML must be provided, either using first argument or filepath argument.')
    elif text and filepath:
        raise TypeError('Either provide first argument or filepath argument, not both.')
    elif filepath:
        with open(filepath, 'r') as f:
            text = f.read()

    if yaml.version_info < (0, 15):
        data = yaml.safe_load(text)  # type: dict
    else:
        yml = yaml.YAML(typ='safe', pure=True)
        data = yml.load(text)

    if not ignore_schema:
        try:
            data = schema.Schema(SCHEMA.statechart).validate(data)
        except schema.SchemaError as e:
            raise StatechartError('YAML validation failed') from e

    sc = import_from_dict(data)

    if not ignore_validation:
        sc.validate()
    return sc 
Example #19
Source File: test_schema.py    From snaql with MIT License 5 votes vote down vote up
def test_wrong_case(self):
        news_queries = self.snaql.load_queries('unsafe_news.sql')
        now = datetime.datetime.utcnow()
        schema = Schema({
            'news_id': And(Use(int), lambda i: i > 0),
            'rating': And(Use(float), lambda r: r > 0),
            'date_from': And(Use(guard_date)),
        })
        context = {
            'news_id': '0',
            'date_from': now,
            'rating': 4.22,
        }
        with self.assertRaises(SchemaError):
            news_queries.select_by_id(schema=schema, **context) 
Example #20
Source File: test_config_validation.py    From nni with MIT License 5 votes vote down vote up
def test_invalid_config(self):
        file_names = glob.glob('./config_files/invalid/*.yml')
        for fn in file_names:
            experiment_config = get_yml_content(fn)
            try:
                validate_all_content(experiment_config, fn)
                print_error('config file:', fn,'Schema error should be raised for invalid config file!')
                assert False
            except SchemaError as e:
                print_green('config file:', fn, 'Expected error catched:', e) 
Example #21
Source File: deployment_map.py    From aws-deployment-framework with Apache License 2.0 5 votes vote down vote up
def _read(self, file_path=None):
        if file_path is None:
            file_path = self.map_path
        try:
            LOGGER.info('Loading deployment_map file %s', file_path)
            with open(file_path, 'r') as stream:
                _input = yaml.load(stream, Loader=yaml.FullLoader)
                return SchemaValidation(_input).validated
        except FileNotFoundError:
            LOGGER.info('No default map file found at %s, continuing', file_path)
            return {}
        except SchemaError as err:
            LOGGER.error(err.code)
            sys.exit(1) 
Example #22
Source File: manifest.py    From tsrc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def validate_repo(data: Any) -> None:
    copy_schema = {"file": str, schema.Optional("dest"): str}
    symlink_schema = {"source": str, "target": str}
    remote_schema = {"name": str, "url": str}
    repo_schema = schema.Schema(
        {
            "dest": str,
            schema.Optional("branch"): str,
            schema.Optional("copy"): [copy_schema],
            schema.Optional("symlink"): [symlink_schema],
            schema.Optional("sha1"): str,
            schema.Optional("tag"): str,
            schema.Optional("remotes"): [remote_schema],
            schema.Optional("url"): str,
        }
    )
    repo_schema.validate(data)
    url = data.get("url")
    remotes = data.get("remotes")
    if url and remotes:
        raise schema.SchemaError(
            "Repo config cannot contain both an url and a list of remotes"
        )
    if not url and not remotes:
        raise schema.SchemaError(
            "Repo config should contain either a url or a non-empty list of remotes"
        ) 
Example #23
Source File: test_config.py    From tsrc with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_invalid_schema(tmp_path: Path) -> None:
    foo_yml = tmp_path / "foo.yml"
    foo_yml.write_text(
        textwrap.dedent(
            """
        foo:
            bar: 42
        """
        )
    )
    foo_schema = schema.Schema({"foo": {"bar": str}})
    with pytest.raises(tsrc.InvalidConfig) as e:
        tsrc.parse_config(foo_yml, foo_schema)
    assert isinstance(e.value.cause, schema.SchemaError) 
Example #24
Source File: __init__.py    From CO2MPAS-TA with European Union Public License 1.1 5 votes vote down vote up
def validate_meta(meta=None, hard_validation=False):
    """
    Validate meta data.

    :param meta:
        Meta data.
    :type meta: dict

    :param hard_validation:
        Add extra data validations.
    :type hard_validation: bool

    :return:
        Validated meta data.
    :rtype: dict
    """
    i, e = _validate_base_with_schema(meta or {}, depth=2)
    if hard_validation:
        from schema import SchemaError
        from .hard import _hard_validation
        for k, v in sorted(sh.stack_nested_keys(i, depth=1)):
            for c, msg in _hard_validation(v, 'meta'):
                sh.get_nested_dicts(e, *k)[c] = SchemaError([], [msg])

    if _log_errors_msg(e):
        return sh.NONE

    return i 
Example #25
Source File: config_schema.py    From nni with MIT License 5 votes vote down vote up
def validate_class_args(self, class_args, algo_type, builtin_name):
        if not builtin_name or not class_args:
            return
        meta = get_builtin_algo_meta(algo_type+'s', builtin_name)
        if meta and 'accept_class_args' in meta and meta['accept_class_args'] == False:
            raise SchemaError('classArgs is not allowed.')

        validator = create_validator_instance(algo_type+'s', builtin_name)
        if validator:
            try:
                validator.validate_class_args(**class_args)
            except Exception as e:
                raise SchemaError(str(e)) 
Example #26
Source File: test_compressor.py    From nni with MIT License 5 votes vote down vote up
def test_torch_quantizer_validation(self):
        # test bad configuraiton
        quantizer_classes = [torch_compressor.__dict__[x] for x in \
            ['NaiveQuantizer', 'QAT_Quantizer', 'DoReFaQuantizer', 'BNNQuantizer']]

        bad_configs = [
            [
                {'bad_key': 'abc'}
            ],
            [
                {'quant_types': 'abc'}
            ],
            [
                {'quant_bits': 34}
            ],
            [
                {'op_types': 'default'}
            ],
            [
                {'quant_bits': {'abc': 123}}
            ]
        ]
        model = TorchModel()
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
        for quantizer_class in quantizer_classes:
            for config_list in bad_configs:
                try:
                    quantizer_class(model, config_list, optimizer)
                    print(config_list)
                    assert False, 'Validation error should be raised for bad configuration'
                except schema.SchemaError:
                    pass
                except:
                    print('FAILED:', quantizer_class, config_list)
                    raise 
Example #27
Source File: test_compressor.py    From nni with MIT License 5 votes vote down vote up
def test_torch_pruner_validation(self):
        # test bad configuraiton
        pruner_classes = [torch_compressor.__dict__[x] for x in \
            ['LevelPruner', 'SlimPruner', 'FPGMPruner', 'L1FilterPruner', 'L2FilterPruner', 'AGP_Pruner', \
            'ActivationMeanRankFilterPruner', 'ActivationAPoZRankFilterPruner']]

        bad_configs = [
            [
                {'sparsity': '0.2'},
                {'sparsity': 0.6 }
            ],
            [
                {'sparsity': 0.2},
                {'sparsity': 1.6 }
            ],
            [
                {'sparsity': 0.2, 'op_types': 'default'},
                {'sparsity': 0.6 }
            ],
            [
                {'sparsity': 0.2 },
                {'sparsity': 0.6, 'op_names': 'abc' }
            ]
        ]
        model = TorchModel()
        optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
        for pruner_class in pruner_classes:
            for config_list in bad_configs:
                try:
                    pruner_class(model, config_list, optimizer)
                    print(config_list)
                    assert False, 'Validation error should be raised for bad configuration'
                except schema.SchemaError:
                    pass
                except:
                    print('FAILED:', pruner_class, config_list)
                    raise 
Example #28
Source File: config_validation.py    From nni with MIT License 5 votes vote down vote up
def validate_op_types_op_names(data):
    if not ('op_types' in data or 'op_names' in data):
        raise SchemaError('Either op_types or op_names must be specified.')
    return True 
Example #29
Source File: Generic.py    From GloboNetworkAPI with Apache License 2.0 5 votes vote down vote up
def _validate_neighbor(self):
        """Validate neighbor against Schema."""

        neighbor_schema = Schema({
            'remote_ip': basestring,
            'remote_as': And(basestring, lambda n: 0 <= int(n) <= 4294967295),
            Optional('password'): basestring,
            Optional('maximum_hops'): And(basestring,
                                          lambda n: 1 <= int(n) <= 255),
            Optional('timer_keepalive'): And(basestring,
                                             lambda n: 1 <= int(n) <= 65535),
            Optional('timer_timeout'): And(basestring,
                                           lambda n: 3 <= int(n) <= 65536),
            Optional('description'): basestring,
            Optional('soft_reconfiguration'): bool,
            Optional('community'): bool,
            Optional('remove_private_as'): bool,
            Optional('next_hop_self'): bool
        })

        try:
            neighbor_schema.validate(self.neighbor)
        except SchemaWrongKeyError:
            # It doesn't matter if neighbor dict has other keys besides these.
            pass
        except SchemaError as e:
            raise InvalidNeighborException(e.code) 
Example #30
Source File: validation.py    From spotty with MIT License 5 votes vote down vote up
def validate_config(schema: Schema, config):
    try:
        validated = schema.validate(config)
    except SchemaError as e:
        raise ValueError(e.errors[-1] if e.errors[-1] else e.autos[-1])

    return validated