Package advene :: Package model :: Package util :: Module locale_cmp
[hide private]
[frames] | no frames]

Source Code for Module advene.model.util.locale_cmp

  1  # 
  2  # Advene: Annotate Digital Videos, Exchange on the NEt 
  3  # Copyright (C) 2008 Olivier Aubert <olivier.aubert@liris.cnrs.fr> 
  4  # 
  5  # Advene is free software; you can redistribute it and/or modify 
  6  # it under the terms of the GNU General Public License as published by 
  7  # the Free Software Foundation; either version 2 of the License, or 
  8  # (at your option) any later version. 
  9  # 
 10  # Advene is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 13  # GNU General Public License for more details. 
 14  # 
 15  # You should have received a copy of the GNU General Public License 
 16  # along with Advene; if not, write to the Free Software 
 17  # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 18  # 
 19  import locale 
 20   
 21  from choice_tree import ChoiceTree 
 22   
 23  try: 
 24      import xml.dom 
 25  except ImportError: 
 26      pass 
 27   
 28   
29 -class Locale (object):
30 """Class for representing and comparing locale settings. 31 """ 32
33 - def __init__ (self, a_string):
34 self.lang = None 35 self.country = None 36 37 if a_string is not None and len (a_string) >= 2: 38 self.lang = a_string[0:2] 39 if len (a_string) >= 5: 40 self.country = a_string[3:5] 41 42 self.__hash = hash (str (self))
43
44 - def fromDomElement (elt):
45 s = elt.getAttributeNS (xml.dom.XML_NAMESPACE,'lang') 46 return Locale (s)
47 fromDomElement = staticmethod (fromDomElement) 48
49 - def fromEnv ():
50 return Locale (locale.getdefaultlocale ()[0])
51 fromEnv = staticmethod (fromEnv) 52 53
54 - def isMoreSpecificThat (self, other):
55 if other.lang is None: 56 return True 57 else: 58 if self.lang == other.lang and other.country is None: 59 return True 60 else: 61 return False
62
63 - def isMoreGeneralThat (self, other):
64 return other.isMoreSpecificThat (self)
65
66 - def __eq__ (self, other):
67 try: 68 return self.lang == other.lang and self.country == self.country 69 except AttributeError: 70 pass 71 return False
72 73
74 - def __str__ (self):
75 if self.lang is None: 76 r = '' 77 elif self.country is None: 78 r = self.lang 79 else: 80 r = '%s_%s' % (self.lang, self.country) 81 return r
82
83 - def __repr__ (self):
84 return "Locale('%s')" % str (self)
85
86 - def __hash (self):
87 return self.__hash
88 89 #nullLocale = Locale('') 90 defaultLocale = Locale.fromEnv () 91
92 -class LocaleChooser (object):
93 """ 94 A tree of element stored hierarchically with respect to their locale. 95 The =getBestFit= method returns the best fit element with respect to the 96 default locale. 97 """ 98
99 - def __init__ (self):
100 self.__ct = ChoiceTree ()
101
102 - def __loc2tuple (self, loc):
103 if loc.lang is None: 104 return () 105 elif loc.country is None: 106 return (loc.lang,) 107 else: 108 return (loc.lang, loc.country)
109
110 - def add (self, elt):
111 seq = self.__loc2tuple (Locale.fromDomElement (elt)) 112 self.__ct.set (seq, elt)
113
114 - def getBestFit (self, loc = defaultLocale):
115 seq = self.__loc2tuple (loc) 116 tree = self.__ct 117 exactMatch = None 118 try: 119 subtree = tree.getSubtree (seq) 120 exactMatch = subtree.getAnyInstance () 121 except KeyError: 122 pass 123 return ( 124 exactMatch or 125 tree.getMostSpecificInstance (seq) or 126 tree.getAny () 127 )
128