[MERGE] forward port of branch saas-3 up to d36eee9
[odoo/odoo.git] / openerp / tools / misc.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #    Copyright (C) 2010-2014 OpenERP s.a. (<http://openerp.com>).
7 #
8 #    This program is free software: you can redistribute it and/or modify
9 #    it under the terms of the GNU Affero General Public License as
10 #    published by the Free Software Foundation, either version 3 of the
11 #    License, or (at your option) any later version.
12 #
13 #    This program is distributed in the hope that it will be useful,
14 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
15 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 #    GNU Affero General Public License for more details.
17 #
18 #    You should have received a copy of the GNU Affero General Public License
19 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 ##############################################################################
22
23
24 """
25 Miscellaneous tools used by OpenERP.
26 """
27
28 from functools import wraps
29 import cProfile
30 from contextlib import contextmanager
31 import subprocess
32 import logging
33 import os
34 import socket
35 import sys
36 import threading
37 import time
38 import werkzeug.utils
39 import zipfile
40 from collections import defaultdict, Mapping
41 from datetime import datetime
42 from itertools import islice, izip, groupby
43 from lxml import etree
44 from which import which
45 from threading import local
46 import traceback
47
48 try:
49     from html2text import html2text
50 except ImportError:
51     html2text = None
52
53 from config import config
54 from cache import *
55 from .parse_version import parse_version 
56
57 import openerp
58 # get_encodings, ustr and exception_to_unicode were originally from tools.misc.
59 # There are moved to loglevels until we refactor tools.
60 from openerp.loglevels import get_encodings, ustr, exception_to_unicode     # noqa
61
62 _logger = logging.getLogger(__name__)
63
64 # List of etree._Element subclasses that we choose to ignore when parsing XML.
65 # We include the *Base ones just in case, currently they seem to be subclasses of the _* ones.
66 SKIPPED_ELEMENT_TYPES = (etree._Comment, etree._ProcessingInstruction, etree.CommentBase, etree.PIBase)
67
68 def find_in_path(name):
69     try:
70         return which(name)
71     except IOError:
72         return None
73
74 def find_pg_tool(name):
75     path = None
76     if config['pg_path'] and config['pg_path'] != 'None':
77         path = config['pg_path']
78     try:
79         return which(name, path=path)
80     except IOError:
81         return None
82
83 def exec_pg_command(name, *args):
84     prog = find_pg_tool(name)
85     if not prog:
86         raise Exception('Couldn\'t find %s' % name)
87     args2 = (prog,) + args
88
89     with open(os.devnull) as dn:
90         return subprocess.call(args2, stdout=dn, stderr=subprocess.STDOUT)
91
92 def exec_pg_command_pipe(name, *args):
93     prog = find_pg_tool(name)
94     if not prog:
95         raise Exception('Couldn\'t find %s' % name)
96     # on win32, passing close_fds=True is not compatible
97     # with redirecting std[in/err/out]
98     pop = subprocess.Popen((prog,) + args, bufsize= -1,
99           stdin=subprocess.PIPE, stdout=subprocess.PIPE,
100           close_fds=(os.name=="posix"))
101     return pop.stdin, pop.stdout
102
103 def exec_command_pipe(name, *args):
104     prog = find_in_path(name)
105     if not prog:
106         raise Exception('Couldn\'t find %s' % name)
107     # on win32, passing close_fds=True is not compatible
108     # with redirecting std[in/err/out]
109     pop = subprocess.Popen((prog,) + args, bufsize= -1,
110           stdin=subprocess.PIPE, stdout=subprocess.PIPE,
111           close_fds=(os.name=="posix"))
112     return pop.stdin, pop.stdout
113
114 #----------------------------------------------------------
115 # File paths
116 #----------------------------------------------------------
117 #file_path_root = os.getcwd()
118 #file_path_addons = os.path.join(file_path_root, 'addons')
119
120 def file_open(name, mode="r", subdir='addons', pathinfo=False):
121     """Open a file from the OpenERP root, using a subdir folder.
122
123     Example::
124     
125     >>> file_open('hr/report/timesheer.xsl')
126     >>> file_open('addons/hr/report/timesheet.xsl')
127     >>> file_open('../../base/report/rml_template.xsl', subdir='addons/hr/report', pathinfo=True)
128
129     @param name name of the file
130     @param mode file open mode
131     @param subdir subdirectory
132     @param pathinfo if True returns tuple (fileobject, filepath)
133
134     @return fileobject if pathinfo is False else (fileobject, filepath)
135     """
136     import openerp.modules as addons
137     adps = addons.module.ad_paths
138     rtp = os.path.normcase(os.path.abspath(config['root_path']))
139
140     basename = name
141
142     if os.path.isabs(name):
143         # It is an absolute path
144         # Is it below 'addons_path' or 'root_path'?
145         name = os.path.normcase(os.path.normpath(name))
146         for root in adps + [rtp]:
147             root = os.path.normcase(os.path.normpath(root)) + os.sep
148             if name.startswith(root):
149                 base = root.rstrip(os.sep)
150                 name = name[len(base) + 1:]
151                 break
152         else:
153             # It is outside the OpenERP root: skip zipfile lookup.
154             base, name = os.path.split(name)
155         return _fileopen(name, mode=mode, basedir=base, pathinfo=pathinfo, basename=basename)
156
157     if name.replace(os.sep, '/').startswith('addons/'):
158         subdir = 'addons'
159         name2 = name[7:]
160     elif subdir:
161         name = os.path.join(subdir, name)
162         if name.replace(os.sep, '/').startswith('addons/'):
163             subdir = 'addons'
164             name2 = name[7:]
165         else:
166             name2 = name
167
168     # First, try to locate in addons_path
169     if subdir:
170         for adp in adps:
171             try:
172                 return _fileopen(name2, mode=mode, basedir=adp,
173                                  pathinfo=pathinfo, basename=basename)
174             except IOError:
175                 pass
176
177     # Second, try to locate in root_path
178     return _fileopen(name, mode=mode, basedir=rtp, pathinfo=pathinfo, basename=basename)
179
180
181 def _fileopen(path, mode, basedir, pathinfo, basename=None):
182     name = os.path.normpath(os.path.join(basedir, path))
183
184     if basename is None:
185         basename = name
186     # Give higher priority to module directories, which is
187     # a more common case than zipped modules.
188     if os.path.isfile(name):
189         fo = open(name, mode)
190         if pathinfo:
191             return fo, name
192         return fo
193
194     # Support for loading modules in zipped form.
195     # This will not work for zipped modules that are sitting
196     # outside of known addons paths.
197     head = os.path.normpath(path)
198     zipname = False
199     while os.sep in head:
200         head, tail = os.path.split(head)
201         if not tail:
202             break
203         if zipname:
204             zipname = os.path.join(tail, zipname)
205         else:
206             zipname = tail
207         zpath = os.path.join(basedir, head + '.zip')
208         if zipfile.is_zipfile(zpath):
209             from cStringIO import StringIO
210             zfile = zipfile.ZipFile(zpath)
211             try:
212                 fo = StringIO()
213                 fo.write(zfile.read(os.path.join(
214                     os.path.basename(head), zipname).replace(
215                         os.sep, '/')))
216                 fo.seek(0)
217                 if pathinfo:
218                     return fo, name
219                 return fo
220             except Exception:
221                 pass
222     # Not found
223     if name.endswith('.rml'):
224         raise IOError('Report %r doesn\'t exist or deleted' % basename)
225     raise IOError('File not found: %s' % basename)
226
227
228 #----------------------------------------------------------
229 # iterables
230 #----------------------------------------------------------
231 def flatten(list):
232     """Flatten a list of elements into a uniqu list
233     Author: Christophe Simonis (christophe@tinyerp.com)
234
235     Examples::
236     >>> flatten(['a'])
237     ['a']
238     >>> flatten('b')
239     ['b']
240     >>> flatten( [] )
241     []
242     >>> flatten( [[], [[]]] )
243     []
244     >>> flatten( [[['a','b'], 'c'], 'd', ['e', [], 'f']] )
245     ['a', 'b', 'c', 'd', 'e', 'f']
246     >>> t = (1,2,(3,), [4, 5, [6, [7], (8, 9), ([10, 11, (12, 13)]), [14, [], (15,)], []]])
247     >>> flatten(t)
248     [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
249     """
250
251     def isiterable(x):
252         return hasattr(x, "__iter__")
253
254     r = []
255     for e in list:
256         if isiterable(e):
257             map(r.append, flatten(e))
258         else:
259             r.append(e)
260     return r
261
262 def reverse_enumerate(l):
263     """Like enumerate but in the other sens
264     
265     Usage::
266     >>> a = ['a', 'b', 'c']
267     >>> it = reverse_enumerate(a)
268     >>> it.next()
269     (2, 'c')
270     >>> it.next()
271     (1, 'b')
272     >>> it.next()
273     (0, 'a')
274     >>> it.next()
275     Traceback (most recent call last):
276       File "<stdin>", line 1, in <module>
277     StopIteration
278     """
279     return izip(xrange(len(l)-1, -1, -1), reversed(l))
280
281 def topological_sort(elems):
282     """ Return a list of elements sorted so that their dependencies are listed
283     before them in the result.
284
285     :param elems: specifies the elements to sort with their dependencies; it is
286         a dictionary like `{element: dependencies}` where `dependencies` is a
287         collection of elements that must appear before `element`. The elements
288         of `dependencies` are not required to appear in `elems`; they will
289         simply not appear in the result.
290
291     :returns: a list with the keys of `elems` sorted according to their
292         specification.
293     """
294     # the algorithm is inspired by [Tarjan 1976],
295     # http://en.wikipedia.org/wiki/Topological_sorting#Algorithms
296     result = []
297     visited = set()
298
299     def visit(n):
300         if n not in visited:
301             visited.add(n)
302             if n in elems:
303                 # first visit all dependencies of n, then append n to result
304                 map(visit, elems[n])
305                 result.append(n)
306
307     map(visit, elems)
308
309     return result
310
311
312 class UpdateableStr(local):
313     """ Class that stores an updateable string (used in wizards)
314     """
315
316     def __init__(self, string=''):
317         self.string = string
318
319     def __str__(self):
320         return str(self.string)
321
322     def __repr__(self):
323         return str(self.string)
324
325     def __nonzero__(self):
326         return bool(self.string)
327
328
329 class UpdateableDict(local):
330     """Stores an updateable dict to use in wizards
331     """
332
333     def __init__(self, dict=None):
334         if dict is None:
335             dict = {}
336         self.dict = dict
337
338     def __str__(self):
339         return str(self.dict)
340
341     def __repr__(self):
342         return str(self.dict)
343
344     def clear(self):
345         return self.dict.clear()
346
347     def keys(self):
348         return self.dict.keys()
349
350     def __setitem__(self, i, y):
351         self.dict.__setitem__(i, y)
352
353     def __getitem__(self, i):
354         return self.dict.__getitem__(i)
355
356     def copy(self):
357         return self.dict.copy()
358
359     def iteritems(self):
360         return self.dict.iteritems()
361
362     def iterkeys(self):
363         return self.dict.iterkeys()
364
365     def itervalues(self):
366         return self.dict.itervalues()
367
368     def pop(self, k, d=None):
369         return self.dict.pop(k, d)
370
371     def popitem(self):
372         return self.dict.popitem()
373
374     def setdefault(self, k, d=None):
375         return self.dict.setdefault(k, d)
376
377     def update(self, E, **F):
378         return self.dict.update(E, F)
379
380     def values(self):
381         return self.dict.values()
382
383     def get(self, k, d=None):
384         return self.dict.get(k, d)
385
386     def has_key(self, k):
387         return self.dict.has_key(k)
388
389     def items(self):
390         return self.dict.items()
391
392     def __cmp__(self, y):
393         return self.dict.__cmp__(y)
394
395     def __contains__(self, k):
396         return self.dict.__contains__(k)
397
398     def __delitem__(self, y):
399         return self.dict.__delitem__(y)
400
401     def __eq__(self, y):
402         return self.dict.__eq__(y)
403
404     def __ge__(self, y):
405         return self.dict.__ge__(y)
406
407     def __gt__(self, y):
408         return self.dict.__gt__(y)
409
410     def __hash__(self):
411         return self.dict.__hash__()
412
413     def __iter__(self):
414         return self.dict.__iter__()
415
416     def __le__(self, y):
417         return self.dict.__le__(y)
418
419     def __len__(self):
420         return self.dict.__len__()
421
422     def __lt__(self, y):
423         return self.dict.__lt__(y)
424
425     def __ne__(self, y):
426         return self.dict.__ne__(y)
427
428 class currency(float):
429     """ Deprecate
430     
431     .. warning::
432     
433     Don't use ! Use res.currency.round()
434     """
435
436     def __init__(self, value, accuracy=2, rounding=None):
437         if rounding is None:
438             rounding=10**-accuracy
439         self.rounding=rounding
440         self.accuracy=accuracy
441
442     def __new__(cls, value, accuracy=2, rounding=None):
443         return float.__new__(cls, round(value, accuracy))
444
445     #def __str__(self):
446     #   display_value = int(self*(10**(-self.accuracy))/self.rounding)*self.rounding/(10**(-self.accuracy))
447     #   return str(display_value)
448
449 def to_xml(s):
450     return s.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')
451
452 def get_iso_codes(lang):
453     if lang.find('_') != -1:
454         if lang.split('_')[0] == lang.split('_')[1].lower():
455             lang = lang.split('_')[0]
456     return lang
457
458 ALL_LANGUAGES = {
459         'ab_RU': u'Abkhazian / аҧсуа',
460         'am_ET': u'Amharic / አምሃርኛ',
461         'ar_SY': u'Arabic / الْعَرَبيّة',
462         'bg_BG': u'Bulgarian / български език',
463         'bs_BA': u'Bosnian / bosanski jezik',
464         'ca_ES': u'Catalan / Català',
465         'cs_CZ': u'Czech / Čeština',
466         'da_DK': u'Danish / Dansk',
467         'de_DE': u'German / Deutsch',
468         'el_GR': u'Greek / Ελληνικά',
469         'en_CA': u'English (CA)',
470         'en_GB': u'English (UK)',
471         'en_US': u'English (US)',
472         'es_AR': u'Spanish (AR) / Español (AR)',
473         'es_BO': u'Spanish (BO) / Español (BO)',
474         'es_CL': u'Spanish (CL) / Español (CL)',
475         'es_CO': u'Spanish (CO) / Español (CO)',
476         'es_CR': u'Spanish (CR) / Español (CR)',
477         'es_DO': u'Spanish (DO) / Español (DO)',
478         'es_EC': u'Spanish (EC) / Español (EC)',
479         'es_ES': u'Spanish / Español',
480         'es_GT': u'Spanish (GT) / Español (GT)',
481         'es_HN': u'Spanish (HN) / Español (HN)',
482         'es_MX': u'Spanish (MX) / Español (MX)',
483         'es_NI': u'Spanish (NI) / Español (NI)',
484         'es_PA': u'Spanish (PA) / Español (PA)',
485         'es_PE': u'Spanish (PE) / Español (PE)',
486         'es_PR': u'Spanish (PR) / Español (PR)',
487         'es_PY': u'Spanish (PY) / Español (PY)',
488         'es_SV': u'Spanish (SV) / Español (SV)',
489         'es_UY': u'Spanish (UY) / Español (UY)',
490         'es_VE': u'Spanish (VE) / Español (VE)',
491         'et_EE': u'Estonian / Eesti keel',
492         'fa_IR': u'Persian / فارس',
493         'fi_FI': u'Finnish / Suomi',
494         'fr_BE': u'French (BE) / Français (BE)',
495         'fr_CA': u'French (CA) / Français (CA)',
496         'fr_CH': u'French (CH) / Français (CH)',
497         'fr_FR': u'French / Français',
498         'gl_ES': u'Galician / Galego',
499         'gu_IN': u'Gujarati / ગુજરાતી',
500         'he_IL': u'Hebrew / עִבְרִי',
501         'hi_IN': u'Hindi / हिंदी',
502         'hr_HR': u'Croatian / hrvatski jezik',
503         'hu_HU': u'Hungarian / Magyar',
504         'id_ID': u'Indonesian / Bahasa Indonesia',
505         'it_IT': u'Italian / Italiano',
506         'iu_CA': u'Inuktitut / ᐃᓄᒃᑎᑐᑦ',
507         'ja_JP': u'Japanese / 日本語',
508         'ko_KP': u'Korean (KP) / 한국어 (KP)',
509         'ko_KR': u'Korean (KR) / 한국어 (KR)',
510         'lo_LA': u'Lao / ພາສາລາວ',
511         'lt_LT': u'Lithuanian / Lietuvių kalba',
512         'lv_LV': u'Latvian / latviešu valoda',
513         'mk_MK': u'Macedonian / македонски јазик',
514         'ml_IN': u'Malayalam / മലയാളം',
515         'mn_MN': u'Mongolian / монгол',
516         'nb_NO': u'Norwegian Bokmål / Norsk bokmål',
517         'nl_NL': u'Dutch / Nederlands',
518         'nl_BE': u'Flemish (BE) / Vlaams (BE)',
519         'oc_FR': u'Occitan (FR, post 1500) / Occitan',
520         'pl_PL': u'Polish / Język polski',
521         'pt_BR': u'Portuguese (BR) / Português (BR)',
522         'pt_PT': u'Portuguese / Português',
523         'ro_RO': u'Romanian / română',
524         'ru_RU': u'Russian / русский язык',
525         'si_LK': u'Sinhalese / සිංහල',
526         'sl_SI': u'Slovenian / slovenščina',
527         'sk_SK': u'Slovak / Slovenský jazyk',
528         'sq_AL': u'Albanian / Shqip',
529         'sr_RS': u'Serbian (Cyrillic) / српски',
530         'sr@latin': u'Serbian (Latin) / srpski',
531         'sv_SE': u'Swedish / svenska',
532         'te_IN': u'Telugu / తెలుగు',
533         'tr_TR': u'Turkish / Türkçe',
534         'vi_VN': u'Vietnamese / Tiếng Việt',
535         'uk_UA': u'Ukrainian / українська',
536         'ur_PK': u'Urdu / اردو',
537         'zh_CN': u'Chinese (CN) / 简体中文',
538         'zh_HK': u'Chinese (HK)',
539         'zh_TW': u'Chinese (TW) / 正體字',
540         'th_TH': u'Thai / ภาษาไทย',
541         'tlh_TLH': u'Klingon',
542     }
543
544 def scan_languages():
545     """ Returns all languages supported by OpenERP for translation
546
547     :returns: a list of (lang_code, lang_name) pairs
548     :rtype: [(str, unicode)]
549     """
550     return sorted(ALL_LANGUAGES.iteritems(), key=lambda k: k[1])
551
552 def get_user_companies(cr, user):
553     def _get_company_children(cr, ids):
554         if not ids:
555             return []
556         cr.execute('SELECT id FROM res_company WHERE parent_id IN %s', (tuple(ids),))
557         res = [x[0] for x in cr.fetchall()]
558         res.extend(_get_company_children(cr, res))
559         return res
560     cr.execute('SELECT company_id FROM res_users WHERE id=%s', (user,))
561     user_comp = cr.fetchone()[0]
562     if not user_comp:
563         return []
564     return [user_comp] + _get_company_children(cr, [user_comp])
565
566 def mod10r(number):
567     """
568     Input number : account or invoice number
569     Output return: the same number completed with the recursive mod10
570     key
571     """
572     codec=[0,9,4,6,8,2,7,1,3,5]
573     report = 0
574     result=""
575     for digit in number:
576         result += digit
577         if digit.isdigit():
578             report = codec[ (int(digit) + report) % 10 ]
579     return result + str((10 - report) % 10)
580
581
582 def human_size(sz):
583     """
584     Return the size in a human readable format
585     """
586     if not sz:
587         return False
588     units = ('bytes', 'Kb', 'Mb', 'Gb')
589     if isinstance(sz,basestring):
590         sz=len(sz)
591     s, i = float(sz), 0
592     while s >= 1024 and i < len(units)-1:
593         s /= 1024
594         i += 1
595     return "%0.2f %s" % (s, units[i])
596
597 def logged(f):
598     @wraps(f)
599     def wrapper(*args, **kwargs):
600         from pprint import pformat
601
602         vector = ['Call -> function: %r' % f]
603         for i, arg in enumerate(args):
604             vector.append('  arg %02d: %s' % (i, pformat(arg)))
605         for key, value in kwargs.items():
606             vector.append('  kwarg %10s: %s' % (key, pformat(value)))
607
608         timeb4 = time.time()
609         res = f(*args, **kwargs)
610
611         vector.append('  result: %s' % pformat(res))
612         vector.append('  time delta: %s' % (time.time() - timeb4))
613         _logger.debug('\n'.join(vector))
614         return res
615
616     return wrapper
617
618 class profile(object):
619     def __init__(self, fname=None):
620         self.fname = fname
621
622     def __call__(self, f):
623         @wraps(f)
624         def wrapper(*args, **kwargs):
625             profile = cProfile.Profile()
626             result = profile.runcall(f, *args, **kwargs)
627             profile.dump_stats(self.fname or ("%s.cprof" % (f.func_name,)))
628             return result
629
630         return wrapper
631
632 __icons_list = ['STOCK_ABOUT', 'STOCK_ADD', 'STOCK_APPLY', 'STOCK_BOLD',
633 'STOCK_CANCEL', 'STOCK_CDROM', 'STOCK_CLEAR', 'STOCK_CLOSE', 'STOCK_COLOR_PICKER',
634 'STOCK_CONNECT', 'STOCK_CONVERT', 'STOCK_COPY', 'STOCK_CUT', 'STOCK_DELETE',
635 'STOCK_DIALOG_AUTHENTICATION', 'STOCK_DIALOG_ERROR', 'STOCK_DIALOG_INFO',
636 'STOCK_DIALOG_QUESTION', 'STOCK_DIALOG_WARNING', 'STOCK_DIRECTORY', 'STOCK_DISCONNECT',
637 'STOCK_DND', 'STOCK_DND_MULTIPLE', 'STOCK_EDIT', 'STOCK_EXECUTE', 'STOCK_FILE',
638 'STOCK_FIND', 'STOCK_FIND_AND_REPLACE', 'STOCK_FLOPPY', 'STOCK_GOTO_BOTTOM',
639 'STOCK_GOTO_FIRST', 'STOCK_GOTO_LAST', 'STOCK_GOTO_TOP', 'STOCK_GO_BACK',
640 'STOCK_GO_DOWN', 'STOCK_GO_FORWARD', 'STOCK_GO_UP', 'STOCK_HARDDISK',
641 'STOCK_HELP', 'STOCK_HOME', 'STOCK_INDENT', 'STOCK_INDEX', 'STOCK_ITALIC',
642 'STOCK_JUMP_TO', 'STOCK_JUSTIFY_CENTER', 'STOCK_JUSTIFY_FILL',
643 'STOCK_JUSTIFY_LEFT', 'STOCK_JUSTIFY_RIGHT', 'STOCK_MEDIA_FORWARD',
644 'STOCK_MEDIA_NEXT', 'STOCK_MEDIA_PAUSE', 'STOCK_MEDIA_PLAY',
645 'STOCK_MEDIA_PREVIOUS', 'STOCK_MEDIA_RECORD', 'STOCK_MEDIA_REWIND',
646 'STOCK_MEDIA_STOP', 'STOCK_MISSING_IMAGE', 'STOCK_NETWORK', 'STOCK_NEW',
647 'STOCK_NO', 'STOCK_OK', 'STOCK_OPEN', 'STOCK_PASTE', 'STOCK_PREFERENCES',
648 'STOCK_PRINT', 'STOCK_PRINT_PREVIEW', 'STOCK_PROPERTIES', 'STOCK_QUIT',
649 'STOCK_REDO', 'STOCK_REFRESH', 'STOCK_REMOVE', 'STOCK_REVERT_TO_SAVED',
650 'STOCK_SAVE', 'STOCK_SAVE_AS', 'STOCK_SELECT_COLOR', 'STOCK_SELECT_FONT',
651 'STOCK_SORT_ASCENDING', 'STOCK_SORT_DESCENDING', 'STOCK_SPELL_CHECK',
652 'STOCK_STOP', 'STOCK_STRIKETHROUGH', 'STOCK_UNDELETE', 'STOCK_UNDERLINE',
653 'STOCK_UNDO', 'STOCK_UNINDENT', 'STOCK_YES', 'STOCK_ZOOM_100',
654 'STOCK_ZOOM_FIT', 'STOCK_ZOOM_IN', 'STOCK_ZOOM_OUT',
655 'terp-account', 'terp-crm', 'terp-mrp', 'terp-product', 'terp-purchase',
656 'terp-sale', 'terp-tools', 'terp-administration', 'terp-hr', 'terp-partner',
657 'terp-project', 'terp-report', 'terp-stock', 'terp-calendar', 'terp-graph',
658 'terp-check','terp-go-month','terp-go-year','terp-go-today','terp-document-new','terp-camera_test',
659 'terp-emblem-important','terp-gtk-media-pause','terp-gtk-stop','terp-gnome-cpu-frequency-applet+',
660 'terp-dialog-close','terp-gtk-jump-to-rtl','terp-gtk-jump-to-ltr','terp-accessories-archiver',
661 'terp-stock_align_left_24','terp-stock_effects-object-colorize','terp-go-home','terp-gtk-go-back-rtl',
662 'terp-gtk-go-back-ltr','terp-personal','terp-personal-','terp-personal+','terp-accessories-archiver-minus',
663 'terp-accessories-archiver+','terp-stock_symbol-selection','terp-call-start','terp-dolar',
664 'terp-face-plain','terp-folder-blue','terp-folder-green','terp-folder-orange','terp-folder-yellow',
665 'terp-gdu-smart-failing','terp-go-week','terp-gtk-select-all','terp-locked','terp-mail-forward',
666 'terp-mail-message-new','terp-mail-replied','terp-rating-rated','terp-stage','terp-stock_format-scientific',
667 'terp-dolar_ok!','terp-idea','terp-stock_format-default','terp-mail-','terp-mail_delete'
668 ]
669
670 def icons(*a, **kw):
671     global __icons_list
672     return [(x, x) for x in __icons_list ]
673
674 def detect_ip_addr():
675     """Try a very crude method to figure out a valid external
676        IP or hostname for the current machine. Don't rely on this
677        for binding to an interface, but it could be used as basis
678        for constructing a remote URL to the server.
679     """
680     def _detect_ip_addr():
681         from array import array
682         from struct import pack, unpack
683
684         try:
685             import fcntl
686         except ImportError:
687             fcntl = None
688
689         ip_addr = None
690
691         if not fcntl: # not UNIX:
692             host = socket.gethostname()
693             ip_addr = socket.gethostbyname(host)
694         else: # UNIX:
695             # get all interfaces:
696             nbytes = 128 * 32
697             s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
698             names = array('B', '\0' * nbytes)
699             #print 'names: ', names
700             outbytes = unpack('iL', fcntl.ioctl( s.fileno(), 0x8912, pack('iL', nbytes, names.buffer_info()[0])))[0]
701             namestr = names.tostring()
702
703             # try 64 bit kernel:
704             for i in range(0, outbytes, 40):
705                 name = namestr[i:i+16].split('\0', 1)[0]
706                 if name != 'lo':
707                     ip_addr = socket.inet_ntoa(namestr[i+20:i+24])
708                     break
709
710             # try 32 bit kernel:
711             if ip_addr is None:
712                 ifaces = filter(None, [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)])
713
714                 for ifname in [iface for iface in ifaces if iface != 'lo']:
715                     ip_addr = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, pack('256s', ifname[:15]))[20:24])
716                     break
717
718         return ip_addr or 'localhost'
719
720     try:
721         ip_addr = _detect_ip_addr()
722     except Exception:
723         ip_addr = 'localhost'
724     return ip_addr
725
726 # RATIONALE BEHIND TIMESTAMP CALCULATIONS AND TIMEZONE MANAGEMENT:
727 #  The server side never does any timestamp calculation, always
728 #  sends them in a naive (timezone agnostic) format supposed to be
729 #  expressed within the server timezone, and expects the clients to
730 #  provide timestamps in the server timezone as well.
731 #  It stores all timestamps in the database in naive format as well,
732 #  which also expresses the time in the server timezone.
733 #  For this reason the server makes its timezone name available via the
734 #  common/timezone_get() rpc method, which clients need to read
735 #  to know the appropriate time offset to use when reading/writing
736 #  times.
737 def get_win32_timezone():
738     """Attempt to return the "standard name" of the current timezone on a win32 system.
739        @return the standard name of the current win32 timezone, or False if it cannot be found.
740     """
741     res = False
742     if sys.platform == "win32":
743         try:
744             import _winreg
745             hklm = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
746             current_tz_key = _winreg.OpenKey(hklm, r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation", 0,_winreg.KEY_ALL_ACCESS)
747             res = str(_winreg.QueryValueEx(current_tz_key,"StandardName")[0])  # [0] is value, [1] is type code
748             _winreg.CloseKey(current_tz_key)
749             _winreg.CloseKey(hklm)
750         except Exception:
751             pass
752     return res
753
754 def detect_server_timezone():
755     """Attempt to detect the timezone to use on the server side.
756        Defaults to UTC if no working timezone can be found.
757        @return the timezone identifier as expected by pytz.timezone.
758     """
759     try:
760         import pytz
761     except Exception:
762         _logger.warning("Python pytz module is not available. "
763             "Timezone will be set to UTC by default.")
764         return 'UTC'
765
766     # Option 1: the configuration option (did not exist before, so no backwards compatibility issue)
767     # Option 2: to be backwards compatible with 5.0 or earlier, the value from time.tzname[0], but only if it is known to pytz
768     # Option 3: the environment variable TZ
769     sources = [ (config['timezone'], 'OpenERP configuration'),
770                 (time.tzname[0], 'time.tzname'),
771                 (os.environ.get('TZ',False),'TZ environment variable'), ]
772     # Option 4: OS-specific: /etc/timezone on Unix
773     if os.path.exists("/etc/timezone"):
774         tz_value = False
775         try:
776             f = open("/etc/timezone")
777             tz_value = f.read(128).strip()
778         except Exception:
779             pass
780         finally:
781             f.close()
782         sources.append((tz_value,"/etc/timezone file"))
783     # Option 5: timezone info from registry on Win32
784     if sys.platform == "win32":
785         # Timezone info is stored in windows registry.
786         # However this is not likely to work very well as the standard name
787         # of timezones in windows is rarely something that is known to pytz.
788         # But that's ok, it is always possible to use a config option to set
789         # it explicitly.
790         sources.append((get_win32_timezone(),"Windows Registry"))
791
792     for (value,source) in sources:
793         if value:
794             try:
795                 tz = pytz.timezone(value)
796                 _logger.info("Using timezone %s obtained from %s.", tz.zone, source)
797                 return value
798             except pytz.UnknownTimeZoneError:
799                 _logger.warning("The timezone specified in %s (%s) is invalid, ignoring it.", source, value)
800
801     _logger.warning("No valid timezone could be detected, using default UTC "
802         "timezone. You can specify it explicitly with option 'timezone' in "
803         "the server configuration.")
804     return 'UTC'
805
806 def get_server_timezone():
807     return "UTC"
808
809
810 DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
811 DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
812 DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
813     DEFAULT_SERVER_DATE_FORMAT,
814     DEFAULT_SERVER_TIME_FORMAT)
815
816 # Python's strftime supports only the format directives
817 # that are available on the platform's libc, so in order to
818 # be cross-platform we map to the directives required by
819 # the C standard (1989 version), always available on platforms
820 # with a C standard implementation.
821 DATETIME_FORMATS_MAP = {
822         '%C': '', # century
823         '%D': '%m/%d/%Y', # modified %y->%Y
824         '%e': '%d',
825         '%E': '', # special modifier
826         '%F': '%Y-%m-%d',
827         '%g': '%Y', # modified %y->%Y
828         '%G': '%Y',
829         '%h': '%b',
830         '%k': '%H',
831         '%l': '%I',
832         '%n': '\n',
833         '%O': '', # special modifier
834         '%P': '%p',
835         '%R': '%H:%M',
836         '%r': '%I:%M:%S %p',
837         '%s': '', #num of seconds since epoch
838         '%T': '%H:%M:%S',
839         '%t': ' ', # tab
840         '%u': ' %w',
841         '%V': '%W',
842         '%y': '%Y', # Even if %y works, it's ambiguous, so we should use %Y
843         '%+': '%Y-%m-%d %H:%M:%S',
844
845         # %Z is a special case that causes 2 problems at least:
846         #  - the timezone names we use (in res_user.context_tz) come
847         #    from pytz, but not all these names are recognized by
848         #    strptime(), so we cannot convert in both directions
849         #    when such a timezone is selected and %Z is in the format
850         #  - %Z is replaced by an empty string in strftime() when
851         #    there is not tzinfo in a datetime value (e.g when the user
852         #    did not pick a context_tz). The resulting string does not
853         #    parse back if the format requires %Z.
854         # As a consequence, we strip it completely from format strings.
855         # The user can always have a look at the context_tz in
856         # preferences to check the timezone.
857         '%z': '',
858         '%Z': '',
859 }
860
861 POSIX_TO_LDML = {
862     'a': 'E',
863     'A': 'EEEE',
864     'b': 'MMM',
865     'B': 'MMMM',
866     #'c': '',
867     'd': 'dd',
868     'H': 'HH',
869     'I': 'hh',
870     'j': 'DDD',
871     'm': 'MM',
872     'M': 'mm',
873     'p': 'a',
874     'S': 'ss',
875     'U': 'w',
876     'w': 'e',
877     'W': 'w',
878     'y': 'yy',
879     'Y': 'yyyy',
880     # see comments above, and babel's format_datetime assumes an UTC timezone
881     # for naive datetime objects
882     #'z': 'Z',
883     #'Z': 'z',
884 }
885
886 def posix_to_ldml(fmt, locale):
887     """ Converts a posix/strftime pattern into an LDML date format pattern.
888
889     :param fmt: non-extended C89/C90 strftime pattern
890     :param locale: babel locale used for locale-specific conversions (e.g. %x and %X)
891     :return: unicode
892     """
893     buf = []
894     pc = False
895     quoted = []
896
897     for c in fmt:
898         # LDML date format patterns uses letters, so letters must be quoted
899         if not pc and c.isalpha():
900             quoted.append(c if c != "'" else "''")
901             continue
902         if quoted:
903             buf.append("'")
904             buf.append(''.join(quoted))
905             buf.append("'")
906             quoted = []
907
908         if pc:
909             if c == '%': # escaped percent
910                 buf.append('%')
911             elif c == 'x': # date format, short seems to match
912                 buf.append(locale.date_formats['short'].pattern)
913             elif c == 'X': # time format, seems to include seconds. short does not
914                 buf.append(locale.time_formats['medium'].pattern)
915             else: # look up format char in static mapping
916                 buf.append(POSIX_TO_LDML[c])
917             pc = False
918         elif c == '%':
919             pc = True
920         else:
921             buf.append(c)
922
923     # flush anything remaining in quoted buffer
924     if quoted:
925         buf.append("'")
926         buf.append(''.join(quoted))
927         buf.append("'")
928
929     return ''.join(buf)
930
931 def server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name,
932         tz_offset=True, ignore_unparsable_time=True):
933     """
934     Convert a source timestamp string into a destination timestamp string, attempting to apply the
935     correct offset if both the server and local timezone are recognized, or no
936     offset at all if they aren't or if tz_offset is false (i.e. assuming they are both in the same TZ).
937
938     WARNING: This method is here to allow formatting dates correctly for inclusion in strings where
939              the client would not be able to format/offset it correctly. DO NOT use it for returning
940              date fields directly, these are supposed to be handled by the client!!
941
942     @param src_tstamp_str: the str value containing the timestamp in the server timezone.
943     @param src_format: the format to use when parsing the server timestamp.
944     @param dst_format: the format to use when formatting the resulting timestamp for the local/client timezone.
945     @param dst_tz_name: name of the destination timezone (such as the 'tz' value of the client context)
946     @param ignore_unparsable_time: if True, return False if src_tstamp_str cannot be parsed
947                                    using src_format or formatted using dst_format.
948
949     @return local/client formatted timestamp, expressed in the local/client timezone if possible
950             and if tz_offset is true, or src_tstamp_str if timezone offset could not be determined.
951     """
952     if not src_tstamp_str:
953         return False
954
955     res = src_tstamp_str
956     if src_format and dst_format:
957         # find out server timezone
958         server_tz = get_server_timezone()
959         try:
960             # dt_value needs to be a datetime.datetime object (so no time.struct_time or mx.DateTime.DateTime here!)
961             dt_value = datetime.strptime(src_tstamp_str, src_format)
962             if tz_offset and dst_tz_name:
963                 try:
964                     import pytz
965                     src_tz = pytz.timezone(server_tz)
966                     dst_tz = pytz.timezone(dst_tz_name)
967                     src_dt = src_tz.localize(dt_value, is_dst=True)
968                     dt_value = src_dt.astimezone(dst_tz)
969                 except Exception:
970                     pass
971             res = dt_value.strftime(dst_format)
972         except Exception:
973             # Normal ways to end up here are if strptime or strftime failed
974             if not ignore_unparsable_time:
975                 return False
976     return res
977
978
979 def split_every(n, iterable, piece_maker=tuple):
980     """Splits an iterable into length-n pieces. The last piece will be shorter
981        if ``n`` does not evenly divide the iterable length.
982        @param ``piece_maker``: function to build the pieces
983        from the slices (tuple,list,...)
984     """
985     iterator = iter(iterable)
986     piece = piece_maker(islice(iterator, n))
987     while piece:
988         yield piece
989         piece = piece_maker(islice(iterator, n))
990
991 if __name__ == '__main__':
992     import doctest
993     doctest.testmod()
994
995 class upload_data_thread(threading.Thread):
996     def __init__(self, email, data, type):
997         self.args = [('email',email),('type',type),('data',data)]
998         super(upload_data_thread,self).__init__()
999     def run(self):
1000         try:
1001             import urllib
1002             args = urllib.urlencode(self.args)
1003             fp = urllib.urlopen('http://www.openerp.com/scripts/survey.php', args)
1004             fp.read()
1005             fp.close()
1006         except Exception:
1007             pass
1008
1009 def upload_data(email, data, type='SURVEY'):
1010     a = upload_data_thread(email, data, type)
1011     a.start()
1012     return True
1013
1014 def get_and_group_by_field(cr, uid, obj, ids, field, context=None):
1015     """ Read the values of ``field´´ for the given ``ids´´ and group ids by value.
1016
1017        :param string field: name of the field we want to read and group by
1018        :return: mapping of field values to the list of ids that have it
1019        :rtype: dict
1020     """
1021     res = {}
1022     for record in obj.read(cr, uid, ids, [field], context=context):
1023         key = record[field]
1024         res.setdefault(key[0] if isinstance(key, tuple) else key, []).append(record['id'])
1025     return res
1026
1027 def get_and_group_by_company(cr, uid, obj, ids, context=None):
1028     return get_and_group_by_field(cr, uid, obj, ids, field='company_id', context=context)
1029
1030 # port of python 2.6's attrgetter with support for dotted notation
1031 def resolve_attr(obj, attr):
1032     for name in attr.split("."):
1033         obj = getattr(obj, name)
1034     return obj
1035
1036 def attrgetter(*items):
1037     if len(items) == 1:
1038         attr = items[0]
1039         def g(obj):
1040             return resolve_attr(obj, attr)
1041     else:
1042         def g(obj):
1043             return tuple(resolve_attr(obj, attr) for attr in items)
1044     return g
1045
1046 class unquote(str):
1047     """A subclass of str that implements repr() without enclosing quotation marks
1048        or escaping, keeping the original string untouched. The name come from Lisp's unquote.
1049        One of the uses for this is to preserve or insert bare variable names within dicts during eval()
1050        of a dict's repr(). Use with care.
1051
1052        Some examples (notice that there are never quotes surrounding
1053        the ``active_id`` name:
1054
1055        >>> unquote('active_id')
1056        active_id
1057        >>> d = {'test': unquote('active_id')}
1058        >>> d
1059        {'test': active_id}
1060        >>> print d
1061        {'test': active_id}
1062     """
1063     def __repr__(self):
1064         return self
1065
1066 class UnquoteEvalContext(defaultdict):
1067     """Defaultdict-based evaluation context that returns 
1068        an ``unquote`` string for any missing name used during
1069        the evaluation.
1070        Mostly useful for evaluating OpenERP domains/contexts that
1071        may refer to names that are unknown at the time of eval,
1072        so that when the context/domain is converted back to a string,
1073        the original names are preserved.
1074
1075        **Warning**: using an ``UnquoteEvalContext`` as context for ``eval()`` or
1076        ``safe_eval()`` will shadow the builtins, which may cause other
1077        failures, depending on what is evaluated.
1078
1079        Example (notice that ``section_id`` is preserved in the final
1080        result) :
1081
1082        >>> context_str = "{'default_user_id': uid, 'default_section_id': section_id}"
1083        >>> eval(context_str, UnquoteEvalContext(uid=1))
1084        {'default_user_id': 1, 'default_section_id': section_id}
1085
1086        """
1087     def __init__(self, *args, **kwargs):
1088         super(UnquoteEvalContext, self).__init__(None, *args, **kwargs)
1089
1090     def __missing__(self, key):
1091         return unquote(key)
1092
1093
1094 class mute_logger(object):
1095     """Temporary suppress the logging.
1096     Can be used as context manager or decorator.
1097
1098         @mute_logger('openerp.plic.ploc')
1099         def do_stuff():
1100             blahblah()
1101
1102         with mute_logger('openerp.foo.bar'):
1103             do_suff()
1104
1105     """
1106     def __init__(self, *loggers):
1107         self.loggers = loggers
1108
1109     def filter(self, record):
1110         return 0
1111
1112     def __enter__(self):
1113         for logger in self.loggers:
1114             assert isinstance(logger, basestring),\
1115                 "A logger name must be a string, got %s" % type(logger)
1116             logging.getLogger(logger).addFilter(self)
1117
1118     def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
1119         for logger in self.loggers:
1120             logging.getLogger(logger).removeFilter(self)
1121
1122     def __call__(self, func):
1123         @wraps(func)
1124         def deco(*args, **kwargs):
1125             with self:
1126                 return func(*args, **kwargs)
1127         return deco
1128
1129 _ph = object()
1130 class CountingStream(object):
1131     """ Stream wrapper counting the number of element it has yielded. Similar
1132     role to ``enumerate``, but for use when the iteration process of the stream
1133     isn't fully under caller control (the stream can be iterated from multiple
1134     points including within a library)
1135
1136     ``start`` allows overriding the starting index (the index before the first
1137     item is returned).
1138
1139     On each iteration (call to :meth:`~.next`), increases its :attr:`~.index`
1140     by one.
1141
1142     .. attribute:: index
1143
1144         ``int``, index of the last yielded element in the stream. If the stream
1145         has ended, will give an index 1-past the stream
1146     """
1147     def __init__(self, stream, start=-1):
1148         self.stream = iter(stream)
1149         self.index = start
1150         self.stopped = False
1151     def __iter__(self):
1152         return self
1153     def next(self):
1154         if self.stopped: raise StopIteration()
1155         self.index += 1
1156         val = next(self.stream, _ph)
1157         if val is _ph:
1158             self.stopped = True
1159             raise StopIteration()
1160         return val
1161
1162 def stripped_sys_argv(*strip_args):
1163     """Return sys.argv with some arguments stripped, suitable for reexecution or subprocesses"""
1164     strip_args = sorted(set(strip_args) | set(['-s', '--save', '-d', '--database', '-u', '--update', '-i', '--init']))
1165     assert all(config.parser.has_option(s) for s in strip_args)
1166     takes_value = dict((s, config.parser.get_option(s).takes_value()) for s in strip_args)
1167
1168     longs, shorts = list(tuple(y) for _, y in groupby(strip_args, lambda x: x.startswith('--')))
1169     longs_eq = tuple(l + '=' for l in longs if takes_value[l])
1170
1171     args = sys.argv[:]
1172
1173     def strip(args, i):
1174         return args[i].startswith(shorts) \
1175             or args[i].startswith(longs_eq) or (args[i] in longs) \
1176             or (i >= 1 and (args[i - 1] in strip_args) and takes_value[args[i - 1]])
1177
1178     return [x for i, x in enumerate(args) if not strip(args, i)]
1179
1180
1181 class ConstantMapping(Mapping):
1182     """
1183     An immutable mapping returning the provided value for every single key.
1184
1185     Useful for default value to methods
1186     """
1187     __slots__ = ['_value']
1188     def __init__(self, val):
1189         self._value = val
1190
1191     def __len__(self):
1192         """
1193         defaultdict updates its length for each individually requested key, is
1194         that really useful?
1195         """
1196         return 0
1197
1198     def __iter__(self):
1199         """
1200         same as len, defaultdict udpates its iterable keyset with each key
1201         requested, is there a point for this?
1202         """
1203         return iter([])
1204
1205     def __getitem__(self, item):
1206         return self._value
1207
1208
1209 def dumpstacks(sig=None, frame=None):
1210     """ Signal handler: dump a stack trace for each existing thread."""
1211     code = []
1212
1213     def extract_stack(stack):
1214         for filename, lineno, name, line in traceback.extract_stack(stack):
1215             yield 'File: "%s", line %d, in %s' % (filename, lineno, name)
1216             if line:
1217                 yield "  %s" % (line.strip(),)
1218
1219     # code from http://stackoverflow.com/questions/132058/getting-stack-trace-from-a-running-python-application#answer-2569696
1220     # modified for python 2.5 compatibility
1221     threads_info = dict([(th.ident, {'name': th.name, 'uid': getattr(th, 'uid', 'n/a')})
1222                         for th in threading.enumerate()])
1223     for threadId, stack in sys._current_frames().items():
1224         thread_info = threads_info.get(threadId)
1225         code.append("\n# Thread: %s (id:%s) (uid:%s)" %
1226                     (thread_info and thread_info['name'] or 'n/a',
1227                      threadId,
1228                      thread_info and thread_info['uid'] or 'n/a'))
1229         for line in extract_stack(stack):
1230             code.append(line)
1231
1232     if openerp.evented:
1233         # code from http://stackoverflow.com/questions/12510648/in-gevent-how-can-i-dump-stack-traces-of-all-running-greenlets
1234         import gc
1235         from greenlet import greenlet
1236         for ob in gc.get_objects():
1237             if not isinstance(ob, greenlet) or not ob:
1238                 continue
1239             code.append("\n# Greenlet: %r" % (ob,))
1240             for line in extract_stack(ob.gr_frame):
1241                 code.append(line)
1242
1243     _logger.info("\n".join(code))
1244
1245 class frozendict(dict):
1246     """ An implementation of an immutable dictionary. """
1247     def __delitem__(self, key):
1248         raise NotImplementedError("'__delitem__' not supported on frozendict")
1249     def __setitem__(self, key, val):
1250         raise NotImplementedError("'__setitem__' not supported on frozendict")
1251     def clear(self):
1252         raise NotImplementedError("'clear' not supported on frozendict")
1253     def pop(self, key, default=None):
1254         raise NotImplementedError("'pop' not supported on frozendict")
1255     def popitem(self):
1256         raise NotImplementedError("'popitem' not supported on frozendict")
1257     def setdefault(self, key, default=None):
1258         raise NotImplementedError("'setdefault' not supported on frozendict")
1259     def update(self, *args, **kwargs):
1260         raise NotImplementedError("'update' not supported on frozendict")
1261
1262 @contextmanager
1263 def ignore(*exc):
1264     try:
1265         yield
1266     except exc:
1267         pass
1268
1269 # Avoid DeprecationWarning while still remaining compatible with werkzeug pre-0.9
1270 if parse_version(getattr(werkzeug, '__version__', '0.0')) < parse_version('0.9.0'):
1271     def html_escape(text):
1272         return werkzeug.utils.escape(text, quote=True)
1273 else:
1274     def html_escape(text):
1275         return werkzeug.utils.escape(text)
1276
1277 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: