16a23d212SGreg Clayton
26a23d212SGreg Claytonclass LookupDictionary(dict):
36a23d212SGreg Clayton    """
46a23d212SGreg Clayton    a dictionary which can lookup value by key, or keys by value
56a23d212SGreg Clayton    """
6*b9c1b51eSKate Stone
76a23d212SGreg Clayton    def __init__(self, items=[]):
86a23d212SGreg Clayton        """items can be a list of pair_lists or a dictionary"""
96a23d212SGreg Clayton        dict.__init__(self, items)
106a23d212SGreg Clayton
116a23d212SGreg Clayton    def get_keys_for_value(self, value, fail_value=None):
126a23d212SGreg Clayton        """find the key(s) as a list given a value"""
136a23d212SGreg Clayton        list_result = [item[0] for item in self.items() if item[1] == value]
146a23d212SGreg Clayton        if len(list_result) > 0:
156a23d212SGreg Clayton            return list_result
166a23d212SGreg Clayton        return fail_value
176a23d212SGreg Clayton
186a23d212SGreg Clayton    def get_first_key_for_value(self, value, fail_value=None):
196a23d212SGreg Clayton        """return the first key of this dictionary given the value"""
206a23d212SGreg Clayton        list_result = [item[0] for item in self.items() if item[1] == value]
216a23d212SGreg Clayton        if len(list_result) > 0:
226a23d212SGreg Clayton            return list_result[0]
236a23d212SGreg Clayton        return fail_value
246a23d212SGreg Clayton
256a23d212SGreg Clayton    def get_value(self, key, fail_value=None):
266a23d212SGreg Clayton        """find the value given a key"""
276a23d212SGreg Clayton        if key in self:
286a23d212SGreg Clayton            return self[key]
296a23d212SGreg Clayton        return fail_value
306a23d212SGreg Clayton
316a23d212SGreg Clayton
326a23d212SGreg Claytonclass Enum(LookupDictionary):
336a23d212SGreg Clayton
346a23d212SGreg Clayton    def __init__(self, initial_value=0, items=[]):
356a23d212SGreg Clayton        """items can be a list of pair_lists or a dictionary"""
366a23d212SGreg Clayton        LookupDictionary.__init__(self, items)
376a23d212SGreg Clayton        self.value = initial_value
386a23d212SGreg Clayton
396a23d212SGreg Clayton    def set_value(self, v):
406a23d212SGreg Clayton        v_typename = typeof(v).__name__
416a23d212SGreg Clayton        if v_typename == 'str':
426a23d212SGreg Clayton            if str in self:
436a23d212SGreg Clayton                v = self[v]
446a23d212SGreg Clayton            else:
456a23d212SGreg Clayton                v = 0
466a23d212SGreg Clayton        else:
476a23d212SGreg Clayton            self.value = v
486a23d212SGreg Clayton
496a23d212SGreg Clayton    def get_enum_value(self):
506a23d212SGreg Clayton        return self.value
516a23d212SGreg Clayton
526a23d212SGreg Clayton    def get_enum_name(self):
536a23d212SGreg Clayton        return self.__str__()
546a23d212SGreg Clayton
556a23d212SGreg Clayton    def __str__(self):
566a23d212SGreg Clayton        s = self.get_first_key_for_value(self.value, None)
57*b9c1b51eSKate Stone        if s is None:
586a23d212SGreg Clayton            s = "%#8.8x" % self.value
596a23d212SGreg Clayton        return s
606a23d212SGreg Clayton
616a23d212SGreg Clayton    def __repr__(self):
626a23d212SGreg Clayton        return self.__str__()
63