Skip to content

Commit

Permalink
Polish PR
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Mar 14, 2023
1 parent 89e7ab1 commit 759f276
Show file tree
Hide file tree
Showing 23 changed files with 145 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from tribler.core.components.metadata_store.db.store import MetadataStore
from tribler.core.utilities.notifier import Notifier
from tribler.core.utilities.pony_utils import run_threaded
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus
from tribler.core.utilities.unicode import hexlify

PROCESS_CHANNEL_DIR = 1
Expand Down Expand Up @@ -107,7 +107,7 @@ async def regenerate_channel_torrent(self, channel_pk, channel_id):

async def check_and_regen_personal_channel_torrent(self, channel_pk, channel_id, channel_download, timeout=60):
try:
await wait_for(channel_download.wait_for_status(Status.DLSTATUS_SEEDING), timeout=timeout)
await wait_for(channel_download.wait_for_status(DownloadStatus.SEEDING), timeout=timeout)
except asyncio.TimeoutError:
self._logger.warning("Time out waiting for personal channel %s %i to seed", hexlify(channel_pk), channel_id)
await self.regenerate_channel_torrent(channel_pk, channel_id)
Expand Down Expand Up @@ -200,7 +200,7 @@ def check_channels_updates(self):
channel.timestamp,
)
self.download_channel(channel)
elif status == Status.DLSTATUS_SEEDING:
elif status == DownloadStatus.SEEDING:
self._logger.info(
"Processing previously downloaded, but unprocessed channel torrent %s ver %i->%i",
channel.dirname,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
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.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus
from tribler.core.utilities.utilities import random_infohash

update_metainfo = None
Expand Down Expand Up @@ -204,7 +204,7 @@ def fake_get_metainfo(infohash, **_):
gigachannel_manager.download_manager.download_exists = lambda _: True

mock_download = MagicMock()
mock_download.get_state.get_status = Status.DLSTATUS_SEEDING
mock_download.get_state.get_status = DownloadStatus.SEEDING

gigachannel_manager.download_manager.get_download = lambda _: mock_download

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from tribler.core.utilities.notifier import Notifier
from tribler.core.utilities.osutils import fix_filebasename
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import DOWNLOAD, Status
from tribler.core.utilities.simpledefs import DOWNLOAD, DownloadStatus
from tribler.core.utilities.unicode import ensure_unicode, hexlify
from tribler.core.utilities.utilities import bdecode_compat

Expand Down Expand Up @@ -436,7 +436,7 @@ def update_lt_status(self, lt_status: lt.torrent_status):

def _stop_if_finished(self):
state = self.get_state()
if state.get_status() == Status.DLSTATUS_SEEDING:
if state.get_status() == DownloadStatus.SEEDING:
mode = self.download_defaults.seeding_mode
seeding_ratio = self.download_defaults.seeding_ratio
seeding_time = self.download_defaults.seeding_time
Expand Down Expand Up @@ -475,7 +475,7 @@ def move_storage(self, new_dir: Path):
@check_handle()
def force_recheck(self):
if not isinstance(self.tdef, TorrentDefNoMetainfo):
if self.get_state().get_status() == Status.DLSTATUS_STOPPED:
if self.get_state().get_status() == DownloadStatus.STOPPED:
self.pause_after_next_hashcheck = True
self.checkpoint_after_next_hashcheck = True
self.handle.resume()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
url_to_path,
)
from tribler.core.utilities.simpledefs import MAX_LIBTORRENT_RATE_LIMIT, STATEDIR_CHECKPOINT_DIR, \
Status
DownloadStatus
from tribler.core.utilities.unicode import hexlify
from tribler.core.utilities.utilities import bdecode_compat, has_bep33_support, parse_magnetlink
from tribler.core.version import version_id
Expand Down Expand Up @@ -839,7 +839,7 @@ async def sesscb_states_callback(self, states_list):
download = ds.get_download()
infohash = download.get_def().get_infohash()

if ds.get_status() == Status.DLSTATUS_SEEDING:
if ds.get_status() == DownloadStatus.SEEDING:
if download.config.get_hops() == 0 and download.config.get_safe_seeding():
# Re-add the download with anonymity enabled
hops = self.download_defaults.number_hops
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import logging

from tribler.core.utilities.simpledefs import (
Status, UPLOAD,
DownloadStatus, UPLOAD,
)

