def setUp(self): V.Object.REQUIRED_PROPERTIES = True V.base.reset_type_names() self.complex_validator = self.parse({ "n": "number", "?i": V.Nullable("integer", 0), "?b": bool, "?e": V.Enum(["r", "g", "b"]), "?d": V.AnyOf("date", "datetime"), "?s": V.String(min_length=1, max_length=8), "?p": V.Nullable(re.compile(r"\d{1,4}$")), "?l": [{ "+s2": "string" }], "?t": (unicode, "number"), "?h": V.Mapping(int, ["string"]), "?o": V.NonNullable({"+i2": "integer"}), })
def test_allof(self): self._testValidation(V.AllOf({"id": "integer"}, V.Mapping("string", "number")), valid=[{ "id": 3 }, { "id": 3, "bar": 4.5 }], invalid=[{ "id": 1.1, "bar": 4.5 }, { "id": 3, "bar": True }, { "id": 3, 12: 4.5 }]) self._testValidation(V.AllOf("number", lambda x: x > 0, V.AdaptBy(datetime.utcfromtimestamp)), adapted=[(1373475820, datetime(2013, 7, 10, 17, 3, 40))], invalid=["1373475820", -1373475820])
def setUp(self): super(OptionalPropertiesTestValidator, self).setUp() V.Object.REQUIRED_PROPERTIES = False self.complex_validator = self.parse({ "+n": "+number", "i": V.Nullable("integer", 0), "b": bool, "e": V.Enum(["r", "g", "b"]), "d": V.AnyOf("date", "datetime"), "s": V.String(min_length=1, max_length=8), "p": V.Nullable(re.compile(r"\d{1,4}$")), "l": [{ "+s2": "string" }], "t": (unicode, "number"), "h": V.Mapping(int, ["string"]), "o": V.NonNullable({"+i2": "integer"}), })
def test_validate_schema(): # create a simple test schema test_schema = valideer.Mapping(key_schema=valideer.String, value_schema=valideer.String) result, message = config_manager.validate_schema({"1": "1"}, test_schema) assert result assert message == "" result, message = config_manager.validate_schema({1: 1}, test_schema) assert result == False assert isinstance(message, str) assert len(message) > 0
def test_mapping(self): for obj in V.Mapping, V.Mapping(): self._testValidation( obj, valid=[{}, { "foo": 3 }], invalid=[1, 1.1, "foo", u"bar", [], False, True]) self._testValidation(V.Mapping("string", "number"), valid=[{ "foo": 3 }, { "foo": 3, u"bar": -2.1, "baz": Decimal("12.3") }], invalid=[{ "foo": 3, ("bar", ): -2.1 }, { "foo": 3, "bar": "2.1" }])
class StatusMessage(BaseMessage): hostname = attr.ib() load = attr.ib() projects = attr.ib() pid = attr.ib() _validator = V.parse({ "hostname": "string", "load": V.AdaptTo(tuple, ("number", "number", "number")), "pid": "integer", "projects": V.Mapping( "string", { "git": "string", "description": "string", "classes": ["string"], "running": "boolean", }) })
return [x] with V.parsing(required_properties=True, additional_properties=V.Object.REMOVE): CONFIG_VALIDATOR = V.parse({ "coordinator_endpoint": xutils.Endpoint(xutils.Endpoint.Side.CONNECT), "?coordinator_timeout": "integer", "?start_delay": "integer", "?github_secret": "string", "?github": V.Mapping("string", V.AdaptBy(_list_wrap)) }) XBBS_CFG_DIR = os.getenv("XBBS_CFG_DIR", "/etc/xbbs") with open(path.join(XBBS_CFG_DIR, "webhooks.toml"), "r") as fcfg: cfg = CONFIG_VALIDATOR.validate(toml.load(fcfg)) coordinator = cfg["coordinator_endpoint"] cmd_timeout = cfg.get("coordinator_timeout", 1500) start_delay = cfg.get("start_delay", 600) hmac_key = cfg.get("github_secret", None) github_mapping = cfg.get("github", {}) def verify_sig(data, secret, signature): s = hmac.new(secret.encode("utf-8"), data, digestmod="sha256")
def test_adapts(self): @V.adapts( body={ "+field_ids": ["integer"], "?scores": V.Mapping("string", float), "?users": [{ "+name": ("+string", "+string"), "?sex": "gender", "?active": V.Nullable("boolean", True), }] }) def f(body): return body adapted = f({ "field_ids": [1, 5], "scores": { "foo": 23.1, "bar": 2.0 }, "users": [ { "name": ("Nick", "C"), "sex": "male" }, { "name": ("Kim", "B"), "active": False }, { "name": ("Joe", "M"), "active": None }, ] }) self.assertEqual(adapted["field_ids"], [1, 5]) self.assertEqual(adapted["scores"]["foo"], 23.1) self.assertEqual(adapted["scores"]["bar"], 2.0) self.assertEqual(adapted["users"][0]["name"], ("Nick", "C")) self.assertEqual(adapted["users"][0]["sex"], "male") self.assertEqual(adapted["users"][0]["active"], True) self.assertEqual(adapted["users"][1]["name"], ("Kim", "B")) self.assertEqual(adapted["users"][1].get("sex"), None) self.assertEqual(adapted["users"][1]["active"], False) self.assertEqual(adapted["users"][2]["name"], ("Joe", "M")) self.assertEqual(adapted["users"][2].get("sex"), None) self.assertEqual(adapted["users"][2].get("active"), True) invalid = [ # missing 'field_ids' from body partial(f, {}), # score value is not float partial(f, { "field_ids": [], "scores": { "a": "2.3" } }), # 'name' is not a length-2 tuple partial(f, { "field_ids": [], "users": [{ "name": ("Bob", "R", "Junior") }] }), # name[1] is not a string partial(f, { "field_ids": [], "users": [{ "name": ("Bob", 12) }] }), # name[1] is required partial(f, { "field_ids": [], "users": [{ "name": ("Bob", None) }] }), ] for fcall in invalid: self.assertRaises(V.ValidationError, fcall)
Specified in routes.yml """ import copy import datetime import getpass import os import platform import re import valideer from kttk.config import config_manager ROUTES_YML = "routes.yml" route_schema = schema = valideer.Mapping(key_schema=valideer.String, value_schema=valideer.String) FOLDER_TEMPLATES_YML = "folder_templates.yml" class RouteNotExists(KeyError): def __init__(self, route_name): super(RouteNotExists, self).__init__( "Route with name {} does not exist! Please head over to {} and create your route!" .format( route_name, os.path.join(config_manager.get_config_folder(), ROUTES_YML))) # load data for folders _data_folders = config_manager.load_file(FOLDER_TEMPLATES_YML,
V.AllOf("string", path.isabs), "intake": V.AdaptBy(_receive_adaptor), "worker_endpoint": xutils.Endpoint(xutils.Endpoint.Side.BIND), # use something like a C identifier, except disallow underscore as a # first character too. this is so that we have a namespace for xbbs # internal directories, such as collection directories "projects": V.Mapping( xutils.PROJECT_REGEX, { "git": "string", "?description": "string", "?classes": V.Nullable(["string"], []), "packages": "string", "?fingerprint": "string", "tools": "string", "?incremental": "boolean", "?distfile_path": "string", "?mirror_root": "string", "?default_branch": "string", }) }) PUBKEY_VALIDATOR = V.parse({ # I'm only validating the keys that xbbs uses "signature-by": "string" }) with V.parsing(required_properties=True, additional_properties=None): # { job_name: job } ARTIFACT_VALIDATOR = V.parse({
{ 'step': 'anim', 'name': 'anim', } ] } """ import valideer from kttk.config import config_manager TASK_PRESETS_YML = "task_presets.yml" task_preset_schema = valideer.Mapping( key_schema=valideer.String, value_schema=valideer.HomogeneousSequence(item_schema=valideer.Object( required={ "name": valideer.String, "step": valideer.String })), ) _data_presets = config_manager.load_file( TASK_PRESETS_YML, lambda data: config_manager.validate_schema(data, task_preset_schema), ) def get_task_presets(entity_type): # type: (str) -> list[dict] """ Returns list of all task templates for given type. Task preset example, everything will be lowercase {
class JobMessage(BaseMessage): @staticmethod def _filter(x, v): return v is not None project = attr.ib() job = attr.ib() repository = attr.ib() revision = attr.ib() output = attr.ib() build_root = attr.ib() needed_pkgs = attr.ib() needed_tools = attr.ib() prod_pkgs = attr.ib() prod_tools = attr.ib() prod_files = attr.ib() tool_repo = attr.ib() pkg_repo = attr.ib() commits_object = attr.ib(repr=False) # XXX: maybe it's worth doing something else xbps_keys = attr.ib(default=None, repr=False) mirror_root = attr.ib(default=None) distfile_path = attr.ib(default=None) _validator = V.parse({ "project": "string", "job": "string", "repository": "string", "revision": "string", "output": "string", "build_root": "string", "needed_pkgs": PKG_TOOL_VALIDATOR, "needed_tools": PKG_TOOL_VALIDATOR, "prod_pkgs": PKG_TOOL_VALIDATOR, "prod_tools": PKG_TOOL_VALIDATOR, "prod_files": ["string"], "tool_repo": "string", "pkg_repo": "string", "?distfile_path": "string", "?mirror_root": "string", "commits_object": V.Mapping("string", { "?rolling_id": "string", "?fixed_commit": "string" }), "?xbps_keys": V.Mapping(re.compile(r"^([a-zA-Z0-9]{2}:){15}[a-zA-Z0-9]{2}$"), bytes), })
@attr.s class WorkMessage(BaseMessage): project = attr.ib() git = attr.ib() revision = attr.ib() _validator = V.parse({ "project": "string", "git": "string", "revision": "string" }) PKG_TOOL_VALIDATOR = V.parse( V.Mapping( "string", { "version": "string", "architecture": V.AnyOf("string", V.AdaptBy(xutils.list_to_set)), })) @attr.s class JobMessage(BaseMessage): @staticmethod def _filter(x, v): return v is not None project = attr.ib() job = attr.ib() repository = attr.ib() revision = attr.ib() output = attr.ib()