Ticket #1971: 0001-Unified-common-options.patch

File 0001-Unified-common-options.patch, 14.5 KB (added by jumentous, 9 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 
     36import optparse 
     37import sys 
     38import logging 
     39import deluge.common 
     40import deluge.log  
     41import deluge.configmanager 
     42 
     43 
     44class 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  
    4242 
    4343import os 
    4444import sys 
    45 from optparse import OptionParser 
     45import optparse 
    4646 
    4747import deluge.log 
    4848import deluge.error 
    49  
    50 try: 
    51     from deluge._libtorrent import lt 
    52     lt_version = "\nlibtorrent: %s" % lt.version 
    53 except ImportError: 
    54     lt_version = "" 
     49from deluge.commonoptions import CommonOptionParser 
    5550 
    5651def start_ui(): 
    5752    """Entry point for ui script""" 
    def start_ui(): 
    5954    deluge.common.setup_translations() 
    6055 
    6156    # 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")) 
    6459 
    65     parser.add_option("-u", "--ui", dest="ui", 
     60    group.add_option("-u", "--ui", dest="ui", 
    6661        help="""The UI that you wish to launch.  The UI choices are:\n 
    6762        \t gtk -- A GTK-based graphical user interface (default)\n 
    6863        \t web -- A web-based interface (http://localhost:8112)\n 
    6964        \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", 
    7566        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", 
    8368        help="Sets the default UI to be run when no UI is specified", action="store", type="str") 
     69     
     70    parser.add_option_group(group) 
    8471 
    8572    # Get the options and args from the OptionParser 
    8673    (options, args) = parser.parse_args() 
    8774 
    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 logger 
    96     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 exist 
    102             try: 
    103                 os.makedirs(options.config) 
    104             except Exception, e: 
    105                 pass 
    106         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  
    11375    if options.default_ui: 
    114         import deluge.configmanager 
    115         if options.config: 
    116             deluge.configmanager.set_config_dir(options.config) 
    117  
    11876        config = deluge.configmanager.ConfigManager("ui.conf") 
    11977        config["default_ui"] = options.default_ui 
    12078        config.save() 
    def start_daemon(): 
    144102        warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted') 
    145103 
    146104    # 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", 
    150109        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", 
    152111        help="Interface daemon will listen for bittorrent connections on, \ 
    153112this should be an IP address", metavar="IFACE", 
    154113        action="store", type="str") 
    155     parser.add_option("-u", "--ui-interface", dest="ui_interface", 
     114    group.add_option("-u", "--ui-interface", dest="ui_interface", 
    156115        help="Interface daemon will listen for UI connections on, this should be\ 
    157116 an IP address", metavar="IFACE", action="store", type="str") 
    158117    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", 
    160119            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", 
    166121        help="Use pidfile to store process id", action="store", type="str") 
    167122    if not deluge.common.windows_check(): 
    168         parser.add_option("-U", "--user", dest="user", 
     123        group.add_option("-U", "--user", dest="user", 
    169124            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", 
    171126            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, 
    179128        help="Profiles the daemon") 
    180129 
     130    parser.add_option_group(group) 
     131 
    181132    # Get the options and args from the OptionParser 
    182133    (options, args) = parser.parse_args() 
    183134 
    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 logger 
    192     deluge.log.setupLogger(level=options.loglevel, filename=options.logfile, 
    193                            filemode=logfile_mode) 
    194  
    195     import deluge.configmanager 
    196     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  
    201135    # Sets the options.logfile to point to the default location 
    202136    def open_logfile(): 
    203137        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  
    3535 
    3636import sys 
    3737import logging 
    38 from optparse import OptionParser, OptionGroup 
    3938import deluge.common 
    4039import deluge.configmanager 
    4140import deluge.log 
    42  
    43 try: 
    44     from deluge._libtorrent import lt 
    45     lt_version = "\nlibtorrent: %s" % lt.version 
    46 except ImportError: 
    47     lt_version = "" 
     41from deluge.commonoptions import CommonOptionParser 
    4842 
    4943DEFAULT_PREFS = { 
    5044    "default_ui": "gtk" 
    def __init__(self, name="gtk"): 
    6357        else: 
    6458            deluge.common.setup_translations() 
    6559 
    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() 
    8061 
    8162    @property 
    8263    def name(self): 
    def args(self): 
    9778    def start(self): 
    9879        (self.__options, self.__args) = self.__parser.parse_args() 
    9980 
    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 logger 
    108         # Setup the logger 
    109         deluge.log.setupLogger(level=self.__options.loglevel, 
    110                                filename=self.__options.logfile, 
    111                                filemode=logfile_mode) 
    112  
    11381        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  
    12082        log.info("Deluge ui %s", deluge.common.get_version()) 
    12183        log.debug("options: %s", self.__options) 
    12284        log.debug("args: %s", self.__args)