Révision edf886e0
Reformat deluge_ with black and flake8
| plugins/torrent/deluge_ | ||
|---|---|---|
| 101 | 101 |
|
| 102 | 102 |
import logging |
| 103 | 103 |
import os |
| 104 |
import string |
|
| 105 | 104 |
import sys |
| 106 | 105 |
|
| 107 | 106 |
|
| 108 | 107 |
try: |
| 109 | 108 |
from deluge.log import setup_logger |
| 110 | 109 |
from deluge.ui.client import client |
| 111 |
from twisted.internet import reactor, defer |
|
| 110 |
from twisted.internet import reactor |
|
| 111 |
|
|
| 112 | 112 |
setup_logger() |
| 113 | 113 |
except (ImportError, NameError): |
| 114 | 114 |
successful_import = False |
| ... | ... | |
| 122 | 122 |
log.setLevel(logging.WARNING) |
| 123 | 123 |
|
| 124 | 124 |
conf = {
|
| 125 |
'host': os.getenv('host', '127.0.0.1'),
|
|
| 126 |
'port': int(os.getenv('port', '58846')),
|
|
| 127 |
'username': os.getenv('username', ''),
|
|
| 128 |
'password': os.getenv('password', '')
|
|
| 125 |
"host": os.getenv("host", "127.0.0.1"),
|
|
| 126 |
"port": int(os.getenv("port", "58846")),
|
|
| 127 |
"username": os.getenv("username", ""),
|
|
| 128 |
"password": os.getenv("password", ""),
|
|
| 129 | 129 |
} |
| 130 | 130 |
|
| 131 | 131 |
modes = ["bandwidth", "peers", "states"] |
| 132 | 132 |
|
| 133 | 133 |
connection_keys = [ |
| 134 |
{ 'id': 'peer.num_peers_connected', 'label': 'Total' },
|
|
| 135 |
{ 'id': 'peer.num_peers_half_open', 'label': 'Half open' },
|
|
| 136 |
{ 'id': 'peer.num_peers_up_interested', 'label': 'Interested (upload)' },
|
|
| 137 |
{ 'id': 'peer.num_peers_down_interested', 'label': 'Interested (download)' }
|
|
| 134 |
{"id": "peer.num_peers_connected", "label": "Total"},
|
|
| 135 |
{"id": "peer.num_peers_half_open", "label": "Half open"},
|
|
| 136 |
{"id": "peer.num_peers_up_interested", "label": "Interested (upload)"},
|
|
| 137 |
{"id": "peer.num_peers_down_interested", "label": "Interested (download)"},
|
|
| 138 | 138 |
] |
| 139 | 139 |
|
| 140 | 140 |
torrent_keys = [ |
| 141 |
{ 'id': 'ses.num_seeding_torrents', 'label': 'Seeding' },
|
|
| 142 |
{ 'id': 'ses.num_upload_only_torrents', 'label': 'Uploading' },
|
|
| 143 |
{ 'id': 'ses.num_downloading_torrents', 'label': 'Downloading' },
|
|
| 144 |
{ 'id': 'ses.num_checking_torrents', 'label': 'Checking' },
|
|
| 145 |
{ 'id': 'ses.num_stopped_torrents', 'label': 'Stopped' },
|
|
| 146 |
{ 'id': 'ses.num_queued_seeding_torrents', 'label': 'Queued seeding' },
|
|
| 147 |
{ 'id': 'ses.num_queued_download_torrents', 'label': 'Queued downloads' },
|
|
| 148 |
{ 'id': 'ses.num_error_torrents', 'label': 'Error' },
|
|
| 141 |
{"id": "ses.num_seeding_torrents", "label": "Seeding"},
|
|
| 142 |
{"id": "ses.num_upload_only_torrents", "label": "Uploading"},
|
|
| 143 |
{"id": "ses.num_downloading_torrents", "label": "Downloading"},
|
|
| 144 |
{"id": "ses.num_checking_torrents", "label": "Checking"},
|
|
| 145 |
{"id": "ses.num_stopped_torrents", "label": "Stopped"},
|
|
| 146 |
{"id": "ses.num_queued_seeding_torrents", "label": "Queued seeding"},
|
|
| 147 |
{"id": "ses.num_queued_download_torrents", "label": "Queued downloads"},
|
|
| 148 |
{"id": "ses.num_error_torrents", "label": "Error"},
|
|
| 149 | 149 |
] |
| 150 | 150 |
|
| 151 |
|
|
| 151 | 152 |
def for_munin(value): |
| 152 |
return value.replace('.', '').replace('_', '')
|
|
| 153 |
return value.replace(".", "").replace("_", "")
|
|
| 153 | 154 |
|
| 154 | 155 |
|
| 155 | 156 |
class StatClient: |
| 156 |
|
|
| 157 | 157 |
def __init__(self, conf, mode): |
| 158 | 158 |
self.conf = conf |
| 159 | 159 |
self.mode = mode |
| ... | ... | |
| 169 | 169 |
reactor.stop() |
| 170 | 170 |
|
| 171 | 171 |
def fetch_info(self): |
| 172 |
log.debug("Connecting to %s:%d ...",
|
|
| 173 |
self.conf['host'], self.conf['port']) |
|
| 172 |
log.debug("Connecting to %s:%d ...", self.conf["host"], self.conf["port"])
|
|
| 174 | 173 |
client.connect( |
| 175 |
self.conf['host'], |
|
| 176 |
self.conf['port'], |
|
| 177 |
self.conf['username'], |
|
| 178 |
self.conf['password']).addCallbacks( |
|
| 174 |
self.conf["host"], |
|
| 175 |
self.conf["port"], |
|
| 176 |
self.conf["username"], |
|
| 177 |
self.conf["password"], |
|
| 178 |
).addCallbacks( |
|
| 179 | 179 |
self.on_connect_success, |
| 180 | 180 |
self.end_session, |
| 181 |
errbackArgs=("Connection failed: check settings and try again."))
|
|
| 181 |
errbackArgs=("Connection failed: check settings and try again."),
|
|
| 182 |
) |
|
| 182 | 183 |
reactor.run() |
| 183 | 184 |
|
| 184 |
|
|
| 185 | 185 |
def on_connect_success(self, result): |
| 186 | 186 |
log.debug("Connection was successful")
|
| 187 | 187 |
self.connected = True |
| ... | ... | |
| 190 | 190 |
log.debug("Calling get_session_status")
|
| 191 | 191 |
keys = [] |
| 192 | 192 |
for connection_key in connection_keys: |
| 193 |
keys.append(connection_key['id'])
|
|
| 193 |
keys.append(connection_key["id"])
|
|
| 194 | 194 |
client.core.get_session_status(keys=keys).addCallbacks( |
| 195 | 195 |
self.on_peer_session_status, |
| 196 |
self.end_session, errbackArgs=("get_session_status failed"))
|
|
| 196 |
self.end_session, |
|
| 197 |
errbackArgs=("get_session_status failed"),
|
|
| 198 |
) |
|
| 197 | 199 |
elif self.mode == "bandwidth": |
| 198 | 200 |
log.debug("Calling get_session_status")
|
| 199 | 201 |
interesting_status = [ |
| 200 |
'upload_rate', 'payload_upload_rate', |
|
| 201 |
'download_rate', 'payload_download_rate'] |
|
| 202 |
"upload_rate", |
|
| 203 |
"payload_upload_rate", |
|
| 204 |
"download_rate", |
|
| 205 |
"payload_download_rate", |
|
| 206 |
] |
|
| 202 | 207 |
client.core.get_session_status(interesting_status).addCallbacks( |
| 203 | 208 |
self.on_bandwidth, |
| 204 | 209 |
self.end_session, |
| 205 |
errbackArgs=("get_session_status failed"))
|
|
| 210 |
errbackArgs=("get_session_status failed"),
|
|
| 211 |
) |
|
| 206 | 212 |
elif self.mode == "states": |
| 207 | 213 |
log.debug("Calling get_session_state")
|
| 208 | 214 |
keys = [] |
| 209 | 215 |
for torrent_key in torrent_keys: |
| 210 |
keys.append(torrent_key['id'])
|
|
| 216 |
keys.append(torrent_key["id"])
|
|
| 211 | 217 |
client.core.get_session_status(keys=keys).addCallbacks( |
| 212 | 218 |
self.on_torrent_session_state, |
| 213 | 219 |
self.end_session, |
| 214 |
errbackArgs=("get_session_state failed"))
|
|
| 220 |
errbackArgs=("get_session_state failed"),
|
|
| 221 |
) |
|
| 215 | 222 |
else: |
| 216 | 223 |
log.error("Unknown mode '%s'", mode)
|
| 217 | 224 |
sys.exit(1) |
| ... | ... | |
| 228 | 235 |
def on_bandwidth(self, values): |
| 229 | 236 |
log.debug("Got bandwidth info from the daemon : %s", values)
|
| 230 | 237 |
|
| 231 |
download_rate = values['download_rate']
|
|
| 232 |
payload_download_rate = values['payload_download_rate']
|
|
| 238 |
download_rate = values["download_rate"]
|
|
| 239 |
payload_download_rate = values["payload_download_rate"]
|
|
| 233 | 240 |
overhead_download_rate = download_rate - payload_download_rate |
| 234 |
upload_rate = values['upload_rate']
|
|
| 235 |
payload_upload_rate = values['payload_upload_rate']
|
|
| 241 |
upload_rate = values["upload_rate"]
|
|
| 242 |
payload_upload_rate = values["payload_upload_rate"]
|
|
| 236 | 243 |
overhead_upload_rate = upload_rate - payload_upload_rate |
| 237 | 244 |
|
| 238 | 245 |
print(f"payloadDownloadRate.value {payload_download_rate}")
|
| ... | ... | |
| 246 | 253 |
|
| 247 | 254 |
for torrent_key in torrent_keys: |
| 248 | 255 |
print( |
| 249 |
f"{for_munin(torrent_key['id'])}.value "
|
|
| 250 |
f"{result[torrent_key['id']]}"
|
|
| 256 |
f"{for_munin(torrent_key['id'])}.value " f"{result[torrent_key['id']]}"
|
|
| 251 | 257 |
) |
| 252 | 258 |
self.end_session("Done")
|
| 253 | 259 |
|
| 254 | 260 |
|
| 255 | 261 |
def get_mode(): |
| 256 | 262 |
script_name = os.path.basename(sys.argv[0]) |
| 257 |
mode = script_name[script_name.rindex('_') + 1:]
|
|
| 263 |
mode = script_name[script_name.rindex("_") + 1 :]
|
|
| 258 | 264 |
|
| 259 | 265 |
log.debug("Mode : %s", mode)
|
| 260 | 266 |
|
| ... | ... | |
| 274 | 280 |
print("graph_scale yes")
|
| 275 | 281 |
print("graph_category filetransfer")
|
| 276 | 282 |
print( |
| 277 |
"graph_info This graph shows the number of peers for the Deluge Torrent client") |
|
| 283 |
"graph_info This graph shows the number of peers for the Deluge Torrent client" |
|
| 284 |
) |
|
| 278 | 285 |
for connection_key in connection_keys: |
| 279 | 286 |
print(f"{for_munin(connection_key['id'])}.label {connection_key['label']}")
|
| 280 | 287 |
print(f"{for_munin(connection_key['id'])}.min 0")
|
| 281 | 288 |
elif mode == "bandwidth": |
| 282 | 289 |
print("graph_title Bandwidth usage")
|
| 283 |
print("graph_order payloadDownloadRate overheadDownloadRate payloadUploadRate "
|
|
| 284 |
"overheadUploadRate") |
|
| 290 |
print( |
|
| 291 |
"graph_order payloadDownloadRate overheadDownloadRate payloadUploadRate " |
|
| 292 |
"overheadUploadRate" |
|
| 293 |
) |
|
| 285 | 294 |
print("graph_args --base 1024 -r")
|
| 286 | 295 |
print("graph_vlabel bytes/s : down(-) and up(+)")
|
| 287 | 296 |
print("graph_scale yes")
|
| ... | ... | |
| 299 | 308 |
print("overheadDownloadRate.draw STACK")
|
| 300 | 309 |
print("overheadDownloadRate.min 0")
|
| 301 | 310 |
print("overheadDownloadRate.graph no")
|
| 302 |
print("overheadDownloadRate.info Bandwidth 'lost' due to overhead while downloading and "
|
|
| 303 |
"uploading torrents") |
|
| 311 |
print( |
|
| 312 |
"overheadDownloadRate.info Bandwidth 'lost' due to overhead while downloading and " |
|
| 313 |
"uploading torrents" |
|
| 314 |
) |
|
| 304 | 315 |
|
| 305 | 316 |
print("payloadUploadRate.label payload")
|
| 306 | 317 |
print("payloadUploadRate.draw AREA")
|
| ... | ... | |
| 312 | 323 |
print("overheadUploadRate.draw STACK")
|
| 313 | 324 |
print("overheadUploadRate.min 0")
|
| 314 | 325 |
print("overheadUploadRate.negative overheadDownloadRate")
|
| 315 |
print("overheadUploadRate.info Bandwidth 'lost' due to overhead while downloading and "
|
|
| 316 |
"uploading torrents") |
|
| 326 |
print( |
|
| 327 |
"overheadUploadRate.info Bandwidth 'lost' due to overhead while downloading and " |
|
| 328 |
"uploading torrents" |
|
| 329 |
) |
|
| 317 | 330 |
elif mode == "states": |
| 318 | 331 |
print("graph_title Torrent states")
|
| 319 | 332 |
print("graph_args --base 1000 -r --lower-limit 0")
|
| 320 | 333 |
print("graph_vlabel number of torrents")
|
| 321 | 334 |
print("graph_scale yes")
|
| 322 |
print("graph_info This graph shows the states of the torrents in Deluge Torrent")
|
|
| 335 |
print( |
|
| 336 |
"graph_info This graph shows the states of the torrents in Deluge Torrent" |
|
| 337 |
) |
|
| 323 | 338 |
print("graph_category filetransfer")
|
| 324 | 339 |
print("graph_period second")
|
| 325 | 340 |
|
| ... | ... | |
| 332 | 347 |
|
| 333 | 348 |
def fetch_info(mode): |
| 334 | 349 |
if not successful_import: |
| 335 |
print('Missing imports, cannot run !', file=sys.stderr)
|
|
| 350 |
print("Missing imports, cannot run !", file=sys.stderr)
|
|
| 336 | 351 |
sys.exit(1) |
| 337 | 352 |
|
| 338 | 353 |
log.debug("Launching tests")
|
| ... | ... | |
| 348 | 363 |
sys.exit(0) |
| 349 | 364 |
elif action == "autoconf": |
| 350 | 365 |
if not successful_import: |
| 351 |
print('no (required modules not found)')
|
|
| 366 |
print("no (required modules not found)")
|
|
| 352 | 367 |
sys.exit(0) |
| 353 |
print('yes')
|
|
| 368 |
print("yes")
|
|
| 354 | 369 |
elif action == "suggest": |
| 355 | 370 |
for mode in modes: |
| 356 | 371 |
print(mode) |
Formats disponibles : Unified diff