deleted bunch of stuff

This commit is contained in:
Lex Berezhny 2019-03-15 01:32:06 -04:00
parent 9fd67aaad6
commit c1b7fdc68e
4 changed files with 0 additions and 227 deletions

View file

@ -1,15 +0,0 @@
import json
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
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)

View file

@ -1,73 +0,0 @@
import json
import binascii
from google.protobuf import json_format # pylint: disable=no-name-in-module
from lbrynet.schema.error import DecodeError, InvalidAddress
from lbrynet.schema.legacy.migrate import migrate as schema_migrator
from lbrynet.schema.claim import ClaimDict
def migrate_legacy_protobuf(old_proto_bytes: bytes):
# TODO: where to put this?
from lbrynet.schema.proto2.legacy_claim_pb2 import Claim as OldClaimPB
from google.protobuf import json_format # pylint: disable=no-name-in-module
from lbrynet.schema.encoding import decode_b64_fields
from lbrynet.schema.current_schema.claim import Claim as CurrentClaim
old_msg = OldClaimPB()
old_msg.ParseFromString(old_proto_bytes)
old_json = json.loads(json_format.MessageToJson(old_msg, True))
return CurrentClaim.load(decode_b64_fields(old_json))
def migrate_json_claim_value(decoded_json):
try:
if 'fee' in decoded_json:
old_fee = decoded_json['fee']
if not old_fee[list(old_fee.keys())[0]]['amount']:
del decoded_json['fee']
return migrate_json_claim_value(decoded_json)
except (TypeError, AttributeError, InvalidAddress):
raise DecodeError("Failed to decode claim")
try:
pb_migrated = schema_migrator(decoded_json)
return pb_migrated
except json_format.ParseError as parse_error:
raise DecodeError("Failed to parse protobuf: %s" % parse_error)
except Exception as err:
raise DecodeError("Failed to migrate claim: %s" % err)
def smart_decode(claim_value):
"""
Decode a claim value
Try decoding claim protobuf, if this fails try decoding json and migrating it.
If unable to decode or migrate, raise DecodeError
"""
# if already decoded, return
if isinstance(claim_value, ClaimDict):
return claim_value
elif isinstance(claim_value, dict):
return ClaimDict.load_dict(claim_value)
try:
claim_value = binascii.unhexlify(claim_value)
except (TypeError, ValueError):
pass
if claim_value[0] in ['{', ord('{')]:
try:
if isinstance(claim_value, bytes):
claim_value = claim_value.decode()
decoded_json = json.loads(claim_value)
return migrate_json_claim_value(decoded_json)
except (ValueError, TypeError):
pass
try:
if isinstance(claim_value, str):
claim_value = claim_value.encode()
return ClaimDict.deserialize(claim_value)
except (DecodeError, InvalidAddress, KeyError, TypeError):
raise DecodeError(claim_value)

View file

@ -1,79 +0,0 @@
from collections import OrderedDict
from lbrynet.schema.address import encode_address, decode_address
from lbrynet.schema.constants import CURRENCY_NAMES, CURRENCY_MAP
from lbrynet.schema.legacy_schema_v1.fee import Fee as FeeHelper
from lbrynet.schema.proto2 import fee_pb2
def migrate(fee):
if len(list(fee.keys())) == 3 and 'currency' in fee and 'amount' in fee and 'address' in fee:
return FeeHelper.load({
"version": "_0_0_1",
"currency": fee['currency'],
"amount": fee['amount'],
"address": decode_address(fee['address'])
})
if len(list(fee.keys())) > 1:
raise Exception("Invalid fee")
currency = list(fee.keys())[0]
amount = fee[currency]['amount']
address = fee[currency]['address']
return FeeHelper.load({
"version": "_0_0_1",
"currency": currency,
"amount": amount,
"address": decode_address(address)
})
class Fee(OrderedDict):
def __init__(self, fee):
if (len(fee) == 4 and "version" in fee and "currency" in fee
and "amount" in fee and "address" in fee):
OrderedDict.__init__(self, fee)
else:
OrderedDict.__init__(self, Fee.load_protobuf(migrate(fee)))
@property
def currency(self):
return self['currency']
@property
def address(self):
return self['address']
@property
def amount(self):
return self['amount']
@property
def version(self):
return self['version']
@property
def protobuf(self):
pb = {
"version": self.version,
"currency": CURRENCY_MAP[self.currency],
"address": decode_address(self.address),
"amount": self.amount
}
return FeeHelper.load(pb)
@classmethod
def load_protobuf(cls, pb):
return cls({
"version": pb.version,
"currency": CURRENCY_NAMES[pb.currency],
"address": encode_address(pb.address),
"amount": pb.amount
})
@classmethod
def deserialize(cls, serialized):
pb = fee_pb2.Fee()
pb.ParseFromString(serialized)
return cls.load_protobuf(pb)

View file

@ -1,60 +0,0 @@
def migrate_001_to_002(metadata):
metadata['ver'] = '0.0.2'
metadata['nsfw'] = False
def migrate_002_to_003(metadata):
metadata['ver'] = '0.0.3'
if 'content-type' in metadata:
metadata['content_type'] = metadata['content-type']
del metadata['content-type']
def migrate_003_to_010(value):
metadata = {
"version": "_0_1_0",
"title": value.get('title', ''),
"description": value.get('description', ''),
"thumbnail": value.get('thumbnail', ''),
"preview": value.get('preview', ''),
"author": value.get('author', ''),
"license": value.get('license', ''),
"licenseUrl": value.get('license_url', ''),
"language": value.get('language', ''),
"nsfw": value.get('nsfw', False),
}
if "fee" in value:
fee = value["fee"]
currency = list(fee.keys())[0]
metadata['fee'] = {
"version": "_0_0_1",
"currency": currency,
"amount": fee[currency]['amount'],
"address": fee[currency]['address']
}
source = {
"source": value['sources']['lbry_sd_hash'],
"contentType": value['content_type'],
"sourceType": "lbry_sd_hash",
"version": "_0_0_1"
}
migrated = {
"version": "_0_0_1",
"claimType": "streamType",
"stream": {
"version": "_0_0_1",
"metadata": metadata,
"source": source
}
}
return migrated
def migrate(value):
if value.get('ver', '0.0.1') == '0.0.1':
migrate_001_to_002(value)
if value['ver'] == '0.0.2':
migrate_002_to_003(value)
if value['ver'] == '0.0.3':
value = migrate_003_to_010(value)
return value