1 | #!/usr/bin/env python
|
---|
2 | #
|
---|
3 | # mapping.py
|
---|
4 | #
|
---|
5 | # Copyright (C) 2008-2009 Ido Abramovich <ido.deluge@gmail.com>
|
---|
6 | #
|
---|
7 | # Deluge is free software.
|
---|
8 | #
|
---|
9 | # You may redistribute it and/or modify it under the terms of the
|
---|
10 | # GNU General Public License, as published by the Free Software
|
---|
11 | # Foundation; either version 3 of the License, or (at your option)
|
---|
12 | # any later version.
|
---|
13 | #
|
---|
14 | # deluge is distributed in the hope that it will be useful,
|
---|
15 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
16 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
---|
17 | # See the GNU General Public License for more details.
|
---|
18 | #
|
---|
19 | # You should have received a copy of the GNU General Public License
|
---|
20 | # along with deluge. If not, write to:
|
---|
21 | # The Free Software Foundation, Inc.,
|
---|
22 | # 51 Franklin Street, Fifth Floor
|
---|
23 | # Boston, MA 02110-1301, USA.
|
---|
24 | #
|
---|
25 | from deluge.ui.client import client
|
---|
26 | from deluge.ui.console.main import match_torrents
|
---|
27 | import re
|
---|
28 | import logging
|
---|
29 | from twisted.internet import defer
|
---|
30 |
|
---|
31 | _idregex = re.compile(r'^[0-9a-f]{40}$')
|
---|
32 |
|
---|
33 | _mapping = {}
|
---|
34 |
|
---|
35 | def _arg_is_id(arg):
|
---|
36 | return bool(_idregex.match(arg))
|
---|
37 |
|
---|
38 | def get_names(torrents):
|
---|
39 | d = defer.Deferred()
|
---|
40 | def _got_torrents_status(states):
|
---|
41 | try:
|
---|
42 | d.callback(list([ (tid, state['name']) for (tid, state) in states.items() ]))
|
---|
43 | except Exception, e:
|
---|
44 | print e
|
---|
45 | d.errback(e)
|
---|
46 |
|
---|
47 | client.core.get_torrents_status({'id':torrents}, ['name']).addCallback(_got_torrents_status)
|
---|
48 | return d
|
---|
49 |
|
---|
50 | def rehash():
|
---|
51 | global _mapping
|
---|
52 | d = defer.Deferred()
|
---|
53 | def on_match_torrents(torrents):
|
---|
54 | def on_get_names(names):
|
---|
55 | _mapping = dict([(x[1],x[0]) for x in names])
|
---|
56 | d.callback()
|
---|
57 | get_names(torrents).addCallback(on_get_names)
|
---|
58 | match_torrents().addCallback(on_match_torrents)
|
---|
59 | return d
|
---|
60 |
|
---|
61 | def to_ids(args):
|
---|
62 | d = defer.Deferred()
|
---|
63 | def on_rehash(result):
|
---|
64 | res = []
|
---|
65 | for i in args:
|
---|
66 | if _arg_is_id(i):
|
---|
67 | res.append(i)
|
---|
68 | else:
|
---|
69 | if i in _mapping:
|
---|
70 | res.append(_mapping[i])
|
---|
71 | d.callback(res)
|
---|
72 | rehash().addCallback(on_rehash)
|
---|
73 |
|
---|
74 | return d
|
---|
75 |
|
---|
76 | def names():
|
---|
77 | return _mapping.keys()
|
---|