Add ability to send arguments to UIs with -a, --args

This commit is contained in:
Andrew Resch 2008-10-26 21:31:13 +00:00
parent 26eb2bbb3d
commit 2d257371d9
3 changed files with 46 additions and 42 deletions

View File

@ -2,19 +2,19 @@
# main.py
#
# Copyright (C) 2007 Andrew Resch ('andar') <andrewresch@gmail.com>
#
#
# Deluge is free software.
#
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with deluge. If not, write to:
# The Free Software Foundation, Inc.,
@ -31,7 +31,7 @@
# this exception statement from your version. If you delete this exception
# statement from all source files in the program, then also delete it here.
# The main starting point for the program. This function is called when the
# 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."""
@ -46,7 +46,7 @@ import deluge.common
def start_ui():
"""Entry point for ui script"""
# Setup the argument parser
parser = OptionParser(usage="%prog [options] [actions]",
parser = OptionParser(usage="%prog [options] [actions]",
version=deluge.common.get_version())
parser.add_option("-u", "--ui", dest="ui",
@ -55,7 +55,9 @@ def start_ui():
help="Set the config location", action="store", type="str")
parser.add_option("-l", "--logfile", dest="logfile",
help="Output to designated logfile instead of stdout", action="store", type="str")
parser.add_option("-a", "--args", dest="args",
help="Arguments to pass to UI, -a '--option args'", action="store", type="str")
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()
@ -69,11 +71,11 @@ def start_ui():
else:
if not os.path.exists(deluge.common.get_default_config_dir()):
os.makedirs(deluge.common.get_default_config_dir())
# Always log to a file in Windows
if deluge.common.windows_check() and not options.logfile:
options.logfile = "deluge.log"
if options.logfile:
if options.config:
logfile = os.path.join(options.config, options.logfile)
@ -83,29 +85,30 @@ def start_ui():
sys.stdout = open(logfile, "wb")
sys.stderr = sys.stdout
sys.stdin = None
from deluge.log import LOG as log
version = deluge.common.get_version()
if deluge.common.get_revision() != "":
version = version + "r" + deluge.common.get_revision()
log.info("Deluge ui %s", version)
log.debug("options: %s", options)
log.debug("args: %s", args)
log.debug("ui_args: %s", args)
from deluge.ui.ui import UI
log.info("Starting ui..")
UI(options, args)
UI(options, args, options.args)
def start_daemon():
"""Entry point for daemon script"""
import deluge.common
# Setup the argument parser
parser = OptionParser(usage="%prog [options] [actions]",
parser = OptionParser(usage="%prog [options] [actions]",
version=deluge.common.get_version())
parser.add_option("-p", "--port", dest="port",
parser.add_option("-p", "--port", dest="port",
help="Port daemon will listen on", action="store", type="int")
parser.add_option("-d", "--do-not-daemonize", dest="donot",
help="Do not daemonize", action="store_true", default=False)
@ -113,7 +116,7 @@ def start_daemon():
help="Set the config location", action="store", type="str")
parser.add_option("-l", "--logfile", dest="logfile",
help="Set the logfile location", action="store", type="str")
# Get the options and args from the OptionParser
(options, args) = parser.parse_args()
@ -127,7 +130,7 @@ def start_daemon():
else:
if not os.path.exists(deluge.common.get_default_config_dir()):
os.makedirs(deluge.common.get_default_config_dir())
# If the donot daemonize is set, then we just skip the forking
if not options.donot and not deluge.common.windows_check():
if os.fork() == 0:
@ -141,7 +144,7 @@ def start_daemon():
else:
config_dir = deluge.common.get_default_config_dir()
logfile = os.path.join(config_dir, "deluged.log")
sys.stdout = open(logfile, "wb")
sys.stderr = sys.stdout
sys.stdin = None
@ -160,11 +163,10 @@ def start_daemon():
else:
config_dir = deluge.common.get_default_config_dir()
logfile = os.path.join(config_dir, "deluged.log")
sys.stdout = open(logfile, "wb")
sys.stderr = sys.stdout
sys.stdin = None
from deluge.core.daemon import Daemon
Daemon(options, args)

View File

@ -12,7 +12,7 @@ import shlex
class OptionParser(optparse.OptionParser):
"""subclass from optparse.OptionParser so exit() won't exit."""
def exit(self, status=0, msg=None):
self.values._exit = True
self.values._exit = True
if msg:
print msg
@ -37,7 +37,7 @@ class BaseCommand(object):
return []
def handle(self, *args, **options):
pass
@property
def name(self):
return 'base'
@ -76,7 +76,7 @@ def load_commands(command_dir, exclude=[]):
def get_command(name):
return getattr(__import__('deluge.ui.null2.commands.%s' % name, {}, {}, ['Command']), 'Command')()
try:
try:
commands = []
for filename in os.listdir(command_dir):
if filename.split('.')[0] in exclude or filename.startswith('_') or not filename.endswith('.py'):
@ -96,14 +96,18 @@ class NullUI(object):
def __init__(self, args=None):
client.set_core_uri("http://localhost:58846")
self._commands = load_commands(os.path.join(UI_PATH, 'commands'))
if args:
self.precmd()
self.onecmd(args)
self.postcmd()
sys.exit(0)
def completedefault(self, *ignored):
"""Method called to complete an input line when no command-specific
method is available.
By default, it returns an empty list.
"""
return []
@ -138,7 +142,7 @@ class NullUI(object):
return self.completion_matches[state]
except IndexError:
return None
def preloop(self):
pass
@ -169,7 +173,7 @@ class NullUI(object):
def postcmd(self):
client.force_call()
def cmdloop(self):
self.preloop()
try:

View File

@ -2,19 +2,19 @@
# ui.py
#
# Copyright (C) 2007 Andrew Resch ('andar') <andrewresch@gmail.com>
#
#
# Deluge is free software.
#
#
# You may redistribute it and/or modify it under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation; either version 3 of the License, or (at your option)
# any later version.
#
#
# deluge is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
#
# You should have received a copy of the GNU General Public License
# along with deluge. If not, write to:
# The Free Software Foundation, Inc.,
@ -40,22 +40,22 @@ DEFAULT_PREFS = {
}
class UI:
def __init__(self, options, args):
def __init__(self, options, args, ui_args):
log.debug("UI init..")
# Set the config directory
deluge.configmanager.set_config_dir(options.config)
config = deluge.configmanager.ConfigManager("ui.conf", DEFAULT_PREFS)
if not options.ui:
selected_ui = config["default_ui"]
else:
selected_ui = options.ui
config.save()
del config
if selected_ui == "gtk":
log.info("Starting GtkUI..")
from deluge.ui.gtkui.gtkui import GtkUI
@ -71,6 +71,4 @@ class UI:
elif selected_ui == "null2":
log.info("Starting NullUI2..")
from deluge.ui.null2.main import NullUI
ui = NullUI(args)
ui.run()
ui = NullUI(ui_args).run()