This file is indexed.

/usr/lib/python3/dist-packages/blockdiag/elements.py is in python3-blockdiag 1.4.7-2ubuntu1.

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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# -*- coding: utf-8 -*-
#  Copyright 2011 Takeshi KOMIYA
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
import re
import copy
from blockdiag.utils import images, unquote, urlutil, uuid, XY
from blockdiag.utils.compat import u
from blockdiag.utils.logging import warning
from blockdiag import noderenderer, plugins


class Base(object):
    basecolor = (255, 255, 255)
    textcolor = (0, 0, 0)
    fontfamily = None
    fontsize = None
    style = None
    int_attrs = ['colwidth', 'colheight', 'fontsize']

    @classmethod
    def set_default_color(cls, color):
        cls.basecolor = images.color_to_rgb(color)

    @classmethod
    def set_default_style(cls, style):
        cls.style = style

    @classmethod
    def set_default_text_color(cls, color):
        cls.textcolor = images.color_to_rgb(color)

    @classmethod
    def set_default_fontfamily(cls, fontfamily):
        cls.fontfamily = fontfamily

    @classmethod
    def set_default_fontsize(cls, fontsize):
        cls.fontsize = int(fontsize)

    @classmethod
    def clear(cls):
        cls.basecolor = (255, 255, 255)
        cls.textcolor = (0, 0, 0)
        cls.fontfamily = None
        cls.fontsize = None
        cls.style = None

    def duplicate(self):
        return copy.copy(self)

    def set_attribute(self, attr):
        name = attr.name
        value = unquote(attr.value)

        if name == 'class':
            if value in Diagram.classes:
                klass = Diagram.classes[value]
                self.set_attributes(klass.attrs)
            else:
                msg = "Unknown class: %s" % value
                raise AttributeError(msg)
        elif hasattr(self, "set_%s" % name):
            getattr(self, "set_%s" % name)(value)
        elif name in self.int_attrs:
            setattr(self, name, int(value))
        elif hasattr(self, name) and not callable(getattr(self, name)):
            setattr(self, name, value)
        else:
            class_name = self.__class__.__name__
            msg = "Unknown attribute: %s.%s" % (class_name, attr.name)
            raise AttributeError(msg)

    def set_attributes(self, attrs):
        for attr in attrs:
            self.set_attribute(attr)

    def set_style(self, value):
        if re.search('^(?:none|solid|dotted|dashed|\d+(,\d+)*)$', value, re.I):
            self.style = value.lower()
        else:
            class_name = self.__class__.__name__
            msg = "unknown %s style: %s" % (class_name, value)
            raise AttributeError(msg)


class Element(Base):
    namespace = {}
    int_attrs = Base.int_attrs + ['width', 'height']

    @classmethod
    def get(cls, elemid):
        if not elemid:
            elemid = uuid.generate()

        unquote_id = unquote(elemid)
        if unquote_id not in cls.namespace:
            obj = cls(elemid)
            cls.namespace[unquote_id] = obj

        return cls.namespace[unquote_id]

    @classmethod
    def clear(cls):
        super(Element, cls).clear()
        cls.namespace = {}
        cls.basecolor = (255, 255, 255)
        cls.textcolor = (0, 0, 0)

    def __init__(self, elemid):
        self.id = unquote(elemid)
        self.label = ''
        self.xy = XY(0, 0)
        self.group = None
        self.drawable = False
        self.order = 0
        self.color = self.basecolor
        self.width = None
        self.height = None
        self.colwidth = 1
        self.colheight = 1
        self.stacked = False

    def __repr__(self):
        _format = "<%s '%s' %s %dx%d at 0x%08x>"
        params = (self.__class__.__name__, self.id, str(self.xy),
                  self.colwidth, self.colheight, id(self))
        return _format % params

    def set_color(self, color):
        self.color = images.color_to_rgb(color)

    def set_textcolor(self, color):
        self.textcolor = images.color_to_rgb(color)


class DiagramNode(Element):
    shape = 'box'
    int_attrs = Element.int_attrs + ['rotate']
    linecolor = (0, 0, 0)
    label_orientation = 'horizontal'
    desctable = []
    attrname = {}

    @classmethod
    def set_default_shape(cls, shape):
        cls.shape = shape

    @classmethod
    def set_default_linecolor(cls, color):
        cls.linecolor = images.color_to_rgb(color)

    @classmethod
    def clear(cls):
        super(DiagramNode, cls).clear()
        cls.shape = 'box'
        cls.linecolor = (0, 0, 0)
        cls.label_orientation = 'horizontal'
        cls.desctable = ['numbered', 'label', 'description']
        cls.attrname = dict(numbered='No', label='Name',
                            description='Description')

    def __init__(self, elemid):
        super(DiagramNode, self).__init__(elemid)

        self.label = unquote(elemid) or ''
        self.numbered = None
        self.icon = None
        self.background = None
        self.description = None
        self.rotate = 0
        self.drawable = True
        self.href = None

        plugins.fire_node_event(self, 'created')

    def set_attribute(self, attr):
        if plugins.fire_node_event(self, 'attr_changing', attr):
            super(DiagramNode, self).set_attribute(attr)
            plugins.fire_node_event(self, 'attr_changed', attr)

    def set_linecolor(self, color):
        self.linecolor = images.color_to_rgb(color)

    def set_shape(self, value):
        if noderenderer.get(value):
            self.shape = value
        else:
            msg = "unknown node shape: %s" % value
            raise AttributeError(msg)

    def set_icon(self, value):
        if urlutil.isurl(value) or os.path.isfile(value):
            self.icon = value
        else:
            warning("icon image not found: %s", value)

    def set_background(self, value):
        if urlutil.isurl(value) or os.path.isfile(value):
            self.background = value
        else:
            warning("background image not found: %s", value)

    def set_stacked(self, _):
        self.stacked = True

    def set_label_orientation(self, value):
        value = value.lower()
        if value in ('horizontal', 'vertical'):
            self.label_orientation = value
        else:
            msg = "unknown label orientation: %s" % value
            raise AttributeError(msg)

    def to_desctable(self):
        attrs = []
        for name in self.desctable:
            value = getattr(self, name)
            if value is None:
                attrs.append(u(""))
            else:
                attrs.append(value)

        return attrs


class NodeGroup(Element):
    basecolor = (243, 152, 0)

    @classmethod
    def clear(cls):
        super(NodeGroup, cls).clear()
        cls.basecolor = (243, 152, 0)

    def __init__(self, elemid):
        super(NodeGroup, self).__init__(elemid)

        self.level = 0
        self.separated = False
        self.shape = 'box'
        self.thick = 3
        self.nodes = []
        self.edges = []
        self.icon = None
        self.orientation = 'landscape'
        self.href = None

    def duplicate(self):
        copied = super(NodeGroup, self).duplicate()
        copied.nodes = []
        copied.edges = []

        return copied

    def parent(self, level):
        if self.level < level:
            return None

        group = self
        while group.level != level:
            group = group.group

        return group

    def is_parent(self, other):
        parent = self.parent(other.level)
        return parent == other

    def traverse_nodes(self, preorder=False):
        for node in self.nodes:
            if isinstance(node, NodeGroup):
                if preorder:
                    yield node

                for subnode in node.traverse_nodes(preorder=preorder):
                    yield subnode

                if not preorder:
                    yield node
            else:
                yield node

    def traverse_edges(self, preorder=False):
        if preorder:
            for edge in self.edges:
                yield edge

        for group in self.traverse_groups(preorder):
            for edge in group.traverse_edges(preorder):
                yield edge

        if not preorder:
            for edge in self.edges:
                yield edge

    def traverse_groups(self, preorder=False):
        for node in self.traverse_nodes(preorder=preorder):
            if isinstance(node, NodeGroup):
                yield node

    def fixiate(self, fixiate_nodes=False):
        if self.separated:
            self.colwidth = 1
            self.colheight = 1

            return
        elif len(self.nodes) > 0:
            self.colwidth = max(x.xy.x + x.colwidth for x in self.nodes)
            self.colheight = max(x.xy.y + x.colheight for x in self.nodes)

        for node in self.nodes:
            if fixiate_nodes:
                node.xy = XY(self.xy.x + node.xy.x,
                             self.xy.y + node.xy.y)

            if isinstance(node, NodeGroup):
                node.fixiate(fixiate_nodes)

    def update_order(self):
        for i, node in enumerate(self.nodes):
            node.order = i

    def set_orientation(self, value):
        value = value.lower()
        if value in ('landscape', 'portrait'):
            self.orientation = value
        else:
            msg = "unknown diagram orientation: %s" % value
            raise AttributeError(msg)

    def set_shape(self, value):
        value = value.lower()
        if value in ('box', 'line'):
            self.shape = value
        else:
            msg = "unknown group shape: %s" % value
            raise AttributeError(msg)


