From c47ca3b625bd9dc413a63761c908428a653b324c Mon Sep 17 00:00:00 2001 From: Stefan Krulj Date: Fri, 5 Oct 2012 22:19:16 +0200 Subject: [PATCH] Separated in modules, cleanup --- .gitignore | 1 + choose.py | 8 +++++++ cowsay.py | 23 ++++++++++++++++++ get_quote.py | 2 +- kraken.py | 21 ++++++++++++++++ logger.py | 7 ++++++ main.py | 68 ++++------------------------------------------------ quote.py | 0 sortiere.py | 14 ----------- 9 files changed, 66 insertions(+), 78 deletions(-) create mode 100644 .gitignore create mode 100644 choose.py create mode 100644 cowsay.py mode change 100755 => 100644 get_quote.py create mode 100644 kraken.py mode change 100755 => 100644 quote.py delete mode 100755 sortiere.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/choose.py b/choose.py new file mode 100644 index 0000000..4722de1 --- /dev/null +++ b/choose.py @@ -0,0 +1,8 @@ +import brathuhn + +def choose(msg,args): + if len(args)==1: + brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" item_1 item_2 ... item_n",'groupchat') + else: + brathuhn.sendMsg(brathuhn.ROOM,args[1+random.randint(0,len(args[1:])-1)],'groupchat') +brathuhn.addCommand("choose","Usage: !choose item_1 item_2 ... item_n",choose) diff --git a/cowsay.py b/cowsay.py new file mode 100644 index 0000000..3d83fe0 --- /dev/null +++ b/cowsay.py @@ -0,0 +1,23 @@ +import brathuhn, subprocess + +def cowsay(msg,args): + cowfile="default" + text="" + if( len(args)==2 ): + text=args[1] + elif( len(args)>=3 ): + cowfile=args[1] + for i in range(2,len(args)): + text+=args[i]+" " + else: + brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [cowfile] text",'groupchat') + return + + try: + txt=subprocess.check_output(["cowsay","-f",cowfile,text]) + for line in txt.split('\n'): + #brathuhn.sendStyledMsg(brathuhn.ROOM,line,'groupchat',) + brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat') + except subprocess.CalledProcessError: + pass +brathuhn.addCommand("cowsay","Usage: !cowsay [cowfile] text",cowsay) diff --git a/get_quote.py b/get_quote.py old mode 100755 new mode 100644 index c78737c..12d6e74 --- a/get_quote.py +++ b/get_quote.py @@ -88,4 +88,4 @@ def quote(msg,args): except exceptions.Exception as e: brathuhn.sendMsg(brathuhn.ROOM,"Network Error "+unicode(e),'groupchat') - +brathuhn.addCommand("quote","Usage: !quote language (two letter code) topic",quote) diff --git a/kraken.py b/kraken.py new file mode 100644 index 0000000..5361f15 --- /dev/null +++ b/kraken.py @@ -0,0 +1,21 @@ +import brathuhn + +kraken_img = """ + _,.---. ,''-._ + Release the kraken! ;- X ,/-- \\ + / \\ ' `. : + | \\ / | | ) + ,''-..--'\\ \\....-'__ ' + / .-...--'': ,. , _.-'O,' + .'/ _O_ O / /O)\\ \\''-O,-' + [\\_ .' /,' ,+-'._/O/ / /_ + \\/'-' :/ ' '`'''''`+ + _;._.:...""" + + +def kraken(mfrom,msg): + if "kraken" in msg.lower() and not brathuhn.room_nick in mfrom: + for line in kraken_img.split('\n'): + brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat') +brathuhn.addGroupListener(kraken) + diff --git a/logger.py b/logger.py index 02bcbbc..7ccc638 100644 --- a/logger.py +++ b/logger.py @@ -61,3 +61,10 @@ def status_logger(mfrom,msg,status): user_logs[key].write(unicode(mfrom)+" is now away\n") if status==brathuhn.STATUS_UNKNOWN: user_logs[key].write(unicode(mfrom)+" is now ???\n") + +brathuhn.addPrivateCommand("show_log","Usage: !show_log",show_log) +brathuhn.addPrivateCommand("start_log","Usage: !start_log",start_log) +brathuhn.addPrivateCommand("stop_log","Usage: !stop_log",stop_log) +brathuhn.addPrivateCommand("delete_log","Usage: !delete_log",del_log) +brathuhn.addGroupListener(message_logger) +brathuhn.addStatusListener(status_logger) diff --git a/main.py b/main.py index 74e26f1..8ca62ee 100755 --- a/main.py +++ b/main.py @@ -2,70 +2,12 @@ # -*- coding: utf-8 -*- import brathuhn -import subprocess -import random -from get_quote import * -brathuhn.addCommand("quote","Usage: !quote language (two letter code) topic",quote) - - -from logger import * -brathuhn.addPrivateCommand("show_log","Usage: !show_log",show_log) -brathuhn.addPrivateCommand("start_log","Usage: !start_log",start_log) -brathuhn.addPrivateCommand("stop_log","Usage: !stop_log",stop_log) -brathuhn.addPrivateCommand("delete_log","Usage: !delete_log",del_log) -brathuhn.addGroupListener(message_logger) -brathuhn.addStatusListener(status_logger) - - -def cowsay(msg,args): - cowfile="default" - text="" - if( len(args)==2 ): - text=args[1] - elif( len(args)>=3 ): - cowfile=args[1] - for i in range(2,len(args)): - text+=args[i]+" " - else: - brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" [cowfile] text",'groupchat') - return - - try: - txt=subprocess.check_output(["cowsay","-f",cowfile,text]) - for line in txt.split('\n'): - #brathuhn.sendStyledMsg(brathuhn.ROOM,line,'groupchat',) - brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat') - except subprocess.CalledProcessError: - pass -brathuhn.addCommand("cowsay","Usage: !cowsay [cowfile] text",cowsay) - - -def choose(msg,args): - if len(args)==1: - brathuhn.sendMsg(brathuhn.ROOM,"Usage: "+args[0]+" item_1 item_2 ... item_n",'groupchat') - else: - brathuhn.sendMsg(brathuhn.ROOM,args[1+random.randint(0,len(args[1:])-1)],'groupchat') -brathuhn.addCommand("choose","Usage: !choose item_1 item_2 ... item_n",choose) - -kraken_img = """ - _,.---. ,''-._ - Release the kraken! ;- X ,/-- \\ - / \\ ' `. : - | \\ / | | ) - ,''-..--'\\ \\....-'__ ' - / .-...--'': ,. , _.-'O,' - .'/ _O_ O / /O)\\ \\''-O,-' - [\\_ .' /,' ,+-'._/O/ / /_ - \\/'-' :/ ' '`'''''`+ - _;._.:...""" - - -def kraken(mfrom,msg): - if "kraken" in msg.lower() and not brathuhn.room_nick in mfrom: - for line in kraken_img.split('\n'): - brathuhn.sendMonoMsg(brathuhn.ROOM,line,'groupchat') -brathuhn.addGroupListener(kraken) +import kraken +import get_quote +import logger +import cowsay +import choose brathuhn.connect_and_join() brathuhn.loop() diff --git a/quote.py b/quote.py old mode 100755 new mode 100644 diff --git a/sortiere.py b/sortiere.py deleted file mode 100755 index f39a7f3..0000000 --- a/sortiere.py +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/python - -def sortiere(liste): - if len(liste)==1: - pivot = liste[0] - return [liste[0]] - elif len(liste)==0: - return [] - pivot = liste[0] - return sortiere([x for x in liste[1:] if x<= pivot]) + \ - [pivot] + \ - sortiere([x for x in liste[1:] if x> pivot]) - -print sortiere([2,3,4,1,2,8,9])