# Map used to convert libtorrent -> Tribler download status
DLSTATUS_MAP = [
Status.DLSTATUS_WAITING4HASHCHECK,
Status.DLSTATUS_HASHCHECKING,
Status.DLSTATUS_METADATA,
Status.DLSTATUS_DOWNLOADING,
Status.DLSTATUS_SEEDING,
Status.DLSTATUS_SEEDING,
Status.DLSTATUS_ALLOCATING_DISKSPACE,
Status.DLSTATUS_HASHCHECKING,
DownloadStatus.WAITING_FOR_HASHCHECK,
DownloadStatus.HASHCHECKING,
DownloadStatus.METADATA,
DownloadStatus.DOWNLOADING,
DownloadStatus.SEEDING,
DownloadStatus.SEEDING,
DownloadStatus.ALLOCATING_DISKSPACE,
DownloadStatus.HASHCHECKING,
]


Expand Down Expand Up @@ -57,17 +57,17 @@ def get_progress(self):
"""
return self.lt_status.progress if self.lt_status else 0

def get_status(self) -> Status:
def get_status(self) -> DownloadStatus:
""" Returns the status of the torrent.
@return DLSTATUS_* """

if self.lt_status:
if self.lt_status.paused:
return Status.DLSTATUS_STOPPED
return DownloadStatus.STOPPED
return DLSTATUS_MAP[self.lt_status.state]
if self.get_error():
return Status.DLSTATUS_STOPPED_ON_ERROR
return Status.DLSTATUS_STOPPED
return DownloadStatus.STOPPED_ON_ERROR
return DownloadStatus.STOPPED

def get_error(self):
""" Returns the Exception that caused the download to be moved to DLSTATUS_STOPPED_ON_ERROR status.
Expand All @@ -80,7 +80,7 @@ def get_current_speed(self, direct):
Returns the current up or download speed.
@return The speed in bytes/s.
"""
if not self.lt_status or self.get_status() not in [Status.DLSTATUS_DOWNLOADING, Status.DLSTATUS_SEEDING]:
if not self.lt_status or self.get_status() not in [DownloadStatus.DOWNLOADING, DownloadStatus.SEEDING]:
return 0
if direct == UPLOAD:
return self.lt_status.upload_rate
Expand All @@ -91,7 +91,7 @@ def get_current_payload_speed(self, direct):
Returns the current up or download payload speed.
@return The speed in bytes/s.
"""
if not self.lt_status or self.get_status() not in [Status.DLSTATUS_DOWNLOADING, Status.DLSTATUS_SEEDING]:
if not self.lt_status or self.get_status() not in [DownloadStatus.DOWNLOADING, DownloadStatus.SEEDING]:
return 0
if direct == UPLOAD:
return self.lt_status.upload_payload_rate
Expand Down Expand Up @@ -130,7 +130,7 @@ def get_num_seeds_peers(self):
Returns the sum of the number of seeds and peers.
@return A tuple (num seeds, num peers)
"""
if not self.lt_status or self.get_status() not in [Status.DLSTATUS_DOWNLOADING, Status.DLSTATUS_SEEDING]:
if not self.lt_status or self.get_status() not in [DownloadStatus.DOWNLOADING, DownloadStatus.SEEDING]:
return 0, 0

total = self.lt_status.list_peers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from asyncio import sleep

from tribler.core.components.libtorrent.utils.torrent_utils import check_vod, get_info_from_handle
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus

# Header and footer sizes are necessary for video client to detect file codecs and muxer metadata.
# Without below pieces are ready, streamer should not start
Expand Down Expand Up @@ -140,7 +140,7 @@ async def enable(self, fileindex=0, prebufpos=None):
# wait until dlstate is downloading or seeding
while True:
status = self.__lt_state().get_status()
if status in [Status.DLSTATUS_DOWNLOADING, Status.DLSTATUS_SEEDING]:
if status in [DownloadStatus.DOWNLOADING, DownloadStatus.SEEDING]:
break
await sleep(1)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import (
DOWNLOAD,
Status, UPLOAD,
DownloadStatus, UPLOAD,
)
from tribler.core.utilities.unicode import ensure_unicode, hexlify
from tribler.core.utilities.utilities import froze_it
Expand Down Expand Up @@ -53,7 +53,7 @@ def _safe_extended_peer_info(ext_peer_info):
return ''.join([chr(c) for c in ext_peer_info])


def get_extended_status(tunnel_community, download) -> Status:
def get_extended_status(tunnel_community, download) -> DownloadStatus:
"""
This function filters the original download status to possibly add tunnel-related status.
Extracted from DownloadState to remove coupling between DownloadState and Tunnels.
Expand All @@ -64,13 +64,13 @@ def get_extended_status(tunnel_community, download) -> Status:
# Nothing to do with tunnels. If stopped - it happened by the user or libtorrent-only reason
stopped_by_user = state.lt_status and state.lt_status.paused

