def collect_values(data: Union[Resource, List[Resource]], follow: str, exception: Callable = Exception) -> List[str]: def _collect(things: List) -> Iterator[str]: for x in things: if isinstance(x, Dict): for k, v in x.items(): if isinstance(v, List): yield from _collect(v) elif isinstance(v, Dict): yield from _collect([v]) else: yield v try: r = Reshaper("") reshaped = dispatch(data, r._reshape_many, r._reshape_one, [follow], False) if reshaped is None: raise Exception(f"Nothing to collect") jsoned = as_json(reshaped, False, False, None, None, None) prepared = jsoned if isinstance(jsoned, List) else [jsoned] return list(_collect(prepared)) except Exception as e: raise exception( f"An error occur when collecting values for path to follow '{follow}': {str(e)}" )
def as_jsonld(data: Union[Resource, List[Resource]], form: str, store_metadata: bool, model_context: Optional[Context], metadata_context: Optional[Context], context_resolver: Optional[Callable]) -> Union[Dict, List[Dict]]: try: valid_form = Form(form.lower()) except ValueError: supported_forms = tuple(item.value for item in Form) raise NotSupportedError( f"supported serialization forms are {supported_forms}") return dispatch(data, _as_jsonld_many, _as_jsonld_one, valid_form, store_metadata, model_context, metadata_context, context_resolver)
def collect_values(data: Union[Resource, List[Resource]], follow: str, exception: Callable = Exception) -> List[str]: def _collect(things: List) -> Iterator[str]: for x in things: if isinstance(x, Dict): for k, v in x.items(): if isinstance(v, List): yield from _collect(v) elif isinstance(v, Dict): yield from _collect([v]) else: yield v try: r = Reshaper("") reshaped = dispatch(data, r._reshape_many, r._reshape_one, [follow], False) jsoned = as_json(reshaped, False, False, None, None, None) prepared = jsoned if isinstance(jsoned, List) else [jsoned] return list(_collect(prepared)) except AttributeError: raise exception("path to follow is incorrect")
def as_graph(data: Union[Resource, List[Resource]], store_metadata: bool, model_context: Optional[Context], metadata_context: Optional[Context], context_resolver: Optional[Callable]) -> Graph: return dispatch(data, _as_graph_many, _as_graph_one, store_metadata, model_context, metadata_context, context_resolver)
def reshape(self, data: Union[Resource, List[Resource]], keep: List[str], versioned: bool) -> Union[Resource, List[Resource]]: # POLICY Resource _last_action and _store_metadata should be None. # POLICY Resource _validated and _synchronized should be False. return dispatch(data, self._reshape_many, self._reshape_one, keep, versioned)