Example #1
0
class SchemaAdapter():
    """
    Manage a biothings_schema.Schema instance.
    Provide native type custom format schema class lists.
    """
    def __init__(self, doc=None):
        contexts = ESSchema.gather_field('@context')
        self._schema = SchemaParser(doc, contexts)
        self._classes_defs = self._schema.list_all_defined_classes()
        self._classes_refs = self._schema.list_all_referenced_classes()

    def __getattr__(self, attr):
        return getattr(self._schema, attr)

    def get_class_defs(self):
        # get only classes defined in this schema
        # each {} will have a field ref: false
        return list(self._get_class_defs().values())

    def get_class_refs(self):
        # get only classes referenced outside this schema
        # each {} will have a field ref: true
        return list(self._get_class_refs().values())

    def get_classes(self):
        # get all classes and label them if they are referenced

        defs = self._get_class_defs()
        refs = self._get_class_refs()
        ans = {}
        ans.update(defs)
        ans.update(refs)
        return list(ans.values())

    @staticmethod
    def _get_class_info(schema_class):
        ans = {}  # biothings_schema.SchemaClass -> { ... }
        schema_class = SchemaClassWrapper(schema_class)
        for key in ('name', 'uri', 'prefix', 'label', 'description',
                    'parent_classes', 'properties', 'validation'):
            try:
                ans[key] = getattr(schema_class, key)
            except AttributeError:
                pass
        logging.info(ans['name'])
        return ans

    def _get_class_defs(self):
        ans = {}
        for schema_class in self._classes_defs:
            if schema_class.name not in ans:
                _schema_class = self._get_class_info(schema_class)
                _schema_class['ref'] = False
                ans[schema_class.name] = _schema_class
        return ans

    def _get_class_refs(self):
        ans = {}
        for schema_class in self._classes_refs:
            if schema_class.name not in ans:
                _schema_class = self._get_class_info(schema_class)
                _schema_class['ref'] = True
                ans[schema_class.name] = _schema_class
        return ans
Example #2
0
class SchemaAdapter():
    """
    Manage a biothings_schema.Schema instance.
    Provide native type custom format schema class lists.
    """
    def __init__(self, doc=None, **kwargs):
        contexts = ESSchema.gather_field('@context')
        self._schema = SchemaParser(schema=doc, context=contexts, **kwargs)
        self._classes_defs = self._schema.list_all_defined_classes()
        self._classes_refs = self._schema.list_all_referenced_classes()

    def __getattr__(self, attr):
        return getattr(self._schema, attr)

    def get_class_defs(self):
        """get only classes defined in this schema
           each {} will have a field ref: false"""
        return list(self._get_class_defs().values())

    def get_class_refs(self):
        """get only classes referenced outside this schema
           each {} will have a field ref: true"""
        return list(self._get_class_refs().values())

    def get_classes(self, include_ref=True):
        """get all classes and label them if they are referenced
           if include_ref is False, only "defined" classes are included.
        """
        defs = self._get_class_defs()
        ans = {}
        ans.update(defs)
        if include_ref:
            refs = self._get_class_refs()
            ans.update(refs)
        return list(ans.values())

    @staticmethod
    def _get_class_info(schema_class):
        ans = {}  # biothings_schema.SchemaClass -> { ... }
        schema_class = SchemaClassWrapper(schema_class)
        for key in ('name', 'uri', 'prefix', 'label', 'description',
                    'parent_classes', 'properties', 'validation'):
            try:
                ans[key] = getattr(schema_class, key)
            except AttributeError:
                pass
        logging.info(ans['name'])
        return ans

    def _get_class_defs(self):
        ans = {}
        for schema_class in self._classes_defs:
            if schema_class.name not in ans:
                _schema_class = self._get_class_info(schema_class)
                _schema_class['ref'] = False
                ans[schema_class.name] = _schema_class
        return ans

    def _get_class_refs(self):
        ans = {}
        for schema_class in self._classes_refs:
            if schema_class.name not in ans:
                _schema_class = self._get_class_info(schema_class)
                _schema_class['ref'] = True
                ans[schema_class.name] = _schema_class
        return ans

    def has_validation_error(self):
        """return True if there is at least one validation error."""
        for err in self._schema.validator.validation_errors:
            if not err.warning:
                return True
        return False

    def get_validation_errors(self):
        """return validation errors as a list of dictionaries"""
        return [
            err.to_dict() for err in self._schema.validator.validation_errors
        ]