Ticket #1971: 0001-Unified-common-options.patch
File 0001-Unified-common-options.patch, 14.5 KB (added by , 13 years ago) |
---|
-
new file deluge/commonoptions.py
From d2245742c77fc907dc794edf082e015ea562745b Mon Sep 17 00:00:00 2001 From: Jamie Lennox <jamielennox@gmail.com> Date: Sat, 19 Nov 2011 16:27:56 +1100 Subject: [PATCH] Unified common options. Created a CommonOptionsParser which handles the standard set of options. Modified main.py and ui/ui.py to use the new CommonOptionsParser --- deluge/commonoptions.py | 91 ++++++++++++++++++++++++++++++++++++++ deluge/main.py | 110 +++++++++------------------------------------- deluge/ui/ui.py | 42 +----------------- 3 files changed, 115 insertions(+), 128 deletions(-) create mode 100644 deluge/commonoptions.py diff --git a/deluge/commonoptions.py b/deluge/commonoptions.py new file mode 100644 index 0000000..76fa705
- + 1 # 2 # optionmanager.py 3 # 4 # Copyright (C) 2007 Andrew Resch <andrewresch@gmail.com> 5 # 6 # Deluge is free software. 7 # 8 # You may redistribute it and/or modify it under the terms of the 9 # GNU General Public License, as published by the Free Software 10 # Foundation; either version 3 of the License, or (at your option) 11 # any later version. 12 # 13 # deluge is distributed in the hope that it will be useful, 14 # but WITHOUT ANY WARRANTY; without even the implied warranty of 15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 16 # See the GNU General Public License for more details. 17 # 18 # You should have received a copy of the GNU General Public License 19 # along with deluge. If not, write to: 20 # The Free Software Foundation, Inc., 21 # 51 Franklin Street, Fifth Floor 22 # Boston, MA 02110-1301, USA. 23 # 24 # In addition, as a special exception, the copyright holders give 25 # permission to link the code of portions of this program with the OpenSSL 26 # library. 27 # You must obey the GNU General Public License in all respects for all of 28 # the code used other than OpenSSL. If you modify file(s) with this 29 # exception, you may extend this exception to your version of the file(s), 30 # but you are not obligated to do so. If you do not wish to do so, delete 31 # this exception statement from your version. If you delete this exception 32 # statement from all source files in the program, then also delete it here. 33 # 34 # 35 36 import optparse 37 import sys 38 import logging 39 import deluge.common 40 import deluge.log 41 import deluge.configmanager 42 43 44 class CommonOptionParser(optparse.OptionParser): 45 def __init__(self, *args, **kwargs): 46 47 if 'version' not in kwargs: 48 try: 49 from deluge._libtorrent import lt 50 lt_version = "\nlibtorrent: %s" % lt.version 51 except ImportError: 52 lt_version = "" 53 finally: 54 kwargs['version'] = "%prog: " + deluge.common.get_version() + lt_version 55 56 optparse.OptionParser.__init__(self, *args, **kwargs) 57 58 self.common_group = optparse.OptionGroup(self, _("Common Options")) 59 self.common_group.add_option("-c", "--config", dest="config", 60 help="Set the config folder location", action="store", type="str") 61 self.common_group.add_option("-l", "--logfile", dest="logfile", 62 help="Output to designated logfile instead of stdout", action="store", type="str") 63 self.common_group.add_option("-L", "--loglevel", dest="loglevel", 64 help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str") 65 self.common_group.add_option("-q", "--quiet", dest="quiet", 66 help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False) 67 self.common_group.add_option("-r", "--rotate-logs", 68 help="Rotate logfiles.", action="store_true", default=False) 69 70 self.add_option_group(self.common_group) 71 72 def parse_args(self, *args): 73 options, args = optparse.OptionParser.parse_args(self, *args) 74 75 if options.quiet: 76 options.loglevel = "none" 77 78 logfile_mode = 'a' if options.rotate_logs else 'w' 79 80 # Setup the logger 81 deluge.log.setupLogger(level=options.loglevel, 82 filename=options.logfile, 83 filemode=logfile_mode) 84 85 if options.config: 86 if not deluge.configmanager.set_config_dir(options.config): 87 log = logging.getLogger(__name__) 88 log.error("There was an error setting the config dir! Exiting..") 89 sys.exit(1) 90 91 return options, args -
deluge/main.py
diff --git a/deluge/main.py b/deluge/main.py index 2ab6178..642f11d 100644
a b 42 42 43 43 import os 44 44 import sys 45 from optparse import OptionParser 45 import optparse 46 46 47 47 import deluge.log 48 48 import deluge.error 49 50 try: 51 from deluge._libtorrent import lt 52 lt_version = "\nlibtorrent: %s" % lt.version 53 except ImportError: 54 lt_version = "" 49 from deluge.commonoptions import CommonOptionParser 55 50 56 51 def start_ui(): 57 52 """Entry point for ui script""" … … def start_ui(): 59 54 deluge.common.setup_translations() 60 55 61 56 # Setup the argument parser 62 parser = OptionParser(usage="%prog [options] [actions]",63 version= "%prog: " + deluge.common.get_version() + lt_version)57 parser = CommonOptionParser() 58 group = optparse.OptionGroup (parser, _("Default Options")) 64 59 65 parser.add_option("-u", "--ui", dest="ui",60 group.add_option("-u", "--ui", dest="ui", 66 61 help="""The UI that you wish to launch. The UI choices are:\n 67 62 \t gtk -- A GTK-based graphical user interface (default)\n 68 63 \t web -- A web-based interface (http://localhost:8112)\n 69 64 \t console -- A console or command-line interface""", action="store", type="str") 70 parser.add_option("-c", "--config", dest="config", 71 help="Set the config folder location", action="store", type="str") 72 parser.add_option("-l", "--logfile", dest="logfile", 73 help="Output to designated logfile instead of stdout", action="store", type="str") 74 parser.add_option("-a", "--args", dest="args", 65 group.add_option("-a", "--args", dest="args", 75 66 help="Arguments to pass to UI, -a '--option args'", action="store", type="str") 76 parser.add_option("-L", "--loglevel", dest="loglevel", 77 help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str") 78 parser.add_option("-q", "--quiet", dest="quiet", 79 help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False) 80 parser.add_option("-r", "--rotate-logs", 81 help="Rotate logfiles.", action="store_true", default=False) 82 parser.add_option("-s", "--set-default-ui", dest="default_ui", 67 group.add_option("-s", "--set-default-ui", dest="default_ui", 83 68 help="Sets the default UI to be run when no UI is specified", action="store", type="str") 69 70 parser.add_option_group(group) 84 71 85 72 # Get the options and args from the OptionParser 86 73 (options, args) = parser.parse_args() 87 74 88 if options.quiet:89 options.loglevel = "none"90 91 logfile_mode = 'w'92 if options.rotate_logs:93 logfile_mode = 'a'94 95 # Setup the logger96 deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,97 filemode=logfile_mode)98 99 if options.config:100 if not os.path.exists(options.config):101 # Try to create the config folder if it doesn't exist102 try:103 os.makedirs(options.config)104 except Exception, e:105 pass106 elif not os.path.isdir(options.config):107 print "Config option needs to be a directory!"108 sys.exit(1)109 else:110 if not os.path.exists(deluge.common.get_default_config_dir()):111 os.makedirs(deluge.common.get_default_config_dir())112 113 75 if options.default_ui: 114 import deluge.configmanager115 if options.config:116 deluge.configmanager.set_config_dir(options.config)117 118 76 config = deluge.configmanager.ConfigManager("ui.conf") 119 77 config["default_ui"] = options.default_ui 120 78 config.save() … … def start_daemon(): 144 102 warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted') 145 103 146 104 # Setup the argument parser 147 parser = OptionParser(usage="%prog [options] [actions]", 148 version= "%prog: " + deluge.common.get_version() + lt_version) 149 parser.add_option("-p", "--port", dest="port", 105 parser = CommonOptionParser(usage="%prog [options] [actions]") 106 107 group = optparse.OptionGroup (parser, _("Daemon Options")) 108 group.add_option("-p", "--port", dest="port", 150 109 help="Port daemon will listen on", action="store", type="int") 151 parser.add_option("-i", "--interface", dest="interface",110 group.add_option("-i", "--interface", dest="interface", 152 111 help="Interface daemon will listen for bittorrent connections on, \ 153 112 this should be an IP address", metavar="IFACE", 154 113 action="store", type="str") 155 parser.add_option("-u", "--ui-interface", dest="ui_interface",114 group.add_option("-u", "--ui-interface", dest="ui_interface", 156 115 help="Interface daemon will listen for UI connections on, this should be\ 157 116 an IP address", metavar="IFACE", action="store", type="str") 158 117 if not (deluge.common.windows_check() or deluge.common.osx_check()): 159 parser.add_option("-d", "--do-not-daemonize", dest="donot",118 group.add_option("-d", "--do-not-daemonize", dest="donot", 160 119 help="Do not daemonize", action="store_true", default=False) 161 parser.add_option("-c", "--config", dest="config", 162 help="Set the config location", action="store", type="str") 163 parser.add_option("-l", "--logfile", dest="logfile", 164 help="Set the logfile location", action="store", type="str") 165 parser.add_option("-P", "--pidfile", dest="pidfile", 120 group.add_option("-P", "--pidfile", dest="pidfile", 166 121 help="Use pidfile to store process id", action="store", type="str") 167 122 if not deluge.common.windows_check(): 168 parser.add_option("-U", "--user", dest="user",123 group.add_option("-U", "--user", dest="user", 169 124 help="User to switch to. Only use it when starting as root", action="store", type="str") 170 parser.add_option("-g", "--group", dest="group",125 group.add_option("-g", "--group", dest="group", 171 126 help="Group to switch to. Only use it when starting as root", action="store", type="str") 172 parser.add_option("-L", "--loglevel", dest="loglevel", 173 help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str") 174 parser.add_option("-q", "--quiet", dest="quiet", 175 help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False) 176 parser.add_option("-r", "--rotate-logs", 177 help="Rotate logfiles.", action="store_true", default=False) 178 parser.add_option("--profile", dest="profile", action="store_true", default=False, 127 group.add_option("--profile", dest="profile", action="store_true", default=False, 179 128 help="Profiles the daemon") 180 129 130 parser.add_option_group(group) 131 181 132 # Get the options and args from the OptionParser 182 133 (options, args) = parser.parse_args() 183 134 184 if options.quiet:185 options.loglevel = "none"186 187 logfile_mode = 'w'188 if options.rotate_logs:189 logfile_mode = 'a'190 191 # Setup the logger192 deluge.log.setupLogger(level=options.loglevel, filename=options.logfile,193 filemode=logfile_mode)194 195 import deluge.configmanager196 if options.config:197 if not deluge.configmanager.set_config_dir(options.config):198 print("There was an error setting the config dir! Exiting..")199 sys.exit(1)200 201 135 # Sets the options.logfile to point to the default location 202 136 def open_logfile(): 203 137 if not options.logfile: -
deluge/ui/ui.py
diff --git a/deluge/ui/ui.py b/deluge/ui/ui.py index 2378f43..43493a6 100644
a b 35 35 36 36 import sys 37 37 import logging 38 from optparse import OptionParser, OptionGroup39 38 import deluge.common 40 39 import deluge.configmanager 41 40 import deluge.log 42 43 try: 44 from deluge._libtorrent import lt 45 lt_version = "\nlibtorrent: %s" % lt.version 46 except ImportError: 47 lt_version = "" 41 from deluge.commonoptions import CommonOptionParser 48 42 49 43 DEFAULT_PREFS = { 50 44 "default_ui": "gtk" … … def __init__(self, name="gtk"): 63 57 else: 64 58 deluge.common.setup_translations() 65 59 66 self.__parser = OptionParser(version="%prog: " + deluge.common.get_version() + lt_version) 67 68 group = OptionGroup(self.__parser, _("Common Options")) 69 group.add_option("-c", "--config", dest="config", 70 help="Set the config folder location", action="store", type="str") 71 group.add_option("-l", "--logfile", dest="logfile", 72 help="Output to designated logfile instead of stdout", action="store", type="str") 73 group.add_option("-L", "--loglevel", dest="loglevel", 74 help="Set the log level: none, info, warning, error, critical, debug", action="store", type="str") 75 group.add_option("-q", "--quiet", dest="quiet", 76 help="Sets the log level to 'none', this is the same as `-L none`", action="store_true", default=False) 77 group.add_option("-r", "--rotate-logs", 78 help="Rotate logfiles.", action="store_true", default=False) 79 self.__parser.add_option_group(group) 60 self.__parser = CommonOptionParser() 80 61 81 62 @property 82 63 def name(self): … … def args(self): 97 78 def start(self): 98 79 (self.__options, self.__args) = self.__parser.parse_args() 99 80 100 if self.__options.quiet:101 self.__options.loglevel = "none"102 103 logfile_mode = 'w'104 if self.__options.rotate_logs:105 logfile_mode = 'a'106 107 # Setup the logger108 # Setup the logger109 deluge.log.setupLogger(level=self.__options.loglevel,110 filename=self.__options.logfile,111 filemode=logfile_mode)112 113 81 log = logging.getLogger(__name__) 114 115 if self.__options.config:116 if not deluge.configmanager.set_config_dir(self.__options.config):117 log.error("There was an error setting the config dir! Exiting..")118 sys.exit(1)119 120 82 log.info("Deluge ui %s", deluge.common.get_version()) 121 83 log.debug("options: %s", self.__options) 122 84 log.debug("args: %s", self.__args)