[Nym3-commit] r165 - in trunk/nym3: . Client

laurent at conuropsis.org laurent at conuropsis.org
Thu Mar 31 19:17:44 CEST 2005


Author: laurent
Date: 2005-03-31 19:17:43 +0200 (Thu, 31 Mar 2005)
New Revision: 165

Modified:
   trunk/nym3/Client/Main.py
   trunk/nym3/Message.py
Log:
Get rid of some numeric litterals.


Modified: trunk/nym3/Client/Main.py
===================================================================
--- trunk/nym3/Client/Main.py	2005-03-30 15:40:43 UTC (rev 164)
+++ trunk/nym3/Client/Main.py	2005-03-31 17:17:43 UTC (rev 165)
@@ -41,7 +41,8 @@
     sr = Message.StrReader(msg)
     comList = sr.readCommandCToSList()
     for com in comList:
-        if (com.ct() == 0):
+        if (com.ct() == 0): # TODO : you really don't want to have
+			    # hard-coded values here.
             pass
         elif (com.ct() == 1):
             pass

Modified: trunk/nym3/Message.py
===================================================================
--- trunk/nym3/Message.py	2005-03-30 15:40:43 UTC (rev 164)
+++ trunk/nym3/Message.py	2005-03-31 17:17:43 UTC (rev 165)
@@ -1,3 +1,5 @@
+#!/usr/bin/python
+# -*- coding: iso-8859-1 -*-
 # $Id$
 # 
 # Copyright (c) 2004,2005 Jean-René Reinhard <jr at komite.net>
@@ -28,6 +30,15 @@
 import types
 import nym3.Common as Common
 
+
+# Every command type has a numerical code as described in nym-spec.
+CToSCODE = { 'Create': 0, 'Create2': 1, 'Surb': 2, 'Newpk': 3,
+	     'Relay': 4, 'Get': 5, 'Summarize': 6, 'Delete': 7,
+	     'Policy': 8 }
+# nym-spec says MSGS and later MSG. use Msg for consistency.
+SToCCODE = { 'Created': 0, 'Status': 1, 'Summary': 2, 'Msg': 3,
+	     'Dropped': 4, 'Error': 5 }
+
 class ParseError(Exception):
 	"""ParseError : error raised if anything goes wrong during parsing"""
 	def __init__(self, value):
@@ -127,23 +138,23 @@
 		try:
 			ct = ord(self.next(1))
 			cs = strToIntBE(self.next(3))
-			if (ct == 0):
+			if (ct == CToSCODE['Create']):
 				a = Create()
-			elif (ct == 1):
+			elif (ct == CToSCODE['Create2']):
 				a = Create2()
-			elif (ct == 2):
+			elif (ct == CToSCODE['Surb']):
 				a = Surb()
-			elif (ct == 3):
+			elif (ct == CToSCODE['Newpk']):
 				a = Newpk()
-			elif (ct == 4):
+			elif (ct == CToSCODE['Relay']):
 				a = Relay()
-			elif (ct == 5):
+			elif (ct == CToSCODE['Get']):
 				a = Get()
-			elif (ct == 6):
+			elif (ct == CToSCODE['Summarize']):
 				a = Summarize()
-			elif (ct == 7):
+			elif (ct == CToSCODE['Delete']):
 				a = Delete()
-			elif (ct == 8):
+			elif (ct == CToSCODE['Policy']):
 				a = Policy()
 			else:
 				raise ParseError("Undefined Command Type")
@@ -165,17 +176,17 @@
 		try:
 			ct = ord(self.next(1))
 			cs = strToIntBE(self.next(3))
-			if (ct == 0):
+			if (ct == SToCCODE['Created']):
 				a = Created()
-			elif (ct == 1):
+			elif (ct == SToCCODE['Status']):
 				a = Status()
-			elif (ct == 2):
+			elif (ct == SToCCODE['Summary']):
 				a = Summary()
-			elif (ct == 3):
+			elif (ct == SToCCODE['Msg']):
 				a = Msg()
-			elif (ct == 4):
+			elif (ct == SToCCODE['Dropped']):
 				a = Dropped()
-			elif (ct == 5):
+			elif (ct == SToCCODE['Error']):
 				a = Error()
 			else:
 				raise ParseError("Undefined Command Type")
@@ -324,7 +335,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 0
+		return CToSCODE['Create']
 
 	def fromData(self, l, pw = ""):
 		"""Fill a Create Object from a list of nyms and a proof of work"""
@@ -357,7 +368,7 @@
 		for i in range(len(self.list)):
 			s = s + chr(len(self.list[i])) + self.list[i]
 		assert len(s) < pow(256, 3)
-		return chr(0) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 		
 class Create2(CommandCToS):
 	"""Create2 command
@@ -368,7 +379,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 1
+		return CToSCODE['Create2']
 
 	def fromData(self, cr):
 		"""Fill Create2 from a string containing the response to a challenge"""
@@ -387,7 +398,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = self.cr
 		assert len(s) < pow(256, 3)
-		return chr(1) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Surb(CommandCToS):
 	"""Surb command
@@ -398,7 +409,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 2
+		return CToSCODE['Surb']
 
 	def fromData(self, s):
 		"""Fill a Surb Object from a string containing Surbs"""
@@ -422,7 +433,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = self.surbs
 		assert len(s) < pow(256, 3)
