Commit e6481ad9 authored by Jim Fulton's avatar Jim Fulton

*** empty log message ***

parent 2109ffe6
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
__doc__='''Python implementation of persistent base types __doc__='''Python implementation of persistent base types
$Id: Persistence.py,v 1.10 1997/10/30 18:49:22 jim Exp $''' $Id: Persistence.py,v 1.11 1997/12/15 23:01:17 jim Exp $'''
# Copyright # Copyright
# #
# Copyright 1996 Digital Creations, L.C., 910 Princess Anne # Copyright 1996 Digital Creations, L.C., 910 Princess Anne
...@@ -59,45 +59,12 @@ $Id: Persistence.py,v 1.10 1997/10/30 18:49:22 jim Exp $''' ...@@ -59,45 +59,12 @@ $Id: Persistence.py,v 1.10 1997/10/30 18:49:22 jim Exp $'''
# #
# (540) 371-6909 # (540) 371-6909
# #
# $Log: Persistence.py,v $ __version__='$Revision: 1.11 $'[11:-2]
# Revision 1.10 1997/10/30 18:49:22 jim
# Changed abort to use jar's abort method.
#
# Revision 1.9 1997/04/22 00:16:50 jim
# Changed to use new cPersistent header.
#
# Revision 1.8 1997/04/04 13:52:27 jim
# Fixed bug in persistent mapping that caused extraneous records to be
# written.
#
# Revision 1.7 1997/04/03 17:33:32 jim
# Changed to pass transaction to jar store method.
#
# Revision 1.6 1997/03/28 23:04:48 jim
# Changed reinit to tolerate being called with no arguments.
#
# Revision 1.5 1997/03/25 20:42:42 jim
# Changed to make all persistent objects transactional.
#
# Revision 1.4 1997/03/14 16:19:55 jim
# Changed so no longer save on del.
# Added check in __save__ so that we don't save if we have decided that
# we haven't changed.
#
# Revision 1.3 1997/03/08 22:03:54 jfulton
# Paul made change to clear method to see if keys exist.
#
# Revision 1.2 1997/03/05 22:59:48 jim
# Added clear method.
#
# Revision 1.1 1997/02/11 13:14:06 jim
# *** empty log message ***
#
#
#
__version__='$Revision: 1.10 $'[11:-2]
class Persistent: try:
from cPersistence import Persistent
except:
class Persistent:
"""\ """\
Persistent object support mix-in class Persistent object support mix-in class
...@@ -111,9 +78,6 @@ class Persistent: ...@@ -111,9 +78,6 @@ class Persistent:
of subclasses that change state other than by setting attributes of subclasses that change state other than by setting attributes
should: 'self.__changed__(1)' to flag instances as changed. should: 'self.__changed__(1)' to flag instances as changed.
Data are not saved automatically. To save an object's state, call
the object's '__save__' method.
You must not override the object's '__getattr__' and '__setattr__' You must not override the object's '__getattr__' and '__setattr__'
methods. If you override the objects '__getstate__' method, then methods. If you override the objects '__getstate__' method, then
you must be careful not to include any attributes with names you must be careful not to include any attributes with names
...@@ -121,48 +85,48 @@ class Persistent: ...@@ -121,48 +85,48 @@ class Persistent:
""" """
_p_oid=None # A Persistent object-id, unique within a jar _p_oid=None # A Persistent object-id, unique within a jar
_p_changed=None # A flag indicating whether the object has changed _p_changed=0 # The object state: None=ghost, 0=normal, 1=changed
_p_read_time=0 # The time when the object was read.
_p_jar=None # The last jar that this object was stored in. _p_jar=None # The last jar that this object was stored in.
def __getattr__(self,key):
' '
try: setstate=self.__dict__['_p_setstate']
except: raise AttributeError, key
setstate(self)
try: return self.__dict__[key]
except: raise AttributeError, key
def _p___init__(self,oid,jar): def _p___init__(self,oid,jar):
"""Post creation initialization
This is *only* used if we have __getinitargs__!
"""
d=self.__dict__ d=self.__dict__
if self._p_oid is None: if d:
d['_p_oid']=oid
if self._p_oid==oid:
newstate={} newstate={}
for key in d.keys(): for key in d.keys():
if key[:3] != '_p_': if key[:3] != '_p_':
newstate[key]=d[key] newstate[key]=d[key]
del d[key] del d[key]
d['_p_newstate']=newstate if newstate: d['_p_newstate']=newstate
d['_p_oid']=oid
d['_p_jar']=jar d['_p_jar']=jar
d['_p_setstate']=jar.setstate d['_p_changed']=None
d['_p_changed']=0
def _p___reinit__(self,copy=None): def _p_deactivate(self,copy=None):
if copy is None: return if copy is None: newstate=None
else: newstate=copy.__dict__
d=self.__dict__ d=self.__dict__
cd=copy.__dict__
oid=self._p_oid oid=self._p_oid
jar=self._p_jar jar=self._p_jar
newstate={} d.clear()
for key in cd.keys(): if newstate: d['_p_newstate']=newstate
if key[:3] != '_p_': newstate[key]=cd[key] d['_p_oid']=oid
for key in d.keys():
if key[:3] != '_p_': del d[key]
d['_p_newstate']=newstate
d['_p_jar']=jar d['_p_jar']=jar
d['_p_setstate']=jar.setstate d['_p_changed']=None
d['_p_changed']=0
_p___reinit=_p_deactivate # Back. Comp.
def __getattr__(self,key):
'Get an item'
if self._p_changed is None and key[:3] != '_p_':
self._p_jar.setstate(self)
if self.__dict__.has_key(key): return self.__dict__[key]
raise AttributeError, key
def __setattr__(self,key,value): def __setattr__(self,key,value):
' ' ' '
...@@ -170,43 +134,40 @@ class Persistent: ...@@ -170,43 +134,40 @@ class Persistent:
self.__dict__[key]=value self.__dict__[key]=value
return return
jar=self._p_jar
if self._p_changed is None: jar.setstate(self)
self.__dict__[key]=value
if jar is not None:
try: try:
setstate=self.__dict__['_p_setstate'] get_transaction().register(self)
try: setstate(self) self._p_changed=1
except: raise TypeError, (sys.exc_type, sys.exc_value, except: pass
sys.exc_traceback)
except KeyError: pass
except TypeError, v: raise v[0], v[1], v[2]
self.__dict__[key]=value def __changed__(self,v=-1):
self.__changed__(1) old=self._p_changed
if v != -1:
if v and not old and self._p_jar is not None:
try:
get_transaction().register(self)
self._p_changed=1
except: pass
def __repr__(self): return old
' '
return '<%s instance at %s>' % (self.__class__.__name__,hex(id(self)))
def __getstate__(self): def __getstate__(self):
# First, update my state, if necessary: # First, update my state, if necessary:
try: if self._p_changed is None: self._p_jar.setstate(self)
setstate=self.__dict__['_p_setstate']
try: setstate(self)
except: raise TypeError, (sys.exc_type, sys.exc_value,
sys.exc_traceback)
except KeyError: pass
except TypeError, v: raise v[0], v[1], v[2]
state={} state={}
d=self.__dict__ d=self.__dict__
for k in d.keys(): for k,v in d.items():
if k[:3] != '_p_': if k[:3] != '_p_' and k[:3] != '_v_': state[k]=v
state[k]=d[k]
return state return state
def __setstate__(self,state): def __setstate__(self,state):
d=self.__dict__ d=self.__dict__
for k in state.keys(): for k,v in state.items(): d[k]=v
d[k]=state[k]
return state return state
def __save__(self): def __save__(self):
...@@ -216,34 +177,10 @@ class Persistent: ...@@ -216,34 +177,10 @@ class Persistent:
jar=self._p_jar jar=self._p_jar
if jar and self._p_changed: jar.store(self) if jar and self._p_changed: jar.store(self)
def __changed__(self,value=None): def __repr__(self):
'''\ ' '
Flag or determine whether an object has changed return '<%s instance at %s>' % (self.__class__.__name__,
hex(id(self)))
If a value is specified, then it should indicate whether an
object has or has not been changed. If no value is specified,
then the return value will indicate whether the object has
changed.
'''
if value is not None:
self.__dict__['_p_changed']=value
else:
return self._p_changed
# The following was copied from the SingleThreadedTransaction module:
#
# Base class for all transactional objects
# Transactional objects, like persistent objects track
# changes in state. Unlike persistent objects, transactional
# objects work in conjunction with a transaction manager to manage
# saving state and recovering from errors.
#
def __changed__(self,v=None):
if v and not self._p_changed and self._p_jar is not None:
try: get_transaction().register(self)
except: pass
return Persistence.Persistent.__changed__(self,v)
def __inform_commit__(self,T,start_time): def __inform_commit__(self,T,start_time):
jar=self._p_jar jar=self._p_jar
...@@ -254,59 +191,43 @@ class Persistent: ...@@ -254,59 +191,43 @@ class Persistent:
except: pass except: pass
try: ############################################################################
import cPersistence # $Log: Persistence.py,v $
from cPersistence import Persistent # Revision 1.11 1997/12/15 23:01:17 jim
except: pass # *** empty log message ***
#
class PersistentMapping(Persistent): # Revision 1.10 1997/10/30 18:49:22 jim
"""\ # Changed abort to use jar's abort method.
A persistent wrapper for mapping objects. #
# Revision 1.9 1997/04/22 00:16:50 jim
This class allows wrapping of mapping objects so that # Changed to use new cPersistent header.
object changes are registered. As a side effect, #
mapping objects may be subclassed. # Revision 1.8 1997/04/04 13:52:27 jim
""" # Fixed bug in persistent mapping that caused extraneous records to be
# written.
def __init__(self,container=None): #
if container is None: container={} # Revision 1.7 1997/04/03 17:33:32 jim
self._container=container # Changed to pass transaction to jar store method.
#
def __getitem__(self, key): # Revision 1.6 1997/03/28 23:04:48 jim
return self._container[key] # Changed reinit to tolerate being called with no arguments.
#
def __setitem__(self, key, v): # Revision 1.5 1997/03/25 20:42:42 jim
self._container[key]=v # Changed to make all persistent objects transactional.
try: del self._v_keys #
except: pass # Revision 1.4 1997/03/14 16:19:55 jim
self.__changed__(1) # Changed so no longer save on del.
# Added check in __save__ so that we don't save if we have decided that
def __delitem__(self, key): # we haven't changed.
del self._container[key] #
try: del self._v_keys # Revision 1.3 1997/03/08 22:03:54 jfulton
except: pass # Paul made change to clear method to see if keys exist.
self.__changed__(1) #
# Revision 1.2 1997/03/05 22:59:48 jim
def __len__(self): return len(self._container) # Added clear method.
#
def keys(self): # Revision 1.1 1997/02/11 13:14:06 jim
try: return self._v_keys # *** empty log message ***
except: pass #
keys=self._v_keys=filter( #
lambda k: k[:1]!='_', #
self._container.keys())
keys.sort()
return keys
def clear(self):
self._container={}
if hasattr(self,'_v_keys'): del self._v_keys
def values(self):
return map(lambda k, d=self: d[k], self.keys())
def items(self):
return map(lambda k, d=self: (k,d[k]), self.keys())
def has_key(self,key): return self._container.has_key(key)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment