/usr/share/djvusmooth/lib/models/outline.py is in djvusmooth 0.2.16-1.
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 | # encoding=UTF-8
# Copyright © 2008-2012 Jakub Wilk <jwilk@jwilk.net>
#
# This file is part of djvusmooth.
#
# djvusmooth is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# djvusmooth 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 General Public License for
# more details.
'''
Models for document outline.
See Lizardtech DjVu Reference (DjVu 3):
- 4 What's new in DjVu File Format.
- 8.3.3 Document Outline Chunk.
'''
import weakref
import itertools
import djvu.sexpr
import djvu.const
from djvusmooth.varietes import not_overridden, wref, fix_uri, indents_to_tree
class Node(object):
def __init__(self, sexpr, owner):
self._owner = owner
self._type = None
self._link_left = self._link_right = self._link_parent = wref(None)
def _set_children(self, children):
self._children = list(children)
prev = None
for child in self._children:
child._link_left = wref(prev)
child._link_parent = wref(self)
prev = child
prev = None
for child in reversed(self._children):
child._link_right = wref(prev)
prev = child
def add_child(self, node):
self._children
if self._children:
self._children[-1]._link_ref = wref(node)
node._link_right = wref(self._children[-1])
node._link_parent = wref(self)
self._children += node,
self._notify_children_change()
def remove_child(self, child):
child_idx = self._children.index(child)
if child_idx - 1 >= 0:
self._children[child_idx - 1]._link_right = child._link_right
if child_idx + 1 < len(self._children):
self._children[child_idx + 1]._link_left = child._link_left
child._link_left = child._link_right = child._link_parent = wref(None)
del self._children[child_idx]
self._notify_children_change()
uri = property()
text = property()
@property
def sexpr(self):
return self._construct_sexpr()
def _construct_sexpr(self):
raise NotImplementedError
@property
def type(self):
return self._type
def __getitem__(self, item):
return self._children[item]
def __len__(self):
return len(self._children)
def __iter__(self):
return iter(self._children)
@apply
def left_sibling():
def get(self):
link = self._link_left()
if link is None:
raise StopIteration
return link
return property(get)
@apply
def right_sibling():
def get(self):
link = self._link_right()
if link is None:
raise StopIteration
return link
return property(get)
@apply
def parent():
def get(self):
link = self._link_parent()
if link is None:
raise StopIteration
return link
return property(get)
@apply
def left_child():
def get(self):
return iter(self).next()
return property(get)
def delete(self):
raise NotImplementedError
def notify_select(self):
self._owner.notify_node_select(self)
def _notify_children_change(self):
return self._owner.notify_node_children_change(self)
class RootNode(Node):
def __init__(self, sexpr, owner):
Node.__init__(self, sexpr, owner)
sexpr = iter(sexpr)
self._type = sexpr.next().value
self._set_children(InnerNode(subexpr, owner) for subexpr in sexpr)
def _construct_sexpr(self):
return djvu.sexpr.Expression(itertools.chain(
(self.type,),
(child._construct_sexpr() for child in self._children)
))
def export_as_plaintext(self, stream):
for child in self:
child.export_as_plaintext(stream, indent=0)
class InnerNode(Node):
def __init__(self, sexpr, owner):
Node.__init__(self, sexpr, owner)
sexpr = iter(sexpr)
self._text = sexpr.next().value.decode('UTF-8', 'replace')
self._uri = fix_uri(sexpr.next().value)
self._set_children(InnerNode(subexpr, owner) for subexpr in sexpr)
def _construct_sexpr(self):
return djvu.sexpr.Expression(itertools.chain(
(self.text, self.uri),
(child._construct_sexpr() for child in self._children)
))
@apply
def uri():
def get(self):
return self._uri
def set(self, value):
self._uri = value
self._notify_change()
return property(get, set)
@apply
def text():
def get(self):
return self._text
def set(self, value):
self._text = value
self._notify_change()
return property(get, set)
def _notify_change(self):
self._owner.notify_node_change(self)
def export_as_plaintext(self, stream, indent):
stream.write(' ' * indent)
stream.write(self.uri)
stream.write(' ')
stream.write(self.text.encode('UTF-8')) # TODO: what about control characters etc.?
stream.write('\n')
for child in self:
child.export_as_plaintext(stream, indent=(indent + 1))
def delete(self):
try:
parent = self.parent
except StopIteration:
return
parent.remove_child(self)
class OutlineCallback(object):
@not_overridden
def notify_tree_change(self, node):
pass
@not_overridden
def notify_node_change(self, node):
pass
@not_overridden
def notify_node_children_change(self, node):
pass
@not_overridden
def notify_node_select(self, node):
pass
class Outline(object):
def __init__(self):
self._callbacks = weakref.WeakKeyDictionary()
self._original_sexpr = self.acquire_data()
self.revert()
def register_callback(self, callback):
if not isinstance(callback, OutlineCallback):
raise TypeError
self._callbacks[callback] = 1
@apply
def root():
def get(self):
return self._root
return property(get)
@apply
def raw_value():
def get(self):
return self._root.sexpr
def set(self, sexpr):
if not sexpr:
sexpr = djvu.const.EMPTY_OUTLINE
self._root = RootNode(sexpr, self)
self.notify_tree_change()
return property(get, set)
def remove(self):
self.raw_value = djvu.const.EMPTY_OUTLINE
def revert(self):
self.raw_value = self._original_sexpr
self._dirty = False
def acquire_data(self):
return djvu.const.EMPTY_OUTLINE
def notify_tree_change(self):
self._dirty = True
for callback in self._callbacks:
callback.notify_tree_change(self._root)
def notify_node_change(self, node):
self._dirty = True
for callback in self._callbacks:
callback.notify_node_change(node)
def notify_node_children_change(self, node):
self._dirty = True
for callback in self._callbacks:
callback.notify_node_children_change(node)
def notify_node_select(self, node):
for callback in self._callbacks:
callback.notify_node_select(node)
def export(self, djvused):
if self.root:
value = self.raw_value
else:
value = None
djvused.set_outline(value)
def export_as_plaintext(self, stream):
return self.root.export_as_plaintext(stream)
def import_plaintext(self, lines):
def fix_node(node):
it = iter(node)
it.next()
for subnode in it:
fix_node(subnode)
text = node[0]
if text is not None:
node[0:1] = (text.split(None, 1) + ['', ''])[1::-1]
tree = indents_to_tree(lines)
fix_node(tree)
tree[0:1] = djvu.const.EMPTY_OUTLINE
self.raw_value = djvu.sexpr.Expression(tree)
# vim:ts=4 sts=4 sw=4 et
|