当前位置: 首页>>代码示例>>Python>>正文


Python Draft3Validator.validate方法代码示例

本文整理汇总了Python中jsonschema.Draft3Validator.validate方法的典型用法代码示例。如果您正苦于以下问题:Python Draft3Validator.validate方法的具体用法?Python Draft3Validator.validate怎么用?Python Draft3Validator.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在jsonschema.Draft3Validator的用法示例。


在下文中一共展示了Draft3Validator.validate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: JSON

# 需要导入模块: from jsonschema import Draft3Validator [as 别名]
# 或者: from jsonschema.Draft3Validator import validate [as 别名]
class JSON(Actor):
    '''**A Wishbone module which converts and validates JSON.**

    This module has 2 main modes:

        - Validate JSON data and convert into a Python data structure.
        - Convert a Python data structure into a JSON string.


    Parameters:

        - name (str):   The instance name when initiated.

        - mode (str):   Determines whether the input has to be encoded, decoded or
                        passed through.
                        Can have 3 values: "encode", "decode", "pass"
                        Default: pass

        - schema (str): The filename of the JSON validation schema to load.  When no
                        schema is defined no validation is done.
                        Default: ''

    Queues:

        - inbox:    Incoming events.

        - outbox:   Outgoing events.


    Data which cannot be converted or which fails the validation is purged.
    The schema should be in valid JSON syntax notation. JSON validation can
    only be done on Python objects so you will have to convert your any JSON
    data to a Python object first.
    '''

    def __init__(self, name, mode="pass", schema=''):

        Actor.__init__(self, name)
        self.name=name
        self.mode=mode
        self.schema=schema

        if mode == "decode":
            self.convert = self.__loads
        elif mode == "encode":
            self.convert = self.__dumps
        elif mode == "pass":
            self.convert = self.__pass
        else:
            raise Exception ("mode should be either 'encode' or 'decode'.")

        if schema != "":
            self.logging.debug("Validation schema defined.  Doing validation.")
            schema_data = self.__loadValidationSchema(schema)
            self.validate = self.__validate
            self.validator=Validator(schema_data)
        else:
            self.logging.debug("No validation schema defined.  No validation.")
            self.validate = self.__noValidate


    def consume(self, event):

        try:
            event["data"] = self.convert(event["data"])
        except Exception as err:
            self.logging.warn("Unable to convert incoming data. Purged.  Reason: %s"%(err))
            return

        try:
            self.validate(event["data"])
        except ValidationError as err:
            self.logging.warn("JSON data does not pass the validation schema.  Purged.  Reason: %s"%(str(err).replace("\n"," > ")))
            return

        try:
            self.queuepool.outbox.put(event)
        except QueueLocked:
            self.queuepool.inbox.rescue(event)
            self.queuepool.outbox.waitUntilPutAllowed()

    def __loadValidationSchema(self, path):
        with open(path,'r') as schema:
            data = ''.join(schema.readlines())
            print loads(data)
            return loads(data)

    def __loads(self, data):
        return loads(data)

    def __dumps(self, data):
        return dumps(data)

    def __pass(self, data):
        return data

    def __validate(self, data):
        return self.validator.validate(data)

    def __noValidate(self, data):
#.........这里部分代码省略.........
开发者ID:Lacrymology,项目名称:wishboneModules,代码行数:103,代码来源:wb_function_json.py


注:本文中的jsonschema.Draft3Validator.validate方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。