some python bindings fixes

This commit is contained in:
Arvid Norberg 2013-08-27 16:04:19 +00:00
parent f2aca363ad
commit 50f98c124f
6 changed files with 38 additions and 56 deletions

View File

@ -2,24 +2,27 @@
// subject to the Boost Software License, Version 1.0. (See accompanying // subject to the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <libtorrent/peer_id.hpp> #include <libtorrent/sha1_hash.hpp>
#include <boost/python.hpp> #include <boost/python.hpp>
void bind_big_number() void bind_sha1_hash()
{ {
using namespace boost::python; using namespace boost::python;
using namespace libtorrent; using namespace libtorrent;
class_<big_number>("big_number") class_<sha1_hash>("sha1_hash")
.def(self == self) .def(self == self)
.def(self != self) .def(self != self)
.def(self < self) .def(self < self)
.def(self_ns::str(self)) .def(self_ns::str(self))
.def(init<char const*>()) .def(init<char const*>())
.def("clear", &big_number::clear) .def("clear", &sha1_hash::clear)
.def("is_all_zeros", &big_number::is_all_zeros) .def("is_all_zeros", &sha1_hash::is_all_zeros)
.def("to_string", &big_number::to_string) .def("to_string", &sha1_hash::to_string)
// .def("__getitem__", &big_number::opreator[]) // .def("__getitem__", &sha1_hash::opreator[])
; ;
scope().attr("big_number") = scope().attr("sha1_hash");
scope().attr("peer_id") = scope().attr("peer_id");
} }

View File

