[MERGE] base: raise an exception if the format of the bank account is wrong
[odoo/odoo.git] / openerp / addons / base / res / res_lang.py
index cb1a688..07b220a 100644 (file)
@@ -21,7 +21,7 @@
 
 import locale
 import logging
-import itertools
+import re
 
 from osv import fields, osv
 from locale import localeconv
@@ -29,6 +29,8 @@ import tools
 from tools.safe_eval import safe_eval as eval
 from tools.translate import _
 
+_logger = logging.getLogger(__name__)
+
 class lang(osv.osv):
     _name = "res.lang"
     _description = "Languages"
@@ -37,22 +39,30 @@ class lang(osv.osv):
     _disallowed_datetime_patterns.remove('%y') # this one is in fact allowed, just not good practice
 
     def install_lang(self, cr, uid, **args):
+        """
+
+        This method is called from openerp/addons/base/base_data.xml to load
+        some language and set it as the default for every partners. The
+        language is set via tools.config by the RPC 'create' method on the
+        'db' object. This is a fragile solution and something else should be
+        found.
+
+        """
         lang = tools.config.get('lang')
         if not lang:
             return False
         lang_ids = self.search(cr, uid, [('code','=', lang)])
-        values_obj = self.pool.get('ir.values')
         if not lang_ids:
-            lang_id = self.load_lang(cr, uid, lang)
-        default_value = values_obj.get(cr, uid, 'default', False, 'res.partner')
+            self.load_lang(cr, uid, lang)
+        ir_values_obj = self.pool.get('ir.values')
+        default_value = ir_values_obj.get(cr, uid, 'default', False, ['res.partner'])
         if not default_value:
-            values_obj.set(cr, uid, 'default', False, 'lang', ['res.partner'], lang)
+            ir_values_obj.set(cr, uid, 'default', False, 'lang', ['res.partner'], lang)
         return True
 
     def load_lang(self, cr, uid, lang, lang_name=None):
         # create the language with locale information
         fail = True
-        logger = logging.getLogger('i18n')
         iso_lang = tools.get_iso_codes(lang)
         for ln in tools.get_locales(lang):
             try:
@@ -64,7 +74,7 @@ class lang(osv.osv):
         if fail:
             lc = locale.getdefaultlocale()[0]
             msg = 'Unable to get information for locale %s. Information from the default locale (%s) have been used.'
-            logger.warning(msg, lang, lc)
+            _logger.warning(msg, lang, lc)
 
         if not lang_name:
             lang_name = tools.get_languages().get(lang, lang)
@@ -113,10 +123,10 @@ class lang(osv.osv):
                     return False
         return True
 
-    def _get_default_date_format(self,cursor,user,context={}):
+    def _get_default_date_format(self, cursor, user, context=None):
         return '%m/%d/%Y'
 
-    def _get_default_time_format(self,cursor,user,context={}):
+    def _get_default_time_format(self, cursor, user, context=None):
         return '%H:%M:%S'
 
     _columns = {
@@ -151,7 +161,7 @@ class lang(osv.osv):
         (_check_format, 'Invalid date/time format directive specified. Please refer to the list of allowed directives, displayed when you edit a language.', ['time_format', 'date_format'])
     ]
 
-    @tools.cache(skiparg=3)
+    @tools.ormcache(skiparg=3)
     def _lang_data_get(self, cr, uid, lang_id, monetary=False):
         conv = localeconv()
         lang_obj = self.browse(cr, uid, lang_id)
@@ -162,7 +172,7 @@ class lang(osv.osv):
 
     def write(self, cr, uid, ids, vals, context=None):
         for lang_id in ids :
-            self._lang_data_get.clear_cache(cr.dbname,lang_id= lang_id)
+            self._lang_data_get.clear_cache(self)
         return super(lang, self).write(cr, uid, ids, vals, context)
 
     def unlink(self, cr, uid, ids, context=None):
@@ -182,7 +192,7 @@ class lang(osv.osv):
             trans_obj.unlink(cr, uid, trans_ids, context=context)
         return super(lang, self).unlink(cr, uid, ids, context=context)
 
-    def format(self, cr, uid, ids, percent, value, grouping=False, monetary=False):
+    def format(self, cr, uid, ids, percent, value, grouping=False, monetary=False, context=None):
         """ Format() will return the language-specific output for float values"""
 
         if percent[0] != '%':
@@ -257,27 +267,6 @@ def original_group(s, grouping, thousands_sep=''):
         seps += 1
     return result + spaces, seps
 
-def take_left_padding(string):
-    """
-    >>> take_left_padding("   hello world ")
-    ('   ', 'hello world ')
-
-    """
-    padding = ''.join(itertools.takewhile(lambda c: c.isspace(), string))
-    rest = string[len(padding):]
-    return padding, rest
-
-def take_right_padding(string):
-    """
-
-    >>> take_right_padding("   hello world ")
-    (' ', '   hello world')
-
-    """
-    def reverse(s): return s[::-1]
-    padding, rest = take_left_padding(reverse(string))
-    return reverse(padding), reverse(rest)
-
 def split(l, counts):
     """
 
@@ -298,6 +287,8 @@ def split(l, counts):
     res = []
     saved_count = len(l) # count to use when encoutering a zero
     for count in counts:
+        if not l:
+            break
         if count == -1:
             break
         if count == 0:
@@ -312,20 +303,19 @@ def split(l, counts):
         res.append(l)
     return res
 
+intersperse_pat = re.compile('([^0-9]*)([^ ]*)(.*)')
+
 def intersperse(string, counts, separator=''):
     """
 
     See the asserts below for examples.
 
     """
-    left_padding, rest = take_left_padding(string)
-    padding = ''.join(itertools.takewhile(lambda c: not c.isdigit(), rest))
-    rest = rest[len(padding):]
-    right_padding, rest = take_right_padding(rest)
+    left, rest, right = intersperse_pat.match(string).groups()
     def reverse(s): return s[::-1]
     splits = split(reverse(rest), counts)
     res = separator.join(map(reverse, reverse(splits)))
-    return left_padding + padding + res + right_padding, len(splits) > 0 and len(splits) -1 or 0
+    return left + res + right, len(splits) > 0 and len(splits) -1 or 0
 
 # TODO rewrite this with a unit test library
 def _group_examples():
@@ -360,7 +350,7 @@ def _group_examples():
         assert g("12345678", [2,0,1], '.') == ('12.34.56.78', 3)
         assert g("12345678", [2,0,0], '.') == ('12.34.56.78', 3)
         assert g("12345678", [2,0,-1], '.') == ('12.34.56.78', 3)
-
+        assert g("12345678", [3,3,3,3], '.') == ('12.345.678', 2)
 
     assert original_group("abc1234567xy", [2], '.') == ('abc1234567.xy', 1)
     assert original_group("abc1234567xy8", [2], '.') == ('abc1234567xy8', 0) # difference here...