Skip to content

Commit

Permalink
cleaning code for pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
ardhipoetra committed May 10, 2016
1 parent cdcbc00 commit b4fa461
Show file tree
Hide file tree
Showing 12 changed files with 752 additions and 731 deletions.
3 changes: 2 additions & 1 deletion Tribler/Core/APIImplementation/LaunchManyCore.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ def load_communities():

self.initComplete = True

def add(self, tdef, dscfg, pstate=None, initialdlstatus=None, setupDelay=0, hidden=False, share_mode=False,checkpoint_disabled=False):
def add(self, tdef, dscfg, pstate=None, initialdlstatus=None, setupDelay=0, hidden=False,
share_mode=False, checkpoint_disabled=False):
""" Called by any thread """
d = None
with self.sesslock:
Expand Down
17 changes: 10 additions & 7 deletions Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ def __init__(self, session, tdef):
self.checkpoint_disabled = False

def __str__(self):
return "LibtorrentDownloadImpl <name: '%s' hops: %d checkpoint_disabled: %d>" % (self.correctedinfoname, self.get_hops(), self.checkpoint_disabled)
return "LibtorrentDownloadImpl <name: '%s' hops: %d checkpoint_disabled: %d>" % \
(self.correctedinfoname, self.get_hops(), self.checkpoint_disabled)

def __repr__(self):
return self.__str__()
Expand All @@ -161,7 +162,7 @@ def set_checkpoint_disabled(self, val=True):
def get_checkpoint_disabled(self):
return self.checkpoint_disabled

def setup(self, dcfg=None, pstate=None, initialdlstatus=None, lm_network_engine_wrapper_created_callback=None,
def setup(self, dcfg=None, pstate=None, initialdlstatus=None,
wrapperDelay=0, share_mode=False, checkpoint_disabled=False):
"""
Create a Download object. Used internally by Session.
Expand Down Expand Up @@ -197,7 +198,8 @@ def setup(self, dcfg=None, pstate=None, initialdlstatus=None, lm_network_engine_

def schedule_create_engine():
self.cew_scheduled = True
create_engine_wrapper_deferred = self.network_create_engine_wrapper(self.pstate_for_restart, initialdlstatus, share_mode=share_mode)
create_engine_wrapper_deferred = self.network_create_engine_wrapper(
self.pstate_for_restart, initialdlstatus, share_mode=share_mode)
create_engine_wrapper_deferred.chainDeferred(deferred)


Expand Down Expand Up @@ -249,7 +251,7 @@ def do_check():
do_check()
return can_create_deferred

def network_create_engine_wrapper(self, pstate, initialdlstatus=None, share_mode=False,checkpoint_disabled=False):
def network_create_engine_wrapper(self, pstate, initialdlstatus=None, share_mode=False, checkpoint_disabled=False):
with self.dllock:
self._logger.debug("LibtorrentDownloadImpl: network_create_engine_wrapper()")

Expand Down Expand Up @@ -300,9 +302,6 @@ def network_create_engine_wrapper(self, pstate, initialdlstatus=None, share_mode

if has_resume_data:
atp["resume_data"] = lt.bencode(resume_data)
if not share_mode:
self._logger.info("%s %s", self.tdef.get_name_as_unicode(), dict((k, v)
for k, v in resume_data.iteritems() if k not in ['pieces', 'piece_priority', 'peers']) if has_resume_data else None)
else:
atp["url"] = self.tdef.get_url() or "magnet:?xt=urn:btih:" + hexlify(self.tdef.get_infohash())
atp["name"] = self.tdef.get_name_as_unicode()
Expand Down Expand Up @@ -823,6 +822,10 @@ def network_get_vod_stats(self):

@staticmethod
def create_peerlist_data(peer_info):
"""
A function to convert peer_info libtorrent object into dictionary
This data is used to identify peers with combination of several flags
"""
peer_dict = {}

peer_dict['id'] = peer_info.pid
Expand Down
2 changes: 1 addition & 1 deletion Tribler/Core/TorrentChecker/torrent_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ def _create_session_for_request(self, infohash, tracker_url):
break

#TODO(ardhi) : quickfix for etree.org can't handle multiple infohash in single call
if session.can_add_request() and "etree" not in session.tracker_url :
if session.can_add_request() and "etree" not in session.tracker_url:
session.add_request(infohash)
self._update_pending_response(infohash)
request_handled = True
Expand Down
41 changes: 17 additions & 24 deletions Tribler/Core/Utilities/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,11 @@ def fix_torrent(file_path):

return fixed_data

def translate_peers_into_health(peer_info_dicts, status=None):
# peer_info_dicts is a peer_info dictionary from LibTorrentDownloadImpl.create_peerlist_data
# status is libtorrent torrent status #TODO : unused for now
# purpose : where we want to measure a swarm's health but no tracker can be contacted

def translate_peers_into_health(peer_info_dicts):
"""
peer_info_dicts is a peer_info dictionary from LibTorrentDownloadImpl.create_peerlist_data
purpose : where we want to measure a swarm's health but no tracker can be contacted
"""
num_seeders = 0
num_leech = 0

Expand All @@ -263,37 +263,30 @@ def translate_peers_into_health(peer_info_dicts, status=None):
# collecting some statistics
for p_info in peer_info_dicts:
upload_only_b = False
finished_b = False
interest_in_us_b = False

if p_info['upload_only']:
upload_only+=1
upload_only += 1
upload_only_b = True
if p_info['uinterested']:
interest_in_us+=1
interest_in_us += 1
interest_in_us_b = True

if p_info['completed'] == 1:
finished+=1
finished_b = True
finished += 1
else:
unfinished_able_dl += 1 if upload_only_b else 0

'''
seeders potentials:
1. it's only want uploading right now (upload only)
2. it's finished (we don't know whether it want to upload or not)
leecher potentials:
1. it's interested in our piece
2. it's unfinished but it's not 'upload only' (it can't leech for some reason)
3. it's unfinished (less restrictive)
# seeders potentials:
# 1. it's only want uploading right now (upload only)
# 2. it's finished (we don't know whether it want to upload or not)
# leecher potentials:
# 1. it's interested in our piece
# 2. it's unfinished but it's not 'upload only' (it can't leech for some reason)
# 3. it's unfinished (less restrictive)

make sure to change those description when change the algorithm
'''
# make sure to change those description when change the algorithm

num_seeders = max(upload_only, finished)
num_leech = max(interest_in_us, min(unfinished_able_dl, num_all_peer - finished))
return (num_seeders, num_leech)
return num_seeders, num_leech

138 changes: 80 additions & 58 deletions Tribler/Main/Dialogs/BoostingDialogs.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
# Written by Egbert Bouman
"""
This module contains wx dialog gui for adding/removing boosting source
Written by Egbert Bouman and Ardhi Putra Pratama H
"""


import wx

from Tribler.Main.Utility.GuiDBHandler import startWorker, GUI_PRI_DISPERSY
from Tribler.Main.vwxGUI.GuiUtility import GUIUtility
from Tribler.Policies.BoostingManager import BoostingManager, ChannelSource


class AddBoostingSource(wx.Dialog):
"""
Class for adding the source for credit mining
"""

def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, 'Add boosting source', size=(275, 275), name="AddBoostingSourceDialog")
wx.Dialog.__init__(self, parent, -1, 'Add boosting source', size=(475, 275), name="AddBoostingSourceDialog")

self.guiutility = GUIUtility.getInstance()
self.channels = []
self.source = ''

Expand All @@ -25,61 +31,71 @@ def __init__(self, parent):
self.rss_dir_radio = wx.RadioButton(self, -1, 'Torrents local directory:')
self.rss_dir_edit = wx.TextCtrl(self, -1)
self.rss_dir_edit.Bind(wx.EVT_TEXT, lambda evt: self.rss_dir_radio.SetValue(True))
self.rss_dir_edit.Bind(wx.EVT_LEFT_DOWN, self.OnDir)
self.rss_dir_edit.Bind(wx.EVT_LEFT_DOWN, self.on_open_dir)

self.archive_check = wx.CheckBox(self, -1, "Archive mode")
ok_btn = wx.Button(self, -1, "OK")
ok_btn.Bind(wx.EVT_BUTTON, self.OnOK)
ok_btn.Bind(wx.EVT_BUTTON, self.on_added_source)
cancel_btn = wx.Button(self, -1, "Cancel")
cancel_btn.Bind(wx.EVT_BUTTON, self.OnCancel)

sourceGrid = wx.FlexGridSizer(2, 2, 0, 0)
sourceGrid.AddGrowableCol(1)
sourceGrid.Add(self.rss_feed_radio, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT | wx.TOP, 5)
sourceGrid.Add(self.rss_feed_edit, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
sourceGrid.Add(self.rss_dir_radio, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT | wx.TOP, 5)
sourceGrid.Add(self.rss_dir_edit, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add(ok_btn, 0, wx.RIGHT | wx.TOP | wx.BOTTOM, 5)
btnSizer.Add(cancel_btn, 0, wx.ALL, 5)
vSizer = wx.BoxSizer(wx.VERTICAL)
vSizer.Add(text, 0, wx.EXPAND | wx.ALL, 5)
vSizer.Add(sourceGrid, 0, wx.EXPAND | wx.ALL, 5)
vSizer.AddSpacer((-1, 5))
vSizer.Add(self.archive_check, 0, wx.LEFT | wx.RIGHT, 10)
vSizer.AddStretchSpacer()
vSizer.Add(btnSizer, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(vSizer)

def OnOK(self, event):
cancel_btn.Bind(wx.EVT_BUTTON, self.on_cancel)

source_grid = wx.FlexGridSizer(2, 2, 0, 0)
source_grid.AddGrowableCol(1)
source_grid.Add(self.rss_feed_radio, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT | wx.TOP, 5)
source_grid.Add(self.rss_feed_edit, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
source_grid.Add(self.rss_dir_radio, 0, wx.ALIGN_CENTER_VERTICAL | wx.LEFT | wx.RIGHT | wx.TOP, 5)
source_grid.Add(self.rss_dir_edit, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
btn_sizer.Add(ok_btn, 0, wx.RIGHT | wx.TOP | wx.BOTTOM, 5)
btn_sizer.Add(cancel_btn, 0, wx.ALL, 5)
v_sizer = wx.BoxSizer(wx.VERTICAL)
v_sizer.Add(text, 0, wx.EXPAND | wx.ALL, 5)
v_sizer.Add(source_grid, 0, wx.EXPAND | wx.ALL, 5)
v_sizer.AddSpacer((-1, 5))
v_sizer.Add(self.archive_check, 0, wx.LEFT | wx.RIGHT, 10)
v_sizer.AddStretchSpacer()
v_sizer.Add(btn_sizer, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(v_sizer)

def on_added_source(self, event):
"""
this function called when user clicked 'OK' button for adding source
"""
if self.rss_feed_radio.GetValue():
self.source = self.rss_feed_edit.GetValue()
else:
self.source = self.rss_dir_edit.GetValue()

self.guiutility.Notify(
GUIUtility.getInstance().Notify(
"Successfully add source for credit mining %s" % self.source)

self.EndModal(wx.ID_OK)

def OnCancel(self, event):
def on_cancel(self, event):
"""
this function called when user clicked 'Cancel' button when adding source
thus, cancelled
"""
self.EndModal(wx.ID_CANCEL)

def GetValue(self):
def get_value(self):
return self.source, self.archive_check.GetValue()

def OnDir(self,event):
self.rss_dir_dialog = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)
def on_open_dir(self, event):
rss_dir_dialog = wx.DirDialog(self, "Choose a directory:",
style=wx.DD_DEFAULT_STYLE | wx.DD_DIR_MUST_EXIST)

if self.rss_dir_dialog.ShowModal() == wx.ID_OK:
self.rss_dir_edit.SetValue(self.rss_dir_dialog.GetPath())
if rss_dir_dialog.ShowModal() == wx.ID_OK:
self.rss_dir_edit.SetValue(rss_dir_dialog.GetPath())


class RemoveBoostingSource(wx.Dialog):

"""
Class for adding the source for credit mining
"""
def __init__(self, parent):
wx.Dialog.__init__(self, parent, -1, 'Remove boosting source', size=(475, 135), name="RemoveBoostingSourceDialog")
wx.Dialog.__init__(self, parent, -1, 'Remove boosting source', size=(475, 135),
name="RemoveBoostingSourceDialog")

self.guiutility = GUIUtility.getInstance()
self.boosting_manager = BoostingManager.get_instance()
Expand All @@ -90,38 +106,44 @@ def __init__(self, parent):
self.source_label = wx.StaticText(self, -1, 'Source:')
self.source_choice = wx.Choice(self, -1)
ok_btn = wx.Button(self, -1, "OK")
ok_btn.Bind(wx.EVT_BUTTON, self.OnOK)
ok_btn.Bind(wx.EVT_BUTTON, self.on_remove_source)
cancel_btn = wx.Button(self, -1, "Cancel")
cancel_btn.Bind(wx.EVT_BUTTON, self.OnCancel)

sourceSizer = wx.BoxSizer(wx.HORIZONTAL)
sourceSizer.Add(self.source_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.TOP, 5)
sourceSizer.Add(self.source_choice, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer.Add(ok_btn, 0, wx.RIGHT | wx.TOP | wx.BOTTOM, 5)
btnSizer.Add(cancel_btn, 0, wx.ALL, 5)
vSizer = wx.BoxSizer(wx.VERTICAL)
vSizer.Add(text, 0, wx.EXPAND | wx.ALL, 5)
vSizer.Add(sourceSizer, 0, wx.EXPAND | wx.ALL, 5)
vSizer.AddStretchSpacer()
vSizer.Add(btnSizer, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(vSizer)

channels = []
cancel_btn.Bind(wx.EVT_BUTTON, self.on_cancel)

sourcesizer = wx.BoxSizer(wx.HORIZONTAL)
sourcesizer.Add(self.source_label, 0, wx.ALIGN_CENTER_VERTICAL | wx.RIGHT | wx.TOP, 5)
sourcesizer.Add(self.source_choice, 1, wx.EXPAND | wx.LEFT | wx.RIGHT | wx.TOP, 5)
btnsizer = wx.BoxSizer(wx.HORIZONTAL)
btnsizer.Add(ok_btn, 0, wx.RIGHT | wx.TOP | wx.BOTTOM, 5)
btnsizer.Add(cancel_btn, 0, wx.ALL, 5)
vsizer = wx.BoxSizer(wx.VERTICAL)
vsizer.Add(text, 0, wx.EXPAND | wx.ALL, 5)
vsizer.Add(sourcesizer, 0, wx.EXPAND | wx.ALL, 5)
vsizer.AddStretchSpacer()
vsizer.Add(btnsizer, 0, wx.EXPAND | wx.ALL, 5)
self.SetSizer(vsizer)

# retrieve all source except channel source
self.sources = [s.getSource() for s in self.boosting_manager.boosting_sources.values()
self.sources = [s.get_source() for s in self.boosting_manager.boosting_sources.values()
if not isinstance(s, ChannelSource)]

self.source_choice.SetItems(self.sources)

def OnOK(self, event):
def on_remove_source(self, event):
"""
this function called when user clicked 'OK' button for removing source
"""
selection = self.source_choice.GetSelection()
if selection < len(self.sources):
self.source = self.sources[selection]
self.EndModal(wx.ID_OK)

def OnCancel(self, event):
def on_cancel(self, event):
"""
this function called when user clicked 'Cancel' button when adding source
thus, cancelled
"""
self.EndModal(wx.ID_CANCEL)

def GetValue(self):
def get_value(self):
return self.source
5 changes: 3 additions & 2 deletions Tribler/Main/tribler_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#
from Tribler.Policies.BoostingManager import BoostingManager
from Tribler.Main.Dialogs.NewVersionDialog import NewVersionDialog
import shutil

try:
import prctl
Expand All @@ -27,7 +28,7 @@
import tempfile
import traceback
import urllib2
import shutil

from collections import defaultdict
from random import randint
from traceback import print_exc
Expand Down Expand Up @@ -778,7 +779,7 @@ def sesscb_ntfy_torrentupdates(self, events):
manager.torrentsUpdated(infohashes)

manager = self.frame.creditminingpanel.cmlist.GetManager()
manager.torrentsUpdated(infohashes)
manager.torrents_updated(infohashes)

from Tribler.Main.Utility.GuiDBTuples import CollectedTorrent

Expand Down
Loading

0 comments on commit b4fa461

Please sign in to comment.