🪲 Fixed again the folder creation for new chats.

🪲 Fixed /period giving an error message despite successfully
saving the new period.
🪲 Fixed /answer not updating.
This commit is contained in:
vylion 2019-03-31 14:55:51 +02:00
parent d075624263
commit f015e4bc60
4 changed files with 41 additions and 30 deletions

View file

@ -24,24 +24,27 @@ class Archivist(object):
self.readOnly = readOnly self.readOnly = readOnly
self.filterCids = filterCids self.filterCids = filterCids
self.bypass = bypass self.bypass = bypass
self.scribeFolder = chatdir + "chat_{tag}"
self.scribePath = chatdir + "chat_{tag}/{file}{ext}" self.scribePath = chatdir + "chat_{tag}/{file}{ext}"
def openfile(self, filename, mode):
if not os.path.exists(os.path.dirname(filename)):
try:
os.makedirs(os.path.dirname(filename))
except OSError as e:
if e.errno != errno.EEXIST:
raise
return open(filename, mode)
def store(self, tag, log, gen): def store(self, tag, log, gen):
scribefolder = self.scribeFolder.format(tag=tag)
cardfile = self.scribePath.format(tag=tag, file="card", ext=".txt")
if self.readOnly: if self.readOnly:
return return
file = self.openfile(self.scribePath.format(tag=tag, file="card", ext=".txt"), 'w') try:
if not os.path.exists(scribefolder):
os.makedirs(scribefolder, exist_ok=True)
self.logger.info("Storing a new chat. Folder {} created.".format(scribefolder))
except:
self.logger.error("Failed creating {} folder.".format(scribefolder))
return
file = open(cardfile, 'w')
file.write(log) file.write(log)
file.close() file.close()
file = self.openfile(self.scribePath.format(tag=tag, file="record", ext=self.chatext), 'w') if gen is not None:
recordfile = self.scribePath.format(tag=tag, file="record", ext=self.chatext)
file = open(recordfile, 'w')
file.write(gen) file.write(gen)
file.close() file.close()
@ -80,8 +83,8 @@ class Archivist(object):
file.close() file.close()
return Markov.loads(record) return Markov.loads(record)
except: except:
self.logger.error("Parrot file {} not found. Assuming first time parrot.".format(filepath)) self.logger.error("Parrot file {} not found.".format(filepath))
return Markov() return None
def wakeScriptorium(self): def wakeScriptorium(self):
scriptorium = {} scriptorium = {}

View file

@ -46,9 +46,10 @@ class Scribe(object):
self.countdown = self.chat.freq self.countdown = self.chat.freq
self.logger = self.archivist.logger self.logger = self.archivist.logger
def FromChat(chat, archivist): def FromChat(chat, archivist, newchat=False):
chatlog = Chatlog(chat.id, chat.type, getTitle(chat)) chatlog = Chatlog(chat.id, chat.type, getTitle(chat))
return Scribe(chatlog, archivist) scribe = Scribe(chatlog, archivist)
return scribe
def FromData(data, archivist): def FromData(data, archivist):
return None return None

View file

@ -2,6 +2,7 @@
import random import random
from scribe import Scribe from scribe import Scribe
from markov import Markov
from telegram.error import * from telegram.error import *
def send(bot, cid, text, replying=None, format=None, logger=None, **kwargs): def send(bot, cid, text, replying=None, format=None, logger=None, **kwargs):
@ -78,7 +79,7 @@ class Speaker(object):
def getScribe(self, chat): def getScribe(self, chat):
cid = str(chat.id) cid = str(chat.id)
if not cid in self.scriptorium: if not cid in self.scriptorium:
scribe = Scribe.FromChat(chat, self.archivist) scribe = Scribe.FromChat(chat, self.archivist, newchat=True)
self.scriptorium[cid] = scribe self.scriptorium[cid] = scribe
return scribe return scribe
else: else:
@ -103,6 +104,16 @@ class Speaker(object):
else: else:
scribe.store(self.parrot.dumps()) scribe.store(self.parrot.dumps())
def loadParrot(self, scribe):
newParrot = False
self.parrot = self.archivist.wakeParrot(scribe.cid())
if self.parrot is None:
newParrot = True
self.parrot = Markov()
scribe.teachParrot(self.parrot)
self.store(scribe)
return newParrot
def read(self, bot, update): def read(self, bot, update):
chat = update.message.chat chat = update.message.chat
scribe = self.getScribe(chat) scribe = self.getScribe(chat)
@ -122,9 +133,7 @@ class Speaker(object):
rid = scribe.getReference() if random.random() <= self.reply else None rid = scribe.getReference() if random.random() <= self.reply else None
self.say(bot, scribe, replying=rid) self.say(bot, scribe, replying=rid)
elif (scribe.freq() - scribe.countdown) % self.archivist.saveCount == 0: elif (scribe.freq() - scribe.countdown) % self.archivist.saveCount == 0:
self.parrot = self.archivist.wakeParrot(scribe.cid()) self.loadParrot(scribe)
scribe.teachParrot(self.parrot)
self.store(scribe)
def speak(self, bot, update): def speak(self, bot, update):
chat = (update.message.chat) chat = (update.message.chat)
@ -158,9 +167,7 @@ class Speaker(object):
if self.filterCids is not None and not scribe.cid() in self.filterCids: if self.filterCids is not None and not scribe.cid() in self.filterCids:
return return
self.parrot = self.archivist.wakeParrot(scribe.cid()) self.loadParrot(scribe)
scribe.teachParrot(self.parrot)
scribe.store(self.parrot)
try: try:
send(bot, scribe.cid(), self.speech(scribe), replying, logger=self.logger, **kwargs) send(bot, scribe.cid(), self.speech(scribe), replying, logger=self.logger, **kwargs)
if self.bypass: if self.bypass:
@ -204,7 +211,7 @@ class Speaker(object):
freq = int(words[1]) freq = int(words[1])
freq = scribe.setFreq(freq) freq = scribe.setFreq(freq)
update.message.reply_text("Period of speaking set to {}.".format(freq)) update.message.reply_text("Period of speaking set to {}.".format(freq))
scribe.store() scribe.store(None)
except: except:
update.message.reply_text("Format was confusing; period unchanged from {}.".format(scribe.freq())) update.message.reply_text("Format was confusing; period unchanged from {}.".format(scribe.freq()))
@ -223,10 +230,10 @@ class Speaker(object):
update.message.reply_text("You do not have permissions to do that.") update.message.reply_text("You do not have permissions to do that.")
return return
try: try:
afreq = int(words[1]) answ = float(words[1])
afreq = scribe.setAnswer(afreq) answ = scribe.setAnswer(answ)
update.message.reply_text("Answer probability set to {}.".format(afreq)) update.message.reply_text("Period of speaking set to {}.".format(answ))
scribe.store() scribe.store(None)
except: except:
update.message.reply_text("Format was confusing; answer probability unchanged from {}.".format(scribe.answer())) update.message.reply_text("Format was confusing; answer probability unchanged from {}.".format(scribe.answer()))

View file

@ -83,7 +83,7 @@ def main():
chatext=".vls", chatext=".vls",
admin=args.admin_id, admin=args.admin_id,
filterCids=filterCids, filterCids=filterCids,
readOnly=True readOnly=False
) )
speakerbot = Speaker("velasco", "@" + username, archivist, logger, wakeup=args.wakeup) speakerbot = Speaker("velasco", "@" + username, archivist, logger, wakeup=args.wakeup)