From ce9dc1836e5b51692a69408a0c607008413c11b1 Mon Sep 17 00:00:00 2001 From: Lex Berezhny <lex@damoti.com> Date: Wed, 13 Mar 2019 19:47:13 -0400 Subject: [PATCH] moved schema/current_schema/claim.py into schema/claim.py --- lbrynet/schema/claim.py | 83 +++++++++++++++++++++-- lbrynet/schema/current_schema/__init__.py | 0 lbrynet/schema/current_schema/claim.py | 73 -------------------- 3 files changed, 77 insertions(+), 79 deletions(-) delete mode 100644 lbrynet/schema/current_schema/__init__.py delete mode 100644 lbrynet/schema/current_schema/claim.py diff --git a/lbrynet/schema/claim.py b/lbrynet/schema/claim.py index 2665d317b..6c131a4c2 100644 --- a/lbrynet/schema/claim.py +++ b/lbrynet/schema/claim.py @@ -1,15 +1,21 @@ import json import binascii -from google.protobuf import json_format # pylint: disable=no-name-in-module -from google.protobuf.message import DecodeError as DecodeError_pb # pylint: disable=no-name-in-module,import-error - +from copy import deepcopy from collections import OrderedDict -from lbrynet.schema.proto2 import legacy_claim_pb2 +import google.protobuf.json_format as json_pb # pylint: disable=no-name-in-module +from google.protobuf import json_format # pylint: disable=no-name-in-module +from google.protobuf.message import DecodeError as DecodeError_pb # pylint: disable=no-name-in-module,import-error +from google.protobuf.message import Message # pylint: disable=no-name-in-module,import-error + +from lbrynet.schema.types.v2 import claim_pb2 as claim_pb +from torba.client.constants import COIN + +from lbrynet.schema.types.v1 import legacy_claim_pb2 from lbrynet.schema.signature import Signature from lbrynet.schema.validator import get_validator from lbrynet.schema.signer import get_signer -from lbrynet.schema.legacy_schema_v1.claim import Claim +from lbrynet.schema.legacy_schema_v1.claim import Claim as LegacyClaim from lbrynet.schema.legacy_schema_v1 import CLAIM_TYPE_NAMES from lbrynet.schema.constants import CURVE_NAMES, SECP256k1 from lbrynet.schema.encoding import decode_fields, decode_b64_fields, encode_fields @@ -35,7 +41,7 @@ class ClaimDict(OrderedDict): def protobuf(self): """Claim message object""" - return Claim.load(self) + return LegacyClaim.load(self) @property def serialized(self): @@ -197,3 +203,68 @@ class ClaimDict(OrderedDict): return curve = CURVE_NAMES[claim.certificate.keyType] return get_validator(curve).load_from_certificate(claim, certificate_id) + + +class Schema(Message): + @classmethod + def load(cls, message): + raise NotImplementedError + + @classmethod + def _load(cls, data, message): + if isinstance(data, dict): + data = json.dumps(data) + return json_pb.Parse(data, message) + + +class Claim(Schema): + CLAIM_TYPE_STREAM = 0 #fixme: 0 is unset, should be fixed on proto file to be 1 and 2! + CLAIM_TYPE_CERT = 1 + + @classmethod + def load(cls, message: dict): + _claim = deepcopy(message) + _message_pb = claim_pb.Claim() + + if "certificate" in _claim: # old protobuf, migrate + _cert = _claim.pop("certificate") + assert isinstance(_cert, dict) + _message_pb.type = Claim.CLAIM_TYPE_CERT + _message_pb.channel.MergeFrom(claim_pb.Channel(public_key=_cert.pop("publicKey"))) + _claim = {} # so we dont need to know what other fields we ignored + elif "channel" in _claim: + _channel = _claim.pop("channel") + _message_pb.type = Claim.CLAIM_TYPE_CERT + _message_pb.channel = claim_pb.Channel(**_channel) + elif "stream" in _claim: + _message_pb.type = Claim.CLAIM_TYPE_STREAM + _stream = _claim.pop("stream") + if "source" in _stream: + _source = _stream.pop("source") + _message_pb.stream.hash = _source.get("source", b'') # fixme: fail if empty? + _message_pb.stream.media_type = _source.pop("contentType") + if "metadata" in _stream: + _metadata = _stream.pop("metadata") + _message_pb.stream.license = _metadata.get("license") + _message_pb.stream.description = _metadata.get("description") + _message_pb.stream.language = _metadata.get("language") + _message_pb.stream.title = _metadata.get("title") + _message_pb.stream.author = _metadata.get("author") + _message_pb.stream.license_url = _metadata.get("licenseUrl") + _message_pb.stream.thumbnail_url = _metadata.get("thumbnail") + if _metadata.get("nsfw"): + _message_pb.stream.tags.append("nsfw") + if "fee" in _metadata: + _message_pb.stream.fee.address = _metadata["fee"]["address"] + _message_pb.stream.fee.currency = { + "LBC": 0, + "USD": 1 + }[_metadata["fee"]["currency"]] + multiplier = COIN if _metadata["fee"]["currency"] == "LBC" else 100 + total = int(_metadata["fee"]["amount"]*multiplier) + _message_pb.stream.fee.amount = total if total >= 0 else 0 + _claim = {} + else: + raise AttributeError + + return cls._load(_claim, _message_pb) diff --git a/lbrynet/schema/current_schema/__init__.py b/lbrynet/schema/current_schema/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/lbrynet/schema/current_schema/claim.py b/lbrynet/schema/current_schema/claim.py deleted file mode 100644 index e915fba22..000000000 --- a/lbrynet/schema/current_schema/claim.py +++ /dev/null @@ -1,73 +0,0 @@ -import json -from copy import deepcopy - -import google.protobuf.json_format as json_pb # pylint: disable=no-name-in-module -from google.protobuf.message import Message # pylint: disable=no-name-in-module,import-error - -from lbrynet.schema.proto3 import claim_pb2 as claim_pb -from torba.client.constants import COIN - - -class Schema(Message): - @classmethod - def load(cls, message): - raise NotImplementedError - - @classmethod - def _load(cls, data, message): - if isinstance(data, dict): - data = json.dumps(data) - return json_pb.Parse(data, message) - - -class Claim(Schema): - CLAIM_TYPE_STREAM = 0 #fixme: 0 is unset, should be fixed on proto file to be 1 and 2! - CLAIM_TYPE_CERT = 1 - - @classmethod - def load(cls, message: dict): - _claim = deepcopy(message) - _message_pb = claim_pb.Claim() - - if "certificate" in _claim: # old protobuf, migrate - _cert = _claim.pop("certificate") - assert isinstance(_cert, dict) - _message_pb.type = Claim.CLAIM_TYPE_CERT - _message_pb.channel.MergeFrom(claim_pb.Channel(public_key=_cert.pop("publicKey"))) - _claim = {} # so we dont need to know what other fields we ignored - elif "channel" in _claim: - _channel = _claim.pop("channel") - _message_pb.type = Claim.CLAIM_TYPE_CERT - _message_pb.channel = claim_pb.Channel(**_channel) - elif "stream" in _claim: - _message_pb.type = Claim.CLAIM_TYPE_STREAM - _stream = _claim.pop("stream") - if "source" in _stream: - _source = _stream.pop("source") - _message_pb.stream.hash = _source.get("source", b'') # fixme: fail if empty? - _message_pb.stream.media_type = _source.pop("contentType") - if "metadata" in _stream: - _metadata = _stream.pop("metadata") - _message_pb.stream.license = _metadata.get("license") - _message_pb.stream.description = _metadata.get("description") - _message_pb.stream.language = _metadata.get("language") - _message_pb.stream.title = _metadata.get("title") - _message_pb.stream.author = _metadata.get("author") - _message_pb.stream.license_url = _metadata.get("licenseUrl") - _message_pb.stream.thumbnail_url = _metadata.get("thumbnail") - if _metadata.get("nsfw"): - _message_pb.stream.tags.append("nsfw") - if "fee" in _metadata: - _message_pb.stream.fee.address = _metadata["fee"]["address"] - _message_pb.stream.fee.currency = { - "LBC": 0, - "USD": 1 - }[_metadata["fee"]["currency"]] - multiplier = COIN if _metadata["fee"]["currency"] == "LBC" else 100 - total = int(_metadata["fee"]["amount"]*multiplier) - _message_pb.stream.fee.amount = total if total >= 0 else 0 - _claim = {} - else: - raise AttributeError - - return cls._load(_claim, _message_pb)