diff --git a/deluge/main.py b/deluge/core/daemon_entry.py similarity index 61% rename from deluge/main.py rename to deluge/core/daemon_entry.py index d43dc4e4a..8356fb559 100644 --- a/deluge/main.py +++ b/deluge/core/daemon_entry.py @@ -7,101 +7,16 @@ # the additional special exception to link portions of this program with the OpenSSL library. # See LICENSE for more details. # - - -# The main starting point for the program. This function is called when the -# user runs the command 'deluge'. - -"""Main starting point for Deluge. Contains the main() entry point.""" from __future__ import print_function import os import sys from logging import FileHandler, getLogger -import pkg_resources - import deluge.common import deluge.configmanager import deluge.error -from deluge.commonoptions import CommonOptionParser - -DEFAULT_PREFS = { - "default_ui": "gtk" -} - - -def start_ui(): - """Entry point for ui script""" - deluge.common.setup_translations() - - # Setup the argument parser - parser = CommonOptionParser() - group = parser.add_argument_group(_("Default Options")) - - ui_entrypoints = dict([(entrypoint.name, entrypoint.load()) - for entrypoint in pkg_resources.iter_entry_points('deluge.ui')]) - - cmd_help = ['The UI that you wish to launch. The UI choices are:'] - max_len = 0 - for k, v in ui_entrypoints.iteritems(): - cmdline = getattr(v, 'cmdline', "") - max_len = max(max_len, len(cmdline)) - - cmd_help.extend(["%s -- %s" % (k, getattr(v, 'cmdline', "")) for k, v in ui_entrypoints.iteritems()]) - - parser.add_argument("-u", "--ui", action="store", - choices=ui_entrypoints.keys(), help="\n* ".join(cmd_help)) - group.add_argument("-a", "--args", action="store", - help='Arguments to pass to the UI. Multiple args must be quoted, e.g. -a "--option args"') - group.add_argument("-s", "--set-default-ui", dest="default_ui", choices=ui_entrypoints.keys(), - help="Sets the default UI to be run when no UI is specified", action="store") - - options = parser.parse_args(deluge.common.unicode_argv()[1:]) - - config = deluge.configmanager.ConfigManager("ui.conf", DEFAULT_PREFS) - log = getLogger(__name__) - - if options.default_ui: - config["default_ui"] = options.default_ui - config.save() - log.info("The default UI has been changed to %s", options.default_ui) - sys.exit(0) - - log.info("Deluge ui %s", deluge.common.get_version()) - log.debug("options: %s", options) - - selected_ui = options.ui if options.ui else config["default_ui"] - - config.save() - del config - - # reconstruct arguments to hand off to child client - client_args = [] - if options.args: - import shlex - client_args.extend(shlex.split(options.args)) - - try: - ui = ui_entrypoints[selected_ui](parser=parser) - except KeyError as ex: - log.error("Unable to find the requested UI: '%s'. Please select a different UI with the '-u' option" - " or alternatively use the '-s' option to select a different default UI.", selected_ui) - except ImportError as ex: - import traceback - error_type, error_value, tb = sys.exc_info() - stack = traceback.extract_tb(tb) - last_frame = stack[-1] - if last_frame[0] == __file__: - log.error("Unable to find the requested UI: %s. Please select a different UI with the '-u' " - "option or alternatively use the '-s' option to select a different default UI.", selected_ui) - else: - log.exception(ex) - log.error("There was an error whilst launching the request UI: %s", selected_ui) - log.error("Look at the traceback above for more information.") - sys.exit(1) - else: - ui.start(client_args) +from deluge.ui.baseargparser import BaseArgParser def add_daemon_options(parser): @@ -147,7 +62,7 @@ def start_daemon(skip_start=False): warnings.filterwarnings('ignore', category=DeprecationWarning, module='twisted') # Setup the argument parser - parser = CommonOptionParser() + parser = BaseArgParser() add_daemon_options(parser) options = parser.parse_args() diff --git a/deluge/tests/common.py b/deluge/tests/common.py index 80e31b757..df411848f 100644 --- a/deluge/tests/common.py +++ b/deluge/tests/common.py @@ -131,12 +131,12 @@ def start_core(listen_port=58846, logfile=None, timeout=10, timeout_msg=None, config_directory = set_tmp_config_dir() daemon_script = """ import sys -import deluge.main +import deluge.core.daemon_entry sys.argv.extend(['-d', '-c', '%s', '-L', 'info', '-p', '%d']) try: - daemon = deluge.main.start_daemon(skip_start=True) + daemon = deluge.core.daemon_entry.start_daemon(skip_start=True) %s daemon.start() except: diff --git a/deluge/ui/ui_entry.py b/deluge/ui/ui_entry.py new file mode 100644 index 000000000..92057862f --- /dev/null +++ b/deluge/ui/ui_entry.py @@ -0,0 +1,100 @@ +# -*- coding: utf-8 -*- +# +# Copyright (C) 2007 Andrew Resch +# Copyright (C) 2010 Pedro Algarvio +# +# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with +# the additional special exception to link portions of this program with the OpenSSL library. +# See LICENSE for more details. +# + +# The main starting point for the program. This function is called when the +# user runs the command 'deluge'. + +"""Main starting point for Deluge""" + +import sys +from logging import getLogger + +import pkg_resources + +import deluge.common +import deluge.configmanager +from deluge.ui.baseargparser import BaseArgParser + +DEFAULT_PREFS = { + "default_ui": "gtk" +} + + +def start_ui(): + """Entry point for ui script""" + deluge.common.setup_translations() + + # Setup the argument parser + parser = BaseArgParser() + group = parser.add_argument_group(_("Default Options")) + + ui_entrypoints = dict([(entrypoint.name, entrypoint.load()) + for entrypoint in pkg_resources.iter_entry_points('deluge.ui')]) + + cmd_help = ['The UI that you wish to launch. The UI choices are:'] + max_len = 0 + for k, v in ui_entrypoints.iteritems(): + cmdline = getattr(v, 'cmdline', "") + max_len = max(max_len, len(cmdline)) + + cmd_help.extend(["%s -- %s" % (k, getattr(v, 'cmdline', "")) for k, v in ui_entrypoints.iteritems()]) + + parser.add_argument("-u", "--ui", action="store", + choices=ui_entrypoints.keys(), help="\n* ".join(cmd_help)) + group.add_argument("-a", "--args", action="store", + help='Arguments to pass to the UI. Multiple args must be quoted, e.g. -a "--option args"') + group.add_argument("-s", "--set-default-ui", dest="default_ui", choices=ui_entrypoints.keys(), + help="Sets the default UI to be run when no UI is specified", action="store") + + options = parser.parse_args(deluge.common.unicode_argv()[1:]) + + config = deluge.configmanager.ConfigManager("ui.conf", DEFAULT_PREFS) + log = getLogger(__name__) + + if options.default_ui: + config["default_ui"] = options.default_ui + config.save() + log.info("The default UI has been changed to %s", options.default_ui) + sys.exit(0) + + log.info("Deluge ui %s", deluge.common.get_version()) + log.debug("options: %s", options) + + selected_ui = options.ui if options.ui else config["default_ui"] + + config.save() + del config + + # reconstruct arguments to hand off to child client + client_args = [] + if options.args: + import shlex + client_args.extend(shlex.split(options.args)) + + try: + ui = ui_entrypoints[selected_ui](parser=parser) + except KeyError as ex: + log.error("Unable to find the requested UI: '%s'. Please select a different UI with the '-u' option" + " or alternatively use the '-s' option to select a different default UI.", selected_ui) + except ImportError as ex: + import traceback + error_type, error_value, tb = sys.exc_info() + stack = traceback.extract_tb(tb) + last_frame = stack[-1] + if last_frame[0] == __file__: + log.error("Unable to find the requested UI: %s. Please select a different UI with the '-u' " + "option or alternatively use the '-s' option to select a different default UI.", selected_ui) + else: + log.exception(ex) + log.error("There was an error whilst launching the request UI: %s", selected_ui) + log.error("Look at the traceback above for more information.") + sys.exit(1) + else: + ui.start(client_args) diff --git a/setup.py b/setup.py index a5c986b4c..382149b5d 100755 --- a/setup.py +++ b/setup.py @@ -305,10 +305,10 @@ entry_points = { 'console_scripts': [ 'deluge-console = deluge.ui.console:start', 'deluge-web = deluge.ui.web:start', - 'deluged = deluge.main:start_daemon' + 'deluged = deluge.core.daemon_entry:start_daemon' ], 'gui_scripts': [ - 'deluge = deluge.main:start_ui', + 'deluge = deluge.ui.ui_entry:start_ui', 'deluge-gtk = deluge.ui.gtkui:start' ], 'deluge.ui': [ @@ -320,9 +320,9 @@ entry_points = { if windows_check(): entry_points['console_scripts'].extend([ - 'deluge-debug = deluge.main:start_ui', + 'deluge-debug = deluge.ui.ui_entry:start_ui', 'deluge-web-debug = deluge.ui.web:start', - 'deluged-debug = deluge.main:start_daemon']) + 'deluged-debug = deluge.core.daemon_entry:start_daemon']) _package_data = {} _package_data['deluge'] = [