python binding update

This commit is contained in:
Arvid Norberg
2008-03-28 21:37:35 +00:00
parent 2b144d7ac7
commit 483da2483e
5 changed files with 218 additions and 145 deletions

View File

@@ -13,13 +13,57 @@ using namespace libtorrent;
namespace namespace
{ {
list url_seeds(torrent_handle& handle)
{
list ret;
std::set<std::string> urls;
{
allow_threading_guard guard;
urls = handle.url_seeds();
}
for (std::set<std::string>::iterator i(urls.begin())
, end(urls.end()); i != end; ++i)
ret.append(*i);
return ret;
}
list piece_availability(torrent_handle& handle)
{
list ret;
std::vector<int> avail;
{
allow_threading_guard guard;
handle.piece_availability(avail);
}
for (std::vector<int>::iterator i(avail.begin())
, end(avail.end()); i != end; ++i)
ret.append(*i);
return ret;
}
list piece_priorities(torrent_handle& handle)
{
list ret;
std::vector<int> prio;
{
allow_threading_guard guard;
prio = handle.piece_priorities();
}
for (std::vector<int>::iterator i(prio.begin())
, end(prio.end()); i != end; ++i)
ret.append(*i);
return ret;
}
std::vector<announce_entry>::const_iterator begin_trackers(torrent_handle& i) std::vector<announce_entry>::const_iterator begin_trackers(torrent_handle& i)
{ {
allow_threading_guard guard; allow_threading_guard guard;
return i.trackers().begin(); return i.trackers().begin();
} }
std::vector<announce_entry>::const_iterator end_trackers(torrent_handle& i) std::vector<announce_entry>::const_iterator end_trackers(torrent_handle& i)
{ {
allow_threading_guard guard; allow_threading_guard guard;
@@ -58,34 +102,49 @@ list get_peer_info(torrent_handle const& handle)
list result; list result;
for (std::vector<peer_info>::iterator i = pi.begin(); i != pi.end(); ++i) for (std::vector<peer_info>::iterator i = pi.begin(); i != pi.end(); ++i)
{
result.append(*i); result.append(*i);
}
return result; return result;
} }
void prioritize_pieces(torrent_handle& info, object o)
{
std::vector<int> result;
try
{
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
while( 1 )
{
object obj = extract<object>( iter_obj.attr( "next" )() );
result.push_back(extract<int const>( obj ));
}
}
catch( error_already_set )
{
PyErr_Clear();
info.prioritize_pieces(result);
return;
}
}
void prioritize_files(torrent_handle& info, object o) void prioritize_files(torrent_handle& info, object o)
{ {
std::vector<int> result;
std::vector<int> result; try
try {
{ object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) ));
object iter_obj = object( handle<>( PyObject_GetIter( o.ptr() ) )); while( 1 )
while( 1 ) {
{ object obj = extract<object>( iter_obj.attr( "next" )() );
object obj = extract<object>( iter_obj.attr( "next" )() ); result.push_back(extract<int const>( obj ));
result.push_back(extract<int const>( obj )); }
} }
} catch( error_already_set )
catch( error_already_set ) {
{ PyErr_Clear();
PyErr_Clear(); info.prioritize_files(result);
info.prioritize_files(result); return;
return; }
}
} }
@@ -111,116 +170,137 @@ void replace_trackers(torrent_handle& info, object trackers)
list get_download_queue(torrent_handle& handle) list get_download_queue(torrent_handle& handle)
{ {
list ret; list ret;
std::vector<partial_piece_info> downloading; std::vector<partial_piece_info> downloading;
{ {
allow_threading_guard guard; allow_threading_guard guard;
handle.get_download_queue(downloading); handle.get_download_queue(downloading);
} }
for (std::vector<partial_piece_info>::iterator i = downloading.begin() for (std::vector<partial_piece_info>::iterator i = downloading.begin()
, end(downloading.end()); i != end; ++i) , end(downloading.end()); i != end; ++i)
{ {
dict partial_piece; dict partial_piece;
partial_piece["piece_index"] = i->piece_index; partial_piece["piece_index"] = i->piece_index;
partial_piece["blocks_in_piece"] = i->blocks_in_piece; partial_piece["blocks_in_piece"] = i->blocks_in_piece;
list block_list; list block_list;
for (int k = 0; k < i->blocks_in_piece; ++k) for (int k = 0; k < i->blocks_in_piece; ++k)
{ {
dict block_info; dict block_info;
block_info["state"] = i->blocks[k].state; block_info["state"] = i->blocks[k].state;
block_info["num_peers"] = i->blocks[k].num_peers; block_info["num_peers"] = i->blocks[k].num_peers;
block_info["bytes_progress"] = i->blocks[k].bytes_progress; block_info["bytes_progress"] = i->blocks[k].bytes_progress;
block_info["block_size"] = i->blocks[k].block_size; block_info["block_size"] = i->blocks[k].block_size;
block_info["peer"] = std::make_pair( block_info["peer"] = std::make_pair(
boost::lexical_cast<std::string>(i->blocks[k].peer.address()), i->blocks[k].peer.port()); boost::lexical_cast<std::string>(i->blocks[k].peer.address()), i->blocks[k].peer.port());
block_list.append(block_info); block_list.append(block_info);
} }
partial_piece["blocks"] = block_list; partial_piece["blocks"] = block_list;
ret.append(partial_piece); ret.append(partial_piece);
} }
return ret; return ret;
} }
namespace namespace
{ {
tcp::endpoint tuple_to_endpoint(tuple const& t) tcp::endpoint tuple_to_endpoint(tuple const& t)
{ {
return tcp::endpoint(address::from_string(extract<std::string>(t[0])), extract<int>(t[1])); return tcp::endpoint(address::from_string(extract<std::string>(t[0])), extract<int>(t[1]));
} }
}
void force_reannounce(torrent_handle& th, int s)
{
th.force_reannounce(boost::posix_time::seconds(s));
} }
void connect_peer(torrent_handle& th, tuple ip, int source) void connect_peer(torrent_handle& th, tuple ip, int source)
{ {
th.connect_peer(tuple_to_endpoint(ip), source); th.connect_peer(tuple_to_endpoint(ip), source);
} }
void set_peer_upload_limit(torrent_handle& th, tuple const& ip, int limit) void set_peer_upload_limit(torrent_handle& th, tuple const& ip, int limit)
{ {
th.set_peer_upload_limit(tuple_to_endpoint(ip), limit); th.set_peer_upload_limit(tuple_to_endpoint(ip), limit);
} }
void set_peer_download_limit(torrent_handle& th, tuple const& ip, int limit) void set_peer_download_limit(torrent_handle& th, tuple const& ip, int limit)
{ {
th.set_peer_download_limit(tuple_to_endpoint(ip), limit); th.set_peer_download_limit(tuple_to_endpoint(ip), limit);
} }
void bind_torrent_handle() void bind_torrent_handle()
{ {
void (torrent_handle::*force_reannounce0)() const = &torrent_handle::force_reannounce; void (torrent_handle::*force_reannounce0)() const = &torrent_handle::force_reannounce;
void (torrent_handle::*force_reannounce1)(boost::posix_time::time_duration) const
= &torrent_handle::force_reannounce;
int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority; int (torrent_handle::*piece_priority0)(int) const = &torrent_handle::piece_priority;
void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority; void (torrent_handle::*piece_priority1)(int, int) const = &torrent_handle::piece_priority;
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
bool (torrent_handle::*resolve_countries0)() const = &torrent_handle::resolve_countries;
void (torrent_handle::*resolve_countries1)(bool) = &torrent_handle::resolve_countries;
#endif
return_value_policy<copy_const_reference> copy; return_value_policy<copy_const_reference> copy;
#define _ allow_threads #define _ allow_threads
class_<torrent_handle>("torrent_handle") class_<torrent_handle>("torrent_handle")
.def("connect_peer", connect_peer) .def("get_peer_info", get_peer_info)
.def("status", _(&torrent_handle::status)) .def("status", _(&torrent_handle::status))
.def("torrent_info", _(&torrent_handle::get_torrent_info), return_internal_reference<>()) .def("get_download_queue", get_download_queue)
.def("is_valid", _(&torrent_handle::is_valid))
.def("write_resume_data", _(&torrent_handle::write_resume_data))
.def("force_reannounce", _(force_reannounce0))
.def("force_reannounce", _(force_reannounce1))
.def("set_tracker_login", _(&torrent_handle::set_tracker_login))
.def("add_url_seed", _(&torrent_handle::add_url_seed))
.def("set_ratio", _(&torrent_handle::set_ratio))
.def("set_max_uploads", _(&torrent_handle::set_max_uploads))
.def("set_peer_upload_limit", set_peer_upload_limit)
.def("set_peer_download_limit", set_peer_download_limit)
.def("set_max_connections", _(&torrent_handle::set_max_connections))
.def("set_upload_limit", _(&torrent_handle::set_upload_limit))
.def("set_download_limit", _(&torrent_handle::set_download_limit))
.def("upload_limit", _(&torrent_handle::upload_limit))
.def("download_limit", _(&torrent_handle::download_limit))
.def("set_sequential_download", _(&torrent_handle::set_sequential_download))
.def("pause", _(&torrent_handle::pause))
.def("resume", _(&torrent_handle::resume))
.def("is_paused", _(&torrent_handle::is_paused))
.def("is_seed", _(&torrent_handle::is_seed))
.def("filter_piece", _(&torrent_handle::filter_piece))
.def("piece_priority", _(piece_priority0))
.def("piece_priority", _(piece_priority1))
.def("is_piece_filtered", _(&torrent_handle::is_piece_filtered))
.def("has_metadata", _(&torrent_handle::has_metadata))
.def("save_path", _(&torrent_handle::save_path))
.def("move_storage", _(&torrent_handle::move_storage))
.def("info_hash", _(&torrent_handle::info_hash), copy)
.def("file_progress", file_progress) .def("file_progress", file_progress)
.def("trackers", range(begin_trackers, end_trackers)) .def("trackers", range(begin_trackers, end_trackers))
.def("replace_trackers", replace_trackers) .def("replace_trackers", replace_trackers)
.def("add_url_seed", _(&torrent_handle::add_url_seed))
.def("remove_url_seed", _(&torrent_handle::remove_url_seed))
.def("url_seeds", url_seeds)
.def("has_metadata", _(&torrent_handle::has_metadata))
.def("get_torrent_info", _(&torrent_handle::get_torrent_info), return_internal_reference<>())
.def("is_valid", _(&torrent_handle::is_valid))
.def("is_seed", _(&torrent_handle::is_seed))
.def("is_paused", _(&torrent_handle::is_paused))
.def("pause", _(&torrent_handle::pause))
.def("resume", _(&torrent_handle::resume))
#ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES
.def("resolve_countries", _(resolve_countries0))
.def("resolve_countries", _(resolve_countries1))
#endif
// deprecated
.def("filter_piece", _(&torrent_handle::filter_piece))
.def("is_piece_filtered", _(&torrent_handle::is_piece_filtered))
.def("piece_availability", piece_availability)
.def("piece_priority", _(piece_priority0))
.def("piece_priority", _(piece_priority1))
.def("prioritize_pieces", prioritize_pieces)
.def("piece_prioritize", piece_priorities)
.def("prioritize_files", prioritize_files) .def("prioritize_files", prioritize_files)
.def("get_peer_info", get_peer_info) .def("use_interface", &torrent_handle::use_interface)
.def("get_download_queue", get_download_queue) .def("write_resume_data", _(&torrent_handle::write_resume_data))
.def("scrape_tracker", (&torrent_handle::scrape_tracker)) .def("force_reannounce", _(force_reannounce0))
.def("force_reannounce", force_reannounce)
.def("scrape_tracker", _(&torrent_handle::scrape_tracker))
.def("name", _(&torrent_handle::name))
.def("set_upload_limit", _(&torrent_handle::set_upload_limit))
.def("upload_limit", _(&torrent_handle::upload_limit))
.def("set_download_limit", _(&torrent_handle::set_download_limit))
.def("download_limit", _(&torrent_handle::download_limit))
.def("set_sequential_download", _(&torrent_handle::set_sequential_download))
.def("set_peer_upload_limit", set_peer_upload_limit)
.def("set_peer_download_limit", set_peer_download_limit)
.def("connect_peer", connect_peer)
.def("set_ratio", _(&torrent_handle::set_ratio))
.def("save_path", _(&torrent_handle::save_path))
.def("set_max_uploads", _(&torrent_handle::set_max_uploads))
.def("set_max_connections", _(&torrent_handle::set_max_connections))
.def("set_tracker_login", _(&torrent_handle::set_tracker_login))
.def("move_storage", _(&torrent_handle::move_storage))
.def("info_hash", _(&torrent_handle::info_hash), copy)
; ;
} }