-		return chr(2) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Newpk(CommandCToS):
 	"""Newpk command
@@ -437,7 +448,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 3
+		return CToSCODE['Newpk']
 
 	def fromData(self, sid, senc):
 		"""Fill a Newpk Object from 2 modulus (int)"""
@@ -476,7 +487,7 @@
 		s=intToStrBE(128 * self.idf, 2) + intToStrBE(self.kid, 128*self.idf) + intToStrBE(self.kenc, 128*self.encf)
 		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(self.ct()) + intToStrBE(len(s),3) + s
 	
 class Relay(CommandCToS):
 	"""Relay command
@@ -490,7 +501,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 4
+		return CToSCODE['Relay']
 
 	def fromData(self, rt, ri, body):
 		"""Fill a Relay Object from a 3 strings : routing type(2 octets) routing info and body"""
@@ -520,7 +531,7 @@
 		"""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
 		assert len(s) < pow(256, 3)
-		return chr(4) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Get(CommandCToS):
 	"""Get command
@@ -531,7 +542,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 5
+		return CToSCODE['Get']
 
 	def fromData(self, li):
 		"""Fill a Get Object from a list of message id (=string of len 20)"""
@@ -557,7 +568,7 @@
 		for i in range(len(self.l)):
 			s = s + self.l[i]
 		assert len(s) < pow(256, 3)
-		return chr(5) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Summarize(CommandCToS):
 	"""Summarize command
@@ -569,7 +580,7 @@
 		
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 6
+		return CToSCODE['Summarize']
 
 	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)
@@ -596,7 +607,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = intToStrBE(self.num, 2) + self.after
 		assert len(s) < pow(256, 3)
-		return chr(6) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Delete(CommandCToS):
 	"""Delete command
@@ -607,7 +618,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 7
+		return CToSCODE['Delete']
 
 	def fromData(self, li):
 		"""Fill a Delete Object from a list of message ID (string of length midLength"""
@@ -633,7 +644,7 @@
 		for i in range(len(self.l)):
 			s = s + self.l[i]
 		assert len(s) < pow(256, 3)
-		return chr(7) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 	
 class Policy(CommandCToS):
 	"""Policy command
@@ -645,7 +656,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 8
+		return CToSCODE['Policy']
 
 	def fromData(self, opt, val):
 		"""Fill a Policy Object from 1 str : the option name
@@ -726,7 +737,7 @@
 		if self.opt in Common.userPolicy[5:]:
 			s = s + self.val
 		assert len(s) < pow(256, 3)
-		return chr(8) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 		
 	
 class CommandSToC:
@@ -744,7 +755,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 0
+		return SToCCODE['Created']
 
 	def fromData(self, nym, ch):
 		"""Fill a Created Object from a nym(str) and a challenge(str)"""
@@ -769,7 +780,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = chr(len(self.nym)) + self.nym + self.challenge
 		assert len(s) < pow(256, 3)
-		return chr(0) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 class Status(CommandSToC):
 	"""command Status
@@ -784,7 +795,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 1
+		return SToCCODE['Status']
 
 	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)"""
@@ -829,7 +840,7 @@
 		for i in range(len(self.acks)):
 			s = s + self.acks[i]
 		assert len(s) < pow(256, 3)
-		return chr(1) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 class Summary(CommandSToC):
 	"""command Summary
@@ -842,7 +853,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 2
+		return SToCCODE['Summary']
 
 	def fromData(self, bf, ess):
 		"""Fill a Object from bit field (str of len 2) and an encrypted set of synopses(str)"""
@@ -866,7 +877,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = self.bf + self.synSet
 		assert len(s) < pow(256, 3)
-		return chr(2) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 class Msg(CommandSToC):
 	"""command Msg
@@ -879,7 +890,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 3
+		return SToCCODE['Msg']
 
 	def fromData(self, mid, msg):
 		"""Fill a Msg Object from a Mid (str of length midLength) and an encrypted msg (str)"""
@@ -904,7 +915,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = self.mid + self.msg
 		assert len(s) < pow(256, 3)
-		return chr(3) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 class Dropped(CommandSToC):
 	"""command Dropped
@@ -916,7 +927,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 4
+		return SToCCODE['Dropped']
 
 	def fromData(self, l):
 		"""Fill a Dropped Object from a list of mid"""
@@ -941,7 +952,7 @@
 		for i in range(len(self.list)):
 			s = s + self.list[i]
 		assert len(s) < pow(256, 3)
-		return chr(4) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 class Error(CommandSToC):
 	"""command Error
@@ -954,7 +965,7 @@
 
 	def ct(self):
 		"""return a int for the code of the command"""
-		return 5
+		return SToCCODE['Error']
 
 	def fromData(self, seqNo, error):
 		"""Fill a Object from a seqno (str of len seqNoLength) and an error message(str)"""
@@ -979,7 +990,7 @@
 		"""Give the string of the Command that would be sent in a control message"""
 		s = self.nonce + self.error
 		assert len(s) < pow(256, 3)
-		return chr(5) + intToStrBE(len(s), 3) + s
+		return chr(self.ct()) + intToStrBE(len(s), 3) + s
 
 def buildMessage(comList):
 	"""Turn a list of Command into a sendable message



More information about the Nym3-commit mailing list