1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 # Copyright (C) 2010-2013 OpenERP s.a. (<http://openerp.com>).
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.
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.
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/>.
21 ##############################################################################
25 Miscellaneous tools used by OpenERP.
28 from functools import wraps
38 from collections import defaultdict
39 from datetime import datetime
40 from itertools import islice, izip, groupby
41 from lxml import etree
42 from which import which
43 from threading import local
46 from html2text import html2text
50 from config import config
53 # get_encodings, ustr and exception_to_unicode were originally from tools.misc.
54 # There are moved to loglevels until we refactor tools.
55 from openerp.loglevels import get_encodings, ustr, exception_to_unicode
57 _logger = logging.getLogger(__name__)
59 # List of etree._Element subclasses that we choose to ignore when parsing XML.
60 # We include the *Base ones just in case, currently they seem to be subclasses of the _* ones.
61 SKIPPED_ELEMENT_TYPES = (etree._Comment, etree._ProcessingInstruction, etree.CommentBase, etree.PIBase)
63 def find_in_path(name):
69 def find_pg_tool(name):
71 if config['pg_path'] and config['pg_path'] != 'None':
72 path = config['pg_path']
74 return which(name, path=path)
78 def exec_pg_command(name, *args):
79 prog = find_pg_tool(name)
81 raise Exception('Couldn\'t find %s' % name)
82 args2 = (prog,) + args
84 return subprocess.call(args2)
86 def exec_pg_command_pipe(name, *args):
87 prog = find_pg_tool(name)
89 raise Exception('Couldn\'t find %s' % name)
90 # on win32, passing close_fds=True is not compatible
91 # with redirecting std[in/err/out]
92 pop = subprocess.Popen((prog,) + args, bufsize= -1,
93 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
94 close_fds=(os.name=="posix"))
95 return pop.stdin, pop.stdout
97 def exec_command_pipe(name, *args):
98 prog = find_in_path(name)
100 raise Exception('Couldn\'t find %s' % name)
101 # on win32, passing close_fds=True is not compatible
102 # with redirecting std[in/err/out]
103 pop = subprocess.Popen((prog,) + args, bufsize= -1,
104 stdin=subprocess.PIPE, stdout=subprocess.PIPE,
105 close_fds=(os.name=="posix"))
106 return pop.stdin, pop.stdout
108 #----------------------------------------------------------
110 #----------------------------------------------------------
111 #file_path_root = os.getcwd()
112 #file_path_addons = os.path.join(file_path_root, 'addons')
114 def file_open(name, mode="r", subdir='addons', pathinfo=False):
115 """Open a file from the OpenERP root, using a subdir folder.
119 >>> file_open('hr/report/timesheer.xsl')
120 >>> file_open('addons/hr/report/timesheet.xsl')
121 >>> file_open('../../base/report/rml_template.xsl', subdir='addons/hr/report', pathinfo=True)
123 @param name name of the file
124 @param mode file open mode
125 @param subdir subdirectory
126 @param pathinfo if True returns tuple (fileobject, filepath)
128 @return fileobject if pathinfo is False else (fileobject, filepath)
130 import openerp.modules as addons
131 adps = addons.module.ad_paths
132 rtp = os.path.normcase(os.path.abspath(config['root_path']))
136 if os.path.isabs(name):
137 # It is an absolute path
138 # Is it below 'addons_path' or 'root_path'?
139 name = os.path.normcase(os.path.normpath(name))
140 for root in adps + [rtp]:
141 root = os.path.normcase(os.path.normpath(root)) + os.sep
142 if name.startswith(root):
143 base = root.rstrip(os.sep)
144 name = name[len(base) + 1:]
147 # It is outside the OpenERP root: skip zipfile lookup.
148 base, name = os.path.split(name)
149 return _fileopen(name, mode=mode, basedir=base, pathinfo=pathinfo, basename=basename)
151 if name.replace(os.sep, '/').startswith('addons/'):
155 name = os.path.join(subdir, name)
156 if name.replace(os.sep, '/').startswith('addons/'):
162 # First, try to locate in addons_path
166 return _fileopen(name2, mode=mode, basedir=adp,
167 pathinfo=pathinfo, basename=basename)
171 # Second, try to locate in root_path
172 return _fileopen(name, mode=mode, basedir=rtp, pathinfo=pathinfo, basename=basename)
175 def _fileopen(path, mode, basedir, pathinfo, basename=None):
176 name = os.path.normpath(os.path.join(basedir, path))
180 # Give higher priority to module directories, which is
181 # a more common case than zipped modules.
182 if os.path.isfile(name):
183 fo = open(name, mode)
188 # Support for loading modules in zipped form.
189 # This will not work for zipped modules that are sitting
190 # outside of known addons paths.
191 head = os.path.normpath(path)
193 while os.sep in head:
194 head, tail = os.path.split(head)
198 zipname = os.path.join(tail, zipname)
201 zpath = os.path.join(basedir, head + '.zip')
202 if zipfile.is_zipfile(zpath):
203 from cStringIO import StringIO
204 zfile = zipfile.ZipFile(zpath)
207 fo.write(zfile.read(os.path.join(
208 os.path.basename(head), zipname).replace(
217 if name.endswith('.rml'):
218 raise IOError('Report %r doesn\'t exist or deleted' % basename)
219 raise IOError('File not found: %s' % basename)
222 #----------------------------------------------------------
224 #----------------------------------------------------------
226 """Flatten a list of elements into a uniqu list
227 Author: Christophe Simonis (christophe@tinyerp.com)
236 >>> flatten( [[], [[]]] )
238 >>> flatten( [[['a','b'], 'c'], 'd', ['e', [], 'f']] )
239 ['a', 'b', 'c', 'd', 'e', 'f']
240 >>> t = (1,2,(3,), [4, 5, [6, [7], (8, 9), ([10, 11, (12, 13)]), [14, [], (15,)], []]])
242 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
246 return hasattr(x, "__iter__")
251 map(r.append, flatten(e))
256 def reverse_enumerate(l):
257 """Like enumerate but in the other sens
260 >>> a = ['a', 'b', 'c']
261 >>> it = reverse_enumerate(a)
269 Traceback (most recent call last):
270 File "<stdin>", line 1, in <module>
273 return izip(xrange(len(l)-1, -1, -1), reversed(l))
275 #----------------------------------------------------------
277 #----------------------------------------------------------
278 # text must be latin-1 encoded
279 def sms_send(user, password, api_id, text, to):
281 url = "http://api.urlsms.com/SendSMS.aspx"
282 #url = "http://196.7.150.220/http/sendmsg"
283 params = urllib.urlencode({'UserID': user, 'Password': password, 'SenderID': api_id, 'MsgText': text, 'RecipientMobileNo':to})
284 urllib.urlopen(url+"?"+params)
285 # FIXME: Use the logger if there is an error
288 class UpdateableStr(local):
289 """ Class that stores an updateable string (used in wizards)
292 def __init__(self, string=''):
296 return str(self.string)
299 return str(self.string)
301 def __nonzero__(self):
302 return bool(self.string)
305 class UpdateableDict(local):
306 """Stores an updateable dict to use in wizards
309 def __init__(self, dict=None):
315 return str(self.dict)
318 return str(self.dict)
321 return self.dict.clear()
324 return self.dict.keys()
326 def __setitem__(self, i, y):
327 self.dict.__setitem__(i, y)
329 def __getitem__(self, i):
330 return self.dict.__getitem__(i)
333 return self.dict.copy()
336 return self.dict.iteritems()
339 return self.dict.iterkeys()
341 def itervalues(self):
342 return self.dict.itervalues()
344 def pop(self, k, d=None):
345 return self.dict.pop(k, d)
348 return self.dict.popitem()
350 def setdefault(self, k, d=None):
351 return self.dict.setdefault(k, d)
353 def update(self, E, **F):
354 return self.dict.update(E, F)
357 return self.dict.values()
359 def get(self, k, d=None):
360 return self.dict.get(k, d)
362 def has_key(self, k):
363 return self.dict.has_key(k)
366 return self.dict.items()
368 def __cmp__(self, y):
369 return self.dict.__cmp__(y)
371 def __contains__(self, k):
372 return self.dict.__contains__(k)
374 def __delitem__(self, y):
375 return self.dict.__delitem__(y)
378 return self.dict.__eq__(y)
381 return self.dict.__ge__(y)
384 return self.dict.__gt__(y)
387 return self.dict.__hash__()
390 return self.dict.__iter__()
393 return self.dict.__le__(y)
396 return self.dict.__len__()
399 return self.dict.__lt__(y)
402 return self.dict.__ne__(y)
404 class currency(float):
409 Don't use ! Use res.currency.round()
412 def __init__(self, value, accuracy=2, rounding=None):
414 rounding=10**-accuracy
415 self.rounding=rounding
416 self.accuracy=accuracy
418 def __new__(cls, value, accuracy=2, rounding=None):
419 return float.__new__(cls, round(value, accuracy))
422 # display_value = int(self*(10**(-self.accuracy))/self.rounding)*self.rounding/(10**(-self.accuracy))
423 # return str(display_value)
426 return s.replace('&','&').replace('<','<').replace('>','>')
428 def get_iso_codes(lang):
429 if lang.find('_') != -1:
430 if lang.split('_')[0] == lang.split('_')[1].lower():
431 lang = lang.split('_')[0]
435 'ab_RU': u'Abkhazian / аҧсуа',
436 'ar_SY': u'Arabic / الْعَرَبيّة',
437 'bg_BG': u'Bulgarian / български език',
438 'bs_BS': u'Bosnian / bosanski jezik',
439 'ca_ES': u'Catalan / Català',
440 'cs_CZ': u'Czech / Čeština',
441 'da_DK': u'Danish / Dansk',
442 'de_DE': u'German / Deutsch',
443 'el_GR': u'Greek / Ελληνικά',
444 'en_CA': u'English (CA)',
445 'en_GB': u'English (UK)',
446 'en_US': u'English (US)',
447 'es_AR': u'Spanish (AR) / Español (AR)',
448 'es_BO': u'Spanish (BO) / Español (BO)',
449 'es_CL': u'Spanish (CL) / Español (CL)',
450 'es_CO': u'Spanish (CO) / Español (CO)',
451 'es_CR': u'Spanish (CR) / Español (CR)',
452 'es_DO': u'Spanish (DO) / Español (DO)',
453 'es_EC': u'Spanish (EC) / Español (EC)',
454 'es_ES': u'Spanish / Español',
455 'es_GT': u'Spanish (GT) / Español (GT)',
456 'es_HN': u'Spanish (HN) / Español (HN)',
457 'es_MX': u'Spanish (MX) / Español (MX)',
458 'es_NI': u'Spanish (NI) / Español (NI)',
459 'es_PA': u'Spanish (PA) / Español (PA)',
460 'es_PE': u'Spanish (PE) / Español (PE)',
461 'es_PR': u'Spanish (PR) / Español (PR)',
462 'es_PY': u'Spanish (PY) / Español (PY)',
463 'es_SV': u'Spanish (SV) / Español (SV)',
464 'es_UY': u'Spanish (UY) / Español (UY)',
465 'es_VE': u'Spanish (VE) / Español (VE)',
466 'et_EE': u'Estonian / Eesti keel',
467 'fa_IR': u'Persian / فارس',
468 'fi_FI': u'Finnish / Suomi',
469 'fr_BE': u'French (BE) / Français (BE)',
470 'fr_CH': u'French (CH) / Français (CH)',
471 'fr_FR': u'French / Français',
472 'gl_ES': u'Galician / Galego',
473 'gu_IN': u'Gujarati / ગુજરાતી',
474 'he_IL': u'Hebrew / עִבְרִי',
475 'hi_IN': u'Hindi / हिंदी',
476 'hr_HR': u'Croatian / hrvatski jezik',
477 'hu_HU': u'Hungarian / Magyar',
478 'id_ID': u'Indonesian / Bahasa Indonesia',
479 'it_IT': u'Italian / Italiano',
480 'iu_CA': u'Inuktitut / ᐃᓄᒃᑎᑐᑦ',
481 'ja_JP': u'Japanese / 日本語',
482 'ko_KP': u'Korean (KP) / 한국어 (KP)',
483 'ko_KR': u'Korean (KR) / 한국어 (KR)',
484 'lt_LT': u'Lithuanian / Lietuvių kalba',
485 'lv_LV': u'Latvian / latviešu valoda',
486 'ml_IN': u'Malayalam / മലയാളം',
487 'mn_MN': u'Mongolian / монгол',
488 'nb_NO': u'Norwegian Bokmål / Norsk bokmål',
489 'nl_NL': u'Dutch / Nederlands',
490 'nl_BE': u'Flemish (BE) / Vlaams (BE)',
491 'oc_FR': u'Occitan (FR, post 1500) / Occitan',
492 'pl_PL': u'Polish / Język polski',
493 'pt_BR': u'Portuguese (BR) / Português (BR)',
494 'pt_PT': u'Portuguese / Português',
495 'ro_RO': u'Romanian / română',
496 'ru_RU': u'Russian / русский язык',
497 'si_LK': u'Sinhalese / සිංහල',
498 'sl_SI': u'Slovenian / slovenščina',
499 'sk_SK': u'Slovak / Slovenský jazyk',
500 'sq_AL': u'Albanian / Shqip',
501 'sr_RS': u'Serbian (Cyrillic) / српски',
502 'sr@latin': u'Serbian (Latin) / srpski',
503 'sv_SE': u'Swedish / svenska',
504 'te_IN': u'Telugu / తెలుగు',
505 'tr_TR': u'Turkish / Türkçe',
506 'vi_VN': u'Vietnamese / Tiếng Việt',
507 'uk_UA': u'Ukrainian / українська',
508 'ur_PK': u'Urdu / اردو',
509 'zh_CN': u'Chinese (CN) / 简体中文',
510 'zh_HK': u'Chinese (HK)',
511 'zh_TW': u'Chinese (TW) / 正體字',
512 'th_TH': u'Thai / ภาษาไทย',
513 'tlh_TLH': u'Klingon',
516 def scan_languages():
517 """ Returns all languages supported by OpenERP for translation
519 :returns: a list of (lang_code, lang_name) pairs
520 :rtype: [(str, unicode)]
522 return sorted(ALL_LANGUAGES.iteritems(), key=lambda k: k[1])
524 def get_user_companies(cr, user):
525 def _get_company_children(cr, ids):
528 cr.execute('SELECT id FROM res_company WHERE parent_id IN %s', (tuple(ids),))
529 res = [x[0] for x in cr.fetchall()]
530 res.extend(_get_company_children(cr, res))
532 cr.execute('SELECT company_id FROM res_users WHERE id=%s', (user,))
533 user_comp = cr.fetchone()[0]
536 return [user_comp] + _get_company_children(cr, [user_comp])
540 Input number : account or invoice number
541 Output return: the same number completed with the recursive mod10
544 codec=[0,9,4,6,8,2,7,1,3,5]
550 report = codec[ (int(digit) + report) % 10 ]
551 return result + str((10 - report) % 10)
556 Return the size in a human readable format
560 units = ('bytes', 'Kb', 'Mb', 'Gb')
561 if isinstance(sz,basestring):
564 while s >= 1024 and i < len(units)-1:
567 return "%0.2f %s" % (s, units[i])
571 def wrapper(*args, **kwargs):
572 from pprint import pformat
574 vector = ['Call -> function: %r' % f]
575 for i, arg in enumerate(args):
576 vector.append(' arg %02d: %s' % (i, pformat(arg)))
577 for key, value in kwargs.items():
578 vector.append(' kwarg %10s: %s' % (key, pformat(value)))
581 res = f(*args, **kwargs)
583 vector.append(' result: %s' % pformat(res))
584 vector.append(' time delta: %s' % (time.time() - timeb4))
585 _logger.debug('\n'.join(vector))
590 class profile(object):
591 def __init__(self, fname=None):
594 def __call__(self, f):
596 def wrapper(*args, **kwargs):
597 profile = cProfile.Profile()
598 result = profile.runcall(f, *args, **kwargs)
599 profile.dump_stats(self.fname or ("%s.cprof" % (f.func_name,)))
604 __icons_list = ['STOCK_ABOUT', 'STOCK_ADD', 'STOCK_APPLY', 'STOCK_BOLD',
605 'STOCK_CANCEL', 'STOCK_CDROM', 'STOCK_CLEAR', 'STOCK_CLOSE', 'STOCK_COLOR_PICKER',
606 'STOCK_CONNECT', 'STOCK_CONVERT', 'STOCK_COPY', 'STOCK_CUT', 'STOCK_DELETE',
607 'STOCK_DIALOG_AUTHENTICATION', 'STOCK_DIALOG_ERROR', 'STOCK_DIALOG_INFO',
608 'STOCK_DIALOG_QUESTION', 'STOCK_DIALOG_WARNING', 'STOCK_DIRECTORY', 'STOCK_DISCONNECT',
609 'STOCK_DND', 'STOCK_DND_MULTIPLE', 'STOCK_EDIT', 'STOCK_EXECUTE', 'STOCK_FILE',
610 'STOCK_FIND', 'STOCK_FIND_AND_REPLACE', 'STOCK_FLOPPY', 'STOCK_GOTO_BOTTOM',
611 'STOCK_GOTO_FIRST', 'STOCK_GOTO_LAST', 'STOCK_GOTO_TOP', 'STOCK_GO_BACK',
612 'STOCK_GO_DOWN', 'STOCK_GO_FORWARD', 'STOCK_GO_UP', 'STOCK_HARDDISK',
613 'STOCK_HELP', 'STOCK_HOME', 'STOCK_INDENT', 'STOCK_INDEX', 'STOCK_ITALIC',
614 'STOCK_JUMP_TO', 'STOCK_JUSTIFY_CENTER', 'STOCK_JUSTIFY_FILL',
615 'STOCK_JUSTIFY_LEFT', 'STOCK_JUSTIFY_RIGHT', 'STOCK_MEDIA_FORWARD',
616 'STOCK_MEDIA_NEXT', 'STOCK_MEDIA_PAUSE', 'STOCK_MEDIA_PLAY',
617 'STOCK_MEDIA_PREVIOUS', 'STOCK_MEDIA_RECORD', 'STOCK_MEDIA_REWIND',
618 'STOCK_MEDIA_STOP', 'STOCK_MISSING_IMAGE', 'STOCK_NETWORK', 'STOCK_NEW',
619 'STOCK_NO', 'STOCK_OK', 'STOCK_OPEN', 'STOCK_PASTE', 'STOCK_PREFERENCES',
620 'STOCK_PRINT', 'STOCK_PRINT_PREVIEW', 'STOCK_PROPERTIES', 'STOCK_QUIT',
621 'STOCK_REDO', 'STOCK_REFRESH', 'STOCK_REMOVE', 'STOCK_REVERT_TO_SAVED',
622 'STOCK_SAVE', 'STOCK_SAVE_AS', 'STOCK_SELECT_COLOR', 'STOCK_SELECT_FONT',
623 'STOCK_SORT_ASCENDING', 'STOCK_SORT_DESCENDING', 'STOCK_SPELL_CHECK',
624 'STOCK_STOP', 'STOCK_STRIKETHROUGH', 'STOCK_UNDELETE', 'STOCK_UNDERLINE',
625 'STOCK_UNDO', 'STOCK_UNINDENT', 'STOCK_YES', 'STOCK_ZOOM_100',
626 'STOCK_ZOOM_FIT', 'STOCK_ZOOM_IN', 'STOCK_ZOOM_OUT',
627 'terp-account', 'terp-crm', 'terp-mrp', 'terp-product', 'terp-purchase',
628 'terp-sale', 'terp-tools', 'terp-administration', 'terp-hr', 'terp-partner',
629 'terp-project', 'terp-report', 'terp-stock', 'terp-calendar', 'terp-graph',
630 'terp-check','terp-go-month','terp-go-year','terp-go-today','terp-document-new','terp-camera_test',
631 'terp-emblem-important','terp-gtk-media-pause','terp-gtk-stop','terp-gnome-cpu-frequency-applet+',
632 'terp-dialog-close','terp-gtk-jump-to-rtl','terp-gtk-jump-to-ltr','terp-accessories-archiver',
633 'terp-stock_align_left_24','terp-stock_effects-object-colorize','terp-go-home','terp-gtk-go-back-rtl',
634 'terp-gtk-go-back-ltr','terp-personal','terp-personal-','terp-personal+','terp-accessories-archiver-minus',
635 'terp-accessories-archiver+','terp-stock_symbol-selection','terp-call-start','terp-dolar',
636 'terp-face-plain','terp-folder-blue','terp-folder-green','terp-folder-orange','terp-folder-yellow',
637 'terp-gdu-smart-failing','terp-go-week','terp-gtk-select-all','terp-locked','terp-mail-forward',
638 'terp-mail-message-new','terp-mail-replied','terp-rating-rated','terp-stage','terp-stock_format-scientific',
639 'terp-dolar_ok!','terp-idea','terp-stock_format-default','terp-mail-','terp-mail_delete'
644 return [(x, x) for x in __icons_list ]
646 def detect_ip_addr():
647 """Try a very crude method to figure out a valid external
648 IP or hostname for the current machine. Don't rely on this
649 for binding to an interface, but it could be used as basis
650 for constructing a remote URL to the server.
652 def _detect_ip_addr():
653 from array import array
654 from struct import pack, unpack
663 if not fcntl: # not UNIX:
664 host = socket.gethostname()
665 ip_addr = socket.gethostbyname(host)
667 # get all interfaces:
669 s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
670 names = array('B', '\0' * nbytes)
671 #print 'names: ', names
672 outbytes = unpack('iL', fcntl.ioctl( s.fileno(), 0x8912, pack('iL', nbytes, names.buffer_info()[0])))[0]
673 namestr = names.tostring()
676 for i in range(0, outbytes, 40):
677 name = namestr[i:i+16].split('\0', 1)[0]
679 ip_addr = socket.inet_ntoa(namestr[i+20:i+24])
684 ifaces = filter(None, [namestr[i:i+32].split('\0', 1)[0] for i in range(0, outbytes, 32)])
686 for ifname in [iface for iface in ifaces if iface != 'lo']:
687 ip_addr = socket.inet_ntoa(fcntl.ioctl(s.fileno(), 0x8915, pack('256s', ifname[:15]))[20:24])
690 return ip_addr or 'localhost'
693 ip_addr = _detect_ip_addr()
695 ip_addr = 'localhost'
698 # RATIONALE BEHIND TIMESTAMP CALCULATIONS AND TIMEZONE MANAGEMENT:
699 # The server side never does any timestamp calculation, always
700 # sends them in a naive (timezone agnostic) format supposed to be
701 # expressed within the server timezone, and expects the clients to
702 # provide timestamps in the server timezone as well.
703 # It stores all timestamps in the database in naive format as well,
704 # which also expresses the time in the server timezone.
705 # For this reason the server makes its timezone name available via the
706 # common/timezone_get() rpc method, which clients need to read
707 # to know the appropriate time offset to use when reading/writing
709 def get_win32_timezone():
710 """Attempt to return the "standard name" of the current timezone on a win32 system.
711 @return the standard name of the current win32 timezone, or False if it cannot be found.
714 if sys.platform == "win32":
717 hklm = _winreg.ConnectRegistry(None,_winreg.HKEY_LOCAL_MACHINE)
718 current_tz_key = _winreg.OpenKey(hklm, r"SYSTEM\CurrentControlSet\Control\TimeZoneInformation", 0,_winreg.KEY_ALL_ACCESS)
719 res = str(_winreg.QueryValueEx(current_tz_key,"StandardName")[0]) # [0] is value, [1] is type code
720 _winreg.CloseKey(current_tz_key)
721 _winreg.CloseKey(hklm)
726 def detect_server_timezone():
727 """Attempt to detect the timezone to use on the server side.
728 Defaults to UTC if no working timezone can be found.
729 @return the timezone identifier as expected by pytz.timezone.
734 _logger.warning("Python pytz module is not available. "
735 "Timezone will be set to UTC by default.")
738 # Option 1: the configuration option (did not exist before, so no backwards compatibility issue)
739 # 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
740 # Option 3: the environment variable TZ
741 sources = [ (config['timezone'], 'OpenERP configuration'),
742 (time.tzname[0], 'time.tzname'),
743 (os.environ.get('TZ',False),'TZ environment variable'), ]
744 # Option 4: OS-specific: /etc/timezone on Unix
745 if os.path.exists("/etc/timezone"):
748 f = open("/etc/timezone")
749 tz_value = f.read(128).strip()
754 sources.append((tz_value,"/etc/timezone file"))
755 # Option 5: timezone info from registry on Win32
756 if sys.platform == "win32":
757 # Timezone info is stored in windows registry.
758 # However this is not likely to work very well as the standard name
759 # of timezones in windows is rarely something that is known to pytz.
760 # But that's ok, it is always possible to use a config option to set
762 sources.append((get_win32_timezone(),"Windows Registry"))
764 for (value,source) in sources:
767 tz = pytz.timezone(value)
768 _logger.info("Using timezone %s obtained from %s.", tz.zone, source)
770 except pytz.UnknownTimeZoneError:
771 _logger.warning("The timezone specified in %s (%s) is invalid, ignoring it.", source, value)
773 _logger.warning("No valid timezone could be detected, using default UTC "
774 "timezone. You can specify it explicitly with option 'timezone' in "
775 "the server configuration.")
778 def get_server_timezone():
782 DEFAULT_SERVER_DATE_FORMAT = "%Y-%m-%d"
783 DEFAULT_SERVER_TIME_FORMAT = "%H:%M:%S"
784 DEFAULT_SERVER_DATETIME_FORMAT = "%s %s" % (
785 DEFAULT_SERVER_DATE_FORMAT,
786 DEFAULT_SERVER_TIME_FORMAT)
788 # Python's strftime supports only the format directives
789 # that are available on the platform's libc, so in order to
790 # be cross-platform we map to the directives required by
791 # the C standard (1989 version), always available on platforms
792 # with a C standard implementation.
793 DATETIME_FORMATS_MAP = {
795 '%D': '%m/%d/%Y', # modified %y->%Y
797 '%E': '', # special modifier
799 '%g': '%Y', # modified %y->%Y
805 '%O': '', # special modifier
809 '%s': '', #num of seconds since epoch
814 '%y': '%Y', # Even if %y works, it's ambiguous, so we should use %Y
815 '%+': '%Y-%m-%d %H:%M:%S',
817 # %Z is a special case that causes 2 problems at least:
818 # - the timezone names we use (in res_user.context_tz) come
819 # from pytz, but not all these names are recognized by
820 # strptime(), so we cannot convert in both directions
821 # when such a timezone is selected and %Z is in the format
822 # - %Z is replaced by an empty string in strftime() when
823 # there is not tzinfo in a datetime value (e.g when the user
824 # did not pick a context_tz). The resulting string does not
825 # parse back if the format requires %Z.
826 # As a consequence, we strip it completely from format strings.
827 # The user can always have a look at the context_tz in
828 # preferences to check the timezone.
833 def server_to_local_timestamp(src_tstamp_str, src_format, dst_format, dst_tz_name,
834 tz_offset=True, ignore_unparsable_time=True):
836 Convert a source timestamp string into a destination timestamp string, attempting to apply the
837 correct offset if both the server and local timezone are recognized, or no
838 offset at all if they aren't or if tz_offset is false (i.e. assuming they are both in the same TZ).
840 WARNING: This method is here to allow formatting dates correctly for inclusion in strings where
841 the client would not be able to format/offset it correctly. DO NOT use it for returning
842 date fields directly, these are supposed to be handled by the client!!
844 @param src_tstamp_str: the str value containing the timestamp in the server timezone.
845 @param src_format: the format to use when parsing the server timestamp.
846 @param dst_format: the format to use when formatting the resulting timestamp for the local/client timezone.
847 @param dst_tz_name: name of the destination timezone (such as the 'tz' value of the client context)
848 @param ignore_unparsable_time: if True, return False if src_tstamp_str cannot be parsed
849 using src_format or formatted using dst_format.
851 @return local/client formatted timestamp, expressed in the local/client timezone if possible
852 and if tz_offset is true, or src_tstamp_str if timezone offset could not be determined.
854 if not src_tstamp_str:
858 if src_format and dst_format:
859 # find out server timezone
860 server_tz = get_server_timezone()
862 # dt_value needs to be a datetime.datetime object (so no time.struct_time or mx.DateTime.DateTime here!)
863 dt_value = datetime.strptime(src_tstamp_str, src_format)
864 if tz_offset and dst_tz_name:
867 src_tz = pytz.timezone(server_tz)
868 dst_tz = pytz.timezone(dst_tz_name)
869 src_dt = src_tz.localize(dt_value, is_dst=True)
870 dt_value = src_dt.astimezone(dst_tz)
873 res = dt_value.strftime(dst_format)
875 # Normal ways to end up here are if strptime or strftime failed
876 if not ignore_unparsable_time:
881 def split_every(n, iterable, piece_maker=tuple):
882 """Splits an iterable into length-n pieces. The last piece will be shorter
883 if ``n`` does not evenly divide the iterable length.
884 @param ``piece_maker``: function to build the pieces
885 from the slices (tuple,list,...)
887 iterator = iter(iterable)
888 piece = piece_maker(islice(iterator, n))
891 piece = piece_maker(islice(iterator, n))
893 if __name__ == '__main__':
897 class upload_data_thread(threading.Thread):
898 def __init__(self, email, data, type):
899 self.args = [('email',email),('type',type),('data',data)]
900 super(upload_data_thread,self).__init__()
904 args = urllib.urlencode(self.args)
905 fp = urllib.urlopen('http://www.openerp.com/scripts/survey.php', args)
911 def upload_data(email, data, type='SURVEY'):
912 a = upload_data_thread(email, data, type)
916 def get_and_group_by_field(cr, uid, obj, ids, field, context=None):
917 """ Read the values of ``field´´ for the given ``ids´´ and group ids by value.
919 :param string field: name of the field we want to read and group by
920 :return: mapping of field values to the list of ids that have it
924 for record in obj.read(cr, uid, ids, [field], context=context):
926 res.setdefault(key[0] if isinstance(key, tuple) else key, []).append(record['id'])
929 def get_and_group_by_company(cr, uid, obj, ids, context=None):
930 return get_and_group_by_field(cr, uid, obj, ids, field='company_id', context=context)
932 # port of python 2.6's attrgetter with support for dotted notation
933 def resolve_attr(obj, attr):
934 for name in attr.split("."):
935 obj = getattr(obj, name)
938 def attrgetter(*items):
942 return resolve_attr(obj, attr)
945 return tuple(resolve_attr(obj, attr) for attr in items)
949 """A subclass of str that implements repr() without enclosing quotation marks
950 or escaping, keeping the original string untouched. The name come from Lisp's unquote.
951 One of the uses for this is to preserve or insert bare variable names within dicts during eval()
952 of a dict's repr(). Use with care.
954 Some examples (notice that there are never quotes surrounding
955 the ``active_id`` name:
957 >>> unquote('active_id')
959 >>> d = {'test': unquote('active_id')}
968 class UnquoteEvalContext(defaultdict):
969 """Defaultdict-based evaluation context that returns
970 an ``unquote`` string for any missing name used during
972 Mostly useful for evaluating OpenERP domains/contexts that
973 may refer to names that are unknown at the time of eval,
974 so that when the context/domain is converted back to a string,
975 the original names are preserved.
977 **Warning**: using an ``UnquoteEvalContext`` as context for ``eval()`` or
978 ``safe_eval()`` will shadow the builtins, which may cause other
979 failures, depending on what is evaluated.
981 Example (notice that ``section_id`` is preserved in the final
984 >>> context_str = "{'default_user_id': uid, 'default_section_id': section_id}"
985 >>> eval(context_str, UnquoteEvalContext(uid=1))
986 {'default_user_id': 1, 'default_section_id': section_id}
989 def __init__(self, *args, **kwargs):
990 super(UnquoteEvalContext, self).__init__(None, *args, **kwargs)
992 def __missing__(self, key):
996 class mute_logger(object):
997 """Temporary suppress the logging.
998 Can be used as context manager or decorator.
1000 @mute_logger('openerp.plic.ploc')
1004 with mute_logger('openerp.foo.bar'):
1008 def __init__(self, *loggers):
1009 self.loggers = loggers
1011 def filter(self, record):
1014 def __enter__(self):
1015 for logger in self.loggers:
1016 logging.getLogger(logger).addFilter(self)
1018 def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
1019 for logger in self.loggers:
1020 logging.getLogger(logger).removeFilter(self)
1022 def __call__(self, func):
1024 def deco(*args, **kwargs):
1026 return func(*args, **kwargs)
1030 class CountingStream(object):
1031 """ Stream wrapper counting the number of element it has yielded. Similar
1032 role to ``enumerate``, but for use when the iteration process of the stream
1033 isn't fully under caller control (the stream can be iterated from multiple
1034 points including within a library)
1036 ``start`` allows overriding the starting index (the index before the first
1039 On each iteration (call to :meth:`~.next`), increases its :attr:`~.index`
1042 .. attribute:: index
1044 ``int``, index of the last yielded element in the stream. If the stream
1045 has ended, will give an index 1-past the stream
1047 def __init__(self, stream, start=-1):
1048 self.stream = iter(stream)
1050 self.stopped = False
1054 if self.stopped: raise StopIteration()
1056 val = next(self.stream, _ph)
1059 raise StopIteration()
1062 def stripped_sys_argv(*strip_args):
1063 """Return sys.argv with some arguments stripped, suitable for reexecution or subprocesses"""
1064 strip_args = sorted(set(strip_args) | set(['-s', '--save', '-d', '--database', '-u', '--update', '-i', '--init']))
1065 assert all(config.parser.has_option(s) for s in strip_args)
1066 takes_value = dict((s, config.parser.get_option(s).takes_value()) for s in strip_args)
1068 longs, shorts = list(tuple(y) for _, y in groupby(strip_args, lambda x: x.startswith('--')))
1069 longs_eq = tuple(l + '=' for l in longs if takes_value[l])
1074 return args[i].startswith(shorts) \
1075 or args[i].startswith(longs_eq) or (args[i] in longs) \
1076 or (i >= 1 and (args[i - 1] in strip_args) and takes_value[args[i - 1]])
1078 return [x for i, x in enumerate(args) if not strip(args, i)]
1080 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: