2009-10-20 10:52:23 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2006-12-07 13:41:40 +00:00
|
|
|
##############################################################################
|
2010-06-26 08:50:34 +00:00
|
|
|
#
|
2006-12-07 13:41:40 +00:00
|
|
|
# Copyright (C) 2003, Fabien Pinckaers, UCL, FSA
|
2013-04-26 16:11:06 +00:00
|
|
|
# Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
|
2006-12-07 13:41:40 +00:00
|
|
|
#
|
|
|
|
# This library is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Lesser General Public
|
|
|
|
# License as published by the Free Software Foundation; either
|
|
|
|
# version 2.1 of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This library is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
# Lesser General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU Lesser General Public
|
|
|
|
# License along with this library; if not, write to the Free Software
|
2013-05-14 14:16:19 +00:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-13017 USA
|
2013-04-26 16:11:06 +00:00
|
|
|
#
|
|
|
|
##############################################################################
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2009-05-12 11:19:55 +00:00
|
|
|
import copy
|
2009-10-07 12:38:07 +00:00
|
|
|
import locale
|
2010-11-23 15:11:10 +00:00
|
|
|
|
2010-11-08 18:53:28 +00:00
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
import reportlab
|
|
|
|
|
2011-02-07 12:57:23 +00:00
|
|
|
import openerp.tools as tools
|
|
|
|
from openerp.tools.safe_eval import safe_eval as eval
|
|
|
|
from openerp.tools.misc import ustr
|
2009-04-28 10:24:21 +00:00
|
|
|
|
2012-01-24 12:55:12 +00:00
|
|
|
_logger = logging.getLogger(__name__)
|
|
|
|
|
2009-04-28 10:24:21 +00:00
|
|
|
_regex = re.compile('\[\[(.+?)\]\]')
|
|
|
|
|
2009-06-10 05:46:06 +00:00
|
|
|
def str2xml(s):
|
2010-03-01 18:42:04 +00:00
|
|
|
return (s or '').replace('&', '&').replace('<', '<').replace('>', '>')
|
2009-06-10 05:46:06 +00:00
|
|
|
|
2009-06-26 11:27:44 +00:00
|
|
|
def xml2str(s):
|
2010-03-01 18:42:04 +00:00
|
|
|
return (s or '').replace('&','&').replace('<','<').replace('>','>')
|
2009-06-26 11:27:44 +00:00
|
|
|
|
2009-04-28 10:24:21 +00:00
|
|
|
def _child_get(node, self=None, tagname=None):
|
|
|
|
for n in node:
|
2009-11-28 11:58:09 +00:00
|
|
|
if self and self.localcontext and n.get('rml_loop'):
|
2009-05-12 11:19:55 +00:00
|
|
|
|
2009-04-28 10:24:21 +00:00
|
|
|
for ctx in eval(n.get('rml_loop'),{}, self.localcontext):
|
|
|
|
self.localcontext.update(ctx)
|
|
|
|
if (tagname is None) or (n.tag==tagname):
|
|
|
|
if n.get('rml_except', False):
|
|
|
|
try:
|
|
|
|
eval(n.get('rml_except'), {}, self.localcontext)
|
2010-06-21 14:37:01 +00:00
|
|
|
except GeneratorExit:
|
|
|
|
continue
|
2010-07-02 06:31:24 +00:00
|
|
|
except Exception, e:
|
2012-01-24 12:55:12 +00:00
|
|
|
_logger.warning('rml_except: "%s"', n.get('rml_except',''), exc_info=True)
|
2009-04-28 10:24:21 +00:00
|
|
|
continue
|
|
|
|
if n.get('rml_tag'):
|
|
|
|
try:
|
|
|
|
(tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
|
2009-05-12 11:19:55 +00:00
|
|
|
n2 = copy.deepcopy(n)
|
2009-04-28 10:24:21 +00:00
|
|
|
n2.tag = tag
|
|
|
|
n2.attrib.update(attr)
|
|
|
|
yield n2
|
2010-06-21 14:37:01 +00:00
|
|
|
except GeneratorExit:
|
|
|
|
yield n
|
2010-07-02 06:31:24 +00:00
|
|
|
except Exception, e:
|
2012-01-24 12:55:12 +00:00
|
|
|
_logger.warning('rml_tag: "%s"', n.get('rml_tag',''), exc_info=True)
|
2009-04-28 10:24:21 +00:00
|
|
|
yield n
|
|
|
|
else:
|
|
|
|
yield n
|
|
|
|
continue
|
2009-11-28 11:58:09 +00:00
|
|
|
if self and self.localcontext and n.get('rml_except'):
|
2009-04-28 10:24:21 +00:00
|
|
|
try:
|
|
|
|
eval(n.get('rml_except'), {}, self.localcontext)
|
2010-06-21 14:37:01 +00:00
|
|
|
except GeneratorExit:
|
|
|
|
continue
|
2010-07-02 06:31:24 +00:00
|
|
|
except Exception, e:
|
2012-01-24 12:55:12 +00:00
|
|
|
_logger.warning('rml_except: "%s"', n.get('rml_except',''), exc_info=True)
|
2009-04-28 10:24:21 +00:00
|
|
|
continue
|
2009-11-28 11:58:09 +00:00
|
|
|
if self and self.localcontext and n.get('rml_tag'):
|
2009-05-12 11:19:55 +00:00
|
|
|
try:
|
|
|
|
(tag,attr) = eval(n.get('rml_tag'),{}, self.localcontext)
|
|
|
|
n2 = copy.deepcopy(n)
|
|
|
|
n2.tag = tag
|
2009-05-22 06:05:41 +00:00
|
|
|
n2.attrib.update(attr or {})
|
2009-05-12 11:19:55 +00:00
|
|
|
yield n2
|
|
|
|
tagname = ''
|
2010-06-21 14:37:01 +00:00
|
|
|
except GeneratorExit:
|
|
|
|
pass
|
2010-07-02 06:31:24 +00:00
|
|
|
except Exception, e:
|
2012-01-24 12:55:12 +00:00
|
|
|
_logger.warning('rml_tag: "%s"', n.get('rml_tag',''), exc_info=True)
|
2009-05-12 11:19:55 +00:00
|
|
|
pass
|
2009-04-28 10:24:21 +00:00
|
|
|
if (tagname is None) or (n.tag==tagname):
|
|
|
|
yield n
|
|
|
|
|
|
|
|
def _process_text(self, txt):
|
2011-11-04 16:42:22 +00:00
|
|
|
"""Translate ``txt`` according to the language in the local context,
|
|
|
|
replace dynamic ``[[expr]]`` with their real value, then escape
|
|
|
|
the result for XML.
|
2011-11-04 15:57:59 +00:00
|
|
|
|
2011-11-04 16:42:22 +00:00
|
|
|
:param str txt: original text to translate (must NOT be XML-escaped)
|
|
|
|
:return: translated text, with dynamic expressions evaluated and
|
|
|
|
with special XML characters escaped (``&,<,>``).
|
|
|
|
"""
|
2009-04-28 10:24:21 +00:00
|
|
|
if not self.localcontext:
|
2009-08-27 10:04:54 +00:00
|
|
|
return str2xml(txt)
|
2009-04-28 10:24:21 +00:00
|
|
|
if not txt:
|
|
|
|
return ''
|
|
|
|
result = ''
|
|
|
|
sps = _regex.split(txt)
|
|
|
|
while sps:
|
|
|
|
# This is a simple text to translate
|
2010-11-08 18:53:28 +00:00
|
|
|
to_translate = tools.ustr(sps.pop(0))
|
|
|
|
result += tools.ustr(self.localcontext.get('translate', lambda x:x)(to_translate))
|
2009-04-28 10:24:21 +00:00
|
|
|
if sps:
|
|
|
|
try:
|
2010-11-08 18:53:28 +00:00
|
|
|
txt = None
|
2009-10-09 10:45:47 +00:00
|
|
|
expr = sps.pop(0)
|
2010-11-08 18:53:28 +00:00
|
|
|
txt = eval(expr, self.localcontext)
|
|
|
|
if txt and isinstance(txt, basestring):
|
|
|
|
txt = tools.ustr(txt)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
if isinstance(txt, basestring):
|
2011-11-04 15:57:59 +00:00
|
|
|
result += txt
|
2010-12-28 11:09:29 +00:00
|
|
|
elif txt and (txt is not None) and (txt is not False):
|
2010-11-23 15:19:30 +00:00
|
|
|
result += ustr(txt)
|
2011-11-04 15:57:59 +00:00
|
|
|
return str2xml(result)
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def text_get(node):
|
2010-11-23 15:19:30 +00:00
|
|
|
return ''.join([ustr(n.text) for n in node])
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
units = [
|
2008-07-22 14:24:36 +00:00
|
|
|
(re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
|
2008-10-13 06:22:32 +00:00
|
|
|
(re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
|
2008-07-22 14:24:36 +00:00
|
|
|
(re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
|
|
|
|
(re.compile('^(-?[0-9\.]+)\s*$'), 1)
|
2006-12-07 13:41:40 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def unit_get(size):
|
2008-07-22 14:24:36 +00:00
|
|
|
global units
|
2009-05-05 06:26:48 +00:00
|
|
|
if size:
|
2009-10-07 12:38:07 +00:00
|
|
|
if size.find('.') == -1:
|
|
|
|
decimal_point = '.'
|
|
|
|
try:
|
2009-10-08 05:33:44 +00:00
|
|
|
decimal_point = locale.nl_langinfo(locale.RADIXCHAR)
|
2010-06-22 09:45:40 +00:00
|
|
|
except Exception:
|
2009-10-07 12:38:07 +00:00
|
|
|
decimal_point = locale.localeconv()['decimal_point']
|
|
|
|
|
|
|
|
size = size.replace(decimal_point, '.')
|
|
|
|
|
2009-05-05 06:26:48 +00:00
|
|
|
for unit in units:
|
|
|
|
res = unit[0].search(size, 0)
|
|
|
|
if res:
|
|
|
|
return unit[1]*float(res.group(1))
|
2008-07-22 14:24:36 +00:00
|
|
|
return False
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def tuple_int_get(node, attr_name, default=None):
|
2009-04-28 10:24:21 +00:00
|
|
|
if not node.get(attr_name):
|
2008-07-22 14:24:36 +00:00
|
|
|
return default
|
2009-11-28 11:58:09 +00:00
|
|
|
return map(int, node.get(attr_name).split(','))
|
2006-12-07 13:41:40 +00:00
|
|
|
|
|
|
|
def bool_get(value):
|
2008-07-22 14:24:36 +00:00
|
|
|
return (str(value)=="1") or (value.lower()=='yes')
|
2006-12-07 13:41:40 +00:00
|
|
|
|
2011-11-07 15:45:56 +00:00
|
|
|
def attr_get(node, attrs, dict=None):
|
|
|
|
if dict is None:
|
|
|
|
dict = {}
|
2008-07-22 14:24:36 +00:00
|
|
|
res = {}
|
|
|
|
for name in attrs:
|
2009-04-28 10:24:21 +00:00
|
|
|
if node.get(name):
|
|
|
|
res[name] = unit_get(node.get(name))
|
2008-07-22 14:24:36 +00:00
|
|
|
for key in dict:
|
2009-04-28 10:24:21 +00:00
|
|
|
if node.get(key):
|
2008-07-22 14:24:36 +00:00
|
|
|
if dict[key]=='str':
|
2010-10-07 14:52:40 +00:00
|
|
|
res[key] = tools.ustr(node.get(key))
|
2008-07-22 14:24:36 +00:00
|
|
|
elif dict[key]=='bool':
|
2009-04-28 10:24:21 +00:00
|
|
|
res[key] = bool_get(node.get(key))
|
2008-07-22 14:24:36 +00:00
|
|
|
elif dict[key]=='int':
|
2009-04-28 10:24:21 +00:00
|
|
|
res[key] = int(node.get(key))
|
2008-08-25 07:12:26 +00:00
|
|
|
elif dict[key]=='unit':
|
2009-04-28 10:24:21 +00:00
|
|
|
res[key] = unit_get(node.get(key))
|
2009-10-09 10:45:47 +00:00
|
|
|
elif dict[key] == 'float' :
|
2009-07-01 08:28:50 +00:00
|
|
|
res[key] = float(node.get(key))
|
2008-07-22 14:24:36 +00:00
|
|
|
return res
|
2008-07-23 15:01:27 +00:00
|
|
|
|
2009-05-22 06:05:41 +00:00
|
|
|
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|