def nquads_transform(doc): ''' This function takes a json-ld document, and parse it into nquads format (rdf) ''' t = jsonld.JsonLdProcessor() nquads = t.parse_nquads(jsonld.to_rdf(doc, {'format': 'application/nquads'}))['@default'] return nquads
def get(self): t_start = datetime.datetime.now() args_dict = { arg_key: arg_val for arg_key, arg_val in request.args.items() if arg_key not in ['compact'] } compact = request.args.get('compact') # Subselect for selected Node-IDs node_ids = db.session.query(Node.id) t_delta1 = int( (datetime.datetime.now() - t_start).total_seconds() * 1000) for arg_key, arg_val in args_dict.items(): filter_it = Node.json[arg_key] == cast(arg_val, JSON) node_ids = node_ids.filter(filter_it) node_ids = node_ids.group_by(Node.id).subquery() # Main select for all Nodes with the IDs from the subselect query = Node.query.join(node_ids, Node.id == node_ids.c.id) t_delta2 = int( (datetime.datetime.now() - t_start).total_seconds() * 1000) expanded_list, expanded_len, tst_3 = self.expanded_list(query) t_delta3 = int((tst_3 - t_start).total_seconds() * 1000) t_delta4 = int( (datetime.datetime.now() - t_start).total_seconds() * 1000) if compact is None: return expanded_list, 200, { 'Access-Control-Expose-Headers': 'X-Total-Count', 'X-Total-Count': expanded_len, 'X-Perf-1-MSec': t_delta1, 'X-Perf-2-MSec': t_delta2, 'X-Perf-3-MSec': t_delta3, 'X-Perf-4-MSec': t_delta4, } else: return_list = [] jsonld_proc = jsonld.JsonLdProcessor() for expanded_node in expanded_list: compacted_node = jsonld_proc.compact(expanded_node, compact, options=None) return_list.append(compacted_node) t_delta5 = int( (datetime.datetime.now() - t_start).total_seconds() * 1000) return return_list, 200, { 'Access-Control-Expose-Headers': 'X-Total-Count', 'X-Total-Count': len(return_list), 'X-Perf-1-MSec': t_delta1, 'X-Perf-2-MSec': t_delta2, 'X-Perf-3-MSec': t_delta3, 'X-Perf-4-MSec': t_delta4, 'X-Perf-5-MSec': t_delta5, }
def _doit(self, j): proc = jsonld.JsonLdProcessor() context = j['@context'] bads = [] try: ctx = proc.process_context(proc._get_initial_context({}), context, {}) except jsonld.JsonLdError as e: for k, v in context.items(): c = {k: v, '@version': context['@version']} try: ctx = proc.process_context(proc._get_initial_context({}), c, {}) except jsonld.JsonLdError as e: bads.append((k, v)) assert not bads, bads
def my_expand_compact(query_in): expand = request.args.get('expand') compact = request.args.get('compact') if expand and expand.lower() == 'true': ret_list = [i.expanded for i in query_in] ret_len = len(ret_list) elif compact is not None: ret_dict = {} for obj in query_in: if obj.id not in ret_dict: ret_dict[obj.id] = {} ret_dict[obj.id].update(obj.expanded) ret_list = [] jsonld_proc = jsonld.JsonLdProcessor() for expanded_node in ret_dict.values(): compacted_node = jsonld_proc.compact(expanded_node, compact, options=None) ret_list.append(compacted_node) ret_len = len(ret_list) else: ret_list = [i.json for i in query_in] ret_len = len(ret_list) return ret_list, ret_len
def nquads_transform(doc): t = jsonld.JsonLdProcessor() nquads = t.parse_nquads(jsonld.to_rdf(doc, {'format': 'application/nquads'}))['@default'] return nquads
ctx = json.load(fh) argv_i += 2 else: ctx = None if len(sys.argv) > argv_i: if sys.argv[argv_i] == '-l': list_file = sys.argv[argv_i+1] with open(list_file, 'r') as fh: files = [f.strip() for f in fh] else: files = sys.argv[argv_i:] else: files = [f.strip() for f in sys.stdin] proc = jsonld.JsonLdProcessor() r = re.compile(r'_:(\S+)') def convert(p): filename = str(p) try: with p.open('r') as fh: bnode_map = {} input = json.load(fh) try: id = input['id'] except KeyError as e: print(f'*** Skipping {filename}') return 0 if not id.startswith('urn:uuid:'): raise JSONLDError(f"file doesn't have a valid top-level UUID: {filename}")
def __init__(self): self.processor = jsonld.JsonLdProcessor() self.temp_attr_id = None self.temp_properties = None self.logger, self.logfile = get_logger('jsonld')
def __init__(self): self.processor = jsonld.JsonLdProcessor() self.temp_attr_id = None