[Nym3-commit] r100 - trunk
nym3-devel@lists.noreply.org
nym3-devel@lists.noreply.org
Fri, 06 Aug 2004 00:18:42 +0200
Author: laurent
Date: 2004-08-06 00:18:39 +0200 (Fri, 06 Aug 2004)
New Revision: 100
Modified:
trunk/Message.py
Log:
- Better source code formatting.
- Use str, not __str__.
- Removed useless del().
Modified: trunk/Message.py
===================================================================
--- trunk/Message.py 2004-08-05 21:19:40 UTC (rev 99)
+++ trunk/Message.py 2004-08-05 22:18:39 UTC (rev 100)
@@ -7,6 +7,7 @@
"""ParseError : error raised if anything goes wrong during parsing"""
def __init__(self,value):
self.value = value
+
def __str__(self):
return repr(self.value)
@@ -14,6 +15,7 @@
"""BadArgument : error raised if a function is given an irrelevant argument"""
def __init__(self, value):
self.value = value
+
def __str__(self):
return repr(self.value)
@@ -60,9 +62,11 @@
self.s = s
self.a = 0
self.b = 0
+
def isEnd(self):
"""True if we have read the whole string"""
return (b == len(s))
+
def next(self, n):
"""return the next n characters"""
if n < 0:
@@ -72,11 +76,13 @@
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.fromStrReader(self)
return h
+
def readCommandCToS(self):
"""Read next CommandCToS from string"""
try:
@@ -106,6 +112,7 @@
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"""
@@ -113,6 +120,7 @@
while (not self.isEnd()):
l.append(self.readCommandCToS)
return l
+
def readCommandSToC(self):
"""Read next CommandSToC from string"""
try:
@@ -137,13 +145,15 @@
except IndexError: #is it really necessary? is it not considered in the various fromStrReader ?
#it should, if not consider it a bug
raise ParseError("Bad Formed Command")
+
def readCommandSToCList(self):
"""Read a list of CommandSToC from string, until
the string is completely read"""
- l=[]
+ l = []
while (not self.isEnd()):
l.append(self.readCommandSToC)
return l
+
def readNym(self, start, cs):
"""Read a Nym length + a nym
Raise ParseError if it takes more characters from the string that previously advertised"""
@@ -151,6 +161,7 @@
if(self.b + nl - start > cs):
raise ParseError("Bad Formed Command")
return self.next(nl)
+
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"""
@@ -161,12 +172,14 @@
return l
except ParseError: #is it really necessary?
raise
+
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):
"""Read a list of str of len length
Raise ParseError if it takes more characters from the string that previously advertised"""
@@ -188,6 +201,7 @@
def __init__(self):
"""Build a Header empty object"""
pass
+
def fromData(self, nym, seqNo, sig = ""):
"""Fill a Header Object from a nym and sequence number and sig if provided
sig : signature
@@ -206,9 +220,11 @@
self.nym = nym
self.seqNo = seqNo
#self.length = sigLength + 1 + self.nl + seqNoLength
+
def __str__(self):
"""Give the string of the header that would be sent in a control message"""
return self.sig + chr(self.nl) + self.nym + self.seqNo
+
def fromStrReader(self,sr):
"""Fill a Header from a StrReader
"""
@@ -266,9 +282,11 @@
def __init__(self):
"""Build a Create empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 0
+
def fromData(self, l, pw = ""):
"""Fill a Create Object from a list of nyms and a proof of work"""
if(len(l) > 255):
@@ -279,6 +297,7 @@
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):
"""Fill a Create command from a StrReader
raise ParseError if it is malformed"""
@@ -292,6 +311,7 @@
#nNym is redundant, we use it to make sure the message is well formed
if(nNym != len(self.list)):
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
@@ -307,13 +327,16 @@
def __init__(self):
"""Build a Create2 empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 1
+
def fromData(self, cr):
"""Fill Create2 from a string containing the response to a challenge"""
self.cr = cr
#if cr is small enough we are sure that the command size is small enough
+
def fromStrReader(self, sr, cs):
"""Fill a Create2 command from a StrReader
raise ParseError if it is malformed"""
@@ -321,6 +344,7 @@
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
@@ -334,9 +358,11 @@
def __init__(self):
"""Build a Surb empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 2
+
def fromData(self, s):
"""Fill a Surb Object from a string containing Surbs"""
if(len(s) % surbLength != 0):
@@ -354,6 +380,7 @@
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
@@ -368,9 +395,11 @@
def __init__(self):
"""Build a Newpk empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 3
+
def fromData(self, sid, senc):
"""Fill a Newpk Object from 2 strings containing an ASN-1 encoded RSA key"""
id_l = len(sid)
@@ -380,6 +409,7 @@
self.kid = sid
self.kenc = senc
#command body is small enough
+
def fromStrReader(self, sr, cs):
"""Fill a Newpk Object from a StrReader
raise ParseError if it is malformed"""
@@ -394,6 +424,7 @@
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
@@ -411,9 +442,11 @@
def __init__(self):
"""Build a Relay empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 4
+
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:
@@ -426,6 +459,7 @@
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):
"""Fill a Relay Object from a StrReader
raise ParseError if it is malformed"""
@@ -436,6 +470,7 @@
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
@@ -449,33 +484,37 @@
def __init__(self):
"""Build a Get empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 5
- def fromData(self,li):
+
+ def fromData(self, li):
"""Fill a Get Object from a list of message id (=string of len 20)"""
#check that each element of the list has 20 bytes
- if( not isMidList(li,midLength)):
+ if not isMidList(li, midLength):
raise BadArgument("Get.fromData : li is not a list of message id")
- self.l=li
- if(midLength*len(self.l)>=256*256*256):
+ self.l = li
+ if midLength * len(self.l) >= 256 ** 3:
raise BadArgument("Get.fromData : Command body too long")
- def fromStrReader(self,sr,cs):
+
+ def fromStrReader(self, sr, cs):
"""Fill a Get Object from a StrReader
raise ParseError if it is malformed"""
- start=sr.b
+ start = sr.b
try:
- self.l=sr.readMidList(midLength,start,cs)
- except (ParseError,IndexError):
+ self.l = sr.readMidList(midLength, start, cs)
+ except (ParseError, IndexError):
raise ParseError("Bad Formed Command : Get")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=""
+ s = ""
for i in range(len(self.l)):
- s=s+self.l[i]
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = s + self.l[i]
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Get.__str__ : command body too long")
- return chr(5)+intToStrBE(len(s),3)+s
+ return chr(5) + intToStrBE(len(s), 3) + s
class Summarize(CommandCToS):
"""Summarize command
@@ -484,34 +523,38 @@
def __init__(self):
"""Build a Summarize empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 6
- def fromData(self,n,m):
+
+ def fromData(self, n, m):
"""Fill a Summarize Object from the max number of synopsis to retrieve (int) and the message id of a message older than the messages we want the synopsis from (string of len 20)
the int is considered modulo 256^2"""
- n_mod=n % (256*256)
- self.num=n_mod
- if(len(m)!=midLength): # or m is not a str
+ n_mod = n % (256 * 256)
+ self.num = n_mod
+ if len(m) != midLength: # or m is not a str
raise BadArgument(" Summarize.fromData : m is not a valid message ID")
- self.after=m
+ self.after = m
#command body is small enough
- def fromStrReader(self,sr,cs):
+
+ def fromStrReader(self, sr, cs):
"""Fill a Summarize Object from a StrReader
raise ParseError if it is malformed"""
- if(cs != 2+midLength):
+ if cs != 2 + midLength:
raise ParseError("Bad Formed Command : Summarize")
try:
- self.num=strToIntBE(sr.next(2))
- self.after=sr.next(midLength)
+ self.num = strToIntBE(sr.next(2))
+ self.after = sr.next(midLength)
except IndexError:
raise ParseError("Bad Formed Command : Summarize")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=intToStrBE(self.num,2)+self.after
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = intToStrBE(self.num, 2) + self.after
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Summarize.__str__ : command body too long")
- return chr(6)+intToStrBE(len(s),3)+s
+ return chr(6) + intToStrBE(len(s), 3) + s
class Delete(CommandCToS):
"""Delete command
@@ -519,33 +562,37 @@
def __init__(self):
"""Build a Delete empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 7
- def fromData(self,li):
+
+ def fromData(self, li):
"""Fill a Delete Object from a list of message ID (string of length midLength"""
#check that each element of the list has 20 bytes
- if( not isMidList(li,midLength)):
- raise BadArgument("Delete.fromData : li is not a list of message id")
- if(midLength*len(li)>=pow(256,3)):
- raise BadArgument("Delete.fromData : Command body too long")
- self.l=li
- def fromStrReader(self,sr,cs):
+ if not isMidList(li, midLength):
+ raise BadArgument("Delete.fromData : li is not a list of message id")
+ if midLength * len(li) >= pow(256, 3):
+ raise BadArgument("Delete.fromData : Command body too long")
+ self.l = li
+
+ def fromStrReader(self, sr, cs):
"""Fill a Delete Object from a StrReader
raise ParseError if it is malformed"""
- start=sr.b
+ start = sr.b
try:
- self.l=sr.readMidList(midLength,start,cs)
- except (ParseError,IndexError):
- raise ParseError("Bad Formed Command : Delete")
+ self.l = sr.readMidList(midLength, start, cs)
+ except (ParseError, IndexError):
+ raise ParseError("Bad Formed Command : Delete")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=""
+ s = ""
for i in range(len(self.l)):
- s=s+self.l[i]
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = s + self.l[i]
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Delete.__str__ : command body too long")
- return chr(7)+intToStrBE(len(s),3)+s
+ return chr(7) + intToStrBE(len(s), 3) + s
class Policy(CommandCToS):
"""Policy command
@@ -554,88 +601,92 @@
def __init__(self):
"""Build a Policy empty object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 8
- def fromData(self,opt,val):
+
+ def fromData(self, opt, val):
"""Fill a Policy Object from 1 str : the option name
and its value (either a str or a int depending on the option
"""
- if(len(opt)>255):
+ if len(opt) > 255:
raise BadArgument("Policy.fromData : Option name length too big")
- if(opt in Common.userPolicy):
- self.opt=opt
- if(opt == "SendMsgAfter"):
- if( val > pow(256,2) ):
+ if opt in Common.userPolicy:
+ self.opt = opt
+ if opt == "SendMsgAfter":
+ if val > pow(256, 2):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
+ self.val = val
elif (opt == "SendSummaryAfter"):
- if( val > pow(256,2) ):
+ if val > pow(256, 2):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
+ self.val = val
elif (opt == "EncryptSummaryAfter"):
- if( val > pow(256,2) ):
+ if val > pow(256, 2):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
+ self.val = val
elif (opt == "MaxSURBsPerDay"):
- if( val > pow(256,2) ):
+ if val > pow(256, 2):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
+ self.val = val
elif (opt == "HoldSURBs"):
- if( val > pow(256,2) ):
+ if val > pow(256, 2):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
+ self.val = val
elif (opt == "HoldUntilAck"):
- if(val in [ 'never', 'quota', 'always' ]):
- self.val=val
+ if val in [ 'never', 'quota', 'always' ]:
+ self.val = val
elif (opt == "ShutdownPhrase"):
- if( len(val) + 1 + len(opt) > pow(256,3) ):
+ if len(val) + 1 + len(opt) > pow(256, 3):
raise BadArgument("Policy.fromData : Option value too long")
- self.val=val
- def fromStrReader(self,sr,cs):
+ self.val = val
+
+ def fromStrReader(self, sr, cs):
"""Fill a Policy Object from a StrReader
raise ParseError if it is malformed"""
try:
- le=ord(sr.next(1))
- self.opt=sr.next(le)
- inter=sr.next(cs-1-le)
+ le = ord(sr.next(1))
+ self.opt = sr.next(le)
+ inter = sr.next(cs - 1 - le)
if(self.opt == "SendMsgAfter"):
- if(len(inter)!=2):
+ if len(inter) != 2:
raise ParseError("Bad Formed Command : Policy")
- self.val=strToIntBE(inter)
+ self.val = strToIntBE(inter)
elif (self.opt == "SendSummaryAfter"):
- if(len(inter)!=2):
+ if len(inter) != 2:
raise ParseError("Bad Formed Command : Policy")
- self.val=strToIntBE(inter)
+ self.val = strToIntBE(inter)
elif (self.opt == "EncryptSummaryAfter"):
- if(len(inter)!=2):
+ if len(inter) != 2:
raise ParseError("Bad Formed Command : Policy")
- self.val=strToIntBE(inter)
+ self.val = strToIntBE(inter)
elif (self.opt == "MaxSURBsPerDay"):
- if(len(inter)!=2):
+ if len(inter) != 2:
raise ParseError("Bad Formed Command : Policy")
- self.val=strToIntBE(inter)
+ self.val = strToIntBE(inter)
elif (self.opt == "HoldSURBs"):
- if(len(inter)!=2):
+ if len(inter) != 2:
raise ParseError("Bad Formed Command : Policy")
- self.val=strToIntBE(inter)
+ self.val = strToIntBE(inter)
elif (self.opt == "HoldUntilAck"):
- if(inter in [ 'never', 'quota', 'always' ]):
- self.val=inter
+ if inter in [ 'never', 'quota', 'always']:
+ self.val = inter
elif (self.opt == "ShutdownPhrase"):
- self.val=inter
- except (IndexError,BadArgument):
+ self.val = inter
+ except (IndexError, BadArgument):
raise ParseError("Bad Formed Command : Policy")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=chr(len(self.opt))+self.opt
- if(self.opt in Common.userPolicy[:5]):
- s = s + intToStrBE(self.val,2)
- if(self.opt in Common.userPolicy[5:]):
+ s = chr(len(self.opt)) + self.opt
+ if self.opt in Common.userPolicy[:5]:
+ s = s + intToStrBE(self.val, 2)
+ if self.opt in Common.userPolicy[5:]:
s = s + self.val
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Policy.__str__ : command body too long")
- return chr(8)+intToStrBE(len(s),3)+s
+ return chr(8) + intToStrBE(len(s), 3) + s
class CommandSToC:
@@ -650,32 +701,36 @@
def __init__(self):
"""Build a empty Created object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 0
- def fromData(self,nym,ch):
+
+ def fromData(self, nym, ch):
"""Fill a Created Object from a nym(str) and a challenge(str) """
- if(len(nym)>255):
+ if len(nym) > 255:
raise BadArgument("Created.fromData : nym too long")
- if(1+len(nym)+len(ch) >= pow(256,3)):
+ if 1 + len(nym) + len(ch) >= pow(256, 3):
raise BadArgument("Created.fromData : challenge too long")
- self.nym=nym
- self.challenge=ch
- def fromStrReader(self,sr,cs):
+ self.nym = nym
+ self.challenge = ch
+
+ def fromStrReader(self, sr, cs):
"""Fill a Created Object from a StrReader
raise ParseError if it is malformed"""
- start=sr.b
+ start = sr.b
try:
- self.nym=sr.readNym(start,cs)
- self.challenge=sr.next(cs+start-sr.b)
- except (IndexError,ParseError,BadArgument):
+ self.nym = sr.readNym(start, cs)
+ self.challenge = sr.next(cs + start - sr.b)
+ except (IndexError, ParseError, BadArgument):
raise ParseError("Bad Formed Command : Created")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=chr(len(self.nym))+self.nym+self.challenge
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = chr(len(self.nym)) + self.nym + self.challenge
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Created.__str__ : command body too long")
- return chr(0)+intToStrBE(len(s),3)+s
+ return chr(0) + intToStrBE(len(s), 3) + s
class Status(CommandSToC):
"""command Status
@@ -687,49 +742,56 @@
def __init__(self):
"""Build a empty Status object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 1
- def fromData(self,nM,nS,q,u,l):
+
+ def fromData(self, nM, nS, q, u, l):
"""Fill a Status Object from a number of message(int), a number of surbs(int), a quota(int), a used(int) and a sequence of seqno(list of (str of size seqNoLength)"""
#TODO replace the error by the taking of the maximal value?
- if(nM>=pow(256,4)):
+ if nM >= pow(256, 4):
raise BadArgument("Status.fromData : number of waiting e-mail too large")
- if(nM>=pow(256,2)):
+ if nM >= pow(256, 2):
raise BadArgument("Status.fromData : number of available surbs too large")
- if(q>=pow(256,4)):
+ if q >= pow(256, 4):
raise BadArgument("Status.fromData : quota too large")
- if(u>=pow(256,4)):
+ if u >= pow(256, 4):
raise BadArgument("Status.fromData : used space too large")
- if(not isMidList(l,seqNoLength)):
+ if not isMidList(l, seqNoLength):
raise BadArgument("Status.fromData : l is not a list of Sequence Number")
- if(14+seqNoLength*len(l)>=pow(256,3)):
+ if 14 + seqNoLength * len(l) >= pow(256, 3):
+ # 14 ? Why not 17 or 42 ? avoid numeric litterals
+ # where possible FIXME
raise BadArgument("Status.fromData : l is too long")
- self.nMsg=nM
- self.nSurb=nS
- self.quota=q
- self.used=u
- self.acks=l
- def fromStrReader(self,sr,cs):
+ self.nMsg = nM
+ self.nSurb = nS
+ self.quota = q
+ self.used = u
+ self.acks = l
+
+ def fromStrReader(self, sr, cs):
"""Fill a Status Object from a StrReader
raise ParseError if it is malformed"""
try:
- start=sr.b
- self.nMsg=strToIntBE(sr.next(4))
- self.nSurb=strToIntBE(sr.next(2))
- self.quota=strToIntBE(sr.next(4))
- self.used=strToIntBE(sr.next(4))
- self.acks=sr.readMidList(seqNoLength,start,cs)
- except (IndexError,ParseError):
+ start = sr.b
+ self.nMsg = strToIntBE(sr.next(4))
+ self.nSurb = strToIntBE(sr.next(2))
+ self.quota = strToIntBE(sr.next(4))
+ self.used = strToIntBE(sr.next(4))
+ self.acks = sr.readMidList(seqNoLength, start, cs)
+ except (IndexError, ParseError):
raise ParseError("Bad Formed Command : Status")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=intToStrBE(self.nMsg,4)+intToStrBE(self.nSurb,2)+intToStrBE(self.quota,4)+intToStrBE(self.used,4)
+ s = intToStrBE(self.nMsg, 4) + intToStrBE(self.nSurb, 2) +
+ intToStrBE(self.quota, 4) + intToStrBE(self.used, 4)
for i in range(len(self.acks)):
- s=s+self.acks[i]
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = s + self.acks[i]
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Status.__str__ : command body too long")
- return chr(1)+intToStrBE(len(s),3)+s
+ return chr(1) + intToStrBE(len(s), 3) + s
class Summary(CommandSToC):
"""command Summary
@@ -739,31 +801,35 @@
def __init__(self):
"""Build a empty Summary object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 2
- def fromData(self,bf,ess):
+
+ def fromData(self, bf, ess):
"""Fill a Object from bit field (str of len 2) and an encrypted set of synopses(str)"""
- if(len(bf)!=2):
+ if len(bf) != 2:
raise BadArgument("Summary.fromData : bit field has not the good size, and size DOES matter")
- if(2+len(ess)>=pow(256,3)):
+ if 2 + len(ess) >= pow(256, 3):
raise BadArgument("Summary.fromData : Encrypted set of synopses too long")
- self.bf=bf
- self.synSet=ess
- def fromStrReader(self,sr,cs):
+ self.bf = bf
+ self.synSet = ess
+
+ def fromStrReader(self, sr, cs):
"""Fill a Object from a StrReader
raise ParseError if it is malformed"""
try:
- self.bf=sr.next(2)
- self.synSet=sr.next(cs-2)
+ self.bf = sr.next(2)
+ self.synSet = sr.next(cs - 2)
except IndexError:
ParseError("Bad Formed Command : Summary")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=self.bf+self.synSet
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = self.bf + self.synSet
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Summary.__str__ : command body too long")
- return chr(2)+intToStrBE(len(s),3)+s
+ return chr(2) + intToStrBE(len(s), 3) + s
class Msg(CommandSToC):
"""command Msg
@@ -773,32 +839,36 @@
def __init__(self):
"""Build a empty Msg object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 3
- def fromData(self,mid,msg):
+
+ def fromData(self, mid, msg):
"""Fill a Msg Object from a Mid (str of length midLength) and an encrypted msg (str)"""
- if(len(mid) != midLength):
+ if len(mid) != midLength:
raise BadArgument("Msg.fromData : mid has not the good length")
- if(midLength + len(msg) >= pow(256,3)):
+ if midLength + len(msg) >= pow(256, 3):
raise BadArgument("Msg.fromData : msg too long")
- self.mid=mid
- self.msg=msg
- def fromStrReader(self,sr,cs):
+ self.mid = mid
+ self.msg = msg
+
+ def fromStrReader(self, sr, cs):
"""Fill a Msg Object from a StrReader
raise ParseError if it is malformed"""
try:
- start=sr.b
- self.mid=sr.readMid(midLength,start,cs)
- self.msg=sr.next(cs-midLength)
- except (IndexError,ParseError):
+ start = sr.b
+ self.mid = sr.readMid(midLength, start, cs)
+ self.msg = sr.next(cs - midLength)
+ except (IndexError, ParseError):
raise ParseError("Bad Formed Command : Msg")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=self.mid+self.msg
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = self.mid + self.msg
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Msg.__str__ : command body too long")
- return chr(3)+intToStrBE(len(s),3)+s
+ return chr(3) + intToStrBE(len(s), 3) + s
class Dropped(CommandSToC):
"""command Dropped
@@ -807,32 +877,36 @@
def __init__(self):
"""Build a empty Dropped object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 4
- def fromData(self,l):
+
+ def fromData(self, l):
"""Fill a Dropped Object from a list of mid"""
- if( not isMidList(l,midLength)):
+ if( not isMidList(l, midLength)):
raise BadArgument("Dropped.fromData : l is not a list of message id")
- if(midLength*len(l)>=pow(256,3)):
+ if midLength * len(l) >= pow(256, 3):
raise BadArgument("Dropped.fromData : Command body too long")
- self.list=l
- def fromStrReader(self,sr,cs):
+ self.list = l
+
+ def fromStrReader(self, sr, cs):
"""Fill a Dropped Object from a StrReader
raise ParseError if it is malformed"""
- start=sr.b
+ start = sr.b
try:
- self.list=sr.readMidList(midLength,start,cs)
- except (ParseError,IndexError):
+ self.list = sr.readMidList(midLength, start, cs)
+ except (ParseError, IndexError):
raise ParseError("Bad Formed Command : Dropped")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=""
+ s = ""
for i in range(len(self.list)):
- s=s+self.list[i]
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = s + self.list[i]
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("DroppedCreated.__str__ : command body too long")
- return chr(4)+intToStrBE(len(s),3)+s
+ return chr(4) + intToStrBE(len(s), 3) + s
class Error(CommandSToC):
"""command Error
@@ -842,220 +916,178 @@
def __init__(self):
"""Build a empty Error object"""
pass
+
def ct(self):
"""return a int for the code of the command"""
return 5
- def fromData(self,seqNo,error):
+
+ def fromData(self, seqNo, error):
"""Fill a Object from a seqno (str of len seqNoLength) and an error message(str)"""
- if(len(seqNo) != seqNoLength):
+ if len(seqNo) != seqNoLength:
raise BadArgument("Error.fromData : seqNo has not the good length")
- if(seqNoLength + len(error) >= pow(256,3)):
+ if seqNoLength + len(error) >= pow(256, 3):
raise BadArgument("Error.fromData : msg too long")
- self.nonce=seqNo
- self.error=error
- def fromStrReader(self,sr,cs):
+ self.nonce = seqNo
+ self.error = error
+
+ def fromStrReader(self, sr, cs):
"""Fill a Object from a StrReader
raise ParseError if it is malformed"""
try:
- start=sr.b
- self.mid=sr.readMid(seqNoLength,start,cs)
- self.msg=sr.next(cs-seqNoLength)
- except (IndexError,ParseError):
+ start = sr.b
+ self.mid = sr.readMid(seqNoLength, start, cs)
+ self.msg = sr.next(cs - seqNoLength)
+ except (IndexError, ParseError):
raise ParseError("Bad Formed Command : Error")
+
def __str__(self):
"""Give the string of the Command that would be sent in a control message"""
- s=self.nonce+self.error
- if(len(s)>=pow(256,3)): #should not be possible, otherwise bug
+ s = self.nonce + self.error
+ if len(s) >= pow(256, 3): #should not be possible, otherwise bug
raise BadArgument("Error.__str__ : command body too long")
- return chr(5)+intToStrBE(len(s),3)+s
+ return chr(5) + intToStrBE(len(s), 3) + s
def buildMessage(comList):
"""Turn a list of Command into a sendable message
"""
s = ""
for c in comList:
- s = s + c.__str__()
+ s = s + str(c)
return s
-
if (__name__ == '__main__'):
import Mail
- l=[]
+ l = []
for i in range(3):
- l.append(Mail.genMid(length=20))
- H= Header()
- H.fromData("JR","01234567890123456789")
+ l.append(Mail.genMid(length = 20))
+ H = Header()
+ H.fromData("JR", "01234567890123456789")
print H
del H
print "test Create"
- C=Create()
- C.fromData(["Lolo_","Marsux","Azatoth_","Keeh"])
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Create()
+ C.fromData(["Lolo_", "Marsux", "Azatoth_", "Keeh"])
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Create2"
- C=Create2()
+ C = Create2()
C.fromData("azertyuiop42")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Surb"
- C=Surb()
- C.fromData("-"*2104)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Surb()
+ C.fromData("-" * 2104)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Newpk"
- C=Newpk()
- C.fromData("1"*256,"2"*128)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Newpk()
+ C.fromData("1" * 256, "2" * 128)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Relay"
- C=Relay()
- C.fromData("ab","too lazy to make real ones here","this is the body of the email. It sholud contain some kind of header, but for testing the parser, who cares?")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Relay()
+ C.fromData("ab", "too lazy to make real ones here", "this is the body of the email. It sholud contain some kind of header, but for testing the parser, who cares?")
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Get"
- C=Get()
+ C = Get()
C.fromData(l)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Summarize"
- C=Summarize()
- C.fromData(10,l[0])
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Summarize()
+ C.fromData(10, l[0])
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Delete"
- C=Delete()
+ C = Delete()
C.fromData(l)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Policy"
- C=Policy()
- C.fromData("HoldUntilAck","never")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandCToS()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Policy()
+ C.fromData("HoldUntilAck", "never")
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandCToS()
+ s2 = str(D)
+ print (s1 == s2)
print "test Created"
- C=Created()
- C.fromData("Marsux","what is the answer to universe life and everything")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Created()
+ C.fromData("Marsux", "what is the answer to universe life and everything")
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)
print "test Status"
- C=Status()
- C.fromData(5,10,424242,212121,l)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Status()
+ C.fromData(5, 10, 424242, 212121, l)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)
print "test Summary"
- C=Summary()
+ C = Summary()
C.fromData("ab","this should be an encrypted set of synopses")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)
print "test Msg"
- C=Msg()
- C.fromData(l[0],"Say something stupid, like, I am wearing female's underwear")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Msg()
+ C.fromData(l[0], "Say something stupid, like, I am wearing female's underwear")
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)
print "test Dropped"
- C=Dropped()
+ C = Dropped()
C.fromData(l)
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)
print "test Error"
- C=Msg()
- C.fromData(l[0],"I am being repressed")
- s1=C.__str__()
- S=StrReader(s1)
- D=S.readCommandSToC()
- s2=D.__str__()
- print (s1==s2)
- del(C)
- del(D)
- del(S)
+ C = Msg()
+ C.fromData(l[0], "Help! Help! I am being repressed!")
+ s1 = str(C)
+ S = StrReader(s1)
+ D = S.readCommandSToC()
+ s2 = str(D)
+ print (s1 == s2)