From 2dd8c01513bde8b30d27faf3a48f07c2e7e95ea7 Mon Sep 17 00:00:00 2001 From: Chris Larson Date: Mon, 12 Apr 2010 18:56:25 -0700 Subject: Make DataSmart inherit the MutableMapping ABC Provide __len__, __iter__, and the getitem/setitem/delitem methods, and its mixed in versions of keys(), values(), items(), etc will automatically behave, making the DataSmart act more like a real mapping. (Bitbake rev: 89b5351c656d263b0ce513cee043bc046d20a01e) Signed-off-by: Chris Larson Signed-off-by: Richard Purdie --- bitbake/lib/bb/data_smart.py | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/bitbake/lib/bb/data_smart.py b/bitbake/lib/bb/data_smart.py index 30f9cbc2d..22dadec5d 100644 --- a/bitbake/lib/bb/data_smart.py +++ b/bitbake/lib/bb/data_smart.py @@ -29,6 +29,7 @@ BitBake build tools. # Based on functions from the base bb module, Copyright 2003 Holger Schurig import copy, re, sys +from collections import MutableMapping import bb from bb import utils from bb.COW import COWDictBase @@ -73,7 +74,7 @@ class VariableParse: return str(value) -class DataSmart: +class DataSmart(MutableMapping): def __init__(self, special = COWDictBase.copy(), seen = COWDictBase.copy() ): self.dict = {} @@ -347,23 +348,28 @@ class DataSmart: return data - # Dictionary Methods - def keys(self): - def _keys(d, mykey): + def __iter__(self): + seen = set() + def _keys(d): if "_data" in d: - _keys(d["_data"], mykey) + for key in _keys(d["_data"]): + yield key - for key in d.keys(): + for key in d: if key != "_data": - mykey[key] = None - keytab = {} - _keys(self.dict, keytab) - return keytab.keys() + if not key in seen: + seen.add(key) + yield key + return _keys(self.dict) + + def __len__(self): + return len(frozenset(self)) def __getitem__(self, item): - #print "Warning deprecated" return self.getVar(item, False) - def __setitem__(self, var, data): - #print "Warning deprecated" - self.setVar(var, data) + def __setitem__(self, var, value): + self.setVar(var, value) + + def __delitem__(self, var): + self.delVar(var) -- cgit v1.2.3