@ -65,13 +65,13 @@ namespace
void bind_create_torrent() void bind_create_torrent()
{ {
void (file_storage::*add_file0)(std::string const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file; void (file_storage::*add_file0)(std::string const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file;
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
void (file_storage::*add_file1)(std::wstring const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file; void (file_storage::*add_file1)(std::wstring const&, size_type, int, std::time_t, std::string const&) = &file_storage::add_file;
#endif #endif
void (file_storage::*set_name0)(std::string const&) = &file_storage::set_name; void (file_storage::*set_name0)(std::string const&) = &file_storage::set_name;
void (file_storage::*rename_file0)(int, std::string const&) = &file_storage::rename_file; void (file_storage::*rename_file0)(int, std::string const&) = &file_storage::rename_file;
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
void (file_storage::*set_name1)(std::wstring const&) = &file_storage::set_name; void (file_storage::*set_name1)(std::wstring const&) = &file_storage::set_name;
void (file_storage::*rename_file1)(int, std::wstring const&) = &file_storage::rename_file; void (file_storage::*rename_file1)(int, std::wstring const&) = &file_storage::rename_file;
#endif #endif
@ -87,7 +87,7 @@ void bind_create_torrent()
.def("is_valid", &file_storage::is_valid) .def("is_valid", &file_storage::is_valid)
.def("add_file", add_file, arg("entry")) .def("add_file", add_file, arg("entry"))
.def("add_file", add_file0, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = "")) .def("add_file", add_file0, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
.def("add_file", add_file1, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = "")) .def("add_file", add_file1, (arg("path"), arg("size"), arg("flags") = 0, arg("mtime") = 0, arg("linkpath") = ""))
#endif #endif
.def("num_files", &file_storage::num_files) .def("num_files", &file_storage::num_files)
@ -106,7 +106,7 @@ void bind_create_torrent()
.def("piece_size", &file_storage::piece_size) .def("piece_size", &file_storage::piece_size)
.def("set_name", set_name0) .def("set_name", set_name0)
.def("rename_file", rename_file0) .def("rename_file", rename_file0)
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
.def("set_name", set_name1) .def("set_name", set_name1)
.def("rename_file", rename_file1) .def("rename_file", rename_file1)
#endif #endif

View File

@ -11,9 +11,7 @@
using namespace boost::python; using namespace boost::python;
using namespace libtorrent; using namespace libtorrent;
extern void dict_to_add_torrent_params(dict params extern void dict_to_add_torrent_params(dict params, add_torrent_params& p);
, add_torrent_params& p, std::vector<char>& rd
, std::vector<boost::uint8_t>& fp);
namespace { namespace {
@ -22,9 +20,7 @@ namespace {
{ {
add_torrent_params p; add_torrent_params p;
std::vector<char> resume_buf; dict_to_add_torrent_params(params, p);
std::vector<boost::uint8_t> files_buf;
dict_to_add_torrent_params(params, p, resume_buf, files_buf);
allow_threading_guard guard; allow_threading_guard guard;

View File

@ -11,7 +11,7 @@
void bind_utility(); void bind_utility();
void bind_fingerprint(); void bind_fingerprint();
void bind_big_number(); void bind_sha1_hash();
void bind_session(); void bind_session();
void bind_entry(); void bind_entry();
void bind_torrent_info(); void bind_torrent_info();
@ -38,7 +38,7 @@ BOOST_PYTHON_MODULE(libtorrent)
bind_error_code(); bind_error_code();
bind_utility(); bind_utility();
bind_fingerprint(); bind_fingerprint();
bind_big_number(); bind_sha1_hash();
bind_entry(); bind_entry();
bind_session(); bind_session();
bind_torrent_info(); bind_torrent_info();

View File

@ -175,8 +175,7 @@ namespace
#endif #endif
} }
void dict_to_add_torrent_params(dict params, add_torrent_params& p void dict_to_add_torrent_params(dict params, add_torrent_params& p)
, std::vector<char>& rd, std::vector<boost::uint8_t>& fp)
{ {
// torrent_info objects are always held by an intrusive_ptr in the python binding // torrent_info objects are always held by an intrusive_ptr in the python binding
if (params.has_key("ti") && params.get("ti") != boost::python::object()) if (params.has_key("ti") && params.get("ti") != boost::python::object())
@ -191,9 +190,7 @@ namespace
if (params.has_key("resume_data")) if (params.has_key("resume_data"))
{ {
std::string resume = extract<std::string>(params["resume_data"]); std::string resume = extract<std::string>(params["resume_data"]);
rd.resize(resume.size()); p.resume_data.assign(resume.begin(), resume.end());
std::memcpy(&rd[0], &resume[0], rd.size());
p.resume_data = &rd;
} }
if (params.has_key("storage_mode")) if (params.has_key("storage_mode"))
p.storage_mode = extract<storage_mode_t>(params["storage_mode"]); p.storage_mode = extract<storage_mode_t>(params["storage_mode"]);
@ -248,14 +245,13 @@ namespace
if (params.has_key("uuid")) if (params.has_key("uuid"))
p.uuid = extract<std::string>(params["uuid"]); p.uuid = extract<std::string>(params["uuid"]);
fp.clear();
if (params.has_key("file_priorities")) if (params.has_key("file_priorities"))
{ {
list l = extract<list>(params["file_priorities"]); list l = extract<list>(params["file_priorities"]);
int n = boost::python::len(l); int n = boost::python::len(l);
for(int i = 0; i < n; i++) for(int i = 0; i < n; i++)
fp.push_back(extract<boost::uint8_t>(l[i])); p.file_priorities.push_back(extract<boost::uint8_t>(l[i]));
p.file_priorities = &fp; p.file_priorities.clear();
} }
} }
@ -265,9 +261,7 @@ namespace
torrent_handle add_torrent(session& s, dict params) torrent_handle add_torrent(session& s, dict params)
{ {
add_torrent_params p; add_torrent_params p;
std::vector<char> resume_buf; dict_to_add_torrent_params(params, p);
std::vector<boost::uint8_t> files_buf;
dict_to_add_torrent_params(params, p, resume_buf, files_buf);
allow_threading_guard guard; allow_threading_guard guard;
@ -282,9 +276,7 @@ namespace
void async_add_torrent(session& s, dict params) void async_add_torrent(session& s, dict params)
{ {
add_torrent_params p; add_torrent_params p;
std::vector<char> resume_buf; dict_to_add_torrent_params(params, p);
std::vector<boost::uint8_t> files_buf;
dict_to_add_torrent_params(params, p, resume_buf, files_buf);
allow_threading_guard guard; allow_threading_guard guard;
@ -296,9 +288,7 @@ namespace
#endif #endif
} }
void dict_to_feed_settings(dict params, feed_settings& feed void dict_to_feed_settings(dict params, feed_settings& feed)
, std::vector<char>& resume_buf
, std::vector<boost::uint8_t> files_buf)
{ {
if (params.has_key("auto_download")) if (params.has_key("auto_download"))
feed.auto_download = extract<bool>(params["auto_download"]); feed.auto_download = extract<bool>(params["auto_download"]);
@ -307,8 +297,7 @@ namespace
if (params.has_key("url")) if (params.has_key("url"))
feed.url = extract<std::string>(params["url"]); feed.url = extract<std::string>(params["url"]);
if (params.has_key("add_args")) if (params.has_key("add_args"))
dict_to_add_torrent_params(dict(params["add_args"]), feed.add_args dict_to_add_torrent_params(dict(params["add_args"]), feed.add_args);
, resume_buf, files_buf);
} }
feed_handle add_feed(session& s, dict params) feed_handle add_feed(session& s, dict params)
@ -316,9 +305,7 @@ namespace
feed_settings feed; feed_settings feed;
// this static here is a bit of a hack. It will // this static here is a bit of a hack. It will
// probably work for the most part // probably work for the most part
static std::vector<char> resume_buf; dict_to_feed_settings(params, feed);
static std::vector<boost::uint8_t> files_buf;
dict_to_feed_settings(params, feed, resume_buf, files_buf);
allow_threading_guard guard; allow_threading_guard guard;
return s.add_feed(feed); return s.add_feed(feed);
@ -364,9 +351,7 @@ namespace
void set_feed_settings(feed_handle& h, dict sett) void set_feed_settings(feed_handle& h, dict sett)
{ {
feed_settings feed; feed_settings feed;
static std::vector<char> resume_buf; dict_to_feed_settings(sett, feed);
static std::vector<boost::uint8_t> files_buf;
dict_to_feed_settings(sett, feed, resume_buf, files_buf);
h.set_settings(feed); h.set_settings(feed);
} }
@ -594,7 +579,6 @@ void bind_session()
; ;
enum_<session::options_t>("options_t") enum_<session::options_t>("options_t")
.value("none", session::none)
.value("delete_files", session::delete_files) .value("delete_files", session::delete_files)
; ;
@ -688,8 +672,7 @@ void bind_session()
#endif #endif
#endif #endif
.def("add_feed", &add_feed) .def("add_feed", &add_feed)
.def("remove_torrent", allow_threads(&session::remove_torrent), arg("option") = session::none .def("remove_torrent", allow_threads(&session::remove_torrent), arg("option") = 0)
)
#ifndef TORRENT_NO_DEPRECATE #ifndef TORRENT_NO_DEPRECATE
.def("set_local_download_rate_limit", allow_threads(&session::set_local_download_rate_limit)) .def("set_local_download_rate_limit", allow_threads(&session::set_local_download_rate_limit))
.def("local_download_rate_limit", allow_threads(&session::local_download_rate_limit)) .def("local_download_rate_limit", allow_threads(&session::local_download_rate_limit))
@ -765,7 +748,7 @@ void bind_session()
.def("is_paused", allow_threads(&session::is_paused)) .def("is_paused", allow_threads(&session::is_paused))
.def("id", allow_threads(&session::id)) .def("id", allow_threads(&session::id))
.def("get_cache_status", allow_threads(&session::get_cache_status)) .def("get_cache_status", allow_threads(&session::get_cache_status))
.def("get_cache_info", get_cache_info) .def("get_cache_info", get_cache_info)
.def("set_peer_id", allow_threads(&session::set_peer_id)) .def("set_peer_id", allow_threads(&session::set_peer_id))
; ;
@ -808,11 +791,11 @@ void bind_session()
register_ptr_to_python<std::auto_ptr<alert> >(); register_ptr_to_python<std::auto_ptr<alert> >();
def("high_performance_seed", high_performance_seed); def("high_performance_seed", high_performance_seed);
def("min_memory_usage", min_memory_usage); def("min_memory_usage", min_memory_usage);
scope().attr("create_metadata_plugin") = "metadata_transfer"; scope().attr("create_metadata_plugin") = "metadata_transfer";
scope().attr("create_ut_metadata_plugin") = "ut_metadata"; scope().attr("create_ut_metadata_plugin") = "ut_metadata";
scope().attr("create_ut_pex_plugin") = "ut_pex"; scope().attr("create_ut_pex_plugin") = "ut_pex";
scope().attr("create_smart_ban_plugin") = "smart_ban"; scope().attr("create_smart_ban_plugin") = "smart_ban";
} }

View File

@ -347,7 +347,7 @@ void bind_torrent_handle()
void (torrent_handle::*move_storage0)(std::string const&, int flags) const = &torrent_handle::move_storage; void (torrent_handle::*move_storage0)(std::string const&, int flags) const = &torrent_handle::move_storage;
void (torrent_handle::*rename_file0)(int, std::string const&) const = &torrent_handle::rename_file; void (torrent_handle::*rename_file0)(int, std::string const&) const = &torrent_handle::rename_file;
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
void (torrent_handle::*move_storage1)(std::wstring const&, int flags) const = &torrent_handle::move_storage; void (torrent_handle::*move_storage1)(std::wstring const&, int flags) const = &torrent_handle::move_storage;
void (torrent_handle::*rename_file1)(int, std::wstring const&) const = &torrent_handle::rename_file; void (torrent_handle::*rename_file1)(int, std::wstring const&) const = &torrent_handle::rename_file;
#endif #endif
@ -459,7 +459,7 @@ void bind_torrent_handle()
.def("force_recheck", _(&torrent_handle::force_recheck)) .def("force_recheck", _(&torrent_handle::force_recheck))
.def("rename_file", _(rename_file0)) .def("rename_file", _(rename_file0))
.def("set_ssl_certificate", &torrent_handle::set_ssl_certificate, (arg("cert"), arg("private_key"), arg("dh_params"), arg("passphrase")="")) .def("set_ssl_certificate", &torrent_handle::set_ssl_certificate, (arg("cert"), arg("private_key"), arg("dh_params"), arg("passphrase")=""))
#if TORRENT_USE_WSTRING #if TORRENT_USE_WSTRING && !defined TORRENT_NO_DEPRECATE
.def("move_storage", _(move_storage1), (arg("path"), arg("flags") = 0)) .def("move_storage", _(move_storage1), (arg("path"), arg("flags") = 0))
.def("rename_file", _(rename_file1)) .def("rename_file", _(rename_file1))
#endif #endif