thread, séparation des fichiers, toussa...

This commit is contained in:
raspbeguy 2016-06-01 15:57:39 +02:00
parent 557ec8bebd
commit b9af403a43
3 changed files with 55 additions and 24 deletions

3
.gitignore vendored
View File

@ -58,3 +58,6 @@ docs/_build/
# PyBuilder
target/
# vim
*.swp
*.swo

41
sexiibot/channel.py Normal file
View File

@ -0,0 +1,41 @@
#!/bin/env python
import time
import threading
#from core import Sexiibot
class Channel(object):
"""Channel class"""
def __init__(self, name, bot, extensions=None):
bot.join(name)
self.__name = name
self.__bot = bot
self.__in = open(bot.getIrcPath() + "%s/in" % name, 'w', 1)
self.__out = open(bot.getIrcPath() + "%s/out" % name, 'r')
self.__exts = extentions
self.__mode = None
self.__alive = True
self.__thread = threading.Thread(target=watch).start()
def message(self, msg):
self.__in.write(msg + "\n")
def action(self, action):
self.message("/j %s" % action)
def leave(self, msg="sexiibot, the bot you're dreaming about"):
self.message("/l %s" % msg)
self.__alive = False
self.__thread.join()
self.__in.close()
self.__out.close()
def watch(self):
while self.__alive:
line = self.__out.readline()
if line:
print(line) #À ce stade du développement on ne traite rien
time.sleep(0.1)

View File

@ -2,30 +2,12 @@
import os
import subprocess
class Channel(object):
"""Channel class"""
def __init__(self, name, network, nick, extensions=None):
self.__name = name
self.__nick = nick
self.__in = open("%s/irc/%s/%s/in" % (os.environ["HOME"], network, name), "w", 1)
self.__out = open("%s/irc/%s/%s/out" % (os.environ["HOME"], network, name), "r")
self.__exts = extentions
self.__mode = None
def message(self, message):
self.__in.write(message + "\n")
def action(self, action):
message("/j %s" % action)
from channel import Channel
class Sexiibot(object):
"""Core sexiibot class"""
def __init__(self, new=True, server, port=None, ssl=False, channel, nick, realname=None, extensions, iipath=None)
def __init__(self, server, nick, new=True, port=None, ssl=False, channel=None, realname=None, extensions=None, iipath=None):
if new:
self.__server = server
else:
@ -34,7 +16,8 @@ class Sexiibot(object):
self.__ssl = ssl
self.__channels = channels
self.__nick = nick
self.__realname = realname
if not realname is None:
self.__realname = nick
else:
self.__realname = realname
self.__in = None
@ -43,7 +26,7 @@ class Sexiibot(object):
sekf.__iiproc = None
def start(self):
exists = os.path.isfile("%s/irc/%s/in" % (os.environ["HOME"], self.__server))
exists = os.path.isfile(self.getIrcPath() + "in")
if not self.__server is None:
# Check if a ii instance isn't already running on our potentially existing FIFO
if exists:
@ -59,13 +42,17 @@ class Sexiibot(object):
if self.__ssl:
cmd += ["-e", "ssl"]
self.__iiproc = subprocess.Popen(cmd) # self.__iiproc.terminate() later to stop
self.__in = open("%s/irc/%s/in" % (os.environ["HOME"], self.__server), "w", 1)
self.__in = open(self.getIrcPath() + "in", 'w', 1)
self.__out = open(self.getIrcPath() + "out", 'r')
def messageServer(self, message):
self.__in.write(message + "\n")
def join(self, channel):
messageServer("/j %s" % channel)
self.messageServer("/j %s" % channel)
def messageChannel(self, channel, message):
self.__channels[channel].message(message)
def getIrcPath():
return "%s/irc/%s/" % (os.environ["HOME"], self.__server)