/usr/share/pyshared/pdfrw/pdfobjects.py is in python-pdfrw 0+svn136-3.
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 | # A part of pdfrw (pdfrw.googlecode.com)
# Copyright (C) 2006-2009 Patrick Maupin, Austin, Texas
# MIT license -- See LICENSE.txt for details
'''
Objects that can occur in PDF files. The most important
objects are arrays and dicts. Either of these can be
indirect or not, and dicts could have an associated
stream.
'''
from __future__ import generators
try:
set
except NameError:
from sets import Set as set
import re
class PdfObject(str):
indirect = False
class PdfArray(list):
indirect = False
class PdfName(object):
def __getattr__(self, name):
return self(name)
def __call__(self, name):
return PdfObject('/' + name)
PdfName = PdfName()
class PdfString(str):
indirect = False
unescape_dict = {'\\b':'\b', '\\f':'\f', '\\n':'\n',
'\\r':'\r', '\\t':'\t',
'\\\r\n': '', '\\\r':'', '\\\n':'',
'\\\\':'\\', '\\':'',
}
unescape_pattern = r'(\\b|\\f|\\n|\\r|\\t|\\\r\n|\\\r|\\\n|\\[0-9]+|\\)'
unescape_func = re.compile(unescape_pattern).split
hex_pattern = '([a-fA-F0-9][a-fA-F0-9]|[a-fA-F0-9])'
hex_func = re.compile(hex_pattern).split
hex_pattern2 = '([a-fA-F0-9][a-fA-F0-9][a-fA-F0-9][a-fA-F0-9]|[a-fA-F0-9][a-fA-F0-9]|[a-fA-F0-9])'
hex_func2 = re.compile(hex_pattern2).split
hex_funcs = hex_func, hex_func2
indirect = False
def decode_regular(self, remap=chr):
assert self[0] == '(' and self[-1] == ')'
mylist = self.unescape_func(self[1:-1])
result = []
unescape = self.unescape_dict.get
for chunk in mylist:
chunk = unescape(chunk, chunk)
if chunk.startswith('\\') and len(chunk) > 1:
value = int(chunk[1:], 8)
# FIXME: TODO: Handle unicode here
if value > 127:
value = 127
chunk = remap(value)
if chunk:
result.append(chunk)
return ''.join(result)
def decode_hex(self, remap=chr, twobytes=False):
data = ''.join(self.split())
data = self.hex_funcs[twobytes](data)
chars = data[1::2]
other = data[0::2]
assert other[0] == '<' and other[-1] == '>' and ''.join(other) == '<>', self
return ''.join([remap(int(x, 16)) for x in chars])
def decode(self, remap=chr, twobytes=False):
if self.startswith('('):
return self.decode_regular(remap)
else:
return self.decode_hex(remap, twobytes)
def encode(cls, source, usehex=False):
assert not usehex, "Not supported yet"
if isinstance(source, unicode):
source = source.encode('utf-8')
else:
source = str(source)
source = source.replace('\\', '\\\\')
source = source.replace('(', '\\(')
source = source.replace(')', '\\)')
return cls('(' +source + ')')
encode = classmethod(encode)
class PdfDict(dict):
indirect = False
stream = None
_special = dict(indirect = ('indirect', False),
stream = ('stream', True),
_stream = ('stream', False),
)
def __setitem__(self, name, value):
assert name.startswith('/'), name
if value is not None:
dict.__setitem__(self, name, value)
elif name in self:
del self[name]
def __init__(self, *args, **kw):
if args:
if len(args) == 1:
args = args[0]
self.update(args)
if isinstance(args, PdfDict):
self.indirect = args.indirect
self._stream = args.stream
for key, value in kw.iteritems():
setattr(self, key, value)
def __getattr__(self, name):
return self.get(PdfName(name))
def __setattr__(self, name, value):
info = self._special.get(name)
if info is None:
self[PdfName(name)] = value
else:
name, setlen = info
self.__dict__[name] = value
if setlen:
notnone = value is not None
self.Length = notnone and PdfObject(len(value)) or None
def iteritems(self):
for key, value in dict.iteritems(self):
if value is not None:
assert key.startswith('/'), (key, value)
yield key, value
def inheritable(self):
''' Search through ancestors as needed for inheritable
dictionary items
'''
class Search(object):
def __init__(self, basedict):
self.basedict = basedict
def __getattr__(self, name):
return self[name]
def __getitem__(self, name):
visited = set()
mydict = self.basedict
while 1:
value = getattr(mydict, name)
if value is not None:
return value
myid = id(mydict)
assert myid not in visited
visited.add(myid)
mydict = mydict.Parent
if mydict is None:
return
return Search(self)
inheritable = property(inheritable)
def private(self):
''' Allows setting private metadata for use in
processing (not sent to PDF file)
'''
class Private(object):
pass
result = Private()
result.__dict__ = self.__dict__
return result
private = property(private)
class IndirectPdfDict(PdfDict):
indirect = True
|