Skip to content

Commit

Permalink
Merge pull request #6594 from drew2a/refactoring/random_utils
Browse files Browse the repository at this point in the history
Removing random_utils.py
  • Loading branch information
drew2a authored Nov 30, 2021
2 parents 563ab49 + ee9d3e1 commit a77ee44
Show file tree
Hide file tree
Showing 19 changed files with 51 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
from tribler_core.components.metadata_store.utils import RequestTimeoutException
from tribler_core.notifier import Notifier
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash

EMPTY_BLOB = b""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@

from tribler_common.simpledefs import DLSTATUS_SEEDING

from tribler_core.components.libtorrent.torrentdef import TorrentDef
from tribler_core.components.gigachannel_manager.gigachannel_manager import GigaChannelManager
from tribler_core.components.libtorrent.torrentdef import TorrentDef
from tribler_core.components.metadata_store.db.orm_bindings.channel_node import NEW
from tribler_core.tests.tools.base_test import MockObject
from tribler_core.tests.tools.common import TORRENT_UBUNTU_FILE
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash

update_metainfo = None

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
HealthItemsPayload,
)
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import random_infohash

CHANNEL_DIR_NAME_PK_LENGTH = 32 # Its not 40 so it could be distinguished from infohash
CHANNEL_DIR_NAME_ID_LENGTH = 16 # Zero-padded long int in hex form
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
COLLECTION_NODE,
CollectionNodePayload,
)
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash


# pylint: disable=too-many-statements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
from datetime import datetime
from unittest.mock import patch

import pytest
from ipv8.keyvault.crypto import default_eccrypto

from pony.orm import db_session

from ipv8.keyvault.crypto import default_eccrypto
from tribler_core.components.metadata_store.remote_query_community.payload_checker import ObjState, ProcessingResult
from tribler_core.components.metadata_store.db.orm_bindings.channel_metadata import CHANNEL_DIR_NAME_LENGTH, \
entries_to_chunk
import pytest

from tribler_core.components.metadata_store.db.orm_bindings.channel_metadata import (
CHANNEL_DIR_NAME_LENGTH,
entries_to_chunk,
)
from tribler_core.components.metadata_store.db.orm_bindings.channel_node import NEW
from tribler_core.components.metadata_store.db.serialization import (
CHANNEL_TORRENT,
Expand All @@ -22,11 +25,11 @@
UnknownBlobTypeException,
int2time,
)
from tribler_core.components.metadata_store.remote_query_community.payload_checker import ObjState, ProcessingResult
from tribler_core.components.metadata_store.tests.test_channel_download import CHANNEL_METADATA_UPDATED
from tribler_core.tests.tools.common import TESTS_DATA_DIR
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash

from tribler_core.utilities.utilities import random_infohash

# pylint: disable=protected-access,unused-argument

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tribler_core.components.metadata_store.db.orm_bindings.torrent_metadata import tdef_to_metadata_dict
from tribler_core.components.metadata_store.db.serialization import CHANNEL_TORRENT, REGULAR_TORRENT
from tribler_core.tests.tools.common import TORRENT_UBUNTU_FILE
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash

EMPTY_BLOB = b""

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import random
import string
from asyncio import sleep
from binascii import unhexlify
from datetime import datetime
Expand All @@ -25,12 +26,16 @@
)
from tribler_core.components.metadata_store.remote_query_community.settings import RemoteQueryCommunitySettings
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash, random_string
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import random_infohash

# pylint: disable=protected-access


def random_string():
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=100))


def add_random_torrent(metadata_cls, name="test", channel=None, seeders=None, leechers=None, last_check=None):
d = {"infohash": random_infohash(), "title": name, "tags": "", "size": 1234, "status": NEW}
if channel:
Expand Down Expand Up @@ -235,7 +240,7 @@ async def test_remote_select_torrents(self):
mds1 = self.nodes[1].overlay.mds

with db_session:
chan = mds0.ChannelMetadata.create_channel(random_string(100), "")
chan = mds0.ChannelMetadata.create_channel(random_string(), "")
torrent_infohash = random_infohash()
torrent = mds0.TorrentMetadata(origin_id=chan.id_, infohash=torrent_infohash, title='title1')
torrent.sign()
Expand Down Expand Up @@ -286,7 +291,7 @@ async def test_remote_select_packets_limit(self):

