From b9ac47d30142b05211e613fde9b37b6622b6f004 Mon Sep 17 00:00:00 2001 From: Pat Riehecky Date: Mar 19 2019 18:01:45 +0000 Subject: Switch ref store events to only use api exported by model --- diff --git a/updateinfo/reference/store/events.py b/updateinfo/reference/store/events.py index 3f73045..fa9668e 100644 --- a/updateinfo/reference/store/events.py +++ b/updateinfo/reference/store/events.py @@ -45,24 +45,6 @@ class ReferenceStoreEvents(object): '''setup''' pass - def __delitem__(self, key): - ''' - Used for: removing like a dict - ''' - del self._reflist[key] - - def __setitem__(self, key, value): - ''' - For simply adding a reference (no merge) - ''' - if not isinstance(value, type(self.Reference())): - raise TypeError('I can only add ' + str(type(self.Reference())) + ' type objects, not ' + str(type(value))) - - if key != value.href: - raise ValueError('Incorrect href, should have been "' + value.href + '"') - - self._reflist[key] = value - def add(self, ref, merge=True): '''add a reference to the list''' if xmletree.iselement(ref): @@ -88,8 +70,6 @@ class ReferenceStoreEvents(object): else: self[ref.href] = ref - self._setup_byreftype_dict() - if ref.href in self: return True @@ -97,6 +77,7 @@ class ReferenceStoreEvents(object): def create(self, reftype=None, href=None, refid=None, title=None, merge=True): '''Add a new reference to this store''' + logging.debug("Trying to create reference: reftype=%s, href=%s, refid=%s, title=%s", reftype, href, refid, title) ref = self.Reference(reftype=reftype, href=href, refid=refid, title=title) return self.add(ref, merge) @@ -117,26 +98,14 @@ class ReferenceStoreEvents(object): # I can't get here in python2 raise ValueError('Pass either the href, or the ref object to remove') # pragma: no cover + logging.debug("Trying to remove reference:%s", obj) del self[href] - self._setup_byreftype_dict() - if href not in self: return True raise RuntimeError('Attempted to remove ref, but found after remove') # pragma: no cover - def _setup_byreftype_dict(self): - '''The byreftype dict is a quick ref we need to maintain''' - for reftype in self.reftypes: - self._byreftype[reftype] = [] - - for href in self.urls: - self._byreftype[self[href].reftype].append(self[href]) - - for reftype in self._byreftype: - self._byreftype[reftype] = tuple(self._byreftype[reftype]) - @staticmethod def _merge_attributes(existing, ref): ''' diff --git a/updateinfo/reference/store/models.py b/updateinfo/reference/store/models.py index f1b1183..2bc2c22 100644 --- a/updateinfo/reference/store/models.py +++ b/updateinfo/reference/store/models.py @@ -93,6 +93,24 @@ class ReferenceStoreModel(object): ''' return self._reflist[key] + def __delitem__(self, key): + ''' + Used for: removing like a dict + ''' + del self._reflist[key] + + def __setitem__(self, key, value): + ''' + For simply adding a reference (no merge) + ''' + if not isinstance(value, type(self.Reference())): + raise TypeError('I can only add ' + str(type(self.Reference())) + ' type objects, not ' + str(type(value))) + + if key != value.href: + raise ValueError('Incorrect href, should have been "' + value.href + '"') + + self._reflist[key] = value + def __contains__(self, key): ''' Used for: x in object @@ -148,7 +166,18 @@ class ReferenceStoreModel(object): A dict of known refs with the following structure self.byreftype[reftype] = (ReferenceModel, ReferenceModel, ReferenceModel) ''' - return self._byreftype + _byreftype = {} + + for reftype in self.reftypes: + _byreftype[reftype] = [] + + for href in self.urls: + _byreftype[self[href].reftype].append(self[href]) + + for reftype in _byreftype: + _byreftype[reftype] = tuple(_byreftype[reftype]) + return _byreftype + @byreftype.setter def byreftype(self, value): '''Raise error'''