def default_value(value): # TODO: replace the as_term function with context.to_symbol if value in DEFAULT_VALUE: return DEFAULT_VALUE[value] elif isinstance(value, URIRef): return as_term(value) elif isinstance(value, Literal): return value.toPython() else: return value
def _validate_many(self, resources: List[Resource]) -> None: for resource in resources: conforms, graph, _ = self.service.validate(resource) if conforms: resource._validated = True action = Action(self._validate_many.__name__, conforms, None) else: resource._validated = False violations = set(" ".join(re.findall('[A-Z][^A-Z]*', as_term(o))) for o in graph.objects(None, SH.sourceConstraintComponent)) message = f"violation(s) of type(s) {', '.join(sorted(violations))}" action = Action(self._validate_many.__name__, conforms, ValidationError(message)) resource._last_action = action
def parse_attributes(node: NodeProperties, only_required: bool, inherited_constraint: Optional[str]) -> Dict: attributes = dict() if hasattr(node, "path"): if only_required is True: if not hasattr(node, "mandatory"): return attributes if hasattr(node, "properties"): parent_constraint = node.constraint if hasattr(node, "constraint") else None v = parse_properties(node.properties, only_required, parent_constraint) else: v = parse_value(node, inherited_constraint) attributes[as_term(node.path)] = v elif hasattr(node, "properties"): parent_constraint = node.constraint if hasattr(node, "constraint") else None attributes.update(parse_properties(node.properties, only_required, parent_constraint)) return attributes
def _generate_context(self) -> Dict: """Materializes all Types into templates and parses the templates to generate a context""" # FIXME: the status of this function is experimental # TODO: check if there are conflicting terms, and throw error context = dict() prefixes = dict() types_ = dict() terms = dict() def traverse_properties(properties) -> Tuple[Dict, Dict]: l_prefixes = dict() l_terms = dict() for property_ in properties: if hasattr(property_, "path"): if property_.path != RDF.type and str(property_.path) != "id": v_prefix, v_namespace, v_name = self._graph.compute_qname(property_.path) l_prefixes.update({v_prefix: str(v_namespace)}) term_obj = {"@id": ":".join((v_prefix, v_name))} if hasattr(property_, "id"): term_obj.update({"@type": "@id"}) if hasattr(property_, "values"): if isinstance(property_.values, str) or len(property_.values) == 1: if isinstance(property_.values, list): obj_type = property_.values[0] else: obj_type = property_.values if obj_type in target_classes: term_obj.update({"@type": "@id"}) else: try: px, ns, n = self.graph.compute_qname(obj_type) l_prefixes.update({px: str(ns)}) if str(ns) == str(XSD): term_obj.update({"@type": ":".join((px, n))}) except Exception: pass l_terms.update({v_name: term_obj}) if hasattr(property_, "properties"): l_p, l_t = traverse_properties(property_.properties) l_prefixes.update(l_p) l_terms.update(l_t) return l_prefixes, l_terms target_classes = list() for k in self.classes_to_shapes.keys(): key = as_term(k) if key not in target_classes: target_classes.append(key) else: # TODO: should this raise an error? print("duplicated term", key, k) for type_, shape in self.classes_to_shapes.items(): t_prefix, t_namespace, t_name = self._graph.compute_qname(type_) prefixes.update({t_prefix: str(t_namespace)}) types_.update({t_name: {"@id": ":".join((t_prefix, t_name))}}) node = self.materialize(shape) if hasattr(node, "properties"): p, t = traverse_properties(node.properties) prefixes.update(p) terms.update(t) context.update({key: prefixes[key] for key in sorted(prefixes)}) context.update({key: types_[key] for key in sorted(types_)}) context.update({key: terms[key] for key in sorted(terms)}) return {"@context": context} if len(context) > 0 else None
def object_value(value): return {"type": as_term(value)}