當前位置: 首頁>>代碼示例>>Python>>正文


Python jsonld.normalize方法代碼示例

本文整理匯總了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"}
    ) 
開發者ID:hyperledger,項目名稱:aries-cloudagent-python,代碼行數:6,代碼來源:create_verify_data.py

示例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() 
開發者ID:autogestion,項目名稱:pubgate,代碼行數:14,代碼來源:datasig.py

示例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() 
開發者ID:autogestion,項目名稱:pubgate,代碼行數:12,代碼來源:datasig.py

示例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 
開發者ID:IMSGlobal,項目名稱:cert-schema,代碼行數:56,代碼來源:jsonld_helpers.py

示例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() 
開發者ID:openstate,項目名稱:open-raadsinformatie,代碼行數:51,代碼來源:delta.py


注:本文中的pyld.jsonld.normalize方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。