[Nym3-commit] r182 - trunk/nym3/Client
laurent at conuropsis.org
laurent at conuropsis.org
Sun Apr 10 16:41:24 CEST 2005
Author: laurent
Date: 2005-04-10 16:41:20 +0200 (Sun, 10 Apr 2005)
New Revision: 182
Modified:
trunk/nym3/Client/Account.py
trunk/nym3/Client/Main.py
Log:
Cleaner interface to Account.
Modified: trunk/nym3/Client/Account.py
===================================================================
--- trunk/nym3/Client/Account.py 2005-04-10 11:22:40 UTC (rev 181)
+++ trunk/nym3/Client/Account.py 2005-04-10 14:41:20 UTC (rev 182)
@@ -121,38 +121,46 @@
- idTag is the identity string used to generate SURBs for this
account. It is therefore unique to this account. Additionnaly
it is used to name the subdirectory holding this account's data
- - datafile is the file which holds all of this. A mere pickling
- - index. What was that again?
- misc parameters that needs further documentation.
- - syn is the synopsis box (synbox)
- - mbox is self explanatory
- data is the hash that contains all of this
- - datafile is the file where we pickle from and to the data hash
+ - datafile() is the file where we pickle from and to the data hash
+ - synbox is the synopsis box, stored in synboxfile()
+ - mbox is self explanatory, stored in mboxfile()
- a journal which is hash of seqno -> (message, time sent) and is
- pickled from/to journalfile"""
+ pickled from/to journalfile()"""
- def __init__(self, config, nickname, create = 0):
+ def __init__(self, config, nickname, create = False):
"""Load from an existing account, or create a new, or fail"""
- # Set data to dummy values
- #self.idTag = None
- self.datafile = None
- self.journalfile = None
- #self.index = None
- #self.mbox = None
- #self.syn = None
- self.data = {}
- self.journal = {}
+ # Some data related to the account are loaded only on demand,
+ # this includes the journal, the mailbox, the synbox.
self.lock = None
self.config = config
- if create == 1:
+ if create:
tagmap = TagMap(config.path + os.sep + 'tagmap')
+ self.data = {}
self['idTag'] = tagmap.getnewId(nickname)
# If the nick already existed, we're out of this because of
# a thrown AlreadySuchAccount.
# Create the dir that holds this account data
- os.mkdir(config.path + os.sep + self['idTag'])
- self.base_path = config.path + os.sep + self['idTag']
+ self.base_path = config.path + os.sep + self['idTag']
+ os.mkdir(self.base_path)
+ self.data_status = 'dirty'
+ self.journal = {}
+ self.journal_status = 'dirty'
+ self.mbox = []
+ self.mbox_status = 'dirty'
+ self.synbox = []
+ self.synbox_status = 'dirty'
+ else:
+ self._load_data()
+ self.journal = None
+ self.journal_status = 'unloaded'
+ self.mbox = None
+ self.mbox_status = 'unloaded'
+ self.synbox = None
+ self.synbox_status = 'unloaded'
+ self.base_path = config.path + os.sep + self['idTag']
self._lock()
def generateSurbs(self, n):
@@ -168,18 +176,19 @@
f.close()
os.unlink(surbspath)
os.rmdir(surbdir)
+ # TODO : the size of the generated surbs is wrong in minion-cvs
return data
def __getitem__(self, key):
"""Gets user account field as in a hashtable"""
+ if self.data_status == 'unloaded': self._load_data()
return self.data[key]
def __setitem__(self, key, value):
"""Gets user account field as in a hashtable"""
self.data[key] = value
+ self.data_status = 'dirty'
- def get_seqno(self): pass
-
def send(self, body):
# TODO : this should be done via ClientAPI.
(f, msgpath) = tempfile.mkstemp()
@@ -196,6 +205,18 @@
"""Gets the path of the mailbox"""
return self.base_path + os.sep + 'mbox'
+ def datafile(self):
+ """Gets the path of the datafile"""
+ return self.base_path + os.sep + 'data'
+
+ def synboxfile(self):
+ """Gets the path of the synbox"""
+ return self.base_path + os.sep + 'synbox'
+
+ def journalfile(self):
+ """Gets the path of the journal"""
+ return self.base_path + os.sep + 'journal'
+
def _lock(self):
"""Locks the user. For well behaved functions."""
self.lock = mixminion.Common.Lockfile(self.config.path + os.sep +
@@ -229,91 +250,94 @@
def record(self, seqno, msg):
"""Store a control message in the journal"""
+ if self.journal_status == 'unloaded': self._load_journal()
self.journal[seqno] = (msg, int(time.time()))
+ self.journal_status = 'dirty'
def get_seqno(self):
"""Return a previously unused sequence number"""
ret = chr(0) * SEQNO_LEN
+ if self.journal_status == 'unloaded': self._load_journal()
while self.journal.has_key(ret):
ret = Mail.genMid(SEQNO_LEN)
return ret
- # Everything below this line has not been reread and can be considered
- # work in progress.
-
+ def _save_data(self):
+ """Flushes the data to the disk"""
+ if not self.data_status == 'dirty': return
+ data = self.datafile()
+ f = open(data, 'w')
+ pickle.dump(self.data, f)
+ f.close()
+ self.data_status = 'ok'
+
+ def _save_mbox(self):
+ """Flushes the mbox to the disk"""
+ if not self.mbox_status == 'dirty': return
+ mbox = self.mboxfile()
+ f = open(mbox, 'w')
+ pickle.dump(self.mbox, f)
+ f.close()
+ self.mbox_status = 'ok'
+
+ def _save_synbox(self):
+ """Flushes the synbox to the disk"""
+ if not self.synbox_status == 'dirty': return
+ synbox = self.synboxfile()
+ f = open(synbox, 'w')
+ pickle.dump(self.synbox, f)
+ f.close()
+ self.synbox_status = 'ok'
+
+ def _save_journal(self):
+ """Flushes the journal to the disk"""
+ if not self.journal_status == 'dirty': return
+ journal = self.journalfile()
+ f = open(journal, 'w')
+ pickle.dump(self.journal, f)
+ f.close()
+ self.journal_status = 'ok'
+
def __del__(self):
"""Flushes the user account to the disk"""
- #self._save_index()
- #self._save_synbox()
- #self._save_mbox()
+ self._save_synbox()
+ self._save_mbox()
+ self._save_journal()
self._save_data()
if self.lock: self._release()
-
- def load_mbox(self):
+
+ def _load_mbox(self):
"""Loads the mailbox from the disk"""
- if not self.mbox == None: return
+ if not self.mbox_status == 'unloaded': return
mbox = self.mboxfile()
try:
f = open(mbox, 'r')
self.mbox = pickle.load(f)
f.close()
except IOError:
- self.mbox = {}
+ self.mbox = {} # ?
+ self.mbox_status = 'ok'
- def _save_mbox(self):
- """Flushes the mailbox to the disk"""
- if self.mbox == None: return
- mbox = self.mboxfile()
- f = open(mbox, 'w')
- pickle.dump(self.mbox, f)
- f.close()
-
- def synboxfile(self):
- """Gets the path of the synbox"""
- return self.home + 'syn'
-
- def indexfile(self):
- """Gets the path of the index"""
- return self.home + 'idx'
-
- def load_synbox(self):
+ def _load_synbox(self):
"""Loads the synbox from the disk"""
- if not self.syn == None: return
+ if not self.synbox_status == 'unloaded': return
synbox = self.synboxfile()
try:
f = open(synbox, 'r')
- self.syn = pickle.load(f)
+ self.synbox = pickle.load(f)
f.close()
except IOError:
- self.syn = []
+ self.synbox = []
+ self.synbox_status = 'ok'
- def _save_synbox(self):
- """Flushes the synbox to the disk"""
- if self.syn == None: return
- synbox = self.synboxfile()
- f = open(synbox, 'w')
- pickle.dump(self.syn, f)
- f.close()
-
- def load_index(self):
- """Loads the index from the disk"""
- if not self.index == None: return
- index = self.indexfile()
+ def _load_journal(self):
+ """Loads the journal from the disk"""
+ if not self.journal_status == 'unloaded': return
+ journal = self.journalfile()
try:
- f = open(index, 'r')
- self.index = pickle.load(f)
+ f = open(journal, 'r')
+ self.journal = pickle.load(f)
f.close()
except IOError:
- self.index = {}
-
- def _save_index(self):
- """Flushes the index to the disk"""
- if self.index == None: return
- index = self.indexfile()
- f = open(index, 'w')
- pickle.dump(self.index, f)
- f.close()
-
- def _save_data(self):
- """Flushes the data to the disk"""
- pass
+ self.journal = {}
+ self.journal_status = 'ok'
Modified: trunk/nym3/Client/Main.py
===================================================================
--- trunk/nym3/Client/Main.py 2005-04-10 11:22:40 UTC (rev 181)
+++ trunk/nym3/Client/Main.py 2005-04-10 14:41:20 UTC (rev 182)
@@ -104,7 +104,7 @@
while True: # chose a suitable account name
account = None
try:
- account = Account.Account(config, nickname, create = 1)
+ account = Account.Account(config, nickname, create = True)
except Account.AlreadySuchAccount:
ui.display("This account name is already in use")
nickname = ui.prompt("New account name")
More information about the Nym3-commit
mailing list