class DiagramEdge(Base):
    basecolor = (0, 0, 0)
    namespace = {}

    @classmethod
    def get(cls, node1, node2):
        if node1 not in cls.namespace:
            cls.namespace[node1] = {}

        if node2 not in cls.namespace[node1]:
            obj = cls(node1, node2)
            cls.namespace[node1][node2] = obj

        return cls.namespace[node1][node2]

    @classmethod
    def find(cls, node1, node2=None):
        if node1 is None and node2 is None:
            return cls.find_all()
        elif isinstance(node1, NodeGroup):
            edges = cls.find(None, node2)
            edges = (e for e in edges if e.node1.group.is_parent(node1))
            return [e for e in edges if not e.node2.group.is_parent(node1)]
        elif isinstance(node2, NodeGroup):
            edges = cls.find(node1, None)
            edges = (e for e in edges if e.node2.group.is_parent(node2))
            return [e for e in edges if not e.node1.group.is_parent(node2)]
        elif node1 is None:
            return [e for e in cls.find_all() if e.node2 == node2]
        else:
            if node1 not in cls.namespace:
                return []

            if node2 is None:
                return cls.namespace[node1].values()

            if node2 not in cls.namespace[node1]:
                return []

        return cls.namespace[node1][node2]

    @classmethod
    def find_all(cls):
        for v1 in cls.namespace.values():
            for v2 in v1.values():
                yield v2

    @classmethod
    def find_by_level(cls, level):
        edges = []
        for e in cls.find_all():
            edge = e.duplicate()
            skips = 0

            if edge.node1.group.level < level:
                skips += 1
            else:
                while edge.node1.group.level != level:
                    edge.node1 = edge.node1.group

            if edge.node2.group.level < level:
                skips += 1
            else:
                while edge.node2.group.level != level:
                    edge.node2 = edge.node2.group

            if skips == 2:
                continue

            edges.append(edge)

        return edges

    @classmethod
    def clear(cls):
        super(DiagramEdge, cls).clear()
        cls.namespace = {}
        cls.basecolor = (0, 0, 0)

    def __init__(self, node1, node2):
        self.node1 = node1
        self.node2 = node2
        self.crosspoints = []
        self.skipped = 0

        self.label = None
        self.description = None
        self.dir = 'forward'
        self.color = self.basecolor
        self.hstyle = None
        self.folded = None
        self.thick = None

    def __repr__(self):
        _format = "<%s '%s' %s - '%s' %s at 0x%08x>"
        params = (self.__class__.__name__, self.node1.id, self.node1.xy,
                  self.node2.id, self.node2.xy, id(self))
        return _format % params

    def set_dir(self, value):
        value = value.lower()
        if value in ('back', 'both', 'none', 'forward'):
            self.dir = value
        elif value == '-<':
            self.dir = 'forward'
            self.hstyle = 'onemany'
        elif value == '>-':
            self.dir = 'back'
            self.hstyle = 'manyone'
        elif value == '>-<':
            self.dir = 'both'
            self.hstyle = 'manymany'
        elif value == '->':
            self.dir = 'forward'
        elif value == '<-':
            self.dir = 'back'
        elif value == '<->':
            self.dir = 'both'
        elif value == '--':
            self.dir = 'none'
        else:
            msg = "unknown edge dir: %s" % value
            raise AttributeError(msg)

    def set_color(self, color):
        self.color = images.color_to_rgb(color)

    def set_hstyle(self, value):
        value = value.lower()
        if value in ('generalization', 'composition', 'aggregation'):
            self.hstyle = value
        elif value == 'oneone':
            self.dir = 'none'
            self.hstyle = value
        elif value == 'onemany':
            self.dir = 'forward'
            self.hstyle = value
        elif value == 'manyone':
            self.dir = 'back'
            self.hstyle = value
        elif value == 'manymany':
            self.dir = 'both'
            self.hstyle = value
        else:
            msg = "unknown edge hstyle: %s" % value
            raise AttributeError(msg)

    def set_folded(self, _):
        self.folded = True

    def set_nofolded(self, _):
        self.folded = False

    def set_thick(self, _):
        self.thick = 3

    @property
    def direction(self):
        node1 = self.node1.xy
        node2 = self.node2.xy

        if node1.x > node2.x:
            if node1.y > node2.y:
                _dir = 'left-up'
            elif node1.y == node2.y:
                _dir = 'left'
            else:
                _dir = 'left-down'
        elif node1.x == node2.x:
            if node1.y > node2.y:
                _dir = 'up'
            elif node1.y == node2.y:
                _dir = 'same'
            else:
                _dir = 'down'
        else:
            if node1.y > node2.y:
                _dir = 'right-up'
            elif node1.y == node2.y:
                _dir = 'right'
            else:
                _dir = 'right-down'

        return _dir

    def to_desctable(self):
        label = "%s -> %s" % (self.node1.label, self.node2.label)
        return [label, self.description]