with db_session:
for _ in range(0, 100):
mds0.ChannelMetadata.create_channel(random_string(100), "")
mds0.ChannelMetadata.create_channel(random_string(), "")

peer = self.nodes[0].my_peer
kwargs_dict = {"metadata_type": [CHANNEL_TORRENT]}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

from tribler_core.components.metadata_store.db.orm_bindings.channel_node import NEW
from tribler_core.components.metadata_store.utils import tag_torrent
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash


@pytest.fixture
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
from tribler_core.components.restapi.rest.base_api_test import do_request
from tribler_core.components.restapi.rest.rest_manager import error_middleware
from tribler_core.tests.tools.common import TORRENT_UBUNTU_FILE
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import random_infohash

PNG_DATA = unhexlify(
"89504e470d0a1a0a0000000d494844520"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
from tribler_core.components.restapi.rest.rest_manager import error_middleware
from tribler_core.components.torrent_checker.torrent_checker.torrent_checker import TorrentChecker
from tribler_core.config.tribler_config import TriblerConfig
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import has_bep33_support
from tribler_core.utilities.utilities import has_bep33_support, random_infohash


# pylint: disable=unused-argument

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
from tribler_core.components.metadata_store.restapi.remote_query_endpoint import RemoteQueryEndpoint
from tribler_core.components.restapi.rest.base_api_test import do_request
from tribler_core.components.restapi.rest.rest_manager import error_middleware
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.unicode import hexlify
from tribler_core.utilities.utilities import random_infohash

# pylint: disable=unused-argument

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from tribler_core.components.metadata_store.restapi.search_endpoint import SearchEndpoint
from tribler_core.components.restapi.rest.base_api_test import do_request
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash


# pylint: disable=unused-argument, redefined-outer-name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
)
from tribler_core.components.metadata_store.db.store import HealthItemsPayload
from tribler_core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash


# pylint: disable=protected-access

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tribler_core.components.tag.community.tag_payload import TagOperation
from tribler_core.components.tag.db.tag_db import TagDatabase, TagOperationEnum
from tribler_core.tests.tools.common import PNG_FILE
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash

# Some random keys used for generating tags.
random_key_1 = default_eccrypto.generate_key('low')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from tribler_core.components.popularity.community.popularity_community import PopularityCommunity
from tribler_core.tests.tools.base_test import MockObject
from tribler_core.utilities.path_util import Path
from tribler_core.utilities.random_utils import random_infohash
from tribler_core.utilities.utilities import random_infohash


class TestPopularityCommunity(TestBase):
Expand Down
45 changes: 0 additions & 45 deletions src/tribler-core/tribler_core/utilities/random_utils.py

This file was deleted.

30 changes: 0 additions & 30 deletions src/tribler-core/tribler_core/utilities/tests/test_random_utils.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
is_simple_match_query,
is_valid_url,
parse_magnetlink,
random_infohash,
)


Expand Down Expand Up @@ -90,3 +91,9 @@ def test_is_channel_public_key():

not_hex = "APPLE6CF85A85CEEDB8ADC4B96CF85A85CEEDB8A"
assert not is_channel_public_key(not_hex)


def test_random_infohash():
test_infohash = random_infohash()
assert isinstance(test_infohash, bytes)
assert len(test_infohash) == 20
9 changes: 8 additions & 1 deletion src/tribler-core/tribler_core/utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""
import binascii
import logging
import random
import re
from base64 import b32decode
from functools import wraps
Expand Down Expand Up @@ -37,6 +38,7 @@ def init_decorator(func):
def wrapper(self, *args, **kwargs):
func(self, *args, **kwargs)
self.__frozen = True

return wrapper

cls.__setattr__ = frozensetattr
Expand All @@ -60,7 +62,7 @@ def is_valid_url(url):
url = url.lower().replace('udp', 'http', 1)
split_url = urlsplit(url)

return not(split_url[0] == '' or split_url[1] == '')
return not (split_url[0] == '' or split_url[1] == '')


def parse_magnetlink(url):
Expand Down Expand Up @@ -162,3 +164,8 @@ def bdecode_compat(packet_buffer):
return lt.bdecode(packet_buffer)
except RuntimeError:
return None


def random_infohash(random_gen=None):
r = random_gen or random
return r.getrandbits(20 * 8).to_bytes(20, byteorder='big')

0 comments on commit a77ee44

Please sign in to comment.