예제 #1
0
    def testValidateSchema(self):
        "Test validate_schema function"
        schema = {'s': 's', 'i': 1, 'd': {'ds': 's', 'di': 1}, 'l': [1, 2]}
        result = validate_schema(schema, schema)
        self.assertEqual(result, True)

        doc = {'s': 1}
        result = validate_schema(doc, schema)
        self.assertEqual(result, False)

        doc = {'s': 's', 'i': 1, 'd': {'ds': 's', 'di': 1}, 'l': [1, '2']}
        result = validate_schema(doc, schema)
        self.assertEqual(result, False)

        doc = {'s': 's', 'i': 1, 'd': {'ds': 1, 'di': 1}, 'l': [1, 2]}
        result = validate_schema(doc, schema)
        self.assertEqual(result, False)
예제 #2
0
def run():
    number = 1000
    res = timeit.timeit('test()', setup="from __main__ import test", number=number)
    print("timeit: iteration {}, time {}, avg time {}".format(number, res, res/number))
    # single time
    time0 = time.time()
    res = validate_schema(schema, doc)
    print("single function call time {}".format(time.time()-time0))
예제 #3
0
def validate(data, verbose=False):
    "Helper function to validate given document against CMSMonitoring schemas"
    doc = data
    if 'data' in data:
        doc = data['data']
    if 'payload' in data:
        doc = data['payload']
    for sname, schema in _schemas.schemas().items():
        if verbose:
            print('validate document {} against schema {} {}'.format(
                data, sname, schema))
        if validate_schema(schema, doc, verbose):
            return True
    return False
예제 #4
0
def validate(doc, schema, logger):
    """
    Helper function to validate given document against a schema

    Schemas are searched for locally, or within the central CMSMonitoring
    schemas. The provided schema name is compared to full file names and
    to their base file names (without extension).

    Return a list of offending keys and a list of unknown keys, or None, None
    if no validation has been performed.
    """
    global _local_schemas
    if _local_schemas is None:
        # First time running, try to find the schema locally
        # Second time, _local_schemas will be a dictionary and this is skipped
        _local_schemas = {}
        if os.path.isfile(schema):
            try:
                _local_schemas[schema] = json.load(open(schema))
                msg = 'Successfully loaded local schema {} for validation'.format(
                    schema)
                logger.warn(msg)
            except ValueError:
                msg = 'Local schema {} is not json compliant'.format(schema)
                logger.error(msg)

    if schema in _local_schemas:
        return validate_schema(_local_schemas[schema], doc, logger)

    else:
        for sch in _schemas.schemas():
            if schema in [sch, os.path.basename(sch).rsplit('.')[0]]:
                return validate_schema(_schemas.schemas()[sch], doc, logger)

    msg = "Schema not found: '{}'".format(schema)
    logger.error(msg)
    return None, None
예제 #5
0
def main():
    "Main function"
    res = validate_schema(schema, doc, verbose=True)
    print("schema {}, doc {}, result={}".format(schema, doc, res))
    run()
예제 #6
0
def test():
    result = validate_schema(doc, doc)