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