Merge pull request #383 from aresch/py3

Add session.post_session_stats() to python bindings
This commit is contained in:
Arvid Norberg 2016-01-13 22:49:12 -05:00
commit 3c1c1f4dad
3 changed files with 28 additions and 0 deletions

View File

@ -6,6 +6,7 @@
#include <libtorrent/alert.hpp>
#include <libtorrent/alert_types.hpp>
#include <libtorrent/piece_picker.hpp> // for piece_block
#include <libtorrent/session_stats.hpp>
#include <memory>
using namespace boost::python;
@ -179,6 +180,19 @@ dict dht_put_item(dht_put_alert const& alert)
return d;
}
dict session_stats_values(session_stats_alert const& alert)
{
std::vector<stats_metric> map = session_stats_metrics();
dict d;
for (std::vector<stats_metric>::const_iterator i = map.begin();
i != map.end(); ++i)
{
d[i->name] = alert.values[i->value_index];
}
return d;
}
void bind_alert()
{
using boost::noncopyable;
@ -725,4 +739,8 @@ void bind_alert()
"dht_put_alert", no_init)
.add_property("item", &dht_put_item)
;
class_<session_stats_alert, bases<alert>, noncopyable>(
"session_stats_alert", no_init)
.add_property("values", &session_stats_values)
;
}

View File

@ -748,6 +748,7 @@ void bind_session()
)
#endif
.def("post_torrent_updates", allow_threads(&lt::session::post_torrent_updates))
.def("post_session_stats", allow_threads(&lt::session::post_session_stats))
.def("outgoing_ports", &outgoing_ports)
.def("is_listening", allow_threads(&lt::session::is_listening))
.def("listen_port", allow_threads(&lt::session::listen_port))

View File

@ -73,6 +73,15 @@ class test_sha1hash(unittest.TestCase):
self.assertEqual(h, str(s))
class test_session(unittest.TestCase):
def test_post_session_stats(self):
s = lt.session({'alert_mask': lt.alert.category_t.stats_notification})
s.post_session_stats()
a = s.wait_for_alert(1000)
self.assertTrue(isinstance(a, lt.session_stats_alert))
self.assertTrue(isinstance(a.values, dict))
self.assertTrue(len(a.values) > 0)
if __name__ == '__main__':
print(lt.__version__)
unittest.main()