Removed doindent and trap KeyboardInterrupt in supybot.

This commit is contained in:
James Vega 2004-02-23 10:50:40 +00:00
parent 52230d16e1
commit ad1a23fcd2
2 changed files with 112 additions and 105 deletions

View File

@ -144,25 +144,27 @@ if __name__ == '__main__':
if len(args) > 1: if len(args) > 1:
parser.error() parser.error()
elif not args: elif not args:
try:
import socket import socket
import ircutils import ircutils
import questions import questions
questions.output("""It seems like you're running supybot for the first questions.output("""It seems like you're running supybot for the
time. Or, perhaps, you just forgot to give this program an argument first time. Or, perhaps, you just forgot to give this program an
for your registry file. If the latter is the case, simply press Ctrl-C argument for your registry file. If the latter is the case,
and this script will exit and you can run it again as indicated. If simply press Ctrl-C and this script will exit and you can run it
the former is the case, however, we'll have a few questions for you again as indicated. If the former is the case, however, we'll
to write your initial registry file.""") have a few questions for you to write your initial registry
file.""")
### ###
# Nick. # Nick.
### ###
nick = questions.something("""What nick would you like your bot to nick = questions.something("""What nick would you like your bot to
use?""") use?""")
while not ircutils.isNick(nick): while not ircutils.isNick(nick):
questions.output("""That's not a valid IRC nick. Please choose a questions.output("""That's not a valid IRC nick. Please
different nick.""") choose a different nick.""")
nick = questions.something("""What nick would you like your bot nick = questions.something("""What nick would you like your
to use?""") bot to use?""")
### ###
# Server. # Server.
@ -173,14 +175,14 @@ if __name__ == '__main__':
questions.output("""%s resolved to %s.""" % (server, ip)) questions.output("""%s resolved to %s.""" % (server, ip))
return True return True
except socket.error: except socket.error:
questions.output("""That's not a valid hostname. Please enter questions.output("""That's not a valid hostname. Please
a hostname that resolves.""") enter a hostname that resolves.""")
return False return False
server = questions.something("""What server would you like your bot server = questions.something("""What server would you like your
to connect to?""") bot to connect to?""")
while not checkServer(server): while not checkServer(server):
server = questions.something("""What server would you like your bot server = questions.something("""What server would you like
to connect to?""") your bot to connect to?""")
### ###
# Channels. # Channels.
@ -190,34 +192,35 @@ if __name__ == '__main__':
if ',' in channel: if ',' in channel:
(channel, _) = channel.split(',', 1) (channel, _) = channel.split(',', 1)
if not ircutils.isChannel(channel): if not ircutils.isChannel(channel):
questions.output("""%s is not a valid IRC channel. Please questions.output("""%s is not a valid IRC channel.
choose a different channel.""" % channel) Please choose a different channel.""" % channel)
return False return False
return True return True
channels = questions.something("""What channels would you like your bot channels = questions.something("""What channels would you like
to join when it connects to %s? Separate your channels by spaces; if your bot to join when it connects to %s? Separate your channels
any channels require a keyword to join, separate the keyword from the by spaces; if any channels require a keyword to join, separate the
channel by a comma. For instance, if you want to join #supybot with
no keyword and #secret with a keyword of 'foo', you would type
'#supybot #secret,foo' without the quotes.""" % server)
while not checkChannels(channels):
channels = questions.something("""What channels would you like your
bot to join when it connects to %s? Separate your channels by
spaces; if any channels require a keyword to join, separate the
keyword from the channel by a comma. For instance, if you want to keyword from the channel by a comma. For instance, if you want to
join #supybot with no keyword and #secret with a keyword of 'foo', join #supybot with no keyword and #secret with a keyword of 'foo',
you would type '#supybot #secret,foo' without the quotes. you would type '#supybot #secret,foo' without the quotes.""" %
""" % server) server)
while not checkChannels(channels):
channels = questions.something("""What channels would you like
your bot to join when it connects to %s? Separate your
channels by spaces; if any channels require a keyword to join,
separate the keyword from the channel by a comma. For
instance, if you want to join #supybot with no keyword and
#secret with a keyword of 'foo', you would type '#supybot
#secret,foo' without the quotes. """ % server)
### ###
# Filename. # Filename.
### ###
def checkFilename(s): def checkFilename(s):
if os.path.exists(s): if os.path.exists(s):
questions.output("""That file already exists. Please choose a questions.output("""That file already exists. Please
file that doesn't exist yet. You can always copy it over choose a file that doesn't exist yet. You can always copy
later, of course, but we'd rather play it safe ourselves and it over later, of course, but we'd rather play it safe
not risk overwriting an important file.""") ourselves and not risk overwriting an important file.""")
return False return False
try: try:
fd = file(s, 'w') fd = file(s, 'w')
@ -225,24 +228,32 @@ if __name__ == '__main__':
fd.write('supybot.server: %s\n' % server) fd.write('supybot.server: %s\n' % server)
fd.write('supybot.channels: %s\n' % channels) fd.write('supybot.channels: %s\n' % channels)
fd.close() fd.close()
questions.output("""File %s written. Now, to run your bot, questions.output("""File %s written. Now, to run your
run this script with just that filename as an option. Once you bot, run this script with just that filename as an option.
do so, your configuration file will become much fuller and more Once you do so, your configuration file will become much
complete, with help descriptions describing all the options and fuller and more complete, with help descriptions
a significant number more options than you see now. Have fun! describing all the options and a significant number more
""" % s) options than you see now. Have fun! """ % s)
return True return True
except EnvironmentError, e: except EnvironmentError, e:
questions.output("""Python told me that it couldn't create your questions.output("""Python told me that it couldn't create
file, giving me this specific error: %s.""" % e) your file, giving me this specific error: %s.""" % e)
return False return False
filename = questions.something("""What filename would you like to write
this configuration to?""")
while not checkFilename(filename):
filename = questions.something("""What filename would you like to filename = questions.something("""What filename would you like to
write this configuration to?""") write this configuration to?""")
while not checkFilename(filename):
filename = questions.something("""What filename would you like
to write this configuration to?""")
questions.output("""Great! Seeya on the flipside!""") questions.output("""Great! Seeya on the flipside!""")
sys.exit(0) sys.exit(0)
except KeyboardInterrupt:
print
print
questions.output("""Well, it looks like you cancelled out of the
bot before it was done. Unfortunately, I didn't get to write
anything to file. Please run the bot/wizard again to
completion.""")
sys.exit(0)
else: else:
registryFilename = args.pop() registryFilename = args.pop()
try: try:

View File

@ -48,19 +48,14 @@ def output(s, unformatted=True):
print s print s
print print
# doindent is used in yn(). def expect(prompt, possibilities, recursed=False, default=None,
def expect(prompt, possibilities, recursed=False, acceptEmpty=False):
doindent=True, default=None, acceptEmpty=False):
"""Prompt the user with prompt, allow them to choose from possibilities. """Prompt the user with prompt, allow them to choose from possibilities.
If possibilities is empty, allow anything. If possibilities is empty, allow anything.
""" """
prompt = utils.normalizeWhitespace(prompt) prompt = utils.normalizeWhitespace(prompt)
originalPrompt = prompt originalPrompt = prompt
if doindent:
indent = ' ' * ((len(originalPrompt)%68) + 2)
else:
indent = ''
if recursed: if recursed:
output('Sorry, that response was not an option.') output('Sorry, that response was not an option.')
if possibilities: if possibilities:
@ -69,7 +64,7 @@ def expect(prompt, possibilities, recursed=False,
prompt = '%s [%s]' % (originalPrompt, '/ '.join(possibilities)) prompt = '%s [%s]' % (originalPrompt, '/ '.join(possibilities))
if default is not None: if default is not None:
prompt = '%s (default: %s)' % (prompt, default) prompt = '%s (default: %s)' % (prompt, default)
prompt = textwrap.fill(prompt, subsequent_indent=indent) prompt = textwrap.fill(prompt)
prompt = prompt.replace('/ ', '/') prompt = prompt.replace('/ ', '/')
prompt = prompt.strip() + ' ' prompt = prompt.strip() + ' '
if useBold: if useBold:
@ -78,6 +73,7 @@ def expect(prompt, possibilities, recursed=False,
if useBold: if useBold:
print ansi.RESET print ansi.RESET
s = s.strip() s = s.strip()
print
if possibilities: if possibilities:
if s in possibilities: if s in possibilities:
return s return s
@ -87,7 +83,7 @@ def expect(prompt, possibilities, recursed=False,
return s return s
else: else:
return expect(originalPrompt, possibilities, recursed=True, return expect(originalPrompt, possibilities, recursed=True,
doindent=doindent, default=default) default=default)
else: else:
if not s and default is not None: if not s and default is not None:
return default return default
@ -112,7 +108,7 @@ def yn(prompt, default=None):
default = 'y' default = 'y'
else: else:
default = 'n' default = 'n'
s = expect(prompt, ['y', 'n'], doindent=False, default=default) s = expect(prompt, ['y', 'n'], default=default)
if s is 'y': if s is 'y':
return True return True
else: else: