本文整理汇总了Python中pyld.jsonld.normalize方法的典型用法代码示例。如果您正苦于以下问题:Python jsonld.normalize方法的具体用法?Python jsonld.normalize怎么用?Python jsonld.normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pyld.jsonld
的用法示例。
在下文中一共展示了jsonld.normalize方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _canonize
# 需要导入模块: from pyld import jsonld [as 别名]
# 或者: from pyld.jsonld import normalize [as 别名]
def _canonize(data):
return jsonld.normalize(
data, {"algorithm": "URDNA2015", "format": "application/n-quads"}
)
示例2: _options_hash
# 需要导入模块: from pyld import jsonld [as 别名]
# 或者: from pyld.jsonld import normalize [as 别名]
def _options_hash(doc):
doc = dict(doc["signature"])
for k in ["type", "id", "signatureValue"]:
if k in doc:
del doc[k]
doc["@context"] = "https://w3id.org/identity/v1"
normalized = jsonld.normalize(
doc, {"algorithm": "URDNA2015", "format": "application/nquads"}
)
h = hashlib.new("sha256")
h.update(normalized.encode("utf-8"))
return h.hexdigest()
示例3: _doc_hash
# 需要导入模块: from pyld import jsonld [as 别名]
# 或者: from pyld.jsonld import normalize [as 别名]
def _doc_hash(doc):
doc = dict(doc)
if "signature" in doc:
del doc["signature"]
normalized = jsonld.normalize(
doc, {"algorithm": "URDNA2015", "format": "application/nquads"}
)
h = hashlib.new("sha256")
h.update(normalized.encode("utf-8"))
return h.hexdigest()
示例4: normalize_jsonld
# 需要导入模块: from pyld import jsonld [as 别名]
# 或者: from pyld.jsonld import normalize [as 别名]
def normalize_jsonld(json_ld_to_normalize, document_loader=preloaded_context_document_loader,
detect_unmapped_fields=False):
"""
Canonicalize the JSON-LD certificate.
The detect_unmapped_fields parameter is a temporary, incomplete, workaround to detecting fields that do not
correspond to items in the JSON-LD schemas. It works in the Blockcerts context because:
- Blockcerts doesn't use a default vocab
- fallback.org is not expected to occur
Because unmapped fields get dropped during canonicalization, this uses a trick of adding
{"@vocab": "http://fallback.org/"} to the json ld, which will cause any unmapped fields to be prefixed with
http://fallback.org/.
If a @vocab is already there (i.e. an issuer adds this in their extensions), then tampering will change the
normalized form, hence the hash of the certificate, so we will still detect this during verification.
This issue will be addressed in a first-class manner in the future by the pyld library.
:param json_ld_to_normalize:
:param document_loader
:param detect_unmapped_fields:
:return:
"""
json_ld = json_ld_to_normalize
options = deepcopy(JSONLD_OPTIONS)
if document_loader:
options['documentLoader'] = document_loader
if detect_unmapped_fields:
json_ld = deepcopy(json_ld_to_normalize)
prev_context = JsonLdProcessor.get_values(json_ld_to_normalize, '@context')
add_fallback = True
for pc in prev_context:
if type(pc) is dict:
for key, value in pc.items():
if key == '@vocab':
# this already has a vocab; unmapped fields will be detected in the hash
add_fallback = False
break
if add_fallback:
prev_context.append(FALLBACK_CONTEXT)
json_ld['@context'] = prev_context
normalized = jsonld.normalize(json_ld, options=options)
if detect_unmapped_fields and FALLBACK_VOCAB in normalized:
unmapped_fields = []
for m in re.finditer('<http://fallback\.org/(.*)>', normalized):
unmapped_fields.append(m.group(0))
error_string = ', '.join(unmapped_fields)
raise BlockcertValidationError(
'There are some fields in the certificate that do not correspond to the expected schema. This has likely been tampered with. Unmapped fields are: ' + error_string)
return normalized
示例5: load_item
# 需要导入模块: from pyld import jsonld [as 别名]
# 或者: from pyld.jsonld import normalize [as 别名]
def load_item(self, doc):
# Skip this loader if host and port are not set
if not settings.KAFKA_HOST or not settings.KAFKA_PORT:
return
kafka_producer = Producer(self.config)
log_identifiers = []
# Recursively index associated models like attachments
for model in doc.traverse():
self.add_metadata(model, doc == model)
# Serialize the body to JSON-LD
jsonld_body = JsonLDSerializer(loader_class=self).serialize(model)
# Serialize the jsonld_body to N-Triples
ntriples = jsonld.normalize(jsonld_body, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})
# Add the graph name to the body. This is done the low-tech way, but could be improved by updating the
# JSON-LD so that the graph information is included when serializing to N-Quads.
ntriples_split = ntriples.split(' .\n')
nquads = f' <http://purl.org/linked-delta/replace?graph={parse.quote(model.get_ori_identifier())}> .\n' \
.join(ntriples_split) \
.strip()
log_identifiers.append(model.get_short_identifier())
message_key_id = '%s_%s' % (settings.KAFKA_MESSAGE_KEY, model.get_short_identifier())
if sys.getsizeof(nquads.encode('utf-8')) > settings.KAFKA_MAX_MESSAGE_BYTES:
# Send statements one by one to avoid exceding max message size in bytes
for message in nquads.split('\n'):
kafka_producer.produce(settings.KAFKA_TOPIC,
message.encode('utf-8'),
message_key_id,
callback=delivery_report)
else:
# Send whole document at once
kafka_producer.produce(settings.KAFKA_TOPIC,
nquads.encode('utf-8'),
message_key_id,
callback=delivery_report)
# See https://github.com/confluentinc/confluent-kafka-python#usage for a complete example of how to use
# the kafka producer with status callbacks.
log.debug(f'DeltaLoader sending document ids to Kafka: {", ".join(log_identifiers)}')
kafka_producer.flush()