Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if TORRENT and '/' in TORRENT:
FILTER, PATH = TORRENT.split('/', maxsplit=1)
renaming_torrent = False
else:
FILTER, PATH = TORRENT, None
renaming_torrent = True
try:
tfilter = self.select_torrents(FILTER,
allow_no_filter=False,
discover_torrent=True)
except ValueError as e:
raise CmdError(e)
else:
response = await self.make_request(
objects.srvapi.torrent.torrents(tfilter, keys=('id',)),
quiet=True)
if not response.success:
raise CmdError()
elif (unique or renaming_torrent) and len(response.torrents) > 1:
# When renaming a torrent or --unique is given, tfilter must
# match exactly one torrent. If it matches zero torrents,
# make_request() below with produce the appropriate error
# message.
raise CmdError('%s matches more than one torrent' % tfilter)
else:
success = True
for torrent in response.torrents:
tid = torrent['id']
response = await self.make_request(
objects.srvapi.torrent.rename(tid, path=PATH, new_name=NEW),
polling_frenzy=True)
async def get_single_torrent(self, tfilter, keys=(), one_or_none=False):
"""
Get a single torrent that matches TorrentFilter `tfilter`
A single Torrent instance is returned if there is only one match or if
one_or_none evaluates to False, in which case all torrents are sorted by
name and the first one is returned.
Return None if no match is found or if there are multiple matches and
`one_or_none` evaluates to True.
"""
keys = tuple(keys)
if 'name' not in keys:
keys = keys + ('name',)
request = objects.srvapi.torrent.torrents(tfilter, keys=keys)
response = await self.make_request(request, polling_frenzy=False, quiet=True)
if response.success:
if len(response.torrents) == 1 or not one_or_none:
from ...client import TorrentSorter
torrents = TorrentSorter(('name',)).apply(response.torrents)
return torrents[0]
async def make_peer_list(self, tfilter, pfilter, sort, columns):
response = await self.make_request(
objects.srvapi.torrent.torrents(tfilter, keys=('name', 'peers')),
quiet=True)
torrents = response.torrents
if len(torrents) < 1:
raise CmdError()
if pfilter is None:
filter_peers = lambda peers: peers
else:
filter_peers = lambda peers: pfilter.apply(peers)
peerlist = []
for torrent in sorted(torrents, key=lambda t: t['name'].lower()):
peerlist.extend(filter_peers(torrent['peers']))
# Pre-lookup peers' IPs
async def _torrent_filter_values(filter_name):
filter_cls = _utils.get_filter_cls('TorrentFilter')
cands = ()
if _utils.filter_takes_completable_values(filter_cls, filter_name):
keys = filter_cls(filter_name).needed_keys
response = await objects.srvapi.torrent.torrents(keys=keys, from_cache=True)
if response.success:
value_getter = _utils.get_filter_spec(filter_cls, filter_name).value_getter
cands = []
for t in response.torrents:
# Get the same value from torrent that the filter would get
value = value_getter(t)
# Some value_getters return multiple values, e.q. the torrent
# filter "tracker", which returns domain names for all tracker
# URLs.
if isinstance(value, (abc.Iterable, abc.Iterator)) and not isinstance(value, str):
cands.extend(value)
else:
cands.append(value)
curarg_seps = itertools.chain(_utils.filter_compare_ops, _utils.filter_combine_ops)
return Candidates(cands,
async def _show_individual_limits(self, TORRENT_FILTER, directions):
try:
tfilter = self.select_torrents(TORRENT_FILTER,
allow_no_filter=False,
discover_torrent=True)
except ValueError as e:
raise CmdError(e)
request = objects.srvapi.torrent.torrents(
tfilter, keys=('name', 'limit-rate-up', 'limit-rate-down'))
response = await self.make_request(request, polling_frenzy=True, quiet=True)
if response.success:
for t in response.torrents:
for d in directions:
self._output('%s %sload rate limit: %s' %
(t['name'], d, t['limit-rate-%s' % d]))
async def _fetch_torrent_data(self, torrent_id, keys):
log.debug('Fetching fresh Torrent #%d with keys: %r', torrent_id, keys)
# Request new torrent because we can't be sure the wanted key
# exists in widget.data
request = objects.srvapi.torrent.torrents((torrent_id,), keys=keys)
response = await self.make_request(request, quiet=True)
if not response.success:
raise CmdError()
else:
return response.torrents[0]
async def make_tracker_list(self, torfilter, trkfilter, sort, columns):
response = await self.make_request(
objects.srvapi.torrent.torrents(torfilter, keys=('name', 'trackers')),
quiet=True)
torrents = response.torrents
if len(torrents) < 1:
raise CmdError()
if trkfilter is None:
filter_trackers = lambda trackers: trackers
else:
filter_trackers = lambda trackers: trkfilter.apply(trackers)
trklist = []
for torrent in sorted(torrents, key=lambda t: t['name'].lower()):
trklist.extend(filter_trackers(torrent['trackers']))
sort.apply(trklist, inplace=True)
grid = urwid.GridFlow([], cell_width=1, h_sep=3, v_sep=1, align='left')
for section in sections:
opts = grid.options('given', section.width)
section_wrapped = add_title(section.title, section)
grid.contents.append((section_wrapped, opts))
self._grid = grid
self._content = Scrollable(grid)
super().__init__(urwid.AttrMap(
ScrollBar(urwid.AttrMap(self._content, 'torrentdetails')),
'torrentdetails.scrollbar'
))
# Register new request in request pool
keys = set(('name',)).union(key for w in sections for key in w.needed_keys)
self._poller = objects.srvapi.create_poller(objects.srvapi.torrent.torrents, (tid,), keys=keys)
self._poller.on_response(self._handle_response)
self._poller.on_error(self._handle_error)