class Diagram(NodeGroup):
    _DiagramNode = DiagramNode
    _DiagramEdge = DiagramEdge
    _NodeGroup = NodeGroup

    classes = {}
    shadow_style = 'blur'
    linecolor = (0, 0, 0)
    int_attrs = (NodeGroup.int_attrs +
                 ['node_width', 'node_height', 'span_width', 'span_height'])

    @classmethod
    def clear(cls):
        super(Diagram, cls).clear()
        cls.shadow_style = 'blur'
        cls.linecolor = (0, 0, 0)
        cls.classes = {}

    def __init__(self):
        super(Diagram, self).__init__(None)

        self.config = None
        self.node_width = None
        self.node_height = None
        self.span_width = None
        self.span_height = None
        self.page_padding = None
        self.edge_layout = None

    def set_plugin(self, name, attrs, **kwargs):
        kwargs.update(dict([str(unquote(attr.name)), unquote(attr.value)]
                           for attr in attrs))
        plugins.load([name], diagram=self, **kwargs)

    def set_plugins(self, value, **kwargs):
        modules = [name.strip() for name in value.split(',')]
        plugins.load(modules, diagram=self, **kwargs)

    def set_default_shape(self, value):
        if noderenderer.get(value):
            DiagramNode.set_default_shape(value)
        else:
            msg = "unknown node shape: %s" % value
            raise AttributeError(msg)

    def set_default_label_orientation(self, value):
        value = value.lower()
        if value in ('horizontal', 'vertical'):
            DiagramNode.label_orientation = value
        else:
            msg = "unknown label orientation: %s" % value
            raise AttributeError(msg)

    def set_default_text_color(self, color):
        warning("default_text_color is obsoleted; use default_textcolor")
        self.set_default_textcolor(color)

    def set_default_textcolor(self, color):
        self.textcolor = images.color_to_rgb(color)
        self._DiagramNode.set_default_text_color(self.textcolor)
        self._NodeGroup.set_default_text_color(self.textcolor)
        self._DiagramEdge.set_default_text_color(self.textcolor)

    def set_default_node_color(self, color):
        color = images.color_to_rgb(color)
        self._DiagramNode.set_default_color(color)

    def set_default_node_style(self, value):
        if re.search('^(?:none|solid|dotted|dashed|\d+(,\d+)*)$', value, re.I):
            self._DiagramNode.set_default_style(value)
        else:
            msg = "unknown node style: %s" % value
            raise AttributeError(msg)

    def set_default_line_color(self, color):
        warning("default_line_color is obsoleted; use default_linecolor")
        self.set_default_linecolor(color)

    def set_default_linecolor(self, color):
        self.linecolor = images.color_to_rgb(color)
        self._DiagramNode.set_default_linecolor(self.linecolor)
        self._DiagramEdge.set_default_color(self.linecolor)

    def set_default_group_color(self, color):
        color = images.color_to_rgb(color)
        self._NodeGroup.set_default_color(color)

    def set_shape_namespace(self, value):
        noderenderer.set_default_namespace(value)

    def set_default_fontfamily(self, fontfamily):
        self._DiagramNode.set_default_fontfamily(fontfamily)
        self._NodeGroup.set_default_fontfamily(fontfamily)
        self._DiagramEdge.set_default_fontfamily(fontfamily)

    def set_default_fontsize(self, fontsize):
        self._DiagramNode.set_default_fontsize(fontsize)
        self._NodeGroup.set_default_fontsize(fontsize)
        self._DiagramEdge.set_default_fontsize(fontsize)

    def set_shadow_style(self, value):
        value = value.lower()
        if value in ('solid', 'blur', 'none'):
            self.shadow_style = value
        else:
            msg = "unknown shadow style: %s" % value
            raise AttributeError(msg)

    def set_edge_layout(self, value):
        value = value.lower()
        if value in ('normal', 'flowchart'):
            warning("edge_layout is very experimental feature!")

            self.edge_layout = value
        else:
            msg = "unknown edge layout: %s" % value
            raise AttributeError(msg)

    def set_fontsize(self, value):
        warning("fontsize is obsoleted; use default_fontsize")
        self.set_default_fontsize(int(value))