Changeset ef252e9


Ignore:
Timestamp:
Feb 28, 2010 12:24:51 PM (15 years ago)
Author:
Ted Faber <faber@…>
Branches:
axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
Children:
2761484
Parents:
358e0b8
Message:

Add support for loading and saving the synch points (pickle didn't work after
all) and enforce space free keys. That requirement is because of the very
simple file format.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • fedd/federation/synch_store.py

    r358e0b8 ref252e9  
    1616    # Thrown if a non-existant key is deleted
    1717    class BadDeletionError(Exception): pass
     18
     19    # Thrown if a key is specified with a white space
     20    class BadKeyError(Exception): pass
    1821
    1922    # A value in the dict.  Each has its own condition (and lock) for threads
     
    3740    # Set the value in the dict.  If threads are waiting, wake them.
    3841    def set_value(self, key, value):
     42        if key.find(' ') != -1:
     43            raise self.BadKeyError("Whitespace is not allowed in keys")
     44
    3945        self.main_lock.acquire()
    4046        if self.values.has_key(key):
     
    8894            self.main_lock.release()
    8995            raise synch_store.BadDeletionError("%s does not exist yet" % key)
     96
     97    def all_keys(self):
     98        self.main_lock.acquire()
     99        rv = [ k for k in self.values.keys()]
     100        self.main_lock.release()
     101        return rv
     102
     103    # Save the store contents to a file in a simple way
     104    def save(self, filename):
     105        self.main_lock.acquire()
     106        f = open(filename, 'w')
     107        for k, v in self.values.items():
     108            self.values[k].condition.acquire()
     109            if self.values[k].value is not None:
     110                print >> f, "%s %s" % ( k, str(self.values[k].value))
     111            else:
     112                print >> f, "%s" % k
     113            self.values[k].condition.release()
     114        self.main_lock.release()
     115        f.close()
     116
     117    # Load the store contents from a file
     118    def load(self, filename):
     119        self.main_lock.acquire()
     120        self.values = { }
     121        f = open(filename, 'r')
     122        for l in f:
     123            i = l.find(' ')
     124            if i != -1: self.values[l[:i]] = synch_store.synch_value(l[i+1:])
     125            else: self.values[l] = synch_store.synch_value()
     126        f.close()
     127        self.main_lock.release()
    90128
    91129    # A debugging representation
Note: See TracChangeset for help on using the changeset viewer.