mirror of
https://git.deluge-torrent.org/deluge
synced 2025-08-07 00:48:41 +00:00
removed the 'builtin' commands and made 'help' a normal command.
This commit is contained in:
parent
4d79df5880
commit
af0bd07d3e
3 changed files with 77 additions and 73 deletions
|
@ -45,7 +45,7 @@ def simple_eval(source):
|
||||||
|
|
||||||
|
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
"""Show configuration values"""
|
"""Show and set configuration values"""
|
||||||
|
|
||||||
option_list = BaseCommand.option_list + (
|
option_list = BaseCommand.option_list + (
|
||||||
make_option('-s', '--set', action='store_true', default=False, dest='set',
|
make_option('-s', '--set', action='store_true', default=False, dest='set',
|
||||||
|
|
41
deluge/ui/null2/commands/help.py
Normal file
41
deluge/ui/null2/commands/help.py
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
from deluge.ui.null2 import UI_PATH
|
||||||
|
from deluge.ui.null2.main import BaseCommand, load_commands
|
||||||
|
from deluge.ui.null2.colors import templates
|
||||||
|
import os
|
||||||
|
|
||||||
|
class Command(BaseCommand):
|
||||||
|
"""displays help on other commands"""
|
||||||
|
|
||||||
|
usage = "Usage: help [command]"
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
BaseCommand.__init__(self)
|
||||||
|
# get a list of commands, exclude 'help' so we won't run into a recursive loop.
|
||||||
|
self._commands = load_commands(os.path.join(UI_PATH,'commands'), exclude=['help'])
|
||||||
|
self._commands['help'] = self
|
||||||
|
|
||||||
|
def handle(self, *args, **options):
|
||||||
|
if args:
|
||||||
|
if len(args) > 1:
|
||||||
|
print usage
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
cmd = self._commands[args[0]]
|
||||||
|
except KeyError:
|
||||||
|
print templates.ERROR('unknown command %r' % args[0])
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
parser = cmd.create_parser()
|
||||||
|
print parser.format_help()
|
||||||
|
except AttributeError, e:
|
||||||
|
print cmd.__doc__ or 'No help for this command'
|
||||||
|
else:
|
||||||
|
max_length = max( len(k) for k in self._commands)
|
||||||
|
for cmd in sorted(self._commands):
|
||||||
|
print templates.help(max_length, cmd, self._commands[cmd].__doc__ or '')
|
||||||
|
print
|
||||||
|
print 'for help on a specific command, use "<command> --help"'
|
||||||
|
|
||||||
|
def complete(self, text, *args):
|
||||||
|
return [ x for x in self._commands.keys() if x.startswith(text) ]
|
|
@ -1,13 +1,13 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
|
import logging
|
||||||
|
logging.disable(logging.ERROR)
|
||||||
import os, sys
|
import os, sys
|
||||||
import optparse
|
import optparse
|
||||||
from deluge.ui.null2 import UI_PATH
|
from deluge.ui.null2 import UI_PATH
|
||||||
from deluge.ui.null2.colors import Template, make_style, templates, default_style as style
|
from deluge.ui.null2.colors import Template, make_style, templates, default_style as style
|
||||||
from deluge.ui.client import aclient as client
|
from deluge.ui.client import aclient as client
|
||||||
import shlex
|
import shlex
|
||||||
import logging
|
|
||||||
|
|
||||||
logging.disable(logging.ERROR)
|
|
||||||
|
|
||||||
class OptionParser(optparse.OptionParser):
|
class OptionParser(optparse.OptionParser):
|
||||||
"""subclass from optparse.OptionParser so exit() won't exit."""
|
"""subclass from optparse.OptionParser so exit() won't exit."""
|
||||||
|
@ -49,7 +49,7 @@ class BaseCommand(object):
|
||||||
def split(self, text):
|
def split(self, text):
|
||||||
return shlex.split(text)
|
return shlex.split(text)
|
||||||
|
|
||||||
def _create_parser(self):
|
def create_parser(self):
|
||||||
return OptionParser(prog = self.name,
|
return OptionParser(prog = self.name,
|
||||||
usage = self.usage,
|
usage = self.usage,
|
||||||
epilog = self.epilog,
|
epilog = self.epilog,
|
||||||
|
@ -72,25 +72,14 @@ def match_torrents(array=None):
|
||||||
client.force_call()
|
client.force_call()
|
||||||
return torrents
|
return torrents
|
||||||
|
|
||||||
class NullUI(object):
|
def load_commands(command_dir, exclude=[]):
|
||||||
prompt = '>>> '
|
|
||||||
|
|
||||||
def __init__(self, args=None):
|
|
||||||
client.set_core_uri("http://localhost:58846")
|
|
||||||
self._commands = self._load_commands()
|
|
||||||
self._builtins = { 'help': self.help }
|
|
||||||
self._all_commands = dict(self._commands)
|
|
||||||
self._all_commands.update(self._builtins)
|
|
||||||
|
|
||||||
def _load_commands(self):
|
|
||||||
def get_command(name):
|
def get_command(name):
|
||||||
return getattr(__import__('deluge.ui.null2.commands.%s' % name, {}, {}, ['Command']), 'Command')()
|
return getattr(__import__('deluge.ui.null2.commands.%s' % name, {}, {}, ['Command']), 'Command')()
|
||||||
|
|
||||||
command_dir = os.path.join(UI_PATH, 'commands')
|
|
||||||
try:
|
try:
|
||||||
commands = []
|
commands = []
|
||||||
for filename in os.listdir(command_dir):
|
for filename in os.listdir(command_dir):
|
||||||
if filename.startswith('_') or not filename.endswith('.py'):
|
if filename.split('.')[0] in exclude or filename.startswith('_') or not filename.endswith('.py'):
|
||||||
continue
|
continue
|
||||||
cmd = get_command(filename[:-3])
|
cmd = get_command(filename[:-3])
|
||||||
aliases = [ filename[:-3] ]
|
aliases = [ filename[:-3] ]
|
||||||
|
@ -98,10 +87,17 @@ class NullUI(object):
|
||||||
for a in aliases:
|
for a in aliases:
|
||||||
commands.append((a, cmd))
|
commands.append((a, cmd))
|
||||||
return dict(commands)
|
return dict(commands)
|
||||||
#return dict([ (f[:-3], get_command(f[:-3])) for f in os.listdir(command_dir) if not f.startswith('_') and f.endswith('.py') ])
|
|
||||||
except OSError, e:
|
except OSError, e:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
class NullUI(object):
|
||||||
|
prompt = '>>> '
|
||||||
|
|
||||||
|
def __init__(self, args=None):
|
||||||
|
client.set_core_uri("http://localhost:58846")
|
||||||
|
self._commands = load_commands(os.path.join(UI_PATH, 'commands'))
|
||||||
|
|
||||||
|
|
||||||
def completedefault(self, *ignored):
|
def completedefault(self, *ignored):
|
||||||
"""Method called to complete an input line when no command-specific
|
"""Method called to complete an input line when no command-specific
|
||||||
method is available.
|
method is available.
|
||||||
|
@ -155,16 +151,11 @@ class NullUI(object):
|
||||||
def onecmd(self, line):
|
def onecmd(self, line):
|
||||||
if not line:
|
if not line:
|
||||||
return
|
return
|
||||||
#v_line = line.split()
|
|
||||||
cmd, _, line = line.partition(' ')
|
cmd, _, line = line.partition(' ')
|
||||||
if cmd in self._builtins:
|
|
||||||
args = shlex.split(line)
|
|
||||||
self._builtins[cmd](*args)
|
|
||||||
else:
|
|
||||||
try:
|
try:
|
||||||
parser = self._commands[cmd]._create_parser()
|
parser = self._commands[cmd].create_parser()
|
||||||
except KeyError:
|
except KeyError:
|
||||||
print templates.ERROR('Error! unknown command: %s' % cmd)
|
print templates.ERROR('unknown command: %s' % cmd)
|
||||||
return
|
return
|
||||||
args = self._commands[cmd].split(line)
|
args = self._commands[cmd].split(line)
|
||||||
options, args = parser.parse_args(args)
|
options, args = parser.parse_args(args)
|
||||||
|
@ -179,34 +170,6 @@ class NullUI(object):
|
||||||
def postcmd(self):
|
def postcmd(self):
|
||||||
client.force_call()
|
client.force_call()
|
||||||
|
|
||||||
def _all_commands_keys_generator(self):
|
|
||||||
return [ (self._commands, key) for key in self._commands] +\
|
|
||||||
[ (self._builtins, key) for key in self._builtins]
|
|
||||||
|
|
||||||
def help(self, *args):
|
|
||||||
"""displays this text"""
|
|
||||||
usage = 'usage: help [command]'
|
|
||||||
if args:
|
|
||||||
if len(args) > 1:
|
|
||||||
print usage
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
cmd = self._all_commands[args[0]]
|
|
||||||
except KeyError:
|
|
||||||
print templates.ERROR('unknown command %r' % args[0])
|
|
||||||
return
|
|
||||||
try:
|
|
||||||
parser = cmd.create_parser()
|
|
||||||
print parser.format_help()
|
|
||||||
except AttributeError, e:
|
|
||||||
print cmd.__doc__ or 'No help for this command'
|
|
||||||
else:
|
|
||||||
max_length = max( len(k) for k in self._all_commands)
|
|
||||||
for cmd in sorted(self._all_commands):
|
|
||||||
print templates.help(max_length, cmd, self._all_commands[cmd].__doc__ or '')
|
|
||||||
print
|
|
||||||
print 'for help on a specific command, use "<command> --help"'
|
|
||||||
|
|
||||||
def cmdloop(self):
|
def cmdloop(self):
|
||||||
self.preloop()
|
self.preloop()
|
||||||
try:
|
try:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue