Esempio n. 1
0
class LocaleChooser (object):
    """
    A tree of element stored hierarchically with respect to their locale.
    The =getBestFit= method returns the best fit element with respect to the
    default locale.
    """

    def __init__ (self):
        self.__ct = ChoiceTree ()

    def __loc2tuple (self, loc):
        if loc.lang is None:
            return ()
        elif loc.country is None:
            return (loc.lang,)
        else:
            return (loc.lang, loc.country)

    def add (self, elt):
        seq = self.__loc2tuple (Locale.fromDomElement (elt))
        self.__ct.set (seq, elt)

    def getBestFit (self, loc = defaultLocale):
        seq = self.__loc2tuple (loc)
        tree = self.__ct
        exactMatch = None
        try:
            subtree = tree.getSubtree (seq)
            exactMatch = subtree.getAnyInstance ()
        except KeyError:
            pass
        return (
          exactMatch or
          tree.getMostSpecificInstance (seq) or
          tree.getAny ()
        )
Esempio n. 2
0
 def __init__ (self):
     self.__ct = ChoiceTree ()