1
0
Fork 0
mirror of git://git.psyced.org/git/pypsyc synced 2024-08-15 03:20:04 +00:00

last state we had in cvs

This commit is contained in:
psyc://psyced.org/~lynX 2010-02-24 09:50:45 +01:00
commit 0f02e9cd76
128 changed files with 9224 additions and 0 deletions

67
mjacob/lib/__init__.py Executable file
View file

@ -0,0 +1,67 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## __init__.py
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# <C> Copyright 2007, Manuel Jacob
import re
# the following two functions are from the 'original' pypsyc
# (and slightly modified)
def get_host(uni):
m = re.match("^psyc:\/\/(.+)?\/~(.+)?\/?$", uni)
if m: return m.group(1)
m = re.match("^psyc:\/\/([^\/@]+)\@(.+?)\/?$", uni)
if m: return m.group(2)
m = re.match("^psyc:\/\/(.+)?\/\@(.+)?\/?$", uni)
if m: return m.group(1)
m = re.match("^psyc:\/\/(.+)$", uni)
if m: return m.group(1)
raise "invalid uni"
def get_user(uni):
m = re.match("^psyc:\/\/(.+)?\/~(.+)?\/?$", uni)
if m: return m.group(2)
m = re.match("^psyc:\/\/([^\/@]+)\@(.+?)\/?$", uni)
if m: return m.group(1)
raise "invalid uni"
def psyctext(packet):
text = packet.data
for key, value in packet.vars.items():
text = text.replace('[%s]' % key, str(value))
return text
class Vars(dict):
def __init__(self, vars, existing = {}):
dict.__init__(self, existing)
self.vars = vars
self.vars.update(existing)
def __setitem__(self, key, value):
dict.__setitem__(self, key, value)
self.vars.__setitem__(key, value)
all_mmpvars = (
'_source', '_source_identification', '_source_location', '_source_relay',
'_target', '_context', '_counter', '_length', '_initialize', '_fragment',
'_encoding', '_amount_fragments', '_list_using_modules',
'_list_require_modules', '_list_understand_modules', '_list_using_encoding',
'_list_require_encoding', '_list_understand_encoding',
'_list_using_protocols', '_list_require_protocols',
'_list_understand_protocols', '_trace', '_tag', '_tag_relay', '_relay')

26
mjacob/lib/packets.py Executable file
View file

@ -0,0 +1,26 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## packets.py
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# <C> Copyright 2007, Manuel Jacob
from __init__ import Vars
class MMPPacket:
def __init__(self, mmpvars = {}, body = ''):
self.vars = {}
self.mmpvars = Vars(self.vars, mmpvars)
self.body = body
class PSYCPacket(MMPPacket):
def __init__(self, mmpvars = {}, psycvars = {}, mc = None, data = ''):
MMPPacket.__init__(self, mmpvars)
self.psycvars = Vars(self.vars, psycvars)
self.mc = mc
self.data = data

159
mjacob/lib/parse.py Executable file
View file

@ -0,0 +1,159 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## parse.py
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# <C> Copyright 2007, Manuel Jacob
from packets import MMPPacket, PSYCPacket
def parse_vars(lines):
cvars = {}
pvars = {}
_list = None
for i in lines:
tmp = i.split('\t')
if tmp[0] == ':':
vars[_list].append(tmp[1])
continue
elif tmp[0].startswith(':'):
vars = cvars
elif tmp[0].startswith('='):
vars = pvars
if tmp[0][1:6] == '_list':
_list = tmp[0][1:]
vars[_list] = [tmp[1]]
else:
_list = None
vars[tmp[0][1:]] = tmp[1]
return cvars, pvars
class LineBased:
def __init__(self):
self.__buffer = ''
self.linemode = True
def data(self, data):
self.__buffer += data
while self.linemode and self.__buffer:
try:
line, self.__buffer = self.__buffer.split('\n', 1)
self.line(line)
except ValueError:
return
else:
if not self.linemode:
__buffer = self.__buffer
self.__buffer = ''
self.raw(__buffer)
class MMPParser(LineBased): # callback-based
pvars = {}
def __init__(self):
LineBased.__init__(self)
self.reset()
def reset(self):
self.mode = 'vars'
self.varlines = []
self.cvars = {}
self.body = ''
def line(self, line):
if line == '.':
self.recv()
elif self.mode == 'vars':
if line:
self.varlines.append(line)
else:
self.cvars, pvars = parse_vars(self.varlines)
for key, value in pvars.items():
if value:
self.pvars[key] = value
else:
if key in self.pvars:
del self.pvars[key]
self.mode = 'body'
else:
self.body += line + '\n'
def recv(self):
vars = self.pvars.copy()
vars.update(self.cvars)
if self.body and self.body[-1] == '\n':
self.body = self.body[:-1]
packet = MMPPacket(vars, self.body)
packet.not_to_render = True
self.recv_packet(packet)
self.reset()
def recv_packet(self, packet):
raise NotImplementedError
class PSYCParser:
pvars = {}
def parse(self, packet):
lines = packet.body.split('\n')
_is_text = False
cvars = {}
varlines = []
textlines = []
mc = ''
for i in lines:
if not i:
continue
tmp = i.split('\t')
if _is_text:
textlines.append(i)
elif i[0] in (':', '='):
varlines.append(i)
elif i[0] == '_':
cvars, pvars = parse_vars(varlines)
for key, value in pvars.items():
if value:
self.pvars[key] = value
else:
if key in self.pvars:
del self.pvars[key]
mc = i
_is_text = True
text = '\n'.join(textlines)
vars = self.pvars.copy()
vars.update(cvars)
packet = PSYCPacket(packet.mmpvars, vars, mc, text)
packet.not_to_render = True
return packet
if __name__ == '__main__':
MMPParser().data(':_context\ti\n=_foo\tbar\n\n_message_public\nHello\n.\n')
MMPParser().data(':_context\ti\n\n_message_public\nHello\n.\n')

70
mjacob/lib/render.py Executable file
View file

@ -0,0 +1,70 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
## render.py
# Licensed under the MIT license
# http://opensource.org/licenses/mit-license.php
# <C> Copyright 2007, Manuel Jacob
from packets import MMPPacket
def to_vars(cvars = {}, pvars = {}, other = {}):
data = ''
for key, value in cvars.items():
data += ":%s\t%s\n" % (key, value)
for key, value in pvars.items():
data += "=%s\t%s\n" % (key, value)
for key, value in other.items():
data += "%s\t%s\n" % (key, value)
return data
class MMPRenderer:
pvars = {}
new_pvars = {}
def render(self, packet):
data = ''
if packet.mmpvars:
data += to_vars(packet.mmpvars, self.new_pvars) + '\n'
self.new_pvars = {}
data += '%s\n.\n' % packet.body
return data.encode('utf-8')
def set_pvar(self, key, value):
self.new_pvars[key] = value
self.pvars[key] = value
def del_pvar(self, key):
self.new_pvars[key] = ''
del self.pvars[key]
class PSYCRenderer:
pvars = {}
new_pvars = {}
def render(self, packet):
data = ''
if packet.psycvars:
data += to_vars(packet.psycvars, self.new_pvars)
self.new_pvars = {}
if packet.mc:
data += packet.mc + '\n'
if packet.data:
data += packet.data
return MMPPacket(packet.mmpvars, data)
def set_pvar(self, key, value):
self.new_pvars[key] = value
self.pvars[key] = value
def del_pvar(self, key):
self.new_pvars[key] = ''
del self.pvars[key]