From 865ab0316a53a32c9caac842bbd54d233cdd2665 Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Tue, 26 Jan 2010 17:10:37 +0000 Subject: [PATCH] nettoyage --- src/client.py | 6 ++--- src/config.py | 9 +++++-- src/connection.py | 12 ++++----- src/gui.py | 62 +++++++++++++++++++++++++++++------------------ src/logging.py | 14 +++++------ src/poezio.cfg | 6 +++++ 6 files changed, 67 insertions(+), 42 deletions(-) create mode 100644 src/poezio.cfg diff --git a/src/client.py b/src/client.py index ecbfb1de..8ab034d0 100644 --- a/src/client.py +++ b/src/client.py @@ -18,9 +18,6 @@ # along with Poezio. If not, see . import sys -#sys.stderr = open('errors', 'w') # never print anyerror -#sys.stdout = open('salut', 'w') - from connection import Connection from multiuserchat import MultiUserChat from config import config @@ -28,6 +25,9 @@ from handler import Handler from gui import Gui from curses import wrapper, initscr +logfile = config.get('logfile') +#sys.stderr = open(logfile, 'a') # print the errors in the logfile + class Client(object): """ Main class diff --git a/src/config.py b/src/config.py index ed50f622..238d2d5e 100644 --- a/src/config.py +++ b/src/config.py @@ -33,6 +33,10 @@ class Config(RawConfigParser): def get(self, option): return RawConfigParser.get(self, self.defsection, option) + def rget(self, option): + res = self.get(option) + print res + def getint(self, option): return int(self.get(option)) @@ -46,8 +50,9 @@ class Config(RawConfigParser): RawConfigParser.set(self, self.defsection, option, value) def save(self): - with copen(self.filename, "w", "utf-8", "ignore") as f: - RawConfigParser.write(self, f) + f = copen(self.filename, "w", "utf-8", "ignore") + RawConfigParser.write(self, f) + f.close() def setAndSave(self, option, value): self.set(option, value) diff --git a/src/connection.py b/src/connection.py index ec1f2552..2ef22425 100644 --- a/src/connection.py +++ b/src/connection.py @@ -21,7 +21,7 @@ import sys import xmpp from config import config -from logging import log +from logging import logger from threading import Thread from multiuserchat import MultiUserChat from handler import Handler @@ -40,23 +40,23 @@ class Connection(Thread): self.online = 0 # 1:connected, 2:auth confirmed self.jid = '' # we don't know our jid yet (anon account) if not self.server: - log.error('You should set a server in the configuration file') + logger.error('You should set a server in the configuration file') self.port = int(config.get('port')) if not self.port: - log.warning('No port set in configuration file, defaulting to 5222') + logger.warning('No port set in configuration file, defaulting to 5222') self.port = 5222 + self.client = xmpp.Client(self.server, debug=[]) def run(self): """ run in a thread connect to server """ - self.client = xmpp.Client(self.server, debug=[]) if not self.connect_to_server(self.server, self.port): - log.error('Could not connect to server') + logger.error('Could not connect to server') sys.exit(-1) if not self.authenticate(): - log.error('Could not authenticate to server') + logger.error('Could not authenticate to server') sys.exit(-1) self.client.sendInitPresence() self.online = 1 # 2 when confirmation of auth is received diff --git a/src/gui.py b/src/gui.py index bc2661b8..95025bb3 100644 --- a/src/gui.py +++ b/src/gui.py @@ -24,6 +24,8 @@ from curses import textpad import locale from datetime import datetime +from logging import logger + locale.setlocale(locale.LC_ALL, '') code = locale.getpreferredencoding() @@ -119,13 +121,21 @@ class Gui(object): 'prev': self.rotate_rooms_right, } - self.handler = Handler() self.handler.connect('on-connected', self.on_connected) self.handler.connect('join-room', self.join_room) self.handler.connect('room-presence', self.room_presence) self.handler.connect('room-message', self.room_message) + def current_room(self): + return self.rooms[0] + + def get_room_by_name(self, name): + for room in self.rooms: + if room.name == name: + return room + return None + def init_curses(self, stdscr): curses.start_color() curses.noecho() @@ -135,48 +145,50 @@ class Gui(object): curses.init_pair(4, curses.COLOR_BLUE, curses.COLOR_BLACK) # Participant curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK) # Visitor + def reset_curses(self): + curses.echo() + def on_connected(self): pass def join_room(self, room, nick): self.rooms.insert(0, Room(room, nick)) - self.window.refresh(self.rooms[0]) + self.window.refresh(self.current_room()) def rotate_rooms_left(self, args): self.rooms.append(self.rooms.pop(0)) - self.window.refresh(self.rooms[0]) + self.window.refresh(self.current_room()) def rotate_rooms_right(self, args): self.rooms.insert(0, self.rooms.pop()) - self.window.refresh(self.rooms[0]) + self.window.refresh(self.current_room()) def room_message(self, stanza): if stanza.getType() != 'groupchat': return # ignore all messages not comming from a MUC room_from = stanza.getFrom().getStripped() nick_from = stanza.getFrom().getResource() - for room in self.rooms: - if room_from == room.name: - room.add_message(nick_from, stanza.getBody()) - if room == self.rooms[0]: - self.window.text_win.refresh(room.lines) - self.window.user_win.refresh(room.users) - self.window.input.refresh() -# self.window.refresh(self.rooms[0]) - curses.doupdate() - break + room = self.get_room_by_name(name) + if not room: + return logger.warning("message received for a non-existing room: %s" % (name)) + room.add_message(nick_from, stanza.getBody()) + if room == self.current_room(): + self.window.text_win.refresh(room.lines) + self.window.user_win.refresh(room.users) + self.window.input.refresh() + curses.doupdate() def room_presence(self, stanza): from_nick = stanza.getFrom().getResource() from_room = stanza.getFrom().getStripped() - for room in self.rooms: - if from_room == room.name: - room.on_presence(stanza, from_nick) - if room == self.rooms[0]: - self.window.text_win.refresh(room.lines) - self.window.user_win.refresh(room.users) - curses.doupdate() - break + room = self.get_room_by_name() + if not room: + return logger.warning("presence received for a non-existing room: %s" % (name)) + room.on_presence(stanza, from_nick) + if room == self.current_rooom(): + self.window.text_win.refresh(room.lines) + self.window.user_win.refresh(room.users) + curses.doupdate() def execute(self): line = self.window.input.get_text() @@ -190,8 +202,9 @@ class Gui(object): func = self.commands[command] func(args) return - if self.rooms[0].name != 'Info': - self.muc.send_message(self.rooms[0].name, line) + if self.current_room().name != 'Info': + self.muc.send_message(self.current_room().name, line) + self.window.input.refresh() def command_join(self, args): room = args[0] @@ -199,6 +212,7 @@ class Gui(object): self.join_room(room, 'poezio') def command_quit(self, args): + self.reset_curses() sys.exit() def main_loop(self, stdscr): diff --git a/src/logging.py b/src/logging.py index b0b8b1aa..4bbedb9d 100644 --- a/src/logging.py +++ b/src/logging.py @@ -19,6 +19,7 @@ from config import config import sys +from datetime import datetime class Logger(object): """ @@ -29,17 +30,16 @@ class Logger(object): self.logfile = config.get('logfile') def warning(self, msg): - # change me - # add timestamp and stuff like that, it's cool - print 'Warning/error ' + msg if self.logfile: fd = open(self.logfile, 'a') - fd.write(msg+'\n') + fd.write(datetime.now().strftime("%H:%M:%S") + ' Warning [' + msg + ']') fd.close() def error(self, msg): - # change me too - self.warning(msg) + if self.logfile: + fd = open(self.logfile, 'a') + fd.write(datetime.now().strftime("%H:%M:%S") + ' Error [' + msg + ']') + fd.close() sys.exit(-1) -log = Logger() +logger = Logger() diff --git a/src/poezio.cfg b/src/poezio.cfg new file mode 100644 index 00000000..1a75f859 --- /dev/null +++ b/src/poezio.cfg @@ -0,0 +1,6 @@ +[Poezio] +logfile = salut +resource = poezio +server = louiz.org +port = 5222 +