/usr/share/pyshared/z3c/rml/attr.py is in python-z3c.rml 2.0.0-0ubuntu3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | ##############################################################################
#
# Copyright (c) 2007 Zope Foundation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""RML Attribute Implementation
$Id: attr.py 128842 2012-12-21 05:38:51Z srichter $
"""
__docformat__ = "reStructuredText"
import cStringIO
import logging
import os
import re
import reportlab.graphics.widgets.markers
import reportlab.lib.colors
import reportlab.lib.pagesizes
import reportlab.lib.styles
import reportlab.lib.units
import reportlab.lib.utils
import urllib
import zope.interface
import zope.schema
from lxml import etree
from z3c.rml import interfaces
MISSING = object()
logger = logging.getLogger("z3c.rml")
def getFileInfo(directive):
root = directive
while root.parent:
root = root.parent
return '(file %s, line %i)' % (
root.filename, directive.element.sourceline)
def getManager(context, interface=None):
if interface is None:
# Avoid circular imports
from z3c.rml import interfaces
interface = interfaces.IManager
# Walk up the path until the manager is found
while (not interface.providedBy(context) and context is not None):
context = context.parent
# If no manager was found, raise an error
if context is None:
raise ValueError('The manager could not be found.')
return context
def deprecated(oldName, attr, reason):
zope.interface.directlyProvides(attr, interfaces.IDeprecated)
attr.deprecatedName = oldName
attr.deprecatedReason = reason
return attr
class RMLAttribute(zope.schema.Field):
"""An attribute of the RML directive."""
missing_value = MISSING
default = MISSING
def fromUnicode(self, ustr):
"""See zope.schema.interfaces.IField"""
if self.context is None:
raise ValueError('Attribute not bound to a context.')
return super(RMLAttribute, self).fromUnicode(unicode(ustr))
def get(self):
"""See zope.schema.interfaces.IField"""
# If the attribute has a deprecated partner and the deprecated name
# has been specified, use it.
if (interfaces.IDeprecated.providedBy(self) and
self.deprecatedName in self.context.element.attrib):
name = self.deprecatedName
logger.warn(
u'Deprecated attribute "%s": %s %s' % (
name, self.deprecatedReason, getFileInfo(self.context)))
else:
name = self.__name__
# Extract the value.
value = self.context.element.get(name, self.missing_value)
# Get the correct default value.
if value is self.missing_value:
if self.default is not None:
return self.default
return self.missing_value
return self.fromUnicode(value)
class BaseChoice(RMLAttribute):
choices = {}
doLower = True
def fromUnicode(self, value):
if self.doLower:
value = value.lower()
if value in self.choices:
return self.choices[value]
raise ValueError(
'%r not a valid value for attribute "%s". %s' % (
value, self.__name__, getFileInfo(self.context)))
class Combination(RMLAttribute):
"""A combination of several other attribute types."""
def __init__(self, value_types=(), *args, **kw):
super(Combination, self).__init__(*args, **kw)
self.value_types = value_types
def fromUnicode(self, value):
for value_type in self.value_types:
bound = value_type.bind(self)
try:
return bound.fromUnicode(value)
except ValueError:
pass
raise ValueError(
'"%s" is not a valid value. %s' %(
value, getFileInfo(self.context)))
class String(RMLAttribute, zope.schema.Bytes):
"""A simple Bytes string."""
class Text(RMLAttribute, zope.schema.Text):
"""A simple unicode string."""
class Integer(RMLAttribute, zope.schema.Int):
"""An integer. A minimum and maximum value can be specified."""
# By making min and max simple attributes, we avoid some validation
# problems.
min = None
max = None
class Float(RMLAttribute, zope.schema.Float):
"""An flaoting point. A minimum and maximum value can be specified."""
# By making min and max simple attributes, we avoid some validation
# problems.
min = None
max = None
class StringOrInt(RMLAttribute):
"""A (bytes) string or an integer."""
def fromUnicode(self, value):
try:
return int(value)
except ValueError:
return str(value)
class Sequence(RMLAttribute, zope.schema._field.AbstractCollection):
"""A list of values of a specified type."""
splitre = re.compile('[ \t\n,;]*')
def __init__(self, splitre=None, *args, **kw):
super(Sequence, self).__init__(*args, **kw)
if splitre is not None:
self.splitre = splitre
def fromUnicode(self, ustr):
if ustr.startswith('(') and ustr.endswith(')'):
ustr = ustr[1:-1]
ustr = ustr.strip()
raw_values = self.splitre.split(ustr)
result = [self.value_type.bind(self.context).fromUnicode(raw.strip())
for raw in raw_values]
if ((self.min_length is not None and len(result) < self.min_length) and
(self.max_length is not None and len(result) > self.max_length)):
raise ValueError(
'Length of sequence must be at least %s and at most %i. %s' % (
self.min_length, self.max_length,
getFileInfo(self.context)))
return result
class IntegerSequence(Sequence):
"""A sequence of integers."""
def fromUnicode(self, ustr):
ustr = ustr.strip()
pieces = self.splitre.split(ustr)
numbers = set([])
for piece in pieces:
# Ignore empty pieces.
if not piece:
continue
# The piece is a range.
if '-' in piece:
start, end = piece.split('-')
# Make range lower and upper bound inclusive.
numbers.update(range(int(start), int(end)+1))
continue
# The piece is just a number
numbers.add(int(piece))
return list(numbers)
class Choice(BaseChoice):
"""A choice of several values. The values are always case-insensitive."""
def __init__(self, choices=None, doLower=True, *args, **kw):
super(Choice, self).__init__(*args, **kw)
if isinstance(choices, (tuple, list)):
choices = dict(
[(val.lower() if doLower else val, val) for val in choices])
else:
choices = dict(
[(key.lower() if doLower else key, val)
for key, val in choices.items()])
self.choices = choices
self.doLower = doLower
class Boolean(BaseChoice):
'''A boolean value.
For true the values "true", "yes", and "1" are allowed. For false, the
values "false", "no", "1" are allowed.
'''
choices = {'true': True, 'false': False,
'yes': True, 'no': False,
'1': True, '0': False,
}
class BooleanWithDefault(Boolean):
'''This is a boolean field that can also receive the value "default".'''
choices = Boolean.choices.copy()
choices.update({'default': None})
class Measurement(RMLAttribute):
'''This field represents a length value.
The units "in" (inch), "cm", "mm", and "pt" are allowed. If no units are
specified, the value is given in points/pixels.
'''
def __init__(self, allowPercentage=False, allowStar=False, *args, **kw):
super(Measurement, self).__init__(*args, **kw)
self.allowPercentage = allowPercentage
self.allowStar = allowStar
units = [
(re.compile('^(-?[0-9\.]+)\s*in$'), reportlab.lib.units.inch),
(re.compile('^(-?[0-9\.]+)\s*cm$'), reportlab.lib.units.cm),
(re.compile('^(-?[0-9\.]+)\s*mm$'), reportlab.lib.units.mm),
(re.compile('^(-?[0-9\.]+)\s*pt$'), 1),
(re.compile('^(-?[0-9\.]+)\s*$'), 1)
]
allowPercentage = False
allowStar = False
def fromUnicode(self, value):
if value == 'None':
return None
if value == '*' and self.allowStar:
return value
if value.endswith('%') and self.allowPercentage:
return value
for unit in self.units:
res = unit[0].search(value, 0)
if res:
return unit[1]*float(res.group(1))
raise ValueError(
'The value %r is not a valid measurement. %s' % (
value, getFileInfo(self.context)))
class File(Text):
"""This field will return a file object.
The value itself can eith be be a relative or absolute path. Additionally
the following syntax is supported: [path.to.python.mpackage]/path/to/file
"""
open = staticmethod(urllib.urlopen)
packageExtract = re.compile('^\[([0-9A-z_.]*)\]/(.*)$')
doNotOpen = False
def __init__(self, doNotOpen=False, *args, **kw):
super(File, self).__init__(*args, **kw)
self.doNotOpen = doNotOpen
def fromUnicode(self, value):
# Check whether the value is of the form:
# [<module.path>]/rel/path/image.gif"
if value.startswith('['):
result = self.packageExtract.match(value)
if result is None:
raise ValueError(
'The package-path-pair you specified was incorrect. %s' %(
getFileInfo(self.context)))
modulepath, path = result.groups()
module = __import__(modulepath, {}, {}, (modulepath))
value = os.path.join(os.path.dirname(module.__file__), path)
# If there is a drive name in the path, then we want a local file to
# be opened. This is only interesting for Windows of course.
if os.path.splitdrive(value)[0]:
value = 'file:///' + value
# If the file is not to be opened, simply return the path.
if self.doNotOpen:
return value
# Open/Download the file
fileObj = self.open(value)
sio = cStringIO.StringIO(fileObj.read())
fileObj.close()
sio.seek(0)
return sio
class Image(File):
"""Similar to the file File attribute, except that an image is internally
expected."""
def __init__(self, onlyOpen=False, *args, **kw):
super(Image, self).__init__(*args, **kw)
self.onlyOpen = onlyOpen
def fromUnicode(self, value):
fileObj = super(Image, self).fromUnicode(value)
if self.onlyOpen:
return fileObj
return reportlab.lib.utils.ImageReader(fileObj)
class Color(RMLAttribute):
"""Requires the input of a color. There are several supported formats.
Three values in a row are interpreted as RGB value ranging from 0-255.
A string is interpreted as a name to a pre-defined color.
The 'CMYK()' wrapper around four values represents a CMYK color
specification.
"""
def __init__(self, acceptNone=False, *args, **kw):
super(Color, self).__init__(*args, **kw)
self.acceptNone = acceptNone
def fromUnicode(self, value):
if self.acceptNone and value.lower() == 'none':
return None
manager = getManager(self.context)
if value.startswith('rml:'):
value = manager.names[value[4:]]
if value in manager.colors:
return manager.colors[value]
try:
return reportlab.lib.colors.toColor(value)
# Bare except, since code raises string exception: Invalid color value
except:
raise ValueError(
'The color specification "%s" is not valid. %s' % (
value, getFileInfo(self.context)))
def _getStyle(context, value):
manager = getManager(context)
for styles in (manager.styles,
reportlab.lib.styles.getSampleStyleSheet().byName):
if value in styles:
return styles[value]
elif 'style.' + value in styles:
return styles['style.' + value]
elif value.startswith('style.') and value[6:] in styles:
return styles[value[6:]]
raise ValueError('Style %r could not be found. %s' % (
value, getFileInfo(context)))
class Style(String):
"""Requires a valid style to be entered.
Whether the style is a paragraph, table or box style is irrelevant, except
that it has to fit the tag.
"""
default = reportlab.lib.styles.getSampleStyleSheet().byName['Normal']
def fromUnicode(self, value):
return _getStyle(self.context, value)
class Padding(Sequence):
"""This attribute is specific for padding and will produce the proper
length of the padding sequence."""
def __init__(self, *args, **kw):
kw.update(dict(value_type=Integer(), min_length=1, max_length=4))
super(Padding, self).__init__(*args, **kw)
def fromUnicode(self, value):
seq = super(Padding, self).fromUnicode(value)
# pdfgen does not like a single paddign value.
if len(seq) == 1:
seq.append(seq[0])
return seq
class Symbol(Text):
"""This attribute should contain the text representation of a symbol to be
used."""
def fromUnicode(self, value):
return reportlab.graphics.widgets.markers.makeMarker(value)
class PageSize(RMLAttribute):
"""A simple measurement pair that specifies the page size. Optionally you
can also specify a the name of a page size, such as A4, letter, or legal.
"""
sizePair = Sequence(value_type=Measurement())
words = Sequence(value_type=String())
def fromUnicode(self, value):
# First try to get a pair
try:
return self.sizePair.bind(self.context).fromUnicode(value)
except ValueError:
pass
# Now we try to lookup a name. The following type of combinations must
# work: "Letter" "LETTER" "A4 landscape" "letter portrait"
words = self.words.bind(self.context).fromUnicode(value)
words = [word.lower() for word in words]
# First look for the orientation
orienter = None
for orientation in ('landscape', 'portrait'):
if orientation in words:
orienter = getattr(reportlab.lib.pagesizes, orientation)
words.remove(orientation)
# We must have exactely one value left that matches a paper size
pagesize = getattr(reportlab.lib.pagesizes, words[0].upper())
# Now do the final touches
if orienter:
pagesize = orienter(pagesize)
return pagesize
class TextNode(RMLAttribute):
"""Return the text content of an element."""
def get(self):
if self.context.element.text is None:
return u''
return unicode(self.context.element.text).strip()
class FirstLevelTextNode(TextNode):
"""Gets all the text content of an element without traversing into any
child-elements."""
def get(self):
text = self.context.element.text or u''
for child in self.context.element.getchildren():
text += child.tail or u''
return text.strip()
class TextNodeSequence(Sequence, TextNode):
"""A sequence of values retrieved from the element's content."""
def get(self):
return self.fromUnicode(self.context.element.text)
class TextNodeGrid(TextNodeSequence):
"""A grid/matrix of values retrieved from the element's content.
The number of columns is specified for every case, but the number of rows
is dynamic.
"""
def __init__(self, columns=None, *args, **kw):
super(TextNodeGrid, self).__init__(*args, **kw)
self.columns = columns
def fromUnicode(self, ustr):
result = super(TextNodeGrid, self).fromUnicode(ustr)
if len(result) % self.columns != 0:
raise ValueError(
'Number of elements must be divisible by %i. %s' %(
self.columns, getFileInfo(self.context)))
return [result[i*self.columns:(i+1)*self.columns]
for i in range(len(result)/self.columns)]
class RawXMLContent(RMLAttribute):
"""Retrieve the raw content of an element.
Only some special element substitution will be made.
"""
def __init__(self, *args, **kw):
super(RawXMLContent, self).__init__(*args, **kw)
def get(self):
# ReportLab's paragraph parser does not like attributes from other
# namespaces; sigh. So we have to improvize.
text = etree.tounicode(self.context.element)
text = text[text.find('>')+1:text.rfind('<')]
return text
class XMLContent(RawXMLContent):
"""Same as 'RawXMLContent', except that the whitespace is normalized."""
def get(self):
text = super(XMLContent, self).get()
return text.strip().replace('\t', ' ')
|