improve python binding of read_resume_data, and specifically add_torrent_params

This commit is contained in:
arvidn 2016-04-12 01:33:01 -04:00
parent c498c25da8
commit 2529dbba7f
4 changed files with 83 additions and 1 deletions

View File

@ -680,6 +680,55 @@ void bind_session()
#endif // TORRENT_DISABLE_DHT
#endif // TORRENT_NO_DEPRECATE
class_<add_torrent_params>("add_torrent_params")
.def_readwrite("version", &add_torrent_params::version)
.def_readwrite("ti", &add_torrent_params::ti)
.def_readwrite("trackers", &add_torrent_params::trackers)
.def_readwrite("tracker_tiers", &add_torrent_params::tracker_tiers)
.def_readwrite("dht_nodes", &add_torrent_params::dht_nodes)
.def_readwrite("name", &add_torrent_params::name)
.def_readwrite("save_path", &add_torrent_params::save_path)
.def_readwrite("storage_mode", &add_torrent_params::storage_mode)
.def_readwrite("storage", &add_torrent_params::storage)
.def_readwrite("file_priorities", &add_torrent_params::file_priorities)
.def_readwrite("trackerid", &add_torrent_params::trackerid)
.def_readwrite("url", &add_torrent_params::url)
.def_readwrite("flags", &add_torrent_params::flags)
.def_readwrite("info_hash", &add_torrent_params::info_hash)
.def_readwrite("max_uploads", &add_torrent_params::max_uploads)
.def_readwrite("max_connections", &add_torrent_params::max_connections)
.def_readwrite("upload_limit", &add_torrent_params::upload_limit)
.def_readwrite("download_limit", &add_torrent_params::download_limit)
.def_readwrite("total_uploaded", &add_torrent_params::total_uploaded)
.def_readwrite("total_downloaded", &add_torrent_params::total_downloaded)
.def_readwrite("active_time", &add_torrent_params::active_time)
.def_readwrite("finished_time", &add_torrent_params::finished_time)
.def_readwrite("seeding_time", &add_torrent_params::seeding_time)
.def_readwrite("added_time", &add_torrent_params::added_time)
.def_readwrite("completed_time", &add_torrent_params::completed_time)
.def_readwrite("last_seen_complete", &add_torrent_params::last_seen_complete)
.def_readwrite("num_complete", &add_torrent_params::num_complete)
.def_readwrite("num_incomplete", &add_torrent_params::num_incomplete)
.def_readwrite("num_downloaded", &add_torrent_params::num_downloaded)
.def_readwrite("http_seeds", &add_torrent_params::http_seeds)
.def_readwrite("url_seeds", &add_torrent_params::url_seeds)
.def_readwrite("peers", &add_torrent_params::peers)
.def_readwrite("banned_peers", &add_torrent_params::banned_peers)
.def_readwrite("unfinished_pieces", &add_torrent_params::unfinished_pieces)
.def_readwrite("have_pieces", &add_torrent_params::have_pieces)
.def_readwrite("verified_pieces", &add_torrent_params::verified_pieces)
.def_readwrite("piece_priorities", &add_torrent_params::piece_priorities)
.def_readwrite("merkle_tree", &add_torrent_params::merkle_tree)
.def_readwrite("renamed_files", &add_torrent_params::renamed_files)
#ifndef TORRENT_NO_DEPRECATE
.def_readwrite("uuid", &add_torrent_params::uuid)
.def_readwrite("source_feed_url", &add_torrent_params::source_feed_url)
.def_readwrite("resume_data", &add_torrent_params::resume_data)
#endif
;
enum_<storage_mode_t>("storage_mode_t")
.value("storage_mode_allocate", storage_mode_allocate)
.value("storage_mode_sparse", storage_mode_sparse)
@ -791,6 +840,8 @@ void bind_session()
#endif // TORRENT_DISABLE_DHT
.def("add_torrent", &add_torrent)
.def("async_add_torrent", &async_add_torrent)
.def("async_add_torrent", &lt::session::async_add_torrent)
.def("add_torrent", allow_threads((lt::torrent_handle (session_handle::*)(add_torrent_params const&))&lt::session::add_torrent))
#ifndef BOOST_NO_EXCEPTIONS
#ifndef TORRENT_NO_DEPRECATE
.def(

View File

@ -21,10 +21,31 @@ class test_torrent_handle(unittest.TestCase):
h.prioritize_pieces([0])
self.assertEqual(h.piece_priorities(), [0])
# also test the overload that takes a list of piece->priority mappings
# also test the overload that takes a list of piece->priority mappings
h.prioritize_pieces([(0, 1)])
self.assertEqual(h.piece_priorities(), [1])
def test_read_resume_data(self):
resume_data = lt.bencode({'file-format': 'libtorrent resume file',
'info-hash': 'abababababababababab',
'name': 'test',
'save_path': '.',
'file_priorities': [0, 1, 1]})
tp = lt.read_resume_data(resume_data)
self.assertEqual(tp.name, 'test')
self.assertEqual(tp.info_hash, lt.sha1_hash('abababababababababab'))
ses = lt.session({'alert_mask': lt.alert.category_t.all_categories})
h = ses.add_torrent(tp)
for i in range(0, 10):
alerts = ses.pop_alerts()
for a in alerts:
print(a.message())
time.sleep(0.1)
class test_torrent_info(unittest.TestCase):
def test_bencoded_constructor(self):

View File

@ -89,6 +89,8 @@ namespace libtorrent
return ret;
}
ret.name = rd.dict_find_string_value("name");
ret.info_hash.assign(info_hash);
// TODO: 4 add unit test for this, and all other fields of the resume data

View File

@ -281,6 +281,8 @@ namespace libtorrent
#ifndef BOOST_NO_EXCEPTIONS
torrent_handle session_handle::add_torrent(add_torrent_params const& params)
{
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
#ifndef TORRENT_NO_DEPRECATE
add_torrent_params p = params;
handle_backwards_compatible_resume_data(p);
@ -296,6 +298,8 @@ namespace libtorrent
torrent_handle session_handle::add_torrent(add_torrent_params const& params, error_code& ec)
{
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
ec.clear();
#ifndef TORRENT_NO_DEPRECATE
add_torrent_params p = params;
@ -308,6 +312,8 @@ namespace libtorrent
void session_handle::async_add_torrent(add_torrent_params const& params)
{
TORRENT_ASSERT_PRECOND(!params.save_path.empty());
add_torrent_params* p = new add_torrent_params(params);
#ifndef TORRENT_NO_DEPRECATE
@ -353,6 +359,8 @@ namespace libtorrent
, storage_constructor_type sc
, void* userdata)
{
TORRENT_ASSERT_PRECOND(!save_path.empty());
add_torrent_params p(sc);
p.trackers.push_back(tracker_url);
p.info_hash = info_hash;