]> code.delx.au - pymsnt/blobdiff - src/utils.py
Updated to twistfix-0.4
[pymsnt] / src / utils.py
index fc29c99f4e99552bf5bbef8b9391020542a535c4..cb73ea392e579f69f2e3051f0dd1964cd8f54faf 100644 (file)
@@ -1,41 +1,45 @@
-# Copyright 2004 James Bunton <james@delx.cjb.net>
+# Copyright 2004-2005 James Bunton <james@delx.cjb.net>
 # Licensed for distribution under the GPL version 2, check COPYING for details
 
-def fudgestr(text, num):
-       if(not (text.__class__ in [str, unicode])): return ""
-       newtext = ""
-       for c in text:
-               i = ord(c)
-               if(i >= num):
-                       i = ord(' ')
-               newtext += chr(i)
-       return newtext
 
-def latin1(text):
-       return fudgestr(text, 128)
 
+def getLang(el):
+       return el.getAttribute((u'http://www.w3.org/XML/1998/namespace', u'lang'))
 
-def copyDict(dic):
-       """ Does a deep copy of a dictionary """
-       out = {}
-       for key in dic.keys():
-               out[key] = dic[key]
-       return out
 
-def copyList(lst):
-       """ Does a deep copy of a list """
-       out = []
-       for i in lst:
-               out.append(i)
-       return out
+import sha
+def socks5Hash(sid, initiator, target):
+       return sha.new("%s%s%s" % (sid, initiator, target)).hexdigest()
 
-def mutilateMe(me):
-       """ Mutilates a class :) """
-#      for key in dir(me):
-#              exec "me." + key + " = None"
 
-def getLang(el):
-       return el.getAttribute((u'http://www.w3.org/XML/1998/namespace', u'lang'))
+import urllib
+import os.path
+def getURLBits(url, assumedType=None):
+       type, rest = urllib.splittype(url)
+       if assumedType and type != assumedType:
+               return
+       hostport, path = urllib.splithost(rest)
+       host, port = urllib.splitnport(hostport, 80)
+       filename = os.path.basename(path)
+       return host, port, path, filename
+
+
+try:
+       import Image
+       import StringIO
+       
+       def convertToPNG(imageData):
+               inbuff = StringIO.StringIO(imageData)
+               outbuff = StringIO.StringIO()
+               Image.open(inbuff).save(outbuff, "PNG")
+               outbuff.seek(0)
+               imageData = outbuff.read()
+               return imageData
+except ImportError:
+       print "WARNING! Only PNG avatars will be understood by this transport. Please install the Python Imaging Library."
+
+       def convertToPNG(imageData):
+               return ""
 
 
 errorCodeMap = {
@@ -63,117 +67,5 @@ errorCodeMap = {
 "unexpected-request"           :       400
 }
 
-def doPath(path):
-       if(path and path[0] == "/"):
-               return path
-       else:
-               return "../" + path
-
-
-def parseText(text):
-       t = TextParser()
-       t.parseString(text)
-       return t.root
-
-def parseFile(filename):
-       t = TextParser()
-       t.parseFile(filename)
-       return t.root
-
-class TextParser:
-       """ Taken from http://xoomer.virgilio.it/dialtone/rsschannel.py """
-
-       def __init__(self):
-               self.root = None
-
-       def parseFile(self, filename):
-               return self.parseString(file(filename).read())
-
-       def parseString(self, data):
-               if(checkTwisted()):
-                       from twisted.xish.domish import SuxElementStream
-               else:
-                       from tlib.domish import SuxElementStream
-               es = SuxElementStream()
-               es.DocumentStartEvent = self.docStart
-               es.DocumentEndEvent = self.docEnd
-               es.ElementEvent = self.element
-               es.parse(data)
-               return self.root
-
-       def docStart(self, e):
-               self.root = e
-
-       def docEnd(self):
-               pass
-
-       def element(self, e):
-               self.root.addChild(e)
-
-
-
-checkTwistedCached = None
-def checkTwisted():
-       """ Returns False if we're using an old version that needs tlib, otherwise returns True """
-       global checkTwistedCached
-       if(checkTwistedCached == None):
-               import twisted.copyright
-               checkTwistedCached = (VersionNumber(twisted.copyright.version) >= VersionNumber("2.0.0"))
-       return checkTwistedCached
-
-class VersionNumber:
-       def __init__(self, vstring):
-               self.varray = [0]
-               index = 0 
-               flag = True
-               for c in vstring:
-                       if(c == '.'):
-                               self.varray.append(0)
-                               index += 1
-                               flag = True
-                       elif(c.isdigit() and flag):
-                               self.varray[index] *= 10
-                               self.varray[index] += int(c)
-                       else:
-                               flag = False
-       
-       def __cmp__(self, other):
-               i = 0
-               while(True):
-                       if(i == len(other.varray)):
-                               if(i < len(self.varray)):
-                                       return 1
-                               else:
-                                       return 0
-                       if(i == len(self.varray)):
-                               if(i < len(other.varray)):
-                                       return -1
-                               else:
-                                       return 0
-
-                       if(self.varray[i] > other.varray[i]):
-                               return 1
-                       elif(self.varray[i] < other.varray[i]):
-                               return -1
-
-                       i += 1
-       
-
-
-class RollingStack:
-       def __init__(self, size):
-               self.lst = []
-               self.size = size
-       
-       def push(self, data):
-               self.lst.append(str(data))
-               if(len(self.lst) > self.size):
-                       self.lst.remove(self.lst[0])
-       
-       def grabAll(self):
-               return "".join(self.lst)
-       
-       def flush(self):
-               self.lst = []