def encode_json_container(bundle): container = defaultdict(dict) prefixes = {} for namespace in bundle._namespaces.get_registered_namespaces(): prefixes[namespace.prefix] = namespace.uri if bundle._namespaces._default: prefixes['default'] = bundle._namespaces._default.uri if prefixes: container['prefix'] = prefixes id_generator = AnonymousIDGenerator() def real_or_anon_id(r): return r._identifier if r._identifier else id_generator.get_anon_id(r) for record in bundle._records: rec_type = record.get_type() rec_label = PROV_N_MAP[rec_type] identifier = six.text_type(real_or_anon_id(record)) record_json = {} if record._attributes: for (attr, values) in record._attributes.items(): if not values: continue attr_name = six.text_type(attr) if attr in PROV_ATTRIBUTE_QNAMES: # TODO: QName export record_json[attr_name] = six.text_type(first(values)) elif attr in PROV_ATTRIBUTE_LITERALS: record_json[attr_name] = first(values).isoformat() else: if len(values) == 1: # single value record_json[attr_name] = encode_json_representation( first(values) ) else: # multiple values record_json[attr_name] = list( encode_json_representation(value) for value in values ) # Check if the container already has the id of the record if identifier not in container[rec_label]: # this is the first instance, just put in the new record container[rec_label][identifier] = record_json else: # the container already has some record(s) of the same identifier # check if this is the second instance current_content = container[rec_label][identifier] if hasattr(current_content, 'items'): # this is a dict, make it a singleton list container[rec_label][identifier] = [current_content] # now append the new record to the list container[rec_label][identifier].append(record_json) return container
def encode_json_container(bundle): container = defaultdict(dict) prefixes = {} for namespace in bundle._namespaces.get_registered_namespaces(): prefixes[namespace.prefix] = namespace.uri if bundle._namespaces._default: prefixes['default'] = bundle._namespaces._default.uri if prefixes: container['prefix'] = prefixes id_generator = AnonymousIDGenerator() real_or_anon_id = \ lambda r: ( r._identifier if r._identifier else id_generator.get_anon_id(r) ) for record in bundle._records: rec_type = record.get_type() rec_label = PROV_N_MAP[rec_type] identifier = six.text_type(real_or_anon_id(record)) record_json = {} if record._attributes: for (attr, values) in record._attributes.items(): if not values: continue attr_name = six.text_type(attr) if attr in PROV_ATTRIBUTE_QNAMES: # TODO: QName export record_json[attr_name] = six.text_type(first(values)) elif attr in PROV_ATTRIBUTE_LITERALS: record_json[attr_name] = first(values).isoformat() else: if len(values) == 1: # single value record_json[attr_name] = encode_json_representation( first(values)) else: # multiple values record_json[attr_name] = list( encode_json_representation(value) for value in values) # Check if the container already has the id of the record if identifier not in container[rec_label]: # this is the first instance, just put in the new record container[rec_label][identifier] = record_json else: # the container already has some record(s) of the same identifier # check if this is the second instance current_content = container[rec_label][identifier] if hasattr(current_content, 'items'): # this is a dict, make it a singleton list container[rec_label][identifier] = [current_content] # now append the new record to the list container[rec_label][identifier].append(record_json) return container
def test_add_bundle_document(self): d1 = self.document_1() d2 = self.document_2() def sub_test_1(): d1.add_bundle(d2) self.assertRaises(ProvException, sub_test_1) ex2_b2 = d2.valid_qualified_name('ex:b2') d1.add_bundle(d2, 'ex:b2') self.assertEqual(ex2_b2, first(d1.bundles).identifier) self.assertNotIn(d2, d1.bundles) b2 = ProvBundle() b2.update(d2) self.assertIn(b2, d1.bundles)