1
0
mirror of https://github.com/vikstrous/pirate-get synced 2025-01-10 10:04:21 +01:00

first step towards prettytable

not pretty yet
This commit is contained in:
Viktor Stanchev 2015-09-19 23:29:12 -07:00
parent 218c3114e5
commit 6adf0581dc
3 changed files with 44 additions and 27 deletions

View File

@ -2,12 +2,14 @@ import builtins
import re import re
import os import os
import gzip import gzip
import colorama
import urllib.parse as parse import urllib.parse as parse
import urllib.request as request import urllib.request as request
import shutil import shutil
from io import BytesIO from io import BytesIO
import colorama
import prettytable
import pirate.data import pirate.data
@ -32,20 +34,25 @@ def print(*args, **kwargs):
return builtins.print(*args, **kwargs) return builtins.print(*args, **kwargs)
# TODO: extract the name from the search results instead of the magnet link when possible # TODO: extract the name from the search results instead of from the magnet link when possible
def search_results(results, local=None): def search_results(results, local=None):
columns = shutil.get_terminal_size((80, 20)).columns columns = shutil.get_terminal_size((80, 20)).columns
cur_color = 'zebra_0' cur_color = 'zebra_0'
if local: if local:
print('{:>4} {:{length}}'.format( table = prettytable.PrettyTable(['LINK', 'NAME'])
'LINK', 'NAME', length=columns - 8),
color='header')
else: else:
print('{:>4} {:>5} {:>5} {:>5} {:9} {:11} {:{length}}'.format( table = prettytable.PrettyTable(['LINK', 'SEED', 'LEECH', 'RATIO', 'SIZE', '', 'UPLOAD', 'NAME'])
'LINK', 'SEED', 'LEECH', 'RATIO', table.align['NAME'] = 'l'
'SIZE', 'UPLOAD', 'NAME', length=columns - 52), table.align['SEED'] = 'r'
color='header') table.align['LEECH'] = 'r'
table.align['RATIO'] = 'r'
table.align['SIZE'] = 'r'
table.align['UPLOAD'] = 'l'
table.max_width = columns
table.border = False
table.padding_width = 1
for n, result in enumerate(results): for n, result in enumerate(results):
# Alternate between colors # Alternate between colors
@ -55,8 +62,7 @@ def search_results(results, local=None):
torrent_name = parse.unquote_plus(name.group(1)) torrent_name = parse.unquote_plus(name.group(1))
if local: if local:
line = '{:5} {:{length}}' content = [n, torrent_name[:columns - 7]]
content = [n, torrent_name[:columns]]
else: else:
no_seeders = int(result['seeds']) no_seeders = int(result['seeds'])
no_leechers = int(result['leechers']) no_leechers = int(result['leechers'])
@ -74,13 +80,11 @@ def search_results(results, local=None):
except ZeroDivisionError: except ZeroDivisionError:
ratio = float('inf') ratio = float('inf')
line = ('{:4} {:5} {:5} {:5.1f} {:5.1f}' content = [n, no_seeders, no_leechers, '{0:.1f}'.format(ratio),
' {:3} {:<11} {:{length}}') '{0:.1f}'.format(size), unit, date, torrent_name[:columns - 53]]
content = [n, no_seeders, no_leechers, ratio,
size, unit, date, torrent_name[:columns - 52]]
# enhanced print output with justified columns table.add_row(content)
print(line.format(*content, length=columns - 52), color=cur_color) print(table)
def descriptions(chosen_links, results, site): def descriptions(chosen_links, results, site):

View File

@ -13,7 +13,7 @@ setup(name='pirate-get',
entry_points={ entry_points={
'console_scripts': ['pirate-get = pirate.pirate:main'] 'console_scripts': ['pirate-get = pirate.pirate:main']
}, },
install_requires=['colorama>=0.3.3', 'pyquery>=1.2.9'], install_requires=['colorama>=0.3.3', 'pyquery>=1.2.9', 'PrettyTable>=0.7.2'],
keywords=['torrent', 'magnet', 'download', 'tpb', 'client'], keywords=['torrent', 'magnet', 'download', 'tpb', 'client'],
classifiers=[ classifiers=[
'Topic :: Utilities', 'Topic :: Utilities',

View File

@ -1,7 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import unittest import unittest
from unittest.mock import patch from unittest.mock import patch, call, MagicMock
from unittest.mock import call
import pirate.print import pirate.print
@ -9,7 +8,11 @@ import pirate.print
class TestPrint(unittest.TestCase): class TestPrint(unittest.TestCase):
def test_print_results(self): def test_print_results(self):
with patch('pirate.print.print') as mock: class MockTable:
add_row = MagicMock()
align = {}
mock = MockTable()
with patch('prettytable.PrettyTable', return_value=mock) as prettytable:
results = [{ results = [{
'magnet': 'dn=name', 'magnet': 'dn=name',
'seeds': 1, 'seeds': 1,
@ -18,12 +21,22 @@ class TestPrint(unittest.TestCase):
'uploaded': 'never' 'uploaded': 'never'
}] }]
pirate.print.search_results(results) pirate.print.search_results(results)
actual = mock.call_args_list prettytable.assert_called_once_with(['LINK', 'SEED', 'LEECH', 'RATIO', 'SIZE', '', 'UPLOAD', 'NAME'])
expected = [ mock.add_row.assert_has_calls([call([0, 1, 2, '0.5', '3.0', 'MiB', 'never', 'name'])])
call('LINK SEED LEECH RATIO SIZE UPLOAD NAME ', color='header'),
call(' 0 1 2 0.5 3.0 MiB never name ', color='zebra_1'), def test_print_results(self):
] class MockTable:
self.assertEqual(expected, actual) add_row = MagicMock()
align = {}
mock = MockTable()
with patch('prettytable.PrettyTable', return_value=mock) as prettytable:
results = [{
'magnet': 'dn=name',
'Name': 'name',
}]
pirate.print.search_results(results, local=True)
prettytable.assert_called_once_with(['LINK', 'NAME'])
mock.add_row.assert_has_calls([call([0, 'name'])])
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()