View File

@@ -44,12 +44,12 @@ namespace
{ {
return i.begin_files(storage); return i.begin_files(storage);
} }
std::vector<file_entry>::const_iterator end_files(torrent_info& i, bool storage) std::vector<file_entry>::const_iterator end_files(torrent_info& i, bool storage)
{ {
return i.end_files(storage); return i.end_files(storage);
} }
//list files(torrent_info const& ti, bool storage) { //list files(torrent_info const& ti, bool storage) {
list files(torrent_info const& ti, bool storage) { list files(torrent_info const& ti, bool storage) {
list result; list result;
@@ -61,7 +61,7 @@ namespace
return result; return result;
} }
} // namespace unnamed } // namespace unnamed
@@ -92,8 +92,8 @@ void bind_torrent_info()
.def("hash_for_piece", &torrent_info::hash_for_piece, copy) .def("hash_for_piece", &torrent_info::hash_for_piece, copy)
.def("piece_size", &torrent_info::piece_size) .def("piece_size", &torrent_info::piece_size)
.def("num_files", &torrent_info::num_files, (arg("storage")=false)) .def("num_files", &torrent_info::num_files, (arg("storage")=false))
.def("file_at", &torrent_info::file_at, return_internal_reference<>()) .def("file_at", &torrent_info::file_at, return_internal_reference<>())
.def("files", &files, (arg("storage")=false)) .def("files", &files, (arg("storage")=false))
@@ -106,6 +106,7 @@ void bind_torrent_info()
.def("add_node", &add_node) .def("add_node", &add_node)
.def("nodes", &nodes) .def("nodes", &nodes)
; ;
class_<file_entry>("file_entry") class_<file_entry>("file_entry")
.add_property( .add_property(
"path" "path"

View File

@@ -18,79 +18,55 @@ object pieces(torrent_status const& s)
return result; return result;
} }
extern char const* torrent_status_doc;
extern char const* torrent_status_state_doc;
extern char const* torrent_status_paused_doc;
extern char const* torrent_status_progress_doc;
extern char const* torrent_status_next_announce_doc;
extern char const* torrent_status_announce_interval_doc;
extern char const* torrent_status_current_tracker_doc;
extern char const* torrent_status_total_download_doc;
extern char const* torrent_status_total_upload_doc;
extern char const* torrent_status_total_payload_download_doc;
extern char const* torrent_status_total_payload_upload_doc;
extern char const* torrent_status_total_failed_bytes_doc;
void bind_torrent_status() void bind_torrent_status()
{ {
scope status = class_<torrent_status>("torrent_status", torrent_status_doc) scope status = class_<torrent_status>("torrent_status")
.def_readonly("state", &torrent_status::state, torrent_status_state_doc) .def_readonly("state", &torrent_status::state)
.def_readonly("paused", &torrent_status::paused, torrent_status_paused_doc) .def_readonly("paused", &torrent_status::paused)
.def_readonly("progress", &torrent_status::progress, torrent_status_progress_doc) .def_readonly("progress", &torrent_status::progress)
.add_property( .add_property(
"next_announce" "next_announce"
, make_getter( , make_getter(
&torrent_status::next_announce, return_value_policy<return_by_value>() &torrent_status::next_announce, return_value_policy<return_by_value>()
) )
, torrent_status_next_announce_doc
) )
.add_property( .add_property(
"announce_interval" "announce_interval"
, make_getter( , make_getter(
&torrent_status::announce_interval, return_value_policy<return_by_value>() &torrent_status::announce_interval, return_value_policy<return_by_value>()
) )
, torrent_status_announce_interval_doc
)
.def_readonly(
"current_tracker", &torrent_status::current_tracker
, torrent_status_current_tracker_doc
)
.def_readonly(
"total_download", &torrent_status::total_download
, torrent_status_total_download_doc
)
.def_readonly(
"total_upload", &torrent_status::total_upload
, torrent_status_total_upload_doc
)
.def_readonly(
"total_payload_download", &torrent_status::total_payload_download
, torrent_status_total_payload_download_doc
)
.def_readonly(
"total_payload_upload", &torrent_status::total_payload_upload
, torrent_status_total_payload_upload_doc
)
.def_readonly(
"total_failed_bytes", &torrent_status::total_failed_bytes
, torrent_status_total_failed_bytes_doc
) )
.def_readonly("current_tracker", &torrent_status::current_tracker)
.def_readonly("total_download", &torrent_status::total_download)
.def_readonly("total_upload", &torrent_status::total_upload)
.def_readonly("total_payload_download", &torrent_status::total_payload_download)
.def_readonly("total_payload_upload", &torrent_status::total_payload_upload)
.def_readonly("total_failed_bytes", &torrent_status::total_failed_bytes)
.def_readonly("total_redundant_bytes", &torrent_status::total_redundant_bytes) .def_readonly("total_redundant_bytes", &torrent_status::total_redundant_bytes)
.def_readonly("download_rate", &torrent_status::download_rate) .def_readonly("download_rate", &torrent_status::download_rate)
.def_readonly("upload_rate", &torrent_status::upload_rate) .def_readonly("upload_rate", &torrent_status::upload_rate)
.def_readonly("download_payload_rate", &torrent_status::download_payload_rate) .def_readonly("download_payload_rate", &torrent_status::download_payload_rate)
.def_readonly("upload_payload_rate", &torrent_status::upload_payload_rate) .def_readonly("upload_payload_rate", &torrent_status::upload_payload_rate)
.def_readonly("num_seeds", &torrent_status::num_seeds)
.def_readonly("num_peers", &torrent_status::num_peers) .def_readonly("num_peers", &torrent_status::num_peers)
.def_readonly("num_complete", &torrent_status::num_complete) .def_readonly("num_complete", &torrent_status::num_complete)
.def_readonly("num_incomplete", &torrent_status::num_incomplete) .def_readonly("num_incomplete", &torrent_status::num_incomplete)
.def_readonly("list_seeds", &torrent_status::list_seeds)
.def_readonly("list_peers", &torrent_status::list_peers)
.add_property("pieces", pieces) .add_property("pieces", pieces)
.def_readonly("num_pieces", &torrent_status::num_pieces) .def_readonly("num_pieces", &torrent_status::num_pieces)
.def_readonly("total_done", &torrent_status::total_done) .def_readonly("total_done", &torrent_status::total_done)
.def_readonly("total_wanted_done", &torrent_status::total_wanted_done) .def_readonly("total_wanted_done", &torrent_status::total_wanted_done)
.def_readonly("total_wanted", &torrent_status::total_wanted) .def_readonly("total_wanted", &torrent_status::total_wanted)
.def_readonly("num_seeds", &torrent_status::num_seeds)
.def_readonly("distributed_copies", &torrent_status::distributed_copies) .def_readonly("distributed_copies", &torrent_status::distributed_copies)
.def_readonly("block_size", &torrent_status::block_size) .def_readonly("block_size", &torrent_status::block_size)
.def_readonly("num_uploads", &torrent_status::num_uploads)
.def_readonly("num_connections", &torrent_status::num_connections)
.def_readonly("uploads_limit", &torrent_status::uploads_limit)
.def_readonly("connections_limit", &torrent_status::connections_limit)
.def_readonly("storage_mode", &torrent_status::storage_mode)
.def_readonly("up_bandwidth_queue", &torrent_status::up_bandwidth_queue)
.def_readonly("down_bandwidth_queue", &torrent_status::down_bandwidth_queue)
; ;
enum_<torrent_status::state_t>("states") enum_<torrent_status::state_t>("states")

View File

@@ -49,11 +49,28 @@ On Mac OS X, this will produce the following python module::
bin/darwin-4.0/release/dht-support-on/link-static/logging-none/threading-multi/libtorrent.so bin/darwin-4.0/release/dht-support-on/link-static/logging-none/threading-multi/libtorrent.so
using using libtorrent in python
===== ==========================
The python interface is nearly identical to the C++ interface. Please refer to The python interface is nearly identical to the C++ interface. Please refer to
the `main library reference`_. the `main library reference`_. The main differences are:
asio::tcp::endpoint
The endpoint type is represented as a tuple of a string (as the address) and an int for
the port number. E.g. ``('127.0.0.1', 6881)`` represents the localhost port 6881.
libtorrent::time_duration
The time duration is represented as a number of seconds in a regular integer.
The following functions takes a reference to a container that is filled with
entries by the function. The python equivalent of these functions instead returns
a list of entries.
* torrent_handle::get_peer_info
* torrent_handle::file_progress
* torrent_handle::get_download_queue
* torrent_handle::piece_availability
.. _`main library reference`: manual.html .. _`main library reference`: manual.html

View File

@@ -281,7 +281,6 @@ namespace libtorrent
torrent_handle(): m_ses(0), m_info_hash(0) {} torrent_handle(): m_ses(0), m_info_hash(0) {}
void get_peer_info(std::vector<peer_info>& v) const; void get_peer_info(std::vector<peer_info>& v) const;
bool send_chat_message(tcp::endpoint ip, std::string message) const;
torrent_status status() const; torrent_status status() const;
void get_download_queue(std::vector<partial_piece_info>& queue) const; void get_download_queue(std::vector<partial_piece_info>& queue) const;