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,105 +144,116 @@ if __name__ == '__main__':
if len(args) > 1:
parser.error()
elif not args:
import socket
import ircutils
import questions
questions.output("""It seems like you're running supybot for the first
time. Or, perhaps, you just forgot to give this program an argument
for your registry file. If the latter is the case, simply press Ctrl-C
and this script will exit and you can run it again as indicated. If
the former is the case, however, we'll have a few questions for you
to write your initial registry file.""")
###
# Nick.
###
nick = questions.something("""What nick would you like your bot to
use?""")
while not ircutils.isNick(nick):
questions.output("""That's not a valid IRC nick. Please choose a
different nick.""")
nick = questions.something("""What nick would you like your bot
to use?""")
###
# Server.
###
def checkServer(server):
try:
ip = socket.gethostbyname(server)
questions.output("""%s resolved to %s.""" % (server, ip))
return True
except socket.error:
questions.output("""That's not a valid hostname. Please enter
a hostname that resolves.""")
return False
server = questions.something("""What server would you like your bot
to connect to?""")
while not checkServer(server):
server = questions.something("""What server would you like your bot
to connect to?""")
###
# Channels.
###
def checkChannels(s):
for channel in s.split():
if ',' in channel:
(channel, _) = channel.split(',', 1)
if not ircutils.isChannel(channel):
questions.output("""%s is not a valid IRC channel. Please
choose a different channel.""" % channel)
try:
import socket
import ircutils
import questions
questions.output("""It seems like you're running supybot for the
first time. Or, perhaps, you just forgot to give this program an
argument for your registry file. If the latter is the case,
simply press Ctrl-C and this script will exit and you can run it
again as indicated. If the former is the case, however, we'll
have a few questions for you to write your initial registry
file.""")
###
# Nick.
###
nick = questions.something("""What nick would you like your bot to
use?""")
while not ircutils.isNick(nick):
questions.output("""That's not a valid IRC nick. Please
choose a different nick.""")
nick = questions.something("""What nick would you like your
bot to use?""")
###
# Server.
###
def checkServer(server):
try:
ip = socket.gethostbyname(server)
questions.output("""%s resolved to %s.""" % (server, ip))
return True
except socket.error:
questions.output("""That's not a valid hostname. Please
enter a hostname that resolves.""")
return False
return True
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)
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
server = questions.something("""What server would you like your
bot to connect to?""")
while not checkServer(server):
server = questions.something("""What server would you like
your bot to connect to?""")
###
# Channels.
###
def checkChannels(s):
for channel in s.split():
if ',' in channel:
(channel, _) = channel.split(',', 1)
if not ircutils.isChannel(channel):
questions.output("""%s is not a valid IRC channel.
Please choose a different channel.""" % channel)
return False
return True
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.
###
def checkFilename(s):
if os.path.exists(s):
questions.output("""That file already exists. Please choose a
file that doesn't exist yet. You can always copy it over
later, of course, but we'd rather play it safe ourselves and
not risk overwriting an important file.""")
return False
try:
fd = file(s, 'w')
fd.write('supybot.nick: %s\n' % nick)
fd.write('supybot.server: %s\n' % server)
fd.write('supybot.channels: %s\n' % channels)
fd.close()
questions.output("""File %s written. Now, to run your bot,
run this script with just that filename as an option. Once you
do so, your configuration file will become much fuller and more
complete, with help descriptions describing all the options and
a significant number more options than you see now. Have fun!
""" % s)
return True
except EnvironmentError, e:
questions.output("""Python told me that it couldn't create your
file, giving me this specific error: %s.""" % e)
return False
filename = questions.something("""What filename would you like to write
this configuration to?""")
while not checkFilename(filename):
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 join #supybot with no keyword and
#secret with a keyword of 'foo', you would type '#supybot
#secret,foo' without the quotes. """ % server)
###
# Filename.
###
def checkFilename(s):
if os.path.exists(s):
questions.output("""That file already exists. Please
choose a file that doesn't exist yet. You can always copy
it over later, of course, but we'd rather play it safe
ourselves and not risk overwriting an important file.""")
return False
try:
fd = file(s, 'w')
fd.write('supybot.nick: %s\n' % nick)
fd.write('supybot.server: %s\n' % server)
fd.write('supybot.channels: %s\n' % channels)
fd.close()
questions.output("""File %s written. Now, to run your
bot, run this script with just that filename as an option.
Once you do so, your configuration file will become much
fuller and more complete, with help descriptions
describing all the options and a significant number more
options than you see now. Have fun! """ % s)
return True
except EnvironmentError, e:
questions.output("""Python told me that it couldn't create
your file, giving me this specific error: %s.""" % e)
return False
filename = questions.something("""What filename would you like to
write this configuration to?""")
questions.output("""Great! Seeya on the flipside!""")
sys.exit(0)
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!""")
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:
registryFilename = args.pop()
try:

View File

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