

from Ft.Ods import Constants

class DictionaryObjectKeyMixin:


    def _4ods_setByKey(self,key,value):
        Constants.g_typeCheck[self._keyType](key,self._keyRepoId)
        key = key._4ods_getId()

        if self._values.has_key(key):
            #A change
            self._dbChanges.append((Constants.DictionaryChanges.CHANGE,key,value))
        else:
            self._dbChanges.append((Constants.DictionaryChanges.ADD,key,value))

        self._values[key] = value


    def _4ods_getByKey(self,key):
        try:
            return self._values[key._4ods_getId()]
        except KeyError:
            raise self.KeyNotFound(key)

    def _4ods_deleteByKey(self,key):
        nk = key._4ods_getId()
        try:
            del self._values[nk]
            self._dbChanges.append((Constants.DictionaryChanges.REMOVE,nk,None))
        except:
            raise self.KeyNotFound(key)

        

    def _4ods_initValueKeys(self):
        pass

    def _4ods_prepareKeyChanges(self):
        pass

    def has_key(self,key):
        return self._values.has_key(key._4ods_getId())

            
    def keys(self):
        return map(lambda x,d=self._db,kt=self._keyType:d._4ods_getPersistentObjectById(kt,x),self._values.keys())
