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("ignore_submodules"): bool, 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" )
def validate(self, data): if "kind" not in data: raise schema.SchemaError( "Required 'kind' key not found in %r" % data) if data["kind"] not in api_schemas: raise schema.SchemaError( "Unknown 'kind' key in %r valid kinds are %r" % (data, api_schemas.keys())) return api_schemas[data["kind"]].validate(data)
def validate(self, data: str) -> pathlib.Path: p = pathlib.Path(data) if self.must_exist and not p.exists(): raise schema.SchemaError(f"{p} must exist already") if self.mkdir: p.mkdir(exist_ok=True, parents=True) return p
def validate_config(cfg: Config) -> None: """Second pass of validation, using the Config class. """ # Note: separated from validate_basic_schema to keep error # messages user friendly current_version = cfg.current_version validate_git_message_template(cfg.git_message_template) validate_git_tag_template(cfg.git_tag_template) match = cfg.version_regex.fullmatch(current_version) if not match: message = "Current version: %s does not match version regex" % current_version raise schema.SchemaError(message) current_version_regex_groups = match.groupdict() for file_config in cfg.files: version_template = file_config.version_template if version_template: validate_version_template(file_config.src, version_template, current_version_regex_groups) for hook in cfg.hooks: validate_hook_cmd(hook.cmd)
def _parse_species_options(cls, options, species_index): """ Return a dictionary containing command-line options for a species. options: dictionary containing all command-line options. species_index: which species to return options for """ species_options = {opts.SPECIES_NAME: options[opts.SPECIES_ARG][species_index]} star_infos = options[opts.SPECIES_INFO_ARG][species_index].split(",") if len(star_infos) == 1: species_options[opts.MAPPER_INDEX] = star_infos[0] species_options[opts.GTF_FILE] = None species_options[opts.GENOME_FASTA] = None elif len(star_infos) == 2: species_options[opts.MAPPER_INDEX] = None species_options[opts.GTF_FILE] = star_infos[0] species_options[opts.GENOME_FASTA] = star_infos[1] else: raise schema.SchemaError( None, "Should specify either a STAR index or both GTF file " + "and genome FASTA directory for species {species}.". format(species=species_options[opts.SPECIES_NAME])) return species_options
def validate_version_template(src: str, version_template: str, known_groups: Dict[str, str]) -> None: try: version_template.format(**known_groups) except KeyError as e: message = "version template for '%s' contains unknown group: %s" % ( src, e) raise schema.SchemaError(message)
def validate(self, data): if data not in self.api: raise schema.SchemaError("%r not found in the API " "valid keys are %r" % (data, self.api.keys())) return data
def validate(self, data): """ Check that data has all the attributes specified, and validate each attribute with the schema provided on construction """ for a, s in self.attrs.items(): s = schema.Schema(s) try: value = getattr(data, a) except AttributeError: raise schema.SchemaError(" Missing attribute %r" % a, []) try: new_value = s.validate(value) except schema.SchemaError as e: raise schema.SchemaError( "Invalid value for attribute %r: %s" % (a, e), []) setattr(data, a, new_value) return data
def validate(self, data): # Check the basic properties schema.Schema(schema.Or(*six.string_types)).validate(data) # No empty strings either if not data: raise schema.SchemaError("String is empty") return data
def validate_value( self, cl_options, file_options, option_values, quant_run_option_values): _PiquantOption.validate_value( self, cl_options, file_options, option_values, quant_run_option_values) if self.name not in quant_run_option_values: raise schema.SchemaError( None, self.name + " option value(s) must be specified.")
def embeds_many_as_object_validate(value): if not is_required and value == None: return None if not isinstance(value, dict): return False for key in value: try: validator.validate_data(value[key]) except Exception as e: # print 'invalid', params['type'], field_name, params['model'], str(e) raise schema.SchemaError( "Invalid property '%s' in embedsManyAsObject '%s': %s" % (key, field_name, str(e))) return True
def _read_file_options(options_file_path): file_options = {} if options_file_path: with open(options_file_path) as options_file: option_names = \ [o.get_option_name() for o in _PiquantOption.OPTIONS] for option_name, vals in \ [line.strip().split() for line in options_file]: if option_name not in option_names: raise schema.SchemaError( None, "Unknown option '{o}' in options file.". format(o=option_name)) file_options[option_name] = vals return file_options
def validate_repo(data: Any) -> None: copy_schema = {"src": str, schema.Optional("dest"): str} remote_schema = {"name": str, "url": str} repo_schema = schema.Schema({ "src": str, schema.Optional("branch"): str, schema.Optional("copy"): [copy_schema], schema.Optional("sha1"): str, schema.Optional("tag"): str, schema.Optional("remotes"): [remote_schema], schema.Optional("url"): str, }) repo_schema.validate(data) if ("url" in data) and ("remotes" in data): raise schema.SchemaError( "Repo config cannot contain both an url and a list of remotes")
def embeds_many_validate(value): if not is_required and value == None: return None if not isinstance(value, (list, dict)): return False if isinstance(value, list): for v in value: validator.validate_data(v) elif isinstance(value, dict): try: validator.validate_data(value) except Exception as e: # print 'invalid', params['type'], field_name, params['model'], str(e) raise schema.SchemaError("Invalid field '%s': %s" % (field_name, str(e))) return True
def validate(self, data): # Check the basic properties schema.Schema({ "url": bool, "value": string_schema, }).validate(data) # For url we are done if data["url"]: return data # Check that if non url we have the link in the API if data["value"] not in self.api: raise schema.SchemaError("Link value %r not found in the API " "valid keys are %r" % (data, self.api.keys())) return data
def _check_min_default_key(db_dict): """ Helper function used to check if there are at least 3 defined default keys, which are 'wf', 'elog' and 'history_object' :param db_dict: the database schema dictionary :type db_dict: dict :return: True if contains, else raise a schema.SchemaError :rtype: bool """ default_key_set = set() for collection in db_dict: default_key_set.add(collection) if 'default' in db_dict[collection]: default_key_set.add(db_dict[collection]['default']) if 'wf' not in default_key_set or 'elog' not in default_key_set or 'history_object' not in default_key_set: raise schema.SchemaError("wf, elog and history_object must be all defined as default key in a collection or as a collection name itself") return True
def validate(self): """ Provide rigid validation for YAML schema. """ if self.method == "update" and "where" not in self.data: raise schema.SchemaError("Update method requires where dict") def _build_all_cols(data, table, use_expr=False): all_cols = {} for column in table.columns: # schema breaks on values expected to be strings which are None if (column.name in data and column.type.python_type == str and data[column.name] is None): data[column.name] = "" all_cols[schema.Optional(column.name)] = ( column.type.python_type if not use_expr else lambda o: isinstance(o, (list, _BinaryExpression))) return all_cols all_cols = _build_all_cols(self.data, self.table) if self.method == "update": where = all_cols["where"] = {} for col_name, value in all_cols.items(): if col_name == "where": continue if isinstance(col_name, schema.Optional): col_name = col_name._schema value = lambda o: isinstance(o, (list, [(_BinaryExpression, str )])) where[schema.Optional(col_name)] = value schema.Schema(all_cols["where"]).validate(self.data["where"]) all_cols[schema.Optional("comment")] = basestring all_cols[schema.Optional("items")] = list all_cols[schema.Optional("merge-from")] = schema.Or(dict, int) all_cols[schema.Optional("extended_costs")] = [{ schema.Optional("where"): _build_all_cols(self.data.get("extended_costs", {}), get_table("ItemTemplate"), True), "cost": int }] schema.Schema(all_cols).validate(self.data)
def parse_sample_info(cls, options): """ Return an object encapsulating samples and their accompanying read files. options: dictionary of command-line options """ sample_info = SampleInfo(options[opts.READS_BASE_DIR]) for line in open(options[opts.SAMPLES_FILE_ARG], 'r'): sample_data = line.split() if len(sample_data) < 2 or len(sample_data) > 3: line = line.rstrip('\n') if len(line) > 80: line = line[0:77] + "..." raise schema.SchemaError( None, "Sample file line should contain sample name and " + "lists of read files (or first and second pairs of " + "read files), separated by whitespace: " + "\n{info}".format(info=line)) sample_info.add_sample_data(sample_data) return sample_info
def validate_template(name: str, pattern: str, value: str) -> None: if pattern not in value: message = "%s should contain the string %s" % (name, pattern) raise schema.SchemaError(message)
def validate_hook_cmd(cmd: str) -> None: try: cmd.format(new_version="dummy", current_version="dummy") except KeyError as e: message = "hook cmd: '%s' uses unknown placeholder: %s" % (cmd, e) raise schema.SchemaError(message)