Skip to content

Commit

Permalink
Merge pull request #7466 from Solomon1732/f-extract-lambdas-into-func…
Browse files Browse the repository at this point in the history
…tions

Replace lambdas with function calls
  • Loading branch information
drew2a committed Jan 15, 2024
2 parents ef54a97 + 896284a commit 7e520b3
Show file tree
Hide file tree
Showing 15 changed files with 339 additions and 140 deletions.
12 changes: 6 additions & 6 deletions .sonar/analyse.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# These env varialble should be set by Jenkins.
SERVER_URL = os.environ.get('SONAR_SERVER_URL', "https://sonarcloud.io")
PROJECT_KEY = os.environ.get('PROJECT_KEY', "org.sonarqube:tribler")
PR_COMMIT = os.environ.get('ghprbActualCommit', u'')
PR_COMMIT = os.environ.get('ghprbActualCommit', '')
TASK_PATH = os.path.join(os.environ.get('WORKSPACE', os.getcwd()), '.scannerwork', 'report-task.txt')

task_status_url = None
Expand Down Expand Up @@ -56,12 +56,12 @@
json_response = requests.get(pr_analysis_url)
data = json.loads(json_response.text)

for pull_request in data[u'pullRequests']:
print("Matching analysis:", pull_request[u'key'], PR_COMMIT, pull_request[u'key'] == PR_COMMIT)
for pull_request in data['pullRequests']:
print("Matching analysis:", pull_request['key'], PR_COMMIT, pull_request['key'] == PR_COMMIT)
# If there is analysis result for the PR commit with status OK, then exit with success status (0)
if pull_request[u'key'] == PR_COMMIT:
print("Quality Gate:", pull_request[u'status'])
if pull_request[u'status'][u'qualityGateStatus'] == u'OK':
if pull_request['key'] == PR_COMMIT:
print("Quality Gate:", pull_request['status'])
if pull_request['status']['qualityGateStatus'] == 'OK':
print("Status: OK")
break
else:
Expand Down
31 changes: 16 additions & 15 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
logging.basicConfig(level=logging.INFO)
logging.info('Start to execute conf.py')

# root_dir = Path(__file__).parent.parent

root_dir = os.path.abspath(os.path.join(os.path.dirname(__name__), '..'))
tribler_source_dirs = [
os.path.join(root_dir, "src"),
Expand All @@ -48,9 +46,12 @@
with patch_import(modules):
from tribler.core.components.restapi.rest.root_endpoint import RootEndpoint

add_endpoint = RootEndpoint.add_endpoint
RootEndpoint.add_endpoint = lambda self, path, ep: add_endpoint(self, path, ep) \
if path not in ['/ipv8', '/market', '/wallets'] else None
original_add_endpoint = RootEndpoint.add_endpoint
def _add_endpoint(self, path, ep):
if path in {'/ipv8', '/market', '/wallets'}:
return None
return original_add_endpoint(self, path, ep)
RootEndpoint.add_endpoint = _add_endpoint

# Extract Swagger docs
from extract_swagger import extract_swagger
Expand Down Expand Up @@ -93,18 +94,18 @@
master_doc = 'index'

# General information about the project.
project = u'Tribler'
copyright = u'2020, Tribler devs'
author = u'Tribler devs'
project = 'Tribler'
copyright = '2020, Tribler devs'
author = 'Tribler devs'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = u'7.5'
version = '7.5'
# The full version, including alpha/beta/rc tags.
release = u'7.5.0'
release = '7.5.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down Expand Up @@ -177,7 +178,7 @@
# The name for this set of Sphinx documents.
# "<project> v<release> documentation" by default.
#
# html_title = u'Tribler v6.6'
# html_title = 'Tribler v6.6'

# A shorter title for the navigation bar. Default is the same as html_title.
#
Expand Down Expand Up @@ -303,8 +304,8 @@
# (source start file, target name, title,
# author, documentclass [howto, manual, or own class]).
latex_documents = [
(master_doc, 'Tribler.tex', u'Tribler Documentation',
u'Tribler devs', 'manual'),
(master_doc, 'Tribler.tex', 'Tribler Documentation',
'Tribler devs', 'manual'),
]

# The name of an image file (relative to this directory) to place at the top of
Expand Down Expand Up @@ -339,7 +340,7 @@
# One entry per manual page. List of tuples
# (source start file, name, description, authors, manual section).
man_pages = [
(master_doc, 'tribler', u'Tribler Documentation',
(master_doc, 'tribler', 'Tribler Documentation',
[author], 1)
]

Expand All @@ -354,7 +355,7 @@
# (source start file, target name, title, author,
# dir menu entry, description, category)
texinfo_documents = [
(master_doc, 'Tribler', u'Tribler Documentation',
(master_doc, 'Tribler', 'Tribler Documentation',
author, 'Tribler', 'One line description of project.',
'Miscellaneous'),
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import operator
import time
import typing
from binascii import unhexlify
Expand Down Expand Up @@ -240,7 +241,7 @@ def build_snippets(self, search_results: typing.List[typing.Dict]) -> typing.Lis

# Sort the search results within each snippet by the number of seeders
for torrents_list in content_to_torrents.values():
torrents_list.sort(key=lambda x: x["num_seeders"], reverse=True)
torrents_list.sort(key=operator.itemgetter("num_seeders"), reverse=True)

# Determine the most popular content items - this is the one we show
sorted_content_info = list(content_to_torrents.items())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,38 @@ def __init__(self,
def libtorrent_port(self):
return self._libtorrent_port

@staticmethod
def convert_rate(rate: int) -> int:
"""
Rate conversion due to the fact that we had a different system with Swift
and the old python BitTorrent core: unlimited == 0, stop == -1, else rate in kbytes
"""
if rate == 0:
return -1
if rate == -1:
return 1
return rate * 1024

@staticmethod
def reverse_convert_rate(rate: int) -> int:
"""
Rate conversion due to the fact that we had a different system with Swift
and the old python BitTorrent core: unlimited == 0, stop == -1, else rate in kbytes
"""
if rate == -1:
return 0
if rate == 1:
return -1
return rate // 1024

async def _check_dht_ready(self, min_dht_peers=60):
# Checks whether we got enough DHT peers. If the number of DHT peers is low,
# checking for a bunch of torrents in a short period of time may result in several consecutive requests
# sent to the same peers. This can trigger those peers' flood protection mechanism,
# which results in DHT checks stuck for hours.
# See https://github.com/Tribler/tribler/issues/5319
"""
Checks whether we got enough DHT peers. If the number of DHT peers is low,
checking for a bunch of torrents in a short period of time may result in several consecutive requests
sent to the same peers. This can trigger those peers' flood protection mechanism,
which results in DHT checks stuck for hours.
See https://github.com/Tribler/tribler/issues/5319
"""
while not (self.get_session() and self.get_session().status().dht_nodes > min_dht_peers):
await asyncio.sleep(1)

Expand Down Expand Up @@ -248,11 +274,11 @@ def create_session(self, hops=0, store_listen_port=True):
self._logger.info(f'Dummy mode: {self.dummy_mode}. Hops: {hops}.')

# Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
fingerprint = ['TL'] + [int(x) for x in version_id.split('-')[0].split('.')] + [0]
fingerprint = ['TL', *map(int, version_id.split('-')[0].split('.')), 0]
if self.dummy_mode:
from unittest.mock import Mock
ltsession = Mock()
ltsession.pop_alerts = lambda: {}
ltsession.pop_alerts = dict
ltsession.listen_port = lambda: 123
ltsession.get_settings = lambda: {"peer_fingerprint": "000"}
else:
Expand All @@ -268,10 +294,10 @@ def create_session(self, hops=0, store_listen_port=True):
settings['enable_incoming_utp'] = enable_utp

settings['prefer_rc4'] = True
settings["listen_interfaces"] = "0.0.0.0:%d" % libtorrent_port
settings["listen_interfaces"] = f"0.0.0.0:{libtorrent_port}"

settings['peer_fingerprint'] = self.peer_mid
settings['handshake_client_version'] = 'Tribler/' + version_id + '/' + hexlify(self.peer_mid)
settings['handshake_client_version'] = f"Tribler/{version_id}/{hexlify(self.peer_mid)}"
else:
settings['enable_outgoing_utp'] = True
settings['enable_incoming_utp'] = True
Expand All @@ -281,7 +307,7 @@ def create_session(self, hops=0, store_listen_port=True):
settings['force_proxy'] = True

# Anon listen port is never used anywhere, so we let Libtorrent set it
# settings["listen_interfaces"] = "0.0.0.0:%d" % anon_port
# settings["listen_interfaces"] = f"0.0.0.0:{anon_port}"

# By default block all IPs except 1.1.1.1 (which is used to ensure libtorrent makes a connection to us)
self.update_ip_filter(ltsession, ['1.1.1.1'])
Expand Down Expand Up @@ -363,7 +389,7 @@ def set_max_connections(self, conns, hops=None):
def set_upload_rate_limit(self, rate, hops=None):
# Rate conversion due to the fact that we had a different system with Swift
# and the old python BitTorrent core: unlimited == 0, stop == -1, else rate in kbytes
libtorrent_rate = int(-1 if rate == 0 else (1 if rate == -1 else rate * 1024))
libtorrent_rate = self.convert_rate(rate=rate)

# Pass outgoing_port and num_outgoing_ports to dict due to bug in libtorrent 0.16.18
settings_dict = {'upload_rate_limit': libtorrent_rate, 'outgoing_port': 0, 'num_outgoing_ports': 1}
Expand All @@ -374,19 +400,19 @@ def get_upload_rate_limit(self, hops=None):
# Rate conversion due to the fact that we had a different system with Swift
# and the old python BitTorrent core: unlimited == 0, stop == -1, else rate in kbytes
libtorrent_rate = self.get_session(hops).upload_rate_limit()
return 0 if libtorrent_rate == -1 else (-1 if libtorrent_rate == 1 else libtorrent_rate / 1024)
return self.reverse_convert_rate(rate=libtorrent_rate)

def set_download_rate_limit(self, rate, hops=None):
libtorrent_rate = int(-1 if rate == 0 else (1 if rate == -1 else rate * 1024))
libtorrent_rate = self.convert_rate(rate=rate)

# Pass outgoing_port and num_outgoing_ports to dict due to bug in libtorrent 0.16.18
settings_dict = {'download_rate_limit': libtorrent_rate}
for session in self.ltsessions.values():
self.set_session_settings(session, settings_dict)

def get_download_rate_limit(self, hops=0):
libtorrent_rate = self.get_session(hops).download_rate_limit()
return 0 if libtorrent_rate == -1 else (-1 if libtorrent_rate == 1 else libtorrent_rate / 1024)
libtorrent_rate = self.get_session(hops=hops).download_rate_limit()
return self.reverse_convert_rate(rate=libtorrent_rate)

def process_alert(self, alert, hops=0):
alert_type = alert.__class__.__name__
Expand Down Expand Up @@ -791,7 +817,7 @@ def update_trackers(self, infohash, trackers):
old_def = download.get_def()
old_trackers = old_def.get_trackers()
new_trackers = list(set(trackers) - old_trackers)
all_trackers = list(old_trackers) + new_trackers
all_trackers = [*old_trackers, *new_trackers]

if new_trackers:
# Add new trackers to the download
Expand Down

0 comments on commit 7e520b3

Please sign in to comment.