From 2e557d3a363e5a98ed7412cfe66d6b79e4cc6ce9 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 13 Mar 2019 19:40:49 -0400 Subject: [PATCH] converted everyting in schema/legacy/* into schema/jsonschema.py --- lbrynet/schema/jsonschema.py | 60 +++++ lbrynet/schema/legacy/StructuredDict.py | 59 ----- lbrynet/schema/legacy/__init__.py | 0 lbrynet/schema/legacy/metadata_schemas.py | 276 ---------------------- lbrynet/schema/legacy/migrate.py | 79 ------- 5 files changed, 60 insertions(+), 414 deletions(-) create mode 100644 lbrynet/schema/jsonschema.py delete mode 100644 lbrynet/schema/legacy/StructuredDict.py delete mode 100644 lbrynet/schema/legacy/__init__.py delete mode 100644 lbrynet/schema/legacy/metadata_schemas.py delete mode 100644 lbrynet/schema/legacy/migrate.py diff --git a/lbrynet/schema/jsonschema.py b/lbrynet/schema/jsonschema.py new file mode 100644 index 000000000..b9fbb770a --- /dev/null +++ b/lbrynet/schema/jsonschema.py @@ -0,0 +1,60 @@ +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 diff --git a/lbrynet/schema/legacy/StructuredDict.py b/lbrynet/schema/legacy/StructuredDict.py deleted file mode 100644 index 31442d0d8..000000000 --- a/lbrynet/schema/legacy/StructuredDict.py +++ /dev/null @@ -1,59 +0,0 @@ -import jsonschema - - -class StructuredDict(dict): - """ - A dictionary that enforces a structure specified by a schema, and supports - migration between different versions of the schema. - """ - - # To be specified in sub-classes, an array in the format - # [(version, schema, migration), ...] - _versions = [] - - # Used internally to allow schema lookups by version number - _schemas = {} - - version = None - - def __init__(self, value, starting_version, migrate=True, target_version=None): - dict.__init__(self, value) - - self.version = starting_version - self._schemas = dict([(version, schema) for (version, schema, _) in self._versions]) - - self.validate(starting_version) - - if migrate: - self.migrate(target_version) - - def _upgrade_version_range(self, start_version, end_version): - after_starting_version = False - for version, schema, migration in self._versions: - if not after_starting_version: - if version == self.version: - after_starting_version = True - continue - - yield version, schema, migration - - if end_version and version == end_version: - break - - def validate(self, version): - jsonschema.validate(self, self._schemas[version]) - - def migrate(self, target_version=None): - if target_version: - assert self._versions.index(target_version) > self._versions.index(self.version), \ - "Current version is above target version" - - for version, schema, migration in self._upgrade_version_range(self.version, target_version): - migration(self) - try: - self.validate(version) - except jsonschema.ValidationError as e: - raise jsonschema.ValidationError( - "Could not migrate to version %s due to validation error: %s" % - (version, e.message)) - self.version = version diff --git a/lbrynet/schema/legacy/__init__.py b/lbrynet/schema/legacy/__init__.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/lbrynet/schema/legacy/metadata_schemas.py b/lbrynet/schema/legacy/metadata_schemas.py deleted file mode 100644 index 6165ccaa6..000000000 --- a/lbrynet/schema/legacy/metadata_schemas.py +++ /dev/null @@ -1,276 +0,0 @@ -VER_001 = { - '$schema': 'http://json-schema.org/draft-04/schema#', - 'title': 'LBRY metadata schema 0.0.1', - 'definitions': { - 'fee_info': { - 'type': 'object', - 'properties': { - 'amount': { - 'type': 'number', - 'minimum': 0, - 'exclusiveMinimum': True, - }, - 'address': { - 'type': 'string' - } - }, - } - }, - 'type': 'object', - - 'properties': { - 'ver': { - 'type': 'string', - 'default': '0.0.1' - }, - 'title': { - 'type': 'string' - }, - 'description': { - 'type': 'string' - }, - 'author': { - 'type': 'string' - }, - 'language': { - 'type': 'string' - }, - 'license': { - 'type': 'string' - }, - 'content-type': { - 'type': 'string' - }, - 'sources': { - 'type': 'object', - 'properties': { - 'lbry_sd_hash': { - 'type': 'string' - }, - 'btih': { - 'type': 'string' - }, - 'url': { - 'type': 'string' - } - }, - 'additionalProperties': False - }, - 'thumbnail': { - 'type': 'string' - }, - 'preview': { - 'type': 'string' - }, - 'fee': { - 'properties': { - 'LBC': {'$ref': '#/definitions/fee_info'}, - 'BTC': {'$ref': '#/definitions/fee_info'}, - 'USD': {'$ref': '#/definitions/fee_info'} - } - }, - 'contact': { - 'type': 'number' - }, - 'pubkey': { - 'type': 'string' - }, - }, - 'required': [ - 'title', 'description', 'author', 'language', 'license', 'content-type', 'sources'], - 'additionalProperties': False -} - - -VER_002 = { - '$schema': 'http://json-schema.org/draft-04/schema#', - 'title': 'LBRY metadata schema 0.0.2', - 'definitions': { - 'fee_info': { - 'type': 'object', - 'properties': { - 'amount': { - 'type': 'number', - 'minimum': 0, - 'exclusiveMinimum': True, - }, - 'address': { - 'type': 'string' - } - }, - } - }, - 'type': 'object', - - 'properties': { - 'ver': { - 'type': 'string', - 'enum': ['0.0.2'], - }, - 'title': { - 'type': 'string' - }, - 'description': { - 'type': 'string' - }, - 'author': { - 'type': 'string' - }, - 'language': { - 'type': 'string' - }, - 'license': { - 'type': 'string' - }, - 'content-type': { - 'type': 'string' - }, - 'sources': { - 'type': 'object', - 'properties': { - 'lbry_sd_hash': { - 'type': 'string' - }, - 'btih': { - 'type': 'string' - }, - 'url': { - 'type': 'string' - } - }, - 'additionalProperties': False - }, - 'thumbnail': { - 'type': 'string' - }, - 'preview': { - 'type': 'string' - }, - 'fee': { - 'properties': { - 'LBC': {'$ref': '#/definitions/fee_info'}, - 'BTC': {'$ref': '#/definitions/fee_info'}, - 'USD': {'$ref': '#/definitions/fee_info'} - } - }, - 'contact': { - 'type': 'number' - }, - 'pubkey': { - 'type': 'string' - }, - 'license_url': { - 'type': 'string' - }, - 'nsfw': { - 'type': 'boolean', - 'default': False - }, - - }, - 'required': [ - 'ver', 'title', 'description', 'author', 'language', 'license', - 'content-type', 'sources', 'nsfw' - ], - 'additionalProperties': False -} - - -VER_003 = { - '$schema': 'http://json-schema.org/draft-04/schema#', - 'title': 'LBRY metadata schema 0.0.3', - 'definitions': { - 'fee_info': { - 'type': 'object', - 'properties': { - 'amount': { - 'type': 'number', - 'minimum': 0, - 'exclusiveMinimum': True, - }, - 'address': { - 'type': 'string' - } - }, - } - }, - 'type': 'object', - - 'properties': { - 'ver': { - 'type': 'string', - 'enum': ['0.0.3'], - }, - 'title': { - 'type': 'string' - }, - 'description': { - 'type': 'string' - }, - 'author': { - 'type': 'string' - }, - 'language': { - 'type': 'string' - }, - 'license': { - 'type': 'string' - }, - 'content_type': { - 'type': 'string' - }, - 'sources': { - 'type': 'object', - 'properties': { - 'lbry_sd_hash': { - 'type': 'string' - }, - 'btih': { - 'type': 'string' - }, - 'url': { - 'type': 'string' - } - }, - 'additionalProperties': False - }, - 'thumbnail': { - 'type': 'string' - }, - 'preview': { - 'type': 'string' - }, - 'fee': { - 'properties': { - 'LBC': {'$ref': '#/definitions/fee_info'}, - 'BTC': {'$ref': '#/definitions/fee_info'}, - 'USD': {'$ref': '#/definitions/fee_info'} - } - }, - 'contact': { - 'type': 'number' - }, - 'pubkey': { - 'type': 'string' - }, - 'license_url': { - 'type': 'string' - }, - 'nsfw': { - 'type': 'boolean', - 'default': False - }, - 'sig': { - 'type': 'string' - } - }, - 'required': [ - 'ver', 'title', 'description', 'author', 'language', 'license', - 'content_type', 'sources', 'nsfw' - ], - 'additionalProperties': False, - 'dependencies': { - 'pubkey': ['sig'], - 'sig': ['pubkey'] - } -} diff --git a/lbrynet/schema/legacy/migrate.py b/lbrynet/schema/legacy/migrate.py deleted file mode 100644 index 3b515c5be..000000000 --- a/lbrynet/schema/legacy/migrate.py +++ /dev/null @@ -1,79 +0,0 @@ -""" -migrate claim json schema (0.0.1-3) to protobuf (0.1.0) -""" - -from lbrynet.schema.legacy import metadata_schemas -from lbrynet.schema.claim import ClaimDict -from .StructuredDict import StructuredDict - - -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'] - - -class LegacyMetadata(StructuredDict): - current_version = '0.0.3' - - _versions = [ - ('0.0.1', metadata_schemas.VER_001, None), - ('0.0.2', metadata_schemas.VER_002, migrate_001_to_002), - ('0.0.3', metadata_schemas.VER_003, migrate_002_to_003) - ] - - def __init__(self, metadata, migrate=True, target_version=None): - if not isinstance(metadata, dict): - raise TypeError("{} is not a dictionary".format(metadata)) - starting_version = metadata.get('ver', '0.0.1') - - StructuredDict.__init__(self, metadata, starting_version, migrate, target_version) - - -def migrate_003_to_010(value): - migrated_to_003 = LegacyMetadata(value) - metadata = { - "version": "_0_1_0" - } - for k in ["author", "description", "language", "license", "nsfw", "thumbnail", "title", - "preview"]: - if k in migrated_to_003: - metadata.update({k: migrated_to_003[k]}) - - if 'license_url' in migrated_to_003: - metadata['licenseUrl'] = migrated_to_003['license_url'] - - if "fee" in migrated_to_003: - fee = migrated_to_003["fee"] - currency = list(fee.keys())[0] - amount = fee[currency]['amount'] - address = fee[currency]['address'] - metadata.update(dict(fee={"currency": currency, "version": "_0_0_1", - "amount": amount, "address": address})) - source = { - "source": migrated_to_003['sources']['lbry_sd_hash'], - "contentType": migrated_to_003['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 ClaimDict.load_dict(migrated) - - -def migrate(value): - return migrate_003_to_010(value)