zephir.utils.trml2pdf.utils

Package zephir :: Package utils :: Package trml2pdf :: Module utils |
[frames] | no frames]

# Source Code for Module zephir.utils.trml2pdf.utils

     1  # trml2pdf - An RML to PDF converter
     2  # Copyright (C) 2003, Fabien Pinckaers, UCL, FSA
     3  #
     4  # This library is free software; you can redistribute it and/or
     5  # modify it under the terms of the GNU Lesser General Public
     6  # License as published by the Free Software Foundation; either
     7  # version 2.1 of the License, or (at your option) any later version.
     8  #
     9  # This library is distributed in the hope that it will be useful,
    10  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    12  # Lesser General Public License for more details.
    13  #
    14  # You should have received a copy of the GNU Lesser General Public
    15  # License along with this library; if not, write to the Free Software
    16  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17
    18  import re
    19  import reportlab
    20
    21 -def text_get(node):
    22          rc = ''
    23          for node in node.childNodes:
    24                  if node.nodeType == node.TEXT_NODE:
    25                          rc = rc + node.data
    26          return rc
    27
    28  units = [
    29          (re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
    30          (re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
    31          (re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
    32          (re.compile('^(-?[0-9\.]+)\s*$'), 1)
    33  ]
    34
    35 -def unit_get(size):
    36          global units
    37          for unit in units:
    38                  res = unit[0].search(size, 0)
    39                  if res:
    40                          return unit[1]*float(res.group(1))
    41          return False
    42
    43 -def tuple_int_get(node, attr_name, default=None):
    44          if not node.hasAttribute(attr_name):
    45                  return default
    46          res = [int(x) for x in node.getAttribute(attr_name).split(',')]
    47          return res
    48
    49 -def bool_get(value):
    50          return (str(value)=="1") or (value.lower()=='yes')
    51
    52 -def attr_get(node, attrs, dict={}):
    53          res = {}
    54          for name in attrs:
    55                  if node.hasAttribute(name):
    56                          res[name] =  unit_get(node.getAttribute(name))
    57          for key in dict:
    58                  if node.hasAttribute(key):
    59                          if dict[key]=='str':
    60                                  res[key] = str(node.getAttribute(key))
    61                          elif dict[key]=='bool':
    62                                  res[key] = bool_get(node.getAttribute(key))
    63                          elif dict[key]=='int':
    64                                  res[key] = int(node.getAttribute(key))
    65          return res
    66