[Nym3-commit] r170 - trunk/nym3/Client

laurent at conuropsis.org laurent at conuropsis.org
Sat Apr 2 23:39:41 CEST 2005


Author: laurent
Date: 2005-04-02 23:39:38 +0200 (Sat, 02 Apr 2005)
New Revision: 170

Modified:
   trunk/nym3/Client/Main.py
   trunk/nym3/Client/User.py
Log:
Clean User.py.


Modified: trunk/nym3/Client/Main.py
===================================================================
--- trunk/nym3/Client/Main.py	2005-04-02 14:22:03 UTC (rev 169)
+++ trunk/nym3/Client/Main.py	2005-04-02 21:39:38 UTC (rev 170)
@@ -42,8 +42,7 @@
     comList = sr.readCommandCToSList()
     for com in comList:
         if (com.ct() == 0): # TODO : you really don't want to have
-			    # hard-coded values here.
-            pass
+	    pass	    # hard-coded values here.
         elif (com.ct() == 1):
             pass
         elif (com.ct() == 2):
@@ -55,7 +54,7 @@
         elif (com.ct() == 5):
             pass
 
-def setUpAccount(serverName):
+def setupAccount(serverName):
     """Tries to setup a new account on the given server"""
     try:
         nymUser = User.User(1)

Modified: trunk/nym3/Client/User.py
===================================================================
--- trunk/nym3/Client/User.py	2005-04-02 14:22:03 UTC (rev 169)
+++ trunk/nym3/Client/User.py	2005-04-02 21:39:38 UTC (rev 170)
@@ -1,5 +1,5 @@
 # $Id$
-# 
+# -*- coding: iso-8859-1 -*-
 # Copyright (c) 2004,2005 Jean-René Reinhard <jr at komite.net>
 # and Laurent Fousse <laurent at komite.net>.
 #
@@ -34,79 +34,80 @@
 import nym3.Message as Message
 import mixminion.ClientAPI as mapi
 
-class NoSuchUser(Exception): pass
+class NoSuchAccount(Exception): pass
 """Exception thrown when a user identified by her nym can't be found"""
 
-class AlreadySuchUser(Exception): pass
+class AlreadySuchAccount(Exception): pass
 """Exception thrown when a user identified by her nym already exists"""
 
-#bullshit we'll gonna have to take a look at
-def generateIdTag():
-    while True:
-        tag = Mail.genMid(8)
-        rtag = Mail.mid2filename(tag)
-        path = Config.path + os.sep + rtag
-        #TODO do we add a lock here?
-        if not os.access(path, os.F_OK):
-            os.mkdir(path, 0700)
-            return rtag
-        
-
-class Tag:
-    """contains correspondance between account id and readable tag"""
+class TagMap:
+    """Contains mapping between idTag and nickname"""
     def __init__(self):
-        self.data = None
+        self.id2nick = None
+        self.nick2id = None
 
-    def __del__(self):
-        if self.data != None:
-            self.save()
-            self._release()
-
-    def __getitem__(self, key):
-        if self.data == None:
-            self.load()
-        return self.data[key]
-
     def _lock(self):
-        """Lock the tag file"""
-        self.lock = mixminion.Common.Lockfile(Config.path + os.sep + 'tag.lck')
+        """Lock the tagmap file"""
+        self.lock = mixminion.Common.Lockfile(Config.path + os.sep +
+					      'tagmap.lck')
         self.lock.acquire()
 
     def _release(self):
         self.lock.release()
 
-    def tagfile(self):
-        return Config.path + os.sep + "tag" 
+    def _tagfile(self):
+        return Config.path + os.sep + "tagmap" 
 
-    def load(self):
-        if not self.data == None: return
-        self._lock()
-	tag = self.tagfile()
+    def _load(self):
+	tag = self._tagfile()
 	try:
 	    f = open(tag, 'r')
-	    self.data = pickle.load(f)
+	    self.id2nick = pickle.load(f)
 	    f.close()
 	except IOError:
-	    self.data = {}
-
+	    self.id2nick = {}
+	for i in self.id2nick.keys():
+	    self.nick2id[self.id2nick[i]] = i
+    
+    def _loadifneeded(self):
+	if self.id2nick == None:
+	    self._lock()
+	    self._load()
+	    self._release()
         
-    def save(self):
-        if self.data == None: return
-	tag = self.tagfile()
-	f = open(mbox, 'w')
-	pickle.dump(self.data, f)
+    def _save(self):
+        if self.id2nick == None: return
+	tagmap = self._tagfile()
+	f = open(tagmap, 'w')
+	pickle.dump(self.id2nick, f)
 	f.close()
 
-    def store(self, tag, idTag):
-        if self.data == None:
-            self.load()
-        if self.data.has_key(tag):
-            print "The tag %s is already in use. IdTag %s is not user tagged" % tag, idTag
-        self.data[tag] = idTag
+    def nickFromId(self, idTag):
+        self._loadifneeded()
+        return self.id2nick[idTag]
 
-class User:
-    """Hold user data"""
+    def idFromNick(self, nick):
+        self._loadifneeded()
+        return self.nick2id[nick]
 
+    def getnewId(self, nick):
+        self._lock()
+	self._load()
+	if self.nick2id.has_key(nick):
+	    raise AlreadySuchAccount()
+	while True:
+	    tag = Mail.genMid(8)
+	    rtag = Mail.mid2filename(tag)
+	    if not self.id2nick.has_key(rtag): break
+	self.nick2id[nick] = rtag
+	self.id2nick[rtag] = nick
+	self._save()
+	self._release()
+	return rtag
+
+class Account:
+    """Hold account data"""
+
     def __init__(self, idTag="", tag="", create = 0, username="bazounga"):
         #TODO what is the criteria determining which account we must load? For the time being we only have a user account
         """0 : load user data correspondig to idTag, throw an error if the account doesn't exist
@@ -114,7 +115,7 @@
         _ : load user data corresponding to idTag, create an account if the account doesn't exist, and in that case discard idTag
         tag is an optionnal string. if used when the account is created the association tag -> idTag is kept in the home.tag
         """
-        self.idTag = idTag
+	self.idTag = idTag
         self.datafile = None
 	self.index = None
 	self.mbox = None


Property changes on: trunk/nym3/Client/User.py
___________________________________________________________________
Name: svn:keywords
   + Id



More information about the Nym3-commit mailing list