2016-08-17 12:28:15 -04:00
|
|
|
import logging
|
|
|
|
|
2016-11-30 09:41:01 -06:00
|
|
|
from lbrynet.core import Error
|
2016-10-14 08:04:40 -04:00
|
|
|
from lbrynet.metadata.StructuredDict import StructuredDict
|
|
|
|
import metadata_schemas
|
2016-08-17 12:28:15 -04:00
|
|
|
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
NAME_ALLOWED_CHARSET = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321-'
|
|
|
|
|
|
|
|
|
|
|
|
def verify_name_characters(name):
|
2016-11-21 21:58:04 -05:00
|
|
|
assert len(name) > 0, "Empty uri"
|
2016-11-30 09:41:01 -06:00
|
|
|
invalid_characters = {c for c in name if c not in NAME_ALLOWED_CHARSET}
|
|
|
|
if invalid_characters:
|
|
|
|
raise Error.InvalidName(name, invalid_characters)
|
2016-08-17 12:28:15 -04:00
|
|
|
return True
|
|
|
|
|
2016-11-30 09:41:01 -06:00
|
|
|
|
2016-10-14 15:31:32 -04:00
|
|
|
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']
|
|
|
|
|
2016-08-17 12:28:15 -04:00
|
|
|
|
2016-10-14 08:04:40 -04:00
|
|
|
class Metadata(StructuredDict):
|
2016-10-14 09:03:28 -04:00
|
|
|
current_version = '0.0.3'
|
|
|
|
|
2016-10-14 15:31:32 -04:00
|
|
|
_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)
|
|
|
|
]
|
2016-08-17 12:28:15 -04:00
|
|
|
|
2016-10-14 15:31:32 -04:00
|
|
|
def __init__(self, metadata, migrate=True, target_version=None):
|
2016-11-04 15:09:40 -05:00
|
|
|
if not isinstance(metadata, dict):
|
2016-12-13 17:08:29 -06:00
|
|
|
raise TypeError("metadata is not a dictionary")
|
2016-10-14 08:04:40 -04:00
|
|
|
starting_version = metadata.get('ver', '0.0.1')
|
2016-08-17 12:28:15 -04:00
|
|
|
|
2016-10-31 13:53:45 -04:00
|
|
|
StructuredDict.__init__(self, metadata, starting_version, migrate, target_version)
|