if status == Status.DLSTATUS_STOPPED and not stopped_by_user:
if status == DownloadStatus.STOPPED and not stopped_by_user:
if download.config.get_hops() > 0:
if tunnel_community.get_candidates(PEER_FLAG_EXIT_BT):
return Status.DLSTATUS_CIRCUITS
return DownloadStatus.CIRCUITS
else:
return Status.DLSTATUS_EXIT_NODES
return Status.DLSTATUS_STOPPED
return DownloadStatus.EXIT_NODES
return DownloadStatus.STOPPED
return status


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from tribler.core.components.restapi.rest.rest_manager import error_middleware
from tribler.core.tests.tools.common import TESTS_DATA_DIR
from tribler.core.utilities.rest_utils import HTTP_SCHEME, path_to_url
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus
from tribler.core.utilities.unicode import hexlify


Expand Down Expand Up @@ -72,7 +72,7 @@ def test_get_extended_status_downloading_nohops_nocandidates(mock_extended_statu
"""
Testing whether a non-anonymous download with state is considered "DOWNLOADING" without candidates.
"""
assert mock_extended_status == Status.DLSTATUS_DOWNLOADING
assert mock_extended_status == DownloadStatus.DOWNLOADING


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -82,7 +82,7 @@ def test_get_extended_status_downloading_nohops_candidates(mock_extended_status)
"""
Testing whether a non-anonymous download with state is considered "DOWNLOADING" with candidates.
"""
assert mock_extended_status == Status.DLSTATUS_DOWNLOADING
assert mock_extended_status == DownloadStatus.DOWNLOADING


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -92,7 +92,7 @@ def test_get_extended_status_downloading_hops_nocandidates(mock_extended_status)
"""
Testing whether an anonymous download with state is considered "DOWNLOADING" without candidates.
"""
assert mock_extended_status == Status.DLSTATUS_DOWNLOADING
assert mock_extended_status == DownloadStatus.DOWNLOADING


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -102,7 +102,7 @@ def test_get_extended_status_downloading_hops_candidates(mock_extended_status):
"""
Testing whether an anonymous download with state is considered "DOWNLOADING" with candidates.
"""
assert mock_extended_status == Status.DLSTATUS_DOWNLOADING
assert mock_extended_status == DownloadStatus.DOWNLOADING


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -112,7 +112,7 @@ def test_get_extended_status_stopped(mock_extended_status):
"""
Testing whether a non-anonymous download without state is considered "STOPPED" without candidates.
"""
assert mock_extended_status == Status.DLSTATUS_STOPPED
assert mock_extended_status == DownloadStatus.STOPPED


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -122,7 +122,7 @@ def test_get_extended_status_stopped_hascandidates(mock_extended_status):
"""
Testing whether a non-anonymous download without state is considered "STOPPED" with candidates.
"""
assert mock_extended_status == Status.DLSTATUS_STOPPED
assert mock_extended_status == DownloadStatus.STOPPED


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -132,7 +132,7 @@ def test_get_extended_status_exit_nodes(mock_extended_status):
"""
Testing whether an anonymous download without state is considered looking for "EXIT_NODES" without candidates.
"""
assert mock_extended_status == Status.DLSTATUS_EXIT_NODES
assert mock_extended_status == DownloadStatus.EXIT_NODES


@pytest.mark.parametrize("mock_extended_status",
Expand All @@ -142,7 +142,7 @@ def test_get_extended_status_circuits(mock_extended_status):
"""
Testing whether an anonymous download without state is considered looking for "CIRCUITS" with candidates.
"""
assert mock_extended_status == Status.DLSTATUS_CIRCUITS
assert mock_extended_status == DownloadStatus.CIRCUITS


async def test_get_downloads_if_checkpoints_are_not_loaded(mock_dlmgr, rest_api):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@
from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.tests.tools.common import TORRENT_UBUNTU_FILE
from tribler.core.utilities.rest_utils import path_to_url
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus


async def test_download_torrent_from_url(tmp_path, file_server, download_manager):
# Setup file server to serve torrent file
shutil.copyfile(TORRENT_UBUNTU_FILE, tmp_path / "ubuntu.torrent")
download = await download_manager.start_download_from_uri(f'http://localhost:{file_server}/ubuntu.torrent',
config=DownloadConfig())
await download.wait_for_status(Status.DLSTATUS_DOWNLOADING)
await download.wait_for_status(DownloadStatus.DOWNLOADING)


async def test_download_torrent_from_file(download_manager):
uri = path_to_url(TORRENT_UBUNTU_FILE)
d = await download_manager.start_download_from_uri(uri, config=DownloadConfig())
await d.wait_for_status(Status.DLSTATUS_DOWNLOADING)
await d.wait_for_status(DownloadStatus.DOWNLOADING)


async def test_download_torrent_from_file_with_escaped_characters(download_manager, tmp_path):
destination = tmp_path / 'ubuntu%20%21 15.04.torrent'
shutil.copyfile(TORRENT_UBUNTU_FILE, destination)
uri = path_to_url(destination)
d = await download_manager.start_download_from_uri(uri, config=DownloadConfig())
await d.wait_for_status(Status.DLSTATUS_DOWNLOADING)
await d.wait_for_status(DownloadStatus.DOWNLOADING)
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from tribler.core.components.libtorrent.torrentdef import TorrentDef, TorrentDefNoMetainfo
from tribler.core.tests.tools.common import TESTS_DATA_DIR, TORRENT_UBUNTU_FILE
from tribler.core.utilities.path_util import Path
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus
from tribler.core.utilities.unicode import hexlify


Expand All @@ -32,7 +32,7 @@ def create_fake_download_and_state():
fake_download.shutdown = lambda: succeed(None)
dl_state = MagicMock()
dl_state.get_infohash = lambda: b'aaaa'
dl_state.get_status = lambda: Status.DLSTATUS_SEEDING
dl_state.get_status = lambda: DownloadStatus.SEEDING
dl_state.get_download = lambda: fake_download
fake_config = MagicMock()
fake_config.get_hops = lambda: 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

from tribler.core.components.libtorrent.download_manager.download_state import DownloadState
from tribler.core.utilities.simpledefs import DOWNLOAD, Status, UPLOAD
from tribler.core.utilities.simpledefs import DOWNLOAD, DownloadStatus, UPLOAD


@pytest.fixture
Expand Down Expand Up @@ -45,7 +45,7 @@ def test_getters_setters_2(mock_download, mock_lt_status):
"""
download_state = DownloadState(mock_download, mock_lt_status, None)

assert download_state.get_status() == Status.DLSTATUS_DOWNLOADING
assert download_state.get_status() == DownloadStatus.DOWNLOADING
assert download_state.get_current_speed(UPLOAD) == 123
assert download_state.get_current_speed(DOWNLOAD) == 43
assert download_state.get_total_transferred(UPLOAD) == 100
Expand Down
4 changes: 2 additions & 2 deletions src/tribler/core/components/libtorrent/tests/test_seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from tribler.core.components.libtorrent.download_manager.download_config import DownloadConfig
from tribler.core.tests.tools.common import TESTS_DATA_DIR
from tribler.core.utilities.simpledefs import Status
from tribler.core.utilities.simpledefs import DownloadStatus


async def test_seeding(download_manager, video_seeder, video_tdef, tmp_path):
Expand All @@ -18,7 +18,7 @@ async def test_seeding(download_manager, video_seeder, video_tdef, tmp_path):
dscfg.set_dest_dir(tmp_path)
download = download_manager.start_download(tdef=video_tdef, config=dscfg)
download.add_peer(("127.0.0.1", video_seeder.libtorrent_port))
await download.wait_for_status(Status.DLSTATUS_SEEDING)
await download.wait_for_status(DownloadStatus.SEEDING)

with open(tmp_path / "video.avi", "rb") as f:
realdata = f.read()
Expand Down
Loading

0 comments on commit 759f276

Please sign in to comment.