added additional fields to uri parser

This commit is contained in:
hackrush 2019-01-09 03:07:53 +05:30 committed by Lex Berezhny
parent 6ae4e68d1c
commit 6c8cbc1b9e
4 changed files with 46 additions and 25 deletions

View file

@ -2133,7 +2133,7 @@ class Daemon(metaclass=JSONRPCServerType):
timeout = timeout if timeout is not None else conf.settings['download_timeout'] timeout = timeout if timeout is not None else conf.settings['download_timeout']
parsed_uri = parse_lbry_uri(uri) parsed_uri = parse_lbry_uri(uri)
if parsed_uri.is_channel and not parsed_uri.path: if parsed_uri.is_channel:
raise Exception("cannot download a channel claim, specify a /path") raise Exception("cannot download a channel claim, specify a /path")
resolved = (await self.wallet_manager.resolve(uri)).get(uri, {}) resolved = (await self.wallet_manager.resolve(uri)).get(uri, {})
@ -2310,7 +2310,7 @@ class Daemon(metaclass=JSONRPCServerType):
""" """
try: try:
parsed = parse_lbry_uri(channel_name) parsed = parse_lbry_uri(channel_name)
if not parsed.is_channel: if not parsed.contains_channel:
raise Exception("Cannot make a new channel for a non channel name") raise Exception("Cannot make a new channel for a non channel name")
if parsed.path: if parsed.path:
raise Exception("Invalid channel uri") raise Exception("Invalid channel uri")
@ -2898,7 +2898,7 @@ class Daemon(metaclass=JSONRPCServerType):
for chan_uri in uris: for chan_uri in uris:
try: try:
parsed = parse_lbry_uri(chan_uri) parsed = parse_lbry_uri(chan_uri)
if not parsed.is_channel: if not parsed.contains_channel:
results[chan_uri] = {"error": "%s is not a channel uri" % parsed.name} results[chan_uri] = {"error": "%s is not a channel uri" % parsed.name}
elif parsed.path: elif parsed.path:
results[chan_uri] = {"error": "%s is a claim in a channel" % parsed.path} results[chan_uri] = {"error": "%s is a claim in a channel" % parsed.path}

View file

@ -307,7 +307,7 @@ class LBRYElectrumX(ElectrumX):
return {'error': err.message} return {'error': err.message}
result = {} result = {}
if parsed_uri.is_channel: if parsed_uri.contains_channel:
certificate = None certificate = None
# TODO: this is also done on the else, refactor # TODO: this is also done on the else, refactor

View file

@ -28,7 +28,7 @@ class URI(object):
self.claim_id = claim_id self.claim_id = claim_id
self.path = path self.path = path
if self.path is not None and not self.is_channel: if self.path is not None and not self.contains_channel:
raise ValueError("Content claims cannot have paths") raise ValueError("Content claims cannot have paths")
def __str__(self): def __str__(self):
@ -39,10 +39,21 @@ class URI(object):
if not hasattr(other, prop) or getattr(self, prop) != getattr(other, prop): if not hasattr(other, prop) or getattr(self, prop) != getattr(other, prop):
return False return False
return self.__class__ == other.__class__ return self.__class__ == other.__class__
@property
def channel_name(self):
return self.name if self.contains_channel else None
@property
def claim_name(self):
return self.name if not self.contains_channel else self.path
@property
def contains_channel(self):
return self.name.startswith(CHANNEL_CHAR)
@property @property
def is_channel(self): def is_channel(self):
return self.name.startswith(CHANNEL_CHAR) return self.contains_channel and not self.path
def to_uri_string(self): def to_uri_string(self):
uri_string = PROTOCOL + "%s" % self.name uri_string = PROTOCOL + "%s" % self.name

View file

@ -25,24 +25,25 @@ from lbrynet.schema.address import decode_address, encode_address
parsed_uri_matches = [ parsed_uri_matches = [
("test", URI("test"), False), ("test", URI("test"), False, False, "test", None),
("test#%s" % claim_id_1, URI("test", claim_id=claim_id_1), False), ("test#%s" % claim_id_1, URI("test", claim_id=claim_id_1), False, False, "test", None),
("test:1", URI("test", claim_sequence=1), False), ("test:1", URI("test", claim_sequence=1), False, False, "test", None),
("test$1", URI("test", bid_position=1), False), ("test$1", URI("test", bid_position=1), False, False, "test", None),
("lbry://test", URI("test"), False), ("lbry://test", URI("test"), False, False, "test", None),
("lbry://test#%s" % claim_id_1, URI("test", claim_id=claim_id_1), False), ("lbry://test#%s" % claim_id_1, URI("test", claim_id=claim_id_1), False, False, "test", None),
("lbry://test:1", URI("test", claim_sequence=1), False), ("lbry://test:1", URI("test", claim_sequence=1), False, False, "test", None),
("lbry://test$1", URI("test", bid_position=1), False), ("lbry://test$1", URI("test", bid_position=1), False, False, "test", None),
("@test", URI("@test"), True), ("@test", URI("@test"), True, True, None, "@test"),
("@test#%s" % claim_id_1, URI("@test", claim_id=claim_id_1), True), ("@test#%s" % claim_id_1, URI("@test", claim_id=claim_id_1), True, True, None, "@test"),
("@test:1", URI("@test", claim_sequence=1), True), ("@test:1", URI("@test", claim_sequence=1), True, True, None, "@test"),
("@test$1", URI("@test", bid_position=1), True), ("@test$1", URI("@test", bid_position=1), True, True, None, "@test"),
("lbry://@test1:1/fakepath", URI("@test1", claim_sequence=1, path="fakepath"), True), ("lbry://@test1:1/fakepath", URI("@test1", claim_sequence=1, path="fakepath"), True, False, "fakepath", "@test1"),
("lbry://@test1$1/fakepath", URI("@test1", bid_position=1, path="fakepath"), True), ("lbry://@test1$1/fakepath", URI("@test1", bid_position=1, path="fakepath"), True, False, "fakepath", "@test1"),
("lbry://@test1#abcdef/fakepath", URI("@test1", claim_id="abcdef", path="fakepath"), True), ("lbry://@test1#abcdef/fakepath", URI("@test1", claim_id="abcdef", path="fakepath"), True, False, "fakepath",
("@z", URI("@z"), True), "@test1"),
("@yx", URI("@yx"), True), ("@z", URI("@z"), True, True, None, "@z"),
("@abc", URI("@abc"), True) ("@yx", URI("@yx"), True, True, None, "@yx"),
("@abc", URI("@abc"), True, True, None, "@abc")
] ]
parsed_uri_raises = [ parsed_uri_raises = [
@ -89,16 +90,25 @@ class TestURIParser(UnitTest):
self.longMessage = True self.longMessage = True
def test_uri_parse(self): def test_uri_parse(self):
for test_string, expected_uri_obj, is_channel in parsed_uri_matches: for test_string, expected_uri_obj, contains_channel, is_channel, claim_name, channel_name in parsed_uri_matches:
try: try:
# string -> URI # string -> URI
self.assertEqual(URI.from_uri_string(test_string), expected_uri_obj, test_string) self.assertEqual(URI.from_uri_string(test_string), expected_uri_obj, test_string)
# URI -> dict -> URI # URI -> dict -> URI
self.assertEqual(URI.from_dict(expected_uri_obj.to_dict()), expected_uri_obj, self.assertEqual(URI.from_dict(expected_uri_obj.to_dict()), expected_uri_obj,
test_string) test_string)
# contains_channel
self.assertEqual(URI.from_uri_string(test_string).contains_channel, contains_channel,
test_string)
# is_channel # is_channel
self.assertEqual(URI.from_uri_string(test_string).is_channel, is_channel, self.assertEqual(URI.from_uri_string(test_string).is_channel, is_channel,
test_string) test_string)
# claim_name
self.assertEqual(URI.from_uri_string(test_string).claim_name, claim_name,
test_string)
# channel_name
self.assertEqual(URI.from_uri_string(test_string).channel_name, channel_name,
test_string)
# convert-to-string test only works if protocol is present in test_string # convert-to-string test only works if protocol is present in test_string
if test_string.startswith('lbry://'): if test_string.startswith('lbry://'):