'''
Running this script twice, gives a "RuntimeError: maximum recursion depth exceeded".
Switching to not using __setattr__, by uncommenting/commenting the
a) setattr lines in __init__
b) the property definitions, while commenting out the __setattr__ def
will not trigger the problem, while achieviong the same result.
'''    

from durus.file_storage import FileStorage
from durus.connection import Connection
from durus.persistent import Persistent
from durus.persistent_dict import PersistentDict
from durus.persistent_list import PersistentList

class PersistentItem(Persistent):

    def set_id(self, item_id):
        if hasattr(self, '_id') and self._id is not None:
            raise AttributeError, 'May not modify PersistentItem id.'
        self._id = item_id
    def get_id(self):
        return self._id 
    id = property(get_id, set_id)
    
class TestObj(PersistentItem): 

    __special_atts__ = ('shortname', 'year')
    
    def __init__(self,
            shortname=None,
            name=None,
            year=None
        ):
        for att in self.__special_atts__:
            #setattr(self, '_'+att, locals()[att]) 
            setattr(self, att, locals()[att]) # For ERROR below...
        self.name = name
        super(TestObj, self).__init__(self) 
            
    '''
    def get_shortname(self):
        return self._shortname
    def set_shortname(self, value):
        # do something special...
        self._shortname = value
    shortname = property(get_shortname, set_shortname)
    
    def get_name(self):
        return self._name
    def set_name(self, value):
        self._name = value
    name = property(get_name, set_name)

    def get_year(self):
        return self._year
    def set_year(self, value):
        # do something special...
        self._year = value
    year = property(get_year, set_year)
    '''
    
    def __setattr__(self, name, value):
        if name in self.__special_atts__:
            # do something special with name..
            pass
        super(TestObj, self).__setattr__(name, value)

#        
        
if __name__ == "__main__":

    _filepath = '%s/%s'%('.', 'durus_recursion_error.durus')
    c = Connection(FileStorage(_filepath))
    r = c.get_root()

    o = r.get('ton', None)
    if o is None:
        o = TestObj('ton','the test object name', 2004)
        r['ton'] = o        
    c.commit()
    
    # Any attribute access gives the error... 
    print o.name
    

#

