Skip to content

Commit

Permalink
Merge pull request Tribler#5 from NielsZeilemaker/test-cleanup
Browse files Browse the repository at this point in the history
Fixed sqlite tests
  • Loading branch information
Elric Milon committed May 2, 2013
2 parents ba400a4 + 809001e commit dfeda37
Show file tree
Hide file tree
Showing 9 changed files with 627 additions and 837 deletions.
51 changes: 25 additions & 26 deletions Tribler/Category/Category.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

from Tribler.__init__ import LIBRARYNAME

DEBUG=False
DEBUG = False
category_file = "category.conf"


Expand All @@ -24,10 +24,10 @@ def __init__(self, install_dir='.'):

if Category.__single:
raise RuntimeError, "Category is singleton"
filename = os.path.join(install_dir,LIBRARYNAME, 'Category', category_file)
filename = os.path.join(install_dir, LIBRARYNAME, 'Category', category_file)
Category.__single = self
self.utility = None
#self.torrent_db = TorrentDBHandler.getInstance() # Arno, 2009-01-30: apparently unused
# self.torrent_db = TorrentDBHandler.getInstance() # Arno, 2009-01-30: apparently unused
try:
self.category_info = getCategoryInfo(filename)
self.category_info.sort(rankcmp)
Expand All @@ -38,9 +38,8 @@ def __init__(self, install_dir='.'):

self.xxx_filter = XXXFilter(install_dir)


if DEBUG:
print >>sys.stderr,"category: Categories defined by user",self.getCategoryNames()
print >> sys.stderr, "category: Categories defined by user", self.getCategoryNames()


# return Category instance
Expand All @@ -52,7 +51,7 @@ def getInstance(*args, **kw):

def init_from_main(self, utility):
self.utility = utility
self.set_family_filter(None) # init family filter to saved state
self.set_family_filter(None) # init family filter to saved state

def getCategoryKeys(self):
if self.category_info is None:
Expand All @@ -65,15 +64,15 @@ def getCategoryKeys(self):
keys.sort()
return keys

def getCategoryNames(self, filter = True):
def getCategoryNames(self, filter=True):
if self.category_info is None:
return []
keys = []
for category in self.category_info:
rank = category['rank']
if rank == -1 and filter:
break
keys.append((category['name'],category['displayname']))
keys.append((category['name'], category['displayname']))
return keys

def hasActiveCategory(self, torrent):
Expand All @@ -82,16 +81,16 @@ def hasActiveCategory(self, torrent):
except:
print >> sys.stderr, 'Torrent: %s has no valid category' % `torrent['content_name']`
return False
for category in [{'name':'other', 'rank':1}]+self.category_info:
for category in [{'name':'other', 'rank':1}] + self.category_info:
rank = category['rank']
if rank == -1:
break
if name.lower() == category['name'].lower():
return True
#print >> sys.stderr, 'Category: %s was not in %s' % (name.lower(), [a['name'].lower() for a in self.category_info if a['rank'] != -1])
# print >> sys.stderr, 'Category: %s was not in %s' % (name.lower(), [a['name'].lower() for a in self.category_info if a['rank'] != -1])
return False

def getCategoryRank(self,cat):
def getCategoryRank(self, cat):
for category in self.category_info:
if category['name'] == cat:
return category['rank']
Expand All @@ -111,11 +110,11 @@ def calculateCategory(self, torrent_dict, display_name):
files_list.append((ifiles['path'][-1], ifiles['length'] / float(self.__size_change)))
except KeyError:
# single mode
files_list.append((torrent_dict['info']["name"],torrent_dict['info']['length'] / float(self.__size_change)))
files_list.append((torrent_dict['info']["name"], torrent_dict['info']['length'] / float(self.__size_change)))

tracker = torrent_dict.get('announce')
if not tracker:
tracker = torrent_dict.get('announce-list',[['']])[0][0]
tracker = torrent_dict.get('announce-list', [['']])[0][0]

comment = torrent_dict.get('comment')
return self.calculateCategoryNonDict(files_list, display_name, tracker, comment)
Expand All @@ -134,7 +133,7 @@ def calculateCategoryNonDict(self, files_list, display_name, tracker, comment):
torrent_category = None
# filename_list ready
strongest_cat = 0.0
for category in self.category_info: # for each category
for category in self.category_info: # for each category
(decision, strength) = self.judge(category, files_list, display_name)
if decision and (strength > strongest_cat):
torrent_category = [category['name']]
Expand All @@ -147,7 +146,7 @@ def calculateCategoryNonDict(self, files_list, display_name, tracker, comment):

# judge whether a torrent file belongs to a certain category
# return bool
def judge(self, category, files_list, display_name = ''):
def judge(self, category, files_list, display_name=''):

# judge file keywords
display_name = display_name.lower()
Expand All @@ -164,22 +163,22 @@ def judge(self, category, files_list, display_name = ''):
if 'strength' in category:
return (True, category['strength'])
else:
return (True, (1- factor))
return (True, (1 - factor))

# judge each file
matchSize = 0
totalSize = 1e-19
for name, length in files_list:
totalSize += length
# judge file size
if ( length < category['minfilesize'] ) or \
(category['maxfilesize'] > 0 and length > category['maxfilesize'] ):
if (length < category['minfilesize']) or \
(category['maxfilesize'] > 0 and length > category['maxfilesize']):
continue

# judge file suffix
OK = False
for isuffix in category['suffix']:
if name.lower().endswith( isuffix ):
if name.lower().endswith(isuffix):
OK = True
break
if OK:
Expand All @@ -194,7 +193,7 @@ def judge(self, category, files_list, display_name = ''):
# pass
try:
fileKeywords.index(ikeywords)
#print ikeywords
# print ikeywords
factor *= 1 - category['keywords'][ikeywords]
except:
pass
Expand All @@ -207,7 +206,7 @@ def judge(self, category, files_list, display_name = ''):
if 'strength' in category:
return (True, category['strength'])
else:
return (True, (matchSize/ totalSize))
return (True, (matchSize / totalSize))

return (False, 0)

Expand All @@ -234,12 +233,12 @@ def family_filter_enabled(self):
def set_family_filter(self, b=None):
assert b in (True, False, None)
old = self.family_filter_enabled()
if b != old or b is None: # update category data if initial call, or if state changes
if b != old or b is None: # update category data if initial call, or if state changes
if b is None:
b=old
b = old
if self.utility is None:
return
#print >> sys.stderr , b
# print >> sys.stderr , b
if b:
self.utility.config.Write('family_filter', '1')
else:
Expand All @@ -260,15 +259,15 @@ def get_family_filter_sql(self, _getCategoryID, table_name=''):
if self.family_filter_enabled():
forbiddencats = [cat['name'] for cat in self.category_info if cat['rank'] == -1]
if table_name:
table_name+='.'
table_name += '.'
if forbiddencats:
return " and %scategory_id not in (%s)" % (table_name, ','.join([str(_getCategoryID([cat])) for cat in forbiddencats]))
return ''




def rankcmp(a,b):
def rankcmp(a, b):
if not ('rank' in a):
return 1
elif not ('rank' in b):
Expand Down
Loading

0 comments on commit dfeda37

Please sign in to comment.