[Nym3-commit] r99 - trunk
nym3-devel@lists.noreply.org
nym3-devel@lists.noreply.org
Thu, 05 Aug 2004 23:19:41 +0200
Author: laurent
Date: 2004-08-05 23:19:40 +0200 (Thu, 05 Aug 2004)
New Revision: 99
Modified:
trunk/Message.py
Log:
That big key on your keyboard is called "space". Is *is* your friend.
Modified: trunk/Message.py
===================================================================
--- trunk/Message.py 2004-08-05 20:54:23 UTC (rev 98)
+++ trunk/Message.py 2004-08-05 21:19:40 UTC (rev 99)
@@ -6,44 +6,44 @@
class ParseError(Exception):
"""ParseError : error raised if anything goes wrong during parsing"""
def __init__(self,value):
- self.value=value
+ self.value = value
def __str__(self):
return repr(self.value)
class BadArgument(Exception):
"""BadArgument : error raised if a function is given an irrelevant argument"""
- def __init__(self,value):
- self.value=value
+ def __init__(self, value):
+ self.value = value
def __str__(self):
return repr(self.value)
def strToIntBE(s):
"""return the Int coded by the string s with the big endian convention"""
- def aux(s,ac):
+ def aux(s, ac):
if(s == ""):
return ac
else:
- return aux(s[1:],256*ac+ord(s[0]))
- return aux(s,0)
+ return aux(s[1:], 256 * ac + ord(s[0]))
+ return aux(s, 0)
def intToStrBE(n,mod):
"""return a string coding the n modulo 256^mod in big endian"""
- def aux(n,ac,mod):
+ def aux(n, ac, mod):
if(mod == 0):
return ac
else:
- return aux(n/256,chr(n%256)+ac,mod-1)
- return aux(n,"",mod)
+ return aux(n / 256, chr(n % 256) + ac, mod - 1)
+ return aux(n, "", mod)
def isMidList(l,n):
"""true if l is a list of strings of length n"""
def isMid(e):
- if( len(e)==n and type(e)==types.StringType ):
+ if( len(e) == n and type(e) == types.StringType):
return True
else:
return False
- lb=map(isMid,l)
- if(reduce(lambda x,y : x and y,lb)):
+ lb = map(isMid, l)
+ if (reduce(lambda x,y : x and y, lb)):
return True
else:
return False
@@ -55,7 +55,7 @@
class StrReader:
"""wraps a string and gives method to read it chunk by chunk"""
- def __init__(self,s):
+ def __init__(self, s):
"""initialize by wrapping a string"""
self.s = s
self.a = 0
@@ -63,18 +63,18 @@
def isEnd(self):
"""True if we have read the whole string"""
return (b == len(s))
- def next(self,n):
+ def next(self, n):
"""return the next n characters"""
- if(n<0):
+ if n < 0:
raise BadArgument("StrReader.next : n < 0")
- if(self.b+n > len(self.s)):
+ if self.b + n > len(self.s):
raise IndexError("StrReader.next : n too long")
self.a = self.b
self.b = self.b + n
return self.s[self.a:self.b]
def readHeader(self):
"""Read next header from the string"""
- h=Header()
+ h = Header()
h.fromStrReader(self)
return h
def readCommandCToS(self):
@@ -83,33 +83,33 @@
ct = ord(self.next(1))
cs = strToIntBE(self.next(3))
if (ct == 0):
- a=Create()
+ a = Create()
elif (ct == 1):
- a=Create2()
+ a = Create2()
elif (ct == 2):
- a=Surb()
+ a = Surb()
elif (ct == 3):
- a=Newpk()
+ a = Newpk()
elif (ct == 4):
- a=Relay()
+ a = Relay()
elif (ct == 5):
- a=Get()
+ a = Get()
elif (ct == 6):
- a=Summarize()
+ a = Summarize()
elif (ct == 7):
- a=Delete()
+ a = Delete()
elif (ct == 8):
- a=Policy()
+ a = Policy()
else:
raise ParseError("Undefined Command Type")
- a.fromStrReader(self,cs)
+ a.fromStrReader(self, cs)
return a
except IndexError: #is it really necessary? is it not considered in the various fromStrReader ?
raise ParseError("Bad Formed Command")
def readCommandCToSList(self):
"""Read a list of CommandSToC from string, until
the string is completely read"""
- l=[]
+ l = []
while (not self.isEnd()):
l.append(self.readCommandCToS)
return l
@@ -119,20 +119,20 @@
ct = ord(self.next(1))
cs = strToIntBE(self.next(3))
if (ct == 0):
- a=Created()
+ a = Created()
elif (ct == 1):
- a=Status()
+ a = Status()
elif (ct == 2):
- a=Summary()
+ a = Summary()
elif (ct == 3):
- a=Msg()
+ a = Msg()
elif (ct == 4):
- a=Dropped()
+ a = Dropped()
elif (ct == 5):
- a=Error()
+ a = Error()
else:
raise ParseError("Undefined Command Type")
- a.fromStrReader(self,cs)
+ a.fromStrReader(self, cs)
return a
except IndexError: #is it really necessary? is it not considered in the various fromStrReader ?
#it should, if not consider it a bug
@@ -144,36 +144,36 @@
while (not self.isEnd()):
l.append(self.readCommandSToC)
return l
- def readNym(self,start,cs):
+ def readNym(self, start, cs):
"""Read a Nym length + a nym
Raise ParseError if it takes more characters from the string that previously advertised"""
- nl = ord (self.next(1))
+ nl = ord(self.next(1))
if(self.b + nl - start > cs):
raise ParseError("Bad Formed Command")
return self.next(nl)
- def readNymList(self,start,cs):
+ def readNymList(self, start, cs):
"""Read a list of (Nym length + a nym)
Raise ParseError if it takes more characters from the string that previously advertised"""
try:
l = []
while(self.b < start + cs):
- l.append(self.readNym(start,cs))
+ l.append(self.readNym(start, cs))
return l
except ParseError: #is it really necessary?
raise
- def readMid(self,length,start,cs):
+ def readMid(self, length, start, cs):
"""Read a str of len = length
Raise ParseError if it takes more characters from the string that previously advertised, in that case, doesn't read the StrReader"""
if(self.b + length - start > cs):
raise ParseError("Bad Formed Command")
return self.next(length)
- def readMidList(self,length,start,cs):
+ def readMidList(self, length, start, cs):
"""Read a list of str of len length
Raise ParseError if it takes more characters from the string that previously advertised"""
try:
l = []
while(self.b < start + cs):
- l.append(self.readMid(length,start,cs))
+ l.append(self.readMid(length, start, cs))
return l
except ParseError: #is it really necessary?
raise
@@ -188,7 +188,7 @@
def __init__(self):
"""Build a Header empty object"""
pass
- def fromData(self,nym,seqNo,sig=""):
+ def fromData(self, nym, seqNo, sig = ""):
"""Fill a Header Object from a nym and sequence number and sig if provided
sig : signature
nl : nym length
@@ -269,24 +269,24 @@
def ct(self):
"""return a int for the code of the command"""
return 0
- def fromData(self,l,pw=""):
+ def fromData(self, l, pw = ""):
"""Fill a Create Object from a list of nyms and a proof of work"""
- if(len(l)>255):
+ if(len(l) > 255):
raise BadArgument("Create.fromData : len(l) too big")
for i in range(len(l)):
- if(len(l[i])>255):
+ if(len(l[i]) > 255):
raise BadArgument("Create.fromData : nym too long")
- self.list=l
- self.pw=pw
+ self.list = l
+ self.pw = pw
#if len(pw) isn't too big, we are sure that the command size is small enough because of the size and the number of nyms
- def fromStrReader(self,sr,cs):
+ def fromStrReader(self, sr, cs):
"""Fill a Create command from a StrReader
raise ParseError if it is malformed"""
- start=sr.b
+ start = sr.b
try:
nNym = ord(sr.next(1))
self.pw = "" #what is a proof of work?
- self.list = sr.readNymList(start,cs)
+ self.list = sr.readNymList(start, cs)
except (ParseError,IndexError):
raise ParseError("Bad Formed Command : Create")
#nNym is redundant, we use it to make sure the message is well formed
@@ -294,15 +294,13 @@
raise ParseError("Bad Formed Command : Create")
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=chr(len(self.list))+self.pw
+ s = chr(len(self.list)) + self.pw
for i in range(len(self.list)):
- s=s+chr(len(self.list[i]))+self.list[i]
- if(len(s)>=pow(256,3)): #should not be possible, otherwise, bug
+ s = s + chr(len(self.list[i])) + self.list[i]
+ if len(s) >= pow(256, 3): #should not be possible, otherwise, bug
raise BadArgument("Create.__str__ : command body too long")
- return chr(0)+intToStrBE(len(s),3)+s
+ return chr(0) + intToStrBE(len(s), 3) + s
-
-
class Create2(CommandCToS):
"""Create2 command
self.cr : challenge response (str)"""
@@ -312,23 +310,23 @@
def ct(self):
"""return a int for the code of the command"""
return 1
- def fromData(self,cr):
+ def fromData(self, cr):
"""Fill Create2 from a string containing the response to a challenge"""
- self.cr=cr
+ self.cr = cr
#if cr is small enough we are sure that the command size is small enough
- def fromStrReader(self,sr,cs):
+ def fromStrReader(self, sr, cs):
"""Fill a Create2 command from a StrReader
raise ParseError if it is malformed"""
try:
- self.cr=sr.next(cs)
+ self.cr = sr.next(cs)
except IndexError:
raise ParseError("Bad Formed Command : Create2")
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=self.cr
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = self.cr
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Create2.__str__ : command body too long")
- return chr(1)+intToStrBE(len(s),3)+s
+ return chr(1) + intToStrBE(len(s), 3) + s
class Surb(CommandCToS):
"""Surb command
@@ -339,28 +337,29 @@
def ct(self):
"""return a int for the code of the command"""
return 2
- def fromData(self,s):
+ def fromData(self, s):
"""Fill a Surb Object from a string containing Surbs"""
if(len(s) % surbLength != 0):
raise BadArgument("Surb.fromData : surbs have not a valid size")
- if(len(s)>=pow(256,3)):
+ if len(s) >= pow(256, 3):
raise BadArgument("Surb.fromData : command body too long")
- self.surbs=s
- def fromStrReader(self,sr,cs):
+ self.surbs = s
+
+ def fromStrReader(self, sr, cs):
"""Fill a Surb Object from a StrReader
raise ParseError if it is malformed"""
- if(cs%surbLength != 0):
+ if(cs % surbLength != 0):
raise ParseError("Bad Formed Command : Surb")
try:
- self.surbs=sr.next(cs)
+ self.surbs = sr.next(cs)
except IndexError:
raise ParseError("Bad Formed Command : Surb")
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=self.surbs
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = self.surbs
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Surb.__str__ : command body too long")
- return chr(2)+intToStrBE(len(s),3)+s
+ return chr(2) + intToStrBE(len(s), 3) + s
class Newpk(CommandCToS):
"""Newpk command
@@ -372,36 +371,36 @@
def ct(self):
"""return a int for the code of the command"""
return 3
- def fromData(self,sid,senc):
+ def fromData(self, sid, senc):
"""Fill a Newpk Object from 2 strings containing an ASN-1 encoded RSA key"""
- id_l=len(sid)
- id_l2=len(senc)
- if( ((id_l != 128) and (id_l != 256)) or ( (id_l2 != 128) and (id_l2 != 256))):
+ id_l = len(sid)
+ id_l2 = len(senc)
+ if(((id_l != 128) and (id_l != 256)) or ((id_l2 != 128) and (id_l2 != 256))):
raise BadArgument("Newpk.fromData : sid or senc as not a valid size")
- self.kid=sid
- self.kenc=senc
+ self.kid = sid
+ self.kenc = senc
#command body is small enough
- def fromStrReader(self,sr,cs):
+ def fromStrReader(self, sr, cs):
"""Fill a Newpk Object from a StrReader
raise ParseError if it is malformed"""
try:
- id_l=strToIntBE(sr.next(2))
- if( (id_l != 128) and (id_l != 256)):
+ id_l = strToIntBE(sr.next(2))
+ if((id_l != 128) and (id_l != 256)):
raise ParseError("Bad Formed Command : Newpk")
- id_l2=cs-2-id_l
- if( (id_l2 != 128) and (id_l2 != 256)):
+ id_l2 = cs-2 - id_l
+ if((id_l2 != 128) and (id_l2 != 256)):
raise ParseError("Bad Formed Command : Newpk")
- self.kid=sr.next(id_l)
- self.kenc=sr.next(id_l2)
+ self.kid = sr.next(id_l)
+ self.kenc = sr.next(id_l2)
except IndexError:
raise ParseError("Bad Formed Command : Newpk")
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
#TODO check assuming len(kid)=128 or 256
- s=intToStrBE(len(self.kid),2)+self.kid+self.kenc
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = intToStrBE(len(self.kid), 2) + self.kid + self.kenc
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Newpk.__str__ : command body too long")
- return chr(3)+intToStrBE(len(s),3)+s
+ return chr(3) + intToStrBE(len(s), 3) + s
class Relay(CommandCToS):
"""Relay command
@@ -415,34 +414,34 @@
def ct(self):
"""return a int for the code of the command"""
return 4
- def fromData(self,rt,ri,body):
+ def fromData(self, rt, ri, body):
"""Fill a Relay Object from a 3 strings : routing type(2 octets) routing info and body"""
- if(len(rt) !=2):
+ if len(rt) != 2:
raise BadArgument("Relay.fromData : RT size isn't 2 bytes")
- self.rt=rt
- self.rs=len(ri)
- if(self.rs >= 256*256):
+ self.rt = rt
+ self.rs = len(ri)
+ if self.rs >= 256 * 256:
raise BadArgument("Relay.fromData : RS size isn't 2 bytes")
- self.ri=ri
- self.body=body
- if(4+self.rs+len(body) >= pow(256,3)):
+ self.ri = ri
+ self.body = body
+ if(4 + self.rs + len(body) >= pow(256, 3)):
raise BadArgument("Relay.fromData : Command body too long")
- def fromStrReader(self,sr,cs):
+ def fromStrReader(self, sr, cs):
"""Fill a Relay Object from a StrReader
raise ParseError if it is malformed"""
try:
- self.rs=strToIntBE(sr.next(2))
- self.rt=sr.next(2)
- self.ri=sr.next(self.rs)
- self.body=sr.next(cs-4-self.rs)
+ self.rs = strToIntBE(sr.next(2))
+ self.rt = sr.next(2)
+ self.ri = sr.next(self.rs)
+ self.body = sr.next(cs - 4 - self.rs)
except (BadArgument, IndexError):
raise ParseError("Bad Formed Command : Relay")
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=intToStrBE(self.rs,2)+self.rt+self.ri+self.body
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = intToStrBE(self.rs, 2) + self.rt + self.ri + self.body
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Relay.__str__ : Command Body too long")
- return chr(4)+intToStrBE(len(s),3)+s
+ return chr(4) + intToStrBE(len(s), 3) + s
class Get(CommandCToS):
"""Get command