/
_mixin.py
179 lines (137 loc) · 5.84 KB
/
_mixin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details
# http://www.gnu.org/licenses/gpl-3.0.txt
"""Mixin classes that are common between TUI and CLI commands"""
from ... import objects
class get_single_torrent():
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]
class get_torrent_sorter():
def get_torrent_sorter(self, args):
"""
Return TorrentSorter instance or None
If `args` evaluates to True, it is passed to TorrentSorter and the
result is returned.
If `args` evaluates to False, None is returned.
"""
if args:
from ...client import TorrentSorter
return TorrentSorter(objects.localcfg.validate('sort.torrents', args))
class get_torrent_columns():
def get_torrent_columns(self, columns):
"""
Check if each item in iterable `columns` is a valid torrent list column name
Raise ValueError or return a new list of `columns`.
"""
return objects.localcfg.validate('columns.torrents', columns)
class get_file_columns():
def get_file_columns(self, columns):
"""
Check if each item in iterable `columns` is a valid file list column name
Raise ValueError or return a new list of `columns`.
"""
return objects.localcfg.validate('columns.files', columns)
class get_peer_sorter():
def get_peer_sorter(self, args):
"""
Return PeerSorter instance or None
If `args` evaluates to True, it is passed to PeerSorter and the result
is returned.
If `args` evaluates to False, None is returned.
"""
if args:
from ...client import PeerSorter
return PeerSorter(objects.localcfg.validate('sort.peers', args))
class get_peer_filter():
def get_peer_filter(self, FILTER):
"""
Return PeerFilter instance or None
If `FILTER` evaluates to True, it is passed to PeerFilter and the
resulting object is returned.
If `FILTER` evaluates to False, None is returned.
"""
if FILTER:
from ...client import PeerFilter
return PeerFilter(FILTER)
class get_peer_columns():
def get_peer_columns(self, columns):
"""
Check if each item in iterable `columns` is a valid peer list column name
Raise ValueError or return a new list of `columns`.
"""
cols = objects.localcfg.validate('columns.peers', columns)
if 'country' in cols:
if not objects.localcfg['geoip']:
cols = cols.copy(*(col for col in cols if col != 'country'))
return cols
class get_tracker_sorter():
def get_tracker_sorter(self, args):
"""
Return TrackerSorter instance or None
If `args` evaluates to True, it is passed to TrackerSorter and
the result is returned.
If `args` evaluates to False, None is returned.
"""
if args:
from ...client import TrackerSorter
return TrackerSorter(objects.localcfg.validate('sort.trackers', args))
class get_tracker_filter():
def get_tracker_filter(self, FILTER):
"""
Return TrackerFilter instance or None
If `FILTER` evaluates to True, it is passed to TrackerFilter and
the resulting object is returned.
If `FILTER` evaluates to False, None is returned.
"""
if FILTER:
from ...client import TrackerFilter
return TrackerFilter(FILTER)
class get_tracker_columns():
def get_tracker_columns(self, columns):
"""
Check if each item in iterable `columns` is a valid tracker list column name
Raise ValueError or return a new list of `columns`.
"""
return objects.localcfg.validate('columns.trackers', columns)
class get_setting_sorter():
def get_setting_sorter(self, args):
"""
Return SettingSorter instance or None
If `args` evaluates to True, it is passed to SettingSorter and the
result is returned.
If `args` evaluates to False, None is returned.
"""
if args:
from ...client import SettingSorter
return SettingSorter(objects.localcfg.validate('sort.settings', args))
class get_setting_columns():
def get_setting_columns(self, columns):
"""
Check if each item in iterable `columns` is a valid setting list column name
Raise ValueError or return a new list of `columns`.
"""
return objects.localcfg.validate('columns.settings', columns)