reworked parsing

This commit is contained in:
Stephen Dranger 2011-02-11 17:37:31 -06:00
parent 728da1632d
commit b73bd2afbd
20 changed files with 115 additions and 100 deletions

View file

@ -5,7 +5,7 @@ from PyQt4 import QtGui, QtCore
from dataobjs import PesterProfile, Mood, PesterHistory
from generic import PesterIcon, RightClickList
from parsetools import escapeBrackets, convertTags
from parsetools import convertTags
class PesterTabWindow(QtGui.QFrame):
def __init__(self, mainwindow, parent=None, convo="convo"):
@ -211,6 +211,7 @@ class PesterText(QtGui.QTextEdit):
parent = self.parent()
window = parent.mainwindow
me = window.profile()
quirks = window.userprofile.quirks if parent.applyquirks else None
if msg == "PESTERCHUM:BEGIN":
parent.setChumOpen(True)
msg = chum.pestermsg(me, systemColor, window.theme["convo/text/beganpester"])
@ -238,6 +239,8 @@ class PesterText(QtGui.QTextEdit):
window.chatlog.log(chum.handle, convertTags(msg, "bbcode"))
self.append(convertTags(msg))
elif msg[0:3] == "/me" or msg[0:13] == "PESTERCHUM:ME":
if quirks:
msg = quirks.apply(msg)
if msg[0:3] == "/me":
start = 3
else:
@ -256,11 +259,10 @@ class PesterText(QtGui.QTextEdit):
window.chatlog.log(chum.handle, convertTags(beginmsg, "bbcode"))
self.append(convertTags(beginmsg))
msg = escapeBrackets(msg)
msg = "<c=%s>%s: %s</c>" % (color, initials, msg)
self.append(convertTags(msg))
self.append(convertTags(msg, quirkobj=quirks))
if chum is me:
window.chatlog.log(parent.chum.handle, convertTags(msg, "bbcode"))
window.chatlog.log(parent.chum.handle, convertTags(msg, "bbcode", quirkobj=quirks))
else:
if window.idle:
idlethreshhold = 60
@ -514,16 +516,13 @@ class PesterConvo(QtGui.QFrame):
if text == "" or text[0:11] == "PESTERCHUM:":
return
self.history.add(text)
# deal with quirks here
if self.applyquirks:
qtext = self.mainwindow.userprofile.quirks.apply(text)
text = qtext
self.addMessage(text, True)
# if ceased, rebegin
if hasattr(self, 'chumopen') and not self.chumopen:
self.mainwindow.newConvoStarted.emit(QtCore.QString(self.title()), True)
# convert color tags
text = convertTags(text, "ctag")
quirkobj = self.mainwindow.userprofile.quirks if self.applyquirks else None
text = convertTags(text, "ctag", quirkobj)
self.messageSent.emit(text, self.title())
self.textInput.setText("")

BIN
convo.pyc

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
irc.pyc

Binary file not shown.

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,7 @@ from datetime import time, timedelta, datetime
from dataobjs import PesterProfile, Mood, PesterHistory
from generic import PesterIcon, RightClickList
from convo import PesterConvo, PesterInput, PesterText, PesterTabWindow
from parsetools import convertTags, escapeBrackets, addTimeInitial, timeProtocol
from parsetools import convertTags, addTimeInitial, timeProtocol
def delta2txt(d, format="pc"):
if format == "pc":
@ -227,6 +227,7 @@ class MemoText(PesterText):
parent = self.parent()
window = parent.mainwindow
me = window.profile()
quirks = window.userprofile.quirks if parent.applyquirks else None
msg = unicode(text)
chumdb = window.chumdb
if chum is not me: # SO MUCH WH1T3SP4C3 >:]
@ -268,6 +269,8 @@ class MemoText(PesterText):
time.openCurrentTime()
if msg[0:3] == "/me" or msg[0:13] == "PESTERCHUM:ME":
if quirks:
msg = quirks.apply(msg)
if msg[0:3] == "/me":
start = 3
else:
@ -279,9 +282,8 @@ class MemoText(PesterText):
else:
if chum is not me:
msg = addTimeInitial(msg, parent.times[chum.handle].getGrammar())
msg = escapeBrackets(msg)
self.append(convertTags(msg))
window.chatlog.log(parent.channel, convertTags(msg, "bbcode"))
self.append(convertTags(msg, quirksobj=quirks))
window.chatlog.log(parent.channel, convertTags(msg, "bbcode", quirks))
def changeTheme(self, theme):
@ -554,23 +556,19 @@ class PesterMemo(PesterConvo):
if self.time.getTime() == None:
self.sendtime()
grammar = self.time.getGrammar()
# deal with quirks here
if self.applyquirks:
qtext = self.mainwindow.userprofile.quirks.apply(text)
else:
qtext = text
if qtext[0:3] != "/me":
if text[0:3] != "/me":
initials = self.mainwindow.profile().initials()
colorcmd = self.mainwindow.profile().colorcmd()
clientText = "<c=%s>%s%s%s: %s</c>" % (colorcmd, grammar.pcf, initials, grammar.number, qtext)
clientText = "<c=%s>%s%s%s: %s</c>" % (colorcmd, grammar.pcf, initials, grammar.number, text)
# account for TC's parsing error
serverText = "<c=%s>%s: %s</c> " % (colorcmd, initials, qtext)
serverText = "<c=%s>%s: %s</c> " % (colorcmd, initials, text)
else:
clientText = qtext
clientText = text
serverText = clientText
self.addMessage(clientText, True)
# convert color tags
serverText = convertTags(unicode(serverText), "ctag")
quirks = self.mainwindow.userprofile.quirks if self.applyquirks else None
serverText = convertTags(unicode(serverText), "ctag", quirks)
self.messageSent.emit(serverText, self.title())
self.textInput.setText("")
@QtCore.pyqtSlot()

BIN
memos.pyc

Binary file not shown.

BIN
menus.pyc

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -4,35 +4,40 @@ from datetime import timedelta
from PyQt4 import QtGui
_ctag_begin = re.compile(r'(?i)<c=(.*?)>')
_ctag_end = re.compile(r'(?i)</c>')
_ctag_rgb = re.compile(r'\d+,\d+,\d+')
_urlre = re.compile(r"(?i)(http://[^\s<[]+)")
_urlre = re.compile(r"(?i)http://[^\s]+")
_memore = re.compile(r" (#[A-Za-z0-9_]+)")
def lexer(string, objlist):
"""objlist is a list: [(objecttype, re),...] list is in order of preference"""
stringlist = [string]
for (oType, regexp) in objlist:
newstringlist = copy(stringlist)
newstringlist = []
for (stri, s) in enumerate(stringlist):
tmp = []
if type(s) not in [str, unicode]:
newstringlist.append(s)
continue
lasti = 0
for m in regexp.finditer(string):
for m in regexp.finditer(s):
start = m.start()
end = m.end()
tag = oType(group(0), *groups())
tmp.append(string[lasti:start])
tmp.append(tag)
tag = oType(m.group(0), *m.groups())
if lasti != start:
newstringlist.append(s[lasti:start])
newstringlist.append(tag)
lasti = end
if lasti < len(string):
tmp.append(string[lasti:])
stringlist = stringlist[0:stri]+tmp+stringlist[stri+1:]
newstringlist.append(s[lasti:])
stringlist = copy(newstringlist)
return stringlist
def convertTags(string, format="html"):
if format not in ["html", "bbcode", "ctag"]:
raise ValueError("Color format not recognized")
def colorrepfunc(matchobj):
color = matchobj.group(1)
class colorBegin(object):
def __init__(self, string, color):
self.string = string
self.color = color
def convert(self, format):
color = self.color
if _ctag_rgb.match(color) is not None:
if format=='ctag':
return "<c=%s>" % (color)
@ -51,69 +56,75 @@ def convertTags(string, format="html"):
elif format == "ctag":
(r,g,b,a) = qc.getRgb()
return '<c=%s,%s,%s>' % (r,g,b)
string = _ctag_begin.sub(colorrepfunc, string)
endtag = {"html": "</span>", "bbcode": "[/color]", "ctag": "</c>"}
string = string.replace("</c>", endtag[format])
def urlrep(matchobj):
if format=="html":
return "<a href='%s'>%s</a>" % (matchobj.group(1).replace("&amp;", "&"), matchobj.group(1))
elif format=="bbcode":
return "[url]%s[/url]" % (matchobj.group(1).replace("&amp;", "&"))
elif format=="ctag":
return matchobj.group(1)
string = _urlre.sub(urlrep, string)
if format == "html":
string = _memore.sub(r" <a href='\1'>\1</a>", string)
for (code, f) in smiledict.iteritems():
string = string.replace(" %s" % (code), " <img src='smilies/%s' />" % (f))
return string
class colorEnd(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "</span>"
elif format == "bbcode":
return "[/color]"
else:
return self.string
class hyperlink(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "<a href='%s'>%s</a>" % (self.string, self.string)
elif format == "bbcode":
return "[url]%s[/url]" % (self.string)
else:
return self.string
class smiley(object):
def __init__(self, string):
self.string = string
def convert(self, format):
if format == "html":
return "<img src='smilies/%s' />" % (smiledict[self.string])
else:
return self.string
def escapeBrackets(string):
class beginTag(object):
def __init__(self, tag):
self.tag = tag
class endTag(object):
pass
newlist = []
begintagpos = [(m.start(), m.end()) for m in _ctag_begin.finditer(string)]
lasti = 0
for (s, e) in begintagpos:
newlist.append(string[lasti:s])
newlist.append(beginTag(string[s:e]))
lasti = e
if lasti < len(string):
newlist.append(string[lasti:])
tmp = []
for o in newlist:
if type(o) is not beginTag:
l = o.split("</c>")
tmp.append(l[0])
l = l[1:]
for item in l:
tmp.append(endTag())
tmp.append(item)
else:
tmp.append(o)
btlen = 0
etlen = 0
retval = ""
newlist = tmp
for o in newlist:
if type(o) is beginTag:
retval += o.tag.replace("&", "&amp;")
btlen +=1
elif type(o) is endTag:
if etlen >= btlen:
continue
def convertTags(string, format="html", quirkobj=None):
if format not in ["html", "bbcode", "ctag"]:
raise ValueError("Color format not recognized")
lexlist = [(colorBegin, _ctag_begin), (colorEnd, _ctag_end),
(hyperlink, _urlre), (hyperlink, _memore),
(smiley, _smilere)]
lexed = lexer(string, lexlist)
balanced = []
beginc = 0
endc = 0
for o in lexed:
if type(o) is colorBegin:
beginc += 1
balanced.append(o)
elif type(o) is colorEnd:
if beginc >= endc:
endc += 1
balanced.append(o)
else:
retval += "</c>"
etlen += 1
balanced.append(o.string)
else:
retval += o.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
if btlen > etlen:
for i in range(0, btlen-etlen):
retval += "</c>"
return retval
balanced.append(o)
if beginc > endc:
for i in range(0, beginc-endc):
balanced.append(colorEnd("</c>"))
escaped = ""
for o in balanced:
if type(o) in [str, unicode]:
if quirkobj:
o = quirkobj.apply(o)
if format == "html":
escaped += o.replace("&", "&amp;").replace(">", "&gt;").replace("<","&lt;")
else:
escaped += o
else:
escaped += o.convert(format)
return escaped
def addTimeInitial(string, grammar):
endofi = string.find(":")
@ -196,3 +207,5 @@ smiledict = {
":befuddled:": "what.gif",
":pumpkin:": "whatpumpkin.gif",
":trollcool:": "trollcool.gif"}
_smilere = re.compile("|".join(smiledict.keys()))

Binary file not shown.

View file

@ -9,6 +9,7 @@ import json
import codecs
import re
import socket
import platform
from PyQt4 import QtGui, QtCore
import pygame
@ -66,7 +67,10 @@ class PesterLog(object):
os.mkdir("logs/%s/%s" % (self.handle, handle))
fp = codecs.open("logs/%s/%s/%s.%s" % (self.handle, handle, handle, time), encoding='utf-8', mode='a')
self.convos[handle] = fp
self.convos[handle].write(msg+"\n")
if platform.system() == "Windows":
self.convos[handle].write(msg+"\r\n")
else:
self.convos[handle].write(msg+"\n")
self.convos[handle].flush()
def finish(self, handle):
if not self.convos.has_key(handle):

View file

@ -0,0 +1 @@
{"color": "#000000", "theme": "pesterchum", "quirks": [], "handle": "doctorHorrible"}

View file

@ -1 +1 @@
{"color": "#ff00ff", "theme": "trollian", "quirks": [], "handle": "ghostDunk"}
{"color": "#ff00ff", "theme": "pesterchum", "quirks": [{"to": "7", "from": "t", "type": "replace"}], "handle": "ghostDunk"}