Fixed some .u stuff, added .bytes, and made Ctrl+C work.

master
Sean B. Palmer 2008-03-02 11:16:25 +00:00
parent 1078791cb6
commit 23c1dffa10
2 changed files with 51 additions and 11 deletions

View File

@ -7,9 +7,26 @@ Licensed under the Eiffel Forum License 2.
http://inamidst.com/phenny/ http://inamidst.com/phenny/
""" """
import sys, time, threading import sys, os, time, threading, signal
import bot import bot
class Watcher(object):
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
def __init__(self):
self.child = os.fork()
if self.child != 0:
self.watch()
def watch(self):
try: os.wait()
except KeyboardInterrupt:
self.kill()
sys.exit()
def kill(self):
try: os.kill(self.child, signal.SIGKILL)
except OSError: pass
def run_phenny(config): def run_phenny(config):
if hasattr(config, 'delay'): if hasattr(config, 'delay'):
delay = config.delay delay = config.delay
@ -19,6 +36,7 @@ def run_phenny(config):
p = bot.Phenny(config) p = bot.Phenny(config)
p.run(config.host) p.run(config.host)
Watcher()
while True: while True:
connect(config) connect(config)
if not isinstance(delay, int): break if not isinstance(delay, int): break

View File

@ -11,7 +11,8 @@ import re, unicodedata
from itertools import islice from itertools import islice
def about(u, cp=None, name=None): def about(u, cp=None, name=None):
if cp is None: cp = ord(u) if cp is None:
cp = ord(u)
if name is None: if name is None:
try: name = unicodedata.name(u) try: name = unicodedata.name(u)
except ValueError: except ValueError:
@ -65,16 +66,27 @@ def codepoint_extended(arg):
def u(phenny, input): def u(phenny, input):
arg = input.bytes[3:] arg = input.bytes[3:]
# phenny.msg('#inamidst', '%r' % arg)
if not arg:
return phenny.reply('You gave me zero length input.')
ascii = True # @@ space
for c in arg: if set(arg.upper()) - set(
if ord(c) >= 0x80: 'ABCDEFGHIJKLMNOPQRSTUVWYXYZ0123456789- .?+*{}[]\\/^$'):
ascii = False printable = False
else: printable = True
if ascii: if printable:
if set(arg.upper()) - set('ABCDEFGHIJKLMNOPQRSTUVWXYZ '): extended = False
for c in '.?+*{}[]\\/^$':
if c in arg:
extended = True extended = True
else: extended = False break
if len(arg) == 4:
try: u = unichr(int(arg, 16))
except ValueError: pass
else: return phenny.say(about(u))
if extended: if extended:
# look up a codepoint with regexp # look up a codepoint with regexp
@ -84,6 +96,8 @@ def u(phenny, input):
phenny.say(result) phenny.say(result)
elif (i == 2) and (len(results) > 3): elif (i == 2) and (len(results) > 3):
phenny.say(result + ' [...]') phenny.say(result + ' [...]')
if not results:
phenny.reply('Sorry, no results')
else: else:
# look up a codepoint freely # look up a codepoint freely
result = codepoint_simple(arg) result = codepoint_simple(arg)
@ -97,7 +111,15 @@ def u(phenny, input):
for u in text: for u in text:
phenny.say(about(u)) phenny.say(about(u))
# look up more than three podecoints # look up more than three podecoints
elif len(text) <= 8: elif len(text) <= 10:
phenny.reply(' '.join('U+%04X' % ord(c) for c in text)) phenny.reply(' '.join('U+%04X' % ord(c) for c in text))
else: phenny.reply('Sorry, your input is too long!') else: phenny.reply('Sorry, your input is too long!')
u.commands = ['u'] u.commands = ['u']
def bytes(phenny, input):
b = input.bytes
phenny.reply('%r' % b[b.find(' ') + 1:])
bytes.commands = ['bytes']
if __name__ == '__main__':
print __doc__.strip()