/usr/lib/python3/dist-packages/packetdiag/builder.py is in python3-nwdiag 1.0.3-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 | # -*- 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.
from __future__ import division
from packetdiag import parser
from packetdiag.elements import Diagram, FieldItem, DiagramNode
from blockdiag.utils import unquote, XY
class DiagramTreeBuilder:
def build(self, tree):
self.diagram = Diagram()
self.instantiate(tree)
return self.diagram
def instantiate(self, tree):
for stmt in tree.stmts:
if isinstance(stmt, parser.Attr):
self.diagram.set_attribute(stmt)
elif isinstance(stmt, parser.FieldItem):
item = FieldItem(stmt.begin, stmt.end, unquote(stmt.label))
item.set_attributes(stmt.attrs)
if item.number is None:
if len(self.diagram.fields) == 0:
item.number = 0
else:
last_item = self.diagram.fields[-1]
item.number = last_item.number + last_item.colwidth
self.diagram.fields.append(item)
elif isinstance(stmt, parser.AttrPlugin):
self.diagram.set_plugin(stmt.name, stmt.attrs)
class DiagramLayoutManager:
def __init__(self, diagram):
self.diagram = diagram
def split_field_by_column(self):
for field in self.diagram.fields:
while True:
x = field.number % self.diagram.colwidth
if x + field.colwidth <= self.diagram.colwidth:
break
else:
colwidth = self.diagram.colwidth - x
splitted = field.duplicate()
splitted.colwidth = colwidth
if self.diagram.scale_direction == "left_to_right":
splitted.separated_right = True
else:
splitted.separated_left = True
self.diagram.fields.append(splitted)
field.number += colwidth
field.colwidth -= colwidth
if self.diagram.scale_direction == "left_to_right":
field.separated_left = True
else:
field.separated_right = True
yield field
def run(self):
filled = {}
for field in self.split_field_by_column():
x = field.number % self.diagram.colwidth
y = field.number // self.diagram.colwidth
if filled.get(y) is None:
filled[y] = {}
for rx in range(x, x + field.colwidth):
if filled[y].get(rx):
msg = ("Field '%s' is conflicted to other field\n" %
field.label)
raise AttributeError(msg)
filled[y][rx] = True
if self.diagram.scale_direction == "right_to_left":
x = self.diagram.colwidth - x - field.colwidth
field.xy = XY(x, y)
self.diagram.fixiate()
class ScreenNodeBuilder:
@classmethod
def build(cls, tree):
DiagramNode.clear()
Diagram.clear()
return cls(tree).run()
def __init__(self, tree):
self.diagram = DiagramTreeBuilder().build(tree)
def run(self):
DiagramLayoutManager(self.diagram).run()
return self.diagram
|