-## A dict which can access its keys and/or values orderly\r
-#\r
-# The class implements a new kind of dict which its keys or values can be\r
-# accessed in the order they are added into the dict. It guarantees the order\r
-# by making use of an internal list to keep a copy of keys.\r
-#\r
-class sdict(IterableUserDict):\r
- ## Constructor\r
- def __init__(self):\r
- IterableUserDict.__init__(self)\r
- self._key_list = []\r
-\r
- ## [] operator\r
- def __setitem__(self, key, value):\r
- if key not in self._key_list:\r
- self._key_list.append(key)\r
- IterableUserDict.__setitem__(self, key, value)\r
-\r
- ## del operator\r
- def __delitem__(self, key):\r
- self._key_list.remove(key)\r
- IterableUserDict.__delitem__(self, key)\r
-\r
- ## used in "for k in dict" loop to ensure the correct order\r
- def __iter__(self):\r
- return self.iterkeys()\r
-\r
- ## len() support\r
- def __len__(self):\r
- return len(self._key_list)\r
-\r
- ## "in" test support\r
- def __contains__(self, key):\r
- return key in self._key_list\r
-\r
- ## indexof support\r
- def index(self, key):\r
- return self._key_list.index(key)\r
-\r
- ## insert support\r
- def insert(self, key, newkey, newvalue, order):\r
- index = self._key_list.index(key)\r
- if order == 'BEFORE':\r
- self._key_list.insert(index, newkey)\r
- IterableUserDict.__setitem__(self, newkey, newvalue)\r
- elif order == 'AFTER':\r
- self._key_list.insert(index + 1, newkey)\r
- IterableUserDict.__setitem__(self, newkey, newvalue)\r
-\r
- ## append support\r
- def append(self, sdict):\r
- for key in sdict:\r
- if key not in self._key_list:\r
- self._key_list.append(key)\r
- IterableUserDict.__setitem__(self, key, sdict[key])\r
-\r
- def has_key(self, key):\r
- return key in self._key_list\r
-\r
- ## Empty the dict\r
- def clear(self):\r
- self._key_list = []\r
- IterableUserDict.clear(self)\r
-\r
- ## Return a copy of keys\r
- def keys(self):\r
- keys = []\r
- for key in self._key_list:\r
- keys.append(key)\r
- return keys\r
-\r
- ## Return a copy of values\r
- def values(self):\r
- values = []\r
- for key in self._key_list:\r
- values.append(self[key])\r
- return values\r
-\r
- ## Return a copy of (key, value) list\r
- def items(self):\r
- items = []\r
- for key in self._key_list:\r
- items.append((key, self[key]))\r
- return items\r
-\r
- ## Iteration support\r
- def iteritems(self):\r
- return iter(self.items())\r
-\r
- ## Keys interation support\r
- def iterkeys(self):\r
- return iter(self.keys())\r
-\r
- ## Values interation support\r
- def itervalues(self):\r
- return iter(self.values())\r
-\r
- ## Return value related to a key, and remove the (key, value) from the dict\r
- def pop(self, key, *dv):\r
- value = None\r
- if key in self._key_list:\r
- value = self[key]\r
- self.__delitem__(key)\r
- elif len(dv) != 0 :\r
- value = kv[0]\r
- return value\r
-\r
- ## Return (key, value) pair, and remove the (key, value) from the dict\r
- def popitem(self):\r
- key = self._key_list[-1]\r
- value = self[key]\r
- self.__delitem__(key)\r
- return key, value\r
-\r
- def update(self, dict=None, **kwargs):\r
- if dict is not None:\r
- for k, v in dict.items():\r
- self[k] = v\r
- if len(kwargs):\r
- for k, v in kwargs.items():\r
- self[k] = v\r
-\r
-## Dictionary with restricted keys\r
-#\r
-class rdict(dict):\r
- ## Constructor\r
- def __init__(self, KeyList):\r
- for Key in KeyList:\r
- dict.__setitem__(self, Key, "")\r
-\r
- ## []= operator\r
- def __setitem__(self, key, value):\r
- if key not in self:\r
- EdkLogger.error("RestrictedDict", ATTRIBUTE_SET_FAILURE, "Key [%s] is not allowed" % key,\r
- ExtraData=", ".join(dict.keys(self)))\r
- dict.__setitem__(self, key, value)\r
-\r
- ## =[] operator\r
- def __getitem__(self, key):\r
- if key not in self:\r
- return ""\r
- return dict.__getitem__(self, key)\r
-\r
- ## del operator\r
- def __delitem__(self, key):\r
- EdkLogger.error("RestrictedDict", ATTRIBUTE_ACCESS_DENIED, ExtraData="del")\r
-\r
- ## Empty the dict\r
- def clear(self):\r
- for Key in self:\r
- self.__setitem__(Key, "")\r
-\r
- ## Return value related to a key, and remove the (key, value) from the dict\r
- def pop(self, key, *dv):\r
- EdkLogger.error("RestrictedDict", ATTRIBUTE_ACCESS_DENIED, ExtraData="pop")\r
-\r
- ## Return (key, value) pair, and remove the (key, value) from the dict\r
- def popitem(self):\r
- EdkLogger.error("RestrictedDict", ATTRIBUTE_ACCESS_DENIED, ExtraData="popitem")\r