Skip to content

Commit 1d2483b

Browse files
author
Richard Jones
committed
roundup-admin now accepts abbreviated commands (eg. l = li = lis = list)
[thanks Engelbert Gruber for the inspiration]
1 parent 47a0a54 commit 1d2483b

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Feature:
99
. Added Structured Text rendering to htmltemplate, thanks Brad Clements.
1010
. Added CGI configuration via env vars (see roundup.cgi for details)
1111
. "roundup.cgi" is now installed to "<python-prefix>/share/roundup/cgi-bin"
12+
. roundup-admin now accepts abbreviated commands (eg. l = li = lis = list)
1213

1314
Fixed:
1415
. Fixed a bug in HTMLTemplate changes.

roundup-admin

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,44 @@
1616
# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1717
# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1818
#
19-
# $Id: roundup-admin,v 1.38 2001-11-05 23:45:40 richard Exp $
19+
# $Id: roundup-admin,v 1.39 2001-11-08 04:29:59 richard Exp $
2020

2121
import sys
2222
if int(sys.version[0]) < 2:
2323
print 'Roundup requires python 2.0 or later.'
2424
sys.exit(1)
2525

26-
import string, os, getpass, getopt, re
26+
import string, os, getpass, getopt, re, UserDict
2727
try:
2828
import csv
2929
except ImportError:
3030
csv = None
3131
from roundup import date, hyperdb, roundupdb, init, password
3232
import roundup.instance
3333

34+
class CommandDict(UserDict.UserDict):
35+
'''Simple dictionary that lets us do lookups using partial keys.
36+
37+
Original code submitted by Engelbert Gruber.
38+
'''
39+
_marker = []
40+
def get(self, key, default=_marker):
41+
d = self.data.get(key, default)
42+
if d is not default: return [(key, d)]
43+
keylist = self.data.keys()
44+
keylist.sort()
45+
l = []
46+
for ki in keylist:
47+
if ki.startswith(key):
48+
l.append((ki, self.data[ki]))
49+
if not l and default is self._marker:
50+
raise KeyError, key
51+
return l
52+
3453
class AdminTool:
3554

3655
def __init__(self):
37-
self.commands = {}
56+
self.commands = CommandDict()
3857
for k in AdminTool.__dict__.keys():
3958
if k[:3] == 'do_':
4059
self.commands[k[3:]] = getattr(self, k)
@@ -131,13 +150,22 @@ Command help:
131150
initopts -- init command options
132151
all -- all available help
133152
'''
134-
help = self.help.get(args[0], None)
135-
if help:
136-
help()
153+
topic = args[0]
154+
155+
# try help_ methods
156+
if self.help.has_key(topic):
157+
self.help[topic]()
158+
return
159+
160+
# try command docstrings
161+
try:
162+
l = self.commands.get(topic)
163+
except KeyError:
164+
print 'Sorry, no help for "%s"'%topic
137165
return
138-
help = self.commands.get(args[0], None)
139-
if help:
140-
# display the help, removing the docsring indent
166+
167+
# display the help for each match, removing the docsring indent
168+
for name, help in l:
141169
lines = nl_re.split(help.__doc__)
142170
print lines[0]
143171
indent = indent_re.match(lines[1])
@@ -147,8 +175,6 @@ Command help:
147175
print line[indent:]
148176
else:
149177
print line
150-
else:
151-
print 'Sorry, no help for "%s"'%args[0]
152178

153179
def help_initopts(self):
154180
import roundup.templates
@@ -601,6 +627,21 @@ Command help:
601627
self.help_all()
602628
return 0
603629

630+
# figure what the command is
631+
try:
632+
functions = self.commands.get(command)
633+
except KeyError:
634+
# not a valid command
635+
print 'Unknown command "%s" ("help commands" for a list)'%command
636+
return 1
637+
638+
# check for multiple matches
639+
if len(functions) > 1:
640+
print 'Multiple commands match "%s": %s'%(command,
641+
', '.join([i[0] for i in functions]))
642+
return 1
643+
command, function = functions[0]
644+
604645
# make sure we have an instance_home
605646
while not self.instance_home:
606647
self.instance_home = raw_input('Enter instance home: ').strip()
@@ -609,13 +650,6 @@ Command help:
609650
if command == 'init':
610651
return self.do_init(self.instance_home, args)
611652

612-
function = self.commands.get(command, None)
613-
614-
# not a valid command
615-
if function is None:
616-
print 'Unknown command "%s" ("help commands" for a list)'%command
617-
return 1
618-
619653
# get the instance
620654
instance = roundup.instance.open(self.instance_home)
621655
self.db = instance.open('admin')
@@ -687,6 +721,10 @@ if __name__ == '__main__':
687721

688722
#
689723
# $Log: not supported by cvs2svn $
724+
# Revision 1.38 2001/11/05 23:45:40 richard
725+
# Fixed newuser_action so it sets the cookie with the unencrypted password.
726+
# Also made it present nicer error messages (not tracebacks).
727+
#
690728
# Revision 1.37 2001/10/23 01:00:18 richard
691729
# Re-enabled login and registration access after lopping them off via
692730
# disabling access for anonymous users.

0 commit comments

Comments
 (0)