| 1 | #!/usr/bin/python |
|---|
| 2 | # |
|---|
| 3 | # This software is in the public domain, furnished "as is", without technical |
|---|
| 4 | # support, and with no warranty, express or implied, as to its usefulness for |
|---|
| 5 | # any purpose. |
|---|
| 6 | # |
|---|
| 7 | # deluge_config.py |
|---|
| 8 | # This code (at least in theory) allows one to alter configuration settings |
|---|
| 9 | # on a deluge backend. At the moment, though, it only alters the parameters |
|---|
| 10 | # that I've found useful to change. |
|---|
| 11 | # |
|---|
| 12 | # Authour: Garett Harnish |
|---|
| 13 | |
|---|
| 14 | from sys import argv, exit, stderr |
|---|
| 15 | from optparse import OptionParser |
|---|
| 16 | |
|---|
| 17 | import logging |
|---|
| 18 | |
|---|
| 19 | def isFloatDigit (string): |
|---|
| 20 | if string.isdigit(): |
|---|
| 21 | return True |
|---|
| 22 | else: |
|---|
| 23 | try: |
|---|
| 24 | tmp = float(string) |
|---|
| 25 | return True |
|---|
| 26 | except: return False; |
|---|
| 27 | |
|---|
| 28 | # set up command-line options |
|---|
| 29 | parser = OptionParser() |
|---|
| 30 | parser.add_option("--port", help="port for deluge backend host (default: 58846)", default="58846", dest="port") |
|---|
| 31 | parser.add_option("--host", help="hostname of deluge backend to connect to (default: localhost)", default="localhost", dest="host") |
|---|
| 32 | parser.add_option("--max_active_limit", help="sets the absolute maximum number of active torrents on the deluge backend", dest="max_active_limit") |
|---|
| 33 | parser.add_option("--max_active_downloading", help="sets the maximum number of active downloading torrents on the deluge backend", dest="max_active_downloading") |
|---|
| 34 | parser.add_option("--max_active_seeding", help="sets the maximum number of active seeding torrents on the deluge backend", dest="max_active_seeding") |
|---|
| 35 | parser.add_option("--max_download_speed", help="sets the maximum global download speed on the deluge backend", dest="max_download_speed") |
|---|
| 36 | parser.add_option("--max_upload_speed", help="sets the maximum global upload speed on the deluge backend", dest="max_upload_speed") |
|---|
| 37 | parser.add_option("--debug", help="outputs debug information to the console", default=False, action="store_true", dest="debug") |
|---|
| 38 | |
|---|
| 39 | # grab command-line options |
|---|
| 40 | (options, args) = parser.parse_args() |
|---|
| 41 | |
|---|
| 42 | if not options.debug: |
|---|
| 43 | logging.disable(logging.ERROR) |
|---|
| 44 | |
|---|
| 45 | settings = {} |
|---|
| 46 | |
|---|
| 47 | # set values if set and valid |
|---|
| 48 | if options.max_active_limit: |
|---|
| 49 | if options.max_active_limit.isdigit() and int(options.max_active_limit) >= 0: |
|---|
| 50 | settings['max_active_limit'] = int(options.max_active_limit) |
|---|
| 51 | else: |
|---|
| 52 | stderr.write ("ERROR: Invalid max_active_limit parameter!\n") |
|---|
| 53 | exit (-1) |
|---|
| 54 | |
|---|
| 55 | if options.max_active_downloading: |
|---|
| 56 | if options.max_active_downloading.isdigit() and int(options.max_active_downloading) >= 0: |
|---|
| 57 | settings['max_active_downloading'] = int(options.max_active_downloading) |
|---|
| 58 | else: |
|---|
| 59 | stderr.write ("ERROR: Invalid max_active_downloading parameter!\n") |
|---|
| 60 | exit (-1) |
|---|
| 61 | |
|---|
| 62 | if options.max_active_seeding: |
|---|
| 63 | if options.max_active_seeding.isdigit() and int(options.max_active_seeding) >= 0: |
|---|
| 64 | settings['max_active_seeding'] = int(options.max_active_seeding) |
|---|
| 65 | else: |
|---|
| 66 | stderr.write ("ERROR: Invalid max_active_seeding parameter!\n") |
|---|
| 67 | exit (-1) |
|---|
| 68 | |
|---|
| 69 | if options.max_download_speed: |
|---|
| 70 | if isFloatDigit(options.max_download_speed) and (float(options.max_download_speed) >= 0.0 or float(options.max_download_speed) == -1.0): |
|---|
| 71 | settings['max_download_speed'] = float(options.max_download_speed) |
|---|
| 72 | else: |
|---|
| 73 | stderr.write ("ERROR: Invalid max_download_speed parameter!\n") |
|---|
| 74 | exit (-1) |
|---|
| 75 | |
|---|
| 76 | if options.max_upload_speed: |
|---|
| 77 | if isFloatDigit(options.max_upload_speed) and (float(options.max_upload_speed) >= 0.0 or float(options.max_upload_speed) == -1.0): |
|---|
| 78 | settings['max_upload_speed'] = float(options.max_upload_speed) |
|---|
| 79 | else: |
|---|
| 80 | stderr.write ("ERROR: Invalid max_upload_speed parameter!\n") |
|---|
| 81 | exit (-1) |
|---|
| 82 | |
|---|
| 83 | # If there is something to do ... |
|---|
| 84 | if settings: |
|---|
| 85 | # create connection to daemon |
|---|
| 86 | from deluge.ui.client import sclient as client |
|---|
| 87 | client.set_core_uri("http://" + options.host + ":" + options.port) |
|---|
| 88 | |
|---|
| 89 | # commit configurations changes |
|---|
| 90 | client.set_config(settings) |
|---|