class CatalogItemConfig: CONF_NAME = 'name' CONF_TYPE = 'type' CONF_DESCRIPTION = 'desc' CONF_LOCATION = 'location' SCHEMA = { CONF_NAME: sc.Use(str), CONF_TYPE: sc.Use(str), sc.Optional(CONF_DESCRIPTION, default=''): sc.Use(str), CONF_LOCATION: { str: object }, sc.Optional(str): object } @classmethod def item_type_map(cls): return {'text': TextCatalogItemConfig} @classmethod def from_dict(cls, dct: Dict[str, Any], base_path: str) -> Dataset: validated = sc.Schema(cls.SCHEMA, ignore_extra_keys=True).validate(dct) item_name = validated[cls.CONF_NAME] item_type = validated[cls.CONF_TYPE] parser = cls.item_type_map().get(item_type) if not parser: raise UnknownCatalogItemError(item_type) return parser.load_from_dict(dct, base_path=os.path.join( base_path, item_name))
def find_or_include(self, objective_id, student_id, tutor_id, by_user, common_assessors=True): """Include an objective among a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be included. :param student_id: is the id of the `User` for whom the `Objective` is being included. :param tutor_id: is the id of the `User` who is including the `Objective`. :param by_user: is the `User` who is calling the action. :param common_assessors: determines whether common assessors should have their assessment updated for the 'Student'. """ include_schema = {'objective_id': s.Use(int), 'user_id': s.Use(int), 'assessor_id': s.Use(int) } u = s.Schema(include_schema).validate({'objective_id': objective_id, 'user_id': student_id, 'assessor_id': tutor_id}) self._check_user_id_or_admin(u['assessor_id'], by_user) userobjective = UserObjective.query.filter_by(**u).first() if userobjective is None: userobjective = UserObjective.create(**u) # DJG - this includes a repetition of the logic to first search for a userobjective before creating it - is it better to have a fat model which listens for the new userobjective to be created and then acts on that if common_assessors: self._set_common_assessors(userobjective) self._set_student_objective(userobjective) return userobjective
class TextCatalogItemConfig: CONF_TEXT_TARGET = 'target_column' CONF_TEXT_TEXT = 'text_column' SCHEMA = { **CatalogItemConfig.SCHEMA, sc.Optional(CONF_TEXT_TARGET, default='target'): sc.Use(str), sc.Optional(CONF_TEXT_TEXT, default='text'): sc.Use(str) } @classmethod def load_from_dict(cls, dct: Dict[str, Any], base_path: str) -> TextDataset: validated = sc.Schema(cls.SCHEMA).validate(dct) data = LocationConfig.from_dict( validated[CatalogItemConfig.CONF_LOCATION], base_path=base_path) target = validated[cls.CONF_TEXT_TARGET] feature = validated[cls.CONF_TEXT_TEXT] data[target] = data[target].astype(str) data[feature] = data[feature].astype(str) return TextDataset( data=data, description=validated[CatalogItemConfig.CONF_DESCRIPTION], text_column=feature, target_column=target)
def assess(self, objective_id, student_id, tutor_id, by_user): """Remove an objective from a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be removed. :param student_id: is the id of the `User` for whom the `Objective` is being removed. :param tutor_id: is the id of the `User` who is removing the `Objective`. :param by_user: is the `User` who is removing the `Objective`. """ assess_schema = {'student_id': s.Use(int), 'tutor_id': s.Use(int), 'objective_id': s.Use(int) } u = s.Schema(assess_schema).validate({'objective_id': objective_id, 'student_id': student_id, 'tutor_id': tutor_id}) self._check_user_id(tutor_id, by_user) userobjective = self.find_or_include(by_user=by_user, common_assessors=True, **u) states = UserObjective.assessment_states().keys() completed = states[(states.index(userobjective.completed) + 1) % len(states)] # Cycles through the list of states userobjective.completed = completed db.session.add(userobjective) db.session.commit() self._set_common_assessors(userobjective) self._set_student_objective(userobjective) return UserObjective.assessment_states()[completed]
def main(): s = schema.Schema({ "--package_name": schema.Or(None, str), "--cmd": schema.Or(None, str), "--repository_path": schema.Or(None, str), "--exe_path": schema.Or(None, str), "--timeout": schema.Or(None, schema.Use(int)), "--log_path": schema.Or(None, schema.Use(Path)), "--verbose": bool, "--help": bool, }) arguments = s.validate(docopt(__doc__)) log_path = arguments["--log_path"] log_level = logging.DEBUG if arguments["--verbose"] else logging.INFO logging.basicConfig(filename=log_path, level=log_level) logger = logging.getLogger(__name__) # timeout could be None if the caller did not provide the value timeout = arguments["--timeout"] repository_path = arguments["--repository_path"] if repository_path is not None and repository_path.strip(): logger.info("Read repository path from program arguments...") else: repository_path = os.getenv("REPOSITORY_PATH") if repository_path is not None and repository_path.strip(): logger.info("Read repository path from environment variables...") else: repository_path = DEFAULT_REPOSITORY_PATH logger.info("Read repository path from default value...") exe_path = arguments["--exe_path"] if exe_path is not None and exe_path.strip(): logger.info("exe folder path from program arguments...") else: exe_path = os.getenv("ONEDOCKER_EXE_PATH") if exe_path is not None and exe_path.strip(): logger.info("exe folder path from environment variables...") else: exe_path = DEFAULT_EXE_FOLDER logger.info("Read repository path from default value...") logger.info("Starting container....") try: run( repository_path=repository_path, exe_path=exe_path, package_name=arguments["--package_name"], cmd=arguments["--cmd"], logger=logger, timeout=timeout, ) except subprocess.TimeoutExpired: logger.error( f"{timeout} seconds have passed. Now exiting the program....") sys.exit(1) except InterruptedError: logger.error( "Receive abort command from user, Now exiting the program....") sys.exit(1)
def validate(kwargs): schema.Schema({ 'username': basestring, 'hostname': basestring, 'password': basestring, 'dummy_vlan': schema.And(schema.Use(int), lambda v: 0 <= v and v <= 4096, schema.Use(str)), }).validate(kwargs)
def transform_args(args): return schema.Schema({ "<langfile>": os.path.isfile, "--indent": schema.Use(parse_indentation_spec), "--model": schema.Or(None, os.path.isfile), "--base": schema.Or(None, os.path.isfile), "--only": schema.Or(None, schema.Use(parse_csv)), str: schema.Or(bool, str), }).validate(args)
def run(self, argv: Optional[List[str]] = None) -> None: s = schema.Schema({ "<cli_log_file>": schema.Use(Path), "<s3_bucket>": str, "<archive_tag>": str, "--input_ids": bool, "--log_path": schema.Or(None, schema.Use(Path)), "--verbose": bool, "--help": bool, }) arguments = s.validate(docopt(__doc__, argv)) cli_log_file = arguments["<cli_log_file>"] archive_tag = arguments["<archive_tag>"] log_path = arguments["--log_path"] # if log_path specified, logging using FileHandler, or console StreamHandler log_handler = (logging.FileHandler(log_path) if log_path else logging.StreamHandler()) logging.Formatter.converter = time.gmtime logging.basicConfig( level=logging.INFO, handlers=[log_handler], format= "%(asctime)sZ %(levelname)s t:%(threadName)s n:%(name)s ! %(message)s", ) self.logger = logging.getLogger(__name__) log_level = logging.DEBUG if arguments["--verbose"] else logging.INFO self.logger.setLevel(log_level) # Concatenate all arguments to a string, with every argument wrapped by quotes. all_options = f"{sys.argv[1:]}"[1:-1].replace("', '", "' '") # E.g. Command line: log_analyzer 'sample_log/intern-output.txt' '--log_path=a.intern.log' ... logging.info(f"Command line: {Path(__file__).stem} {all_options}") self.s3_bucket = self._get_s3_bucket_name(arguments["<s3_bucket>"]) self.logger.info( f"Will upload log archive to S3 bucket: {self.s3_bucket}") self.container_ids = self._extract_container_ids( cli_log_file, arguments["--input_ids"] or False) self.logger.info( f"Found container ID's count: {len(self.container_ids)}") if log_level == logging.DEBUG: ids_lines = "\n".join(self.container_ids) self.logger.debug(f"Found container ID's:\n{ids_lines}") self.aws_region = self._get_aws_region(self.container_ids) self.logger.info(f"Found aws_region: {self.aws_region}") self.aws_container_logs = AwsContainerLogs(tag_name=archive_tag, aws_region=self.aws_region) self.aws_container_logs.upload_logs_to_s3_from_cloudwatch( self.s3_bucket, self.container_ids) self.logger.info("After uploading log archive")
class DBConnection(Loggable): """ Wraps the database (sqlalchemy) connection For further :py:class:`sqlalchemy.engine.Engine` information, especially for the `config['options']` part, see here: https://docs.sqlalchemy.org/en/latest/core/engines.html For further :py:class:`sqlalchemy.engine.Connection` information see here. Provides high-level functionality for a wrapped DB-API connection. https://docs.sqlalchemy.org/en/latest/core/connections.html """ connection_schema = schema.Schema({ 'uri': schema.Use(str), 'table': schema.Use(str), schema.Optional('options', default={}): schema.Or( {}, {schema.Use(str): object} ) }) def validate(self, instance, attribute) -> bool: """ validators runs after the instance is initialized this is why `self` works here """ return self.connection_schema.is_valid(attribute) config: Dict[str, Any] = attr.ib(validator=validate) connection: Connection = attr.ib(init=False, repr=False) engine: Engine = attr.ib(init=False, repr=False) def __attrs_post_init__(self) -> None: self.config = self.connection_schema.validate(self.config) self.engine = create_engine(self.config['uri']) def connect(self) -> Connection: self.logger.debug( "Establishing DB connection with {}".format(self.__repr__()) ) self.connection = self.engine.connect() return self.connection def __exit__(self): self.close() def close(self) -> None: self.logger.debug("Closing DB connection...") self.connection.close() def is_connected(self) -> bool: return not self.connection.closed
def validate(data, set_dir): def is_file(path): return path.is_file() def to_fixture(data): return Fixture(**data) def to_step(data): return Step(**data) def to_test(data): return Test(**data) def absolute_path(path): absolute = Path(os.path.expanduser(path)) if not absolute.is_absolute(): absolute = (set_dir / path).resolve() return absolute fixture = schema.Schema( schema.And({ 'enter': schema.And(str, len), 'exit': schema.And(str, len) }, schema.Use(to_fixture))) fixtures = schema.Schema({schema.And(str, len): fixture}) step = schema.Schema( schema.And({ 'command': schema.And( schema.Const(schema.And(str, len)), schema.Use(shlex.split)), schema.Optional('input', default=None): schema.And(schema.Use(absolute_path), is_file) }, schema.Use(to_step))) test = schema.Schema( schema.And({ schema.Optional('tags', default=None): [str], schema.Optional('condition', default=None): str, schema.Optional('fixture', default=None): str, 'steps': [step] }, schema.Use(to_test))) tests = schema.Schema({schema.And(str, len): test}) sch = schema.Schema({'fixtures': fixtures, 'tests': tests}) return sch.validate(data)
def main() -> None: args_schema = schema.Schema({ "<spine_path>": schema.Use(pathlib.Path, os.path.exists), "<output_path>": schema.Use(pathlib.Path), "--keep_rate": schema.Use(float), schema.Optional("--log_every_n"): schema.Or(None, schema.Use(int)), "--help": bool, }) args = args_schema.validate(docopt.docopt(__doc__)) gen_ids_from_spine(args)
def main() -> None: args_schema = schema.Schema( { "<input_path>": schema.Use(pathlib.Path, os.path.exists), "<new_output_path>": schema.Or( None, schema.Use(pathlib.Path, os.path.exists) ), "--replace": str, "--accept_all": bool, "--from": schema.Or(None, str), "--help": bool, } ) args = args_schema.validate(docopt.docopt(__doc__)) gen_config(args)
class MessageService(BaseService): __model__ = Message _base_schema = { 'from_id': s.Use(int), 'to_id' 'subject': basestring, 'recommended_material_id': s.Or(None, s.Use(int)), 'assign_objective_id': s.Or(None, s.Use(int)), 'assign_scheme_id': s.Or(None, s.Use(int)) } def send(self, message_data, by_user): """Create a new Message from the given data :param message_data: is a dictionary of data used to populate the initial Message. It must match the schema defined within. :param by_user: the `User` who is creating the `Message`. """ creation_schema = self._base_schema m = s.Schema(creation_schema).validate(message_data) self._check_user_id(m['from_id'], by_user) #self._can_message(m['from_id'], m['to_id']) now = datetime.utcnow() m['sent'] = now message = Message(**m) db.session.add(message) db.session.commit() return message def populate_message_form(self, user, subject_id=None): form = SendMessage() form.recommended_material.choices = select_choices( user.visible_modules(), True) form.assign_objective.choices = select_choices( self.services.objectives.objectives_for_selection( user, subject_id), True) form.assign_scheme.choices = select_choices( self.services.objectives.schemes_for_selection(user, subject_id), True) return form
def create(self, objective_data, by_user): """Create a new Objective from the given data :param objective_data: is a dictionary of data used to populate the initial Objective. It must match the schema defined within. :param by_user: the `User` who is creating the `Objective`. """ creation_schema = merge(self._base_schema, { s.Optional('id'): None, 'subject_id': s.Or(None, s.Use(int)), }) o = s.Schema(creation_schema).validate(objective_data) self._validate_topic(o['topic_id'], o['subject_id']) self._validate_name(o['name']) prerequisites = self._validate_prerequisites(o['prerequisites'], by_user) now = datetime.utcnow() o['prerequisites'] = prerequisites o['created_by_id'] = by_user.id o['last_updated'] = now o['time_created'] = now objective = Objective(**o) db.session.add(objective) db.session.commit() return objective
def validate_basic_schema(config: Any) -> None: """First pass of validation, using schema""" # Note: asserts that we won't get KeyError or invalid types # when building or initial Config instance file_schema = schema.Schema({ "src": str, schema.Optional("search"): str, schema.Optional("version_template"): str, }) hook_schema = schema.Schema({"name": str, "cmd": str}) def validate_re(regex: str) -> str: re.compile(regex, re.VERBOSE) return regex tbump_schema = schema.Schema({ "version": { "current": str, "regex": schema.Use(validate_re) }, "git": { "message_template": str, "tag_template": str }, "file": [file_schema], schema.Optional("hook"): [hook_schema], # retro-compat schema.Optional("before_push"): [hook_schema], # retro-compat schema.Optional("before_commit"): [hook_schema], schema.Optional("after_push"): [hook_schema], schema.Optional("github_url"): str, }) tbump_schema.validate(config)
def serve(port): """Run a development api server. Don't use this in production.""" try: port = schema.And( schema.Use(int), lambda n: MIN_PORT_NUMBER <= n <= MAX_PORT_NUMBER).validate(port) except schema.SchemaError: raise InvalidAPIArgumentsException( 'Error: Invaid port. Must be in the range 1-65535.') except Exception as e: sys.exit('Unxpected Error!!! \n %s' % e) """Start the HIL API server""" config.setup() if cfg.has_option('devel', 'debug'): debug = cfg.getboolean('devel', 'debug') else: debug = False # We need to import api here so that the functions within it get registered # (via `rest_call`), though we don't use it directly: # pylint: disable=unused-variable from hil import api, rest server.init() migrations.check_db_schema() server.stop_orphan_consoles() rest.serve(port, debug=debug)
def get_pool_config(): posint = schema.And(int, lambda a: a >=0) pool_conf = schema_builder.opt_up({ opt('instance_size'): schema.And(int, lambda n: 0 < n <= 64), opt('instances'): int, opt('instance_group'): schema.Or('Backend', 'Common'), opt('one_per_host', default=True): bool, opt('use_canary', default=True): bool, opt('hostname'): str, opt('vips'): schema_builder.none_or(schema.Use(_parse_vip)), opt('health_check'): { opt('grace_period_seconds'): posint, opt('interval_seconds'): posint, opt('timeout_seconds'): posint, opt('max_consecutive_failures'): posint, opt('command', default=DEFAULT_HEALTH_CMD): str, }, opt('haproxy_group', default='internal'): schema.Or('internal', 'external'), }) return { schema.Optional('default'): pool_conf, schema.Or(*ENVIRONMENTS): { #instance-id e.g. /backends/urlshortener str: schema_builder.nullable_dict(pool_conf), } }
class CsvLocationConfig: CONF_URI = 'uri' CONF_SEP = 'separator' SCHEMA = { **LocationConfig.SCHEMA, CONF_URI: sc.Use(str), sc.Optional(CONF_SEP, default=','): sc.Use(str) } @classmethod def load_from_dict(cls, dct: Dict[str, Any], base_path: str) -> pd.DataFrame: validated = sc.Schema(cls.SCHEMA).validate(dct) file_path = validated[cls.CONF_URI] separator = validated[cls.CONF_SEP] return pd.read_csv(os.path.join(base_path, file_path), sep=separator)
def optional(schema, real_name=None): """ An optional configuration field. In order to access it, first check for its :code:`.has_value`. If this is false, this field was absent from the configuration. If this is true, the field's value will be in the :code:`.value` attribute. Args: schema (a type or callable): the specification for this field. default: the default value if the field is not in the configuration. real_name (str): the name of the configuration field. It is optional -- the default is the name of the attribute. This is useful especially in cases where the field's name is not a valid identifier -- for example, if it contains dashes. """ underlying_schema = schemalib.Schema(schema) def _to_something(value): return _Something(underlying_schema.validate(value)) schema = schemalib.Use(_to_something) pulgas_schema = _PulgasSchema(schema=schema, optional=True, real_name=real_name) return attr.attrib(default=_Nothing(), metadata={PULGAS_SCHEMA: pulgas_schema})
def validator(fn): """ Decorate a function for use as a validator with `schema`_ .. _schema: https://github.com/keleshev/schema """ return schema.Use(fn)
class Apt(Whiteprint): cfg_schema = { "packages": [schema.And(str, schema.Use(str.lower))], } def _execute(self, mode: str) -> None: if mode == "install": self.exec( "DEBIAN_FRONTEND=noninteractive " "apt install -y %s" % " ".join(self.cfg["packages"]) ) def _validate(self, mode: str) -> Optional[str]: if mode == "install": res = self.exec("apt -qq list %s" % " ".join(self.cfg["packages"])) installed_packages = set() for line in res.stdout.decode("utf-8").splitlines(): installed_package, _ = line.split("/", 1) installed_packages.add(installed_package.lower()) for package in self.cfg["packages"]: if package not in installed_packages: return "Apt package %r missing." % package return None else: return None
def validate_int_option(int_option, msg, min_val): msg = "{msg}: '{val}'".format(msg=msg, val=int_option) validator = schema.Use(int) if min_val is not None: validator = schema.And(validator, lambda x: x >= min_val) return schema.Schema(validator, error=msg).validate(int_option)
def update(self, objective_data, by_user): """Update an existing Objective from the given data. :param objective_data: is a dictionary of data with the updated state of the Objective. It must match the schema defined within. :param by_user: the `User` who is creating the `Objective`. """ update_schema = merge(self._base_schema, { 'id': s.Use(int), }) o = s.Schema(update_schema).validate(objective_data) objective = self.require_by_id(o['id']) self._validate_topic(o['topic_id'], objective.subject_id) self._validate_name(o['name'], old_name=objective.name) prerequisites = self._validate_prerequisites( o['prerequisites'], by_user, check_cyclic_against=objective) self._check_update_auth(objective, by_user) now = datetime.utcnow() objective.last_updated = now objective.name = o['name'] objective.prerequisites = prerequisites objective.topic_id = o['topic_id'] db.session.add(objective) db.session.commit() return objective
def main(argv: Optional[List[str]] = None) -> None: s = schema.Schema({ "<logs_file_to_analyze>": schema.Use(Path), "--log_path": schema.Or(None, schema.Use(Path)), "--out": schema.Or(None, schema.Use(Path)), "--verbose": bool, "--help": bool, }) arguments = s.validate(docopt(__doc__, argv)) logs_file = arguments["<logs_file_to_analyze>"] log_path = arguments["--log_path"] output_json_path = arguments["--out"] # if log_path specified, logging using FileHandler, or console StreamHandler log_handler = logging.FileHandler( log_path) if log_path else logging.StreamHandler() logging.Formatter.converter = time.gmtime logging.basicConfig( level=logging.INFO, handlers=[log_handler], format= "%(asctime)sZ %(levelname)s t:%(threadName)s n:%(name)s ! %(message)s", ) logger = logging.getLogger(__name__) log_level = logging.DEBUG if arguments["--verbose"] else logging.INFO logger.setLevel(log_level) # Concatenate all arguments to a string, with every argument wrapped by quotes. all_options = f"{sys.argv[1:]}"[1:-1].replace("', '", "' '") # E.g. Command line: log_analyzer 'sample_log/intern-output.txt' '--log_path=a.intern.log' ... logging.info(f"Command line: {Path(__file__).stem} {all_options}") digest = LogDigest(logs_file, logger) run_study = digest.analyze_logs() logger.info(f"Parsed log line count: {run_study.total_line_num}") # pyre-ignore summary_json = run_study.to_json(indent=4) if output_json_path: with open(output_json_path, "w") as outfile: outfile.write(summary_json) else: logger.info(f"Generated run study digest:\n{summary_json}") logger.info(f"Done. Instance count: {len(run_study.instances)}")
def main() -> None: Utils.configure_logger("log/server.log") logger = logging.getLogger(__name__) s = schema.Schema({ "--ipv6": bool, "--port": schema.Use(int), "--help": bool, }) arguments = s.validate(docopt(__doc__)) current_script_dir = os.path.dirname(os.path.abspath(__file__)) thrift_path = os.path.join(current_script_dir, "thrift/logging_service.thrift") logger.info(f"Loading the thrift definition from: {thrift_path};") logging_service_thrift = thriftpy2.load( thrift_path, module_name="logging_service_thrift", ) if arguments["--ipv6"]: socket_family = socket.AF_INET6 any_host_interface = "::" socket_family_name = "IPv6" else: socket_family = socket.AF_INET any_host_interface = "0.0.0.0" socket_family_name = "IPv4" server_port = arguments["--port"] queue_manager = MemoryQueueManager() logging_client = MetaLoggingClient() metadata_manager = MetadataManager(queue_manager, logging_client) handler = LoggingServiceHandler(metadata_manager, logging_client, logging_service_thrift) proc = TProcessor(logging_service_thrift.LoggingService, handler) server = TThreadedServer( proc, TServerSocket( host=any_host_interface, socket_family=socket_family, port=server_port, client_timeout=CLIENT_TIMEOUT_MS, ), iprot_factory=TBinaryProtocolFactory(), itrans_factory=TBufferedTransportFactory(), ) logger.info( f"Logging service server listens host={any_host_interface}[{socket_family_name}], port={server_port}." ) server.serve() logger.info("done.")
def remove(self, objective_id, student_id, tutor_id, by_user): """Remove an objective from a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be removed. :param student_id: is the id of the `User` for whom the `Objective` is being removed. :param tutor_id: is the id of the `User` who is removing the `Objective`. :param by_user: is the `User` who is calling the action. """ remove_schema = {'id': s.Use(int), 'student_id': s.Use(int), 'tutor_id': s.Use(int) } s.Schema(remove_schema).validate({'id': objective_id, 'student_id': student_id, 'tutor_id': tutor_id}) self._check_user_id_or_admin(tutor_id, by_user) UserObjective.ignore_or_delete(student_id, tutor_id, objective_id)
def verify_config(config: Box) -> bool: """Return (True|False) result if the config matches the schema.""" config_schema = schema.Schema({ "discovergy_account": { "email": schema.And(str, len), "password": schema.Optional(str), "save_password": schema.And(schema.Use(str.lower), lambda x: x in ("true", "false")), }, "file_location": { "data_dir": str, "log_dir": str, }, "poll": { "default": schema.Use(int), "try_sleep": schema.Use(int), }, schema.Optional("open_weather_map"): { "id": str }, schema.Optional("oauth1_token"): { "key": str, "client_secret": str, "token": str, "token_secret": str, }, # meters stores the meters to read if configured. Otherwise, read all. The key is a nice name, # value is the meter id. schema.Optional("meters"): { str: str }, }) try: config_schema.validate(config) except schema.SchemaError as e: log.error(f"Caught a config schema violation error: {e}") return False return True
def test_schema(): # 检查数字 print '----------------------int' print schema.Schema(int).validate(123) print schema.Schema(int).is_valid(123) # 检查字符串 print '----------------------str' # Regex 没有 is_valid 方法 print schema.Regex(r'^foo').validate('foobar') print schema.Schema(lambda n: "foo" in n).is_valid('foobar') print 'False:%s ' % schema.Schema(lambda n: "foo" in n).is_valid('fobar') # 检查字典 print '----------------------dict' rules = { 'name': schema.And(str, len), 'age': schema.And(schema.Use(int), lambda n: 18 <= n <= 99), schema.Optional('gender'): schema.And(str, schema.Use(str.lower), lambda s: s in ('squid', 'kid'))} data = {'name': 'Sue', 'age': '28', 'gender': 'Squid'} print schema.Schema(rules).validate(data) print schema.Schema(rules).is_valid(data) print '----------------------list-dict' rules = [{ 'name': schema.And(str, len), 'age': schema.And(schema.Use(int), lambda n: 18 <= n <= 99), schema.Optional('gender'): schema.And(str, schema.Use(str.lower), lambda s: s in ('squid', 'kid'))}] data = [{'name': 'Sue', 'age': '28', 'gender': 'Squid'}, {'name': 'Sam', 'age': '42'}, {'name': 'Sacha', 'age': '20', 'gender': 'KID'}] print schema.Schema(rules).validate(data) print schema.Schema(rules).is_valid(data)
def sub(klass): """ Validate as a pulgas subsection. Args: klass (type): pulgas configuration to validate as a subsection Returns: a value of type :code:`klass` as read from the configuration Raises: schema.SchemaError: invalid configuration data """ return schemalib.Use(klass.validate)
def delete(self, objective_id, by_user): """Delete an objective. :param objective_id: is the id of the `Objective` to be deleted. :param by_user: the `User` who is deleting the `Objective`. """ delete_schema = {'id': s.Use(int)} o = s.Schema(delete_schema).validate({'id': objective_id}) # DJG - do I need to be using schema here. I just want to check the single id data item objective = self.require_by_id(o['id']) self._check_delete_auth(objective, by_user) db.session.delete(objective) db.session.commit()