Debian Codegraph @

src/packetdiag/utils/rst/directives.py

raw
1 # -*- coding: utf-8 -*-
2 # Copyright 2012 Takeshi KOMIYA
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 from blockdiag.utils.compat import cmp_to_key
17 from blockdiag.utils.rst import directives
18 from docutils.parsers import rst
19
20 from packetdiag.elements import FieldItem
21 from packetdiag.utils.rst.nodes import packetdiag as packetdiag_node
22
23 directive_options_default = dict(format='PNG',
24 antialias=False,
25 fontpath=None,
26 outputdir=None,
27 nodoctype=False,
28 noviewbox=False,
29 inline_svg=False,
30 ignore_pil=False)
31 directive_options = {}
32
33
34 class PacketdiagDirective(directives.BlockdiagDirective):
35 name = "packetdiag"
36 node_class = packetdiag_node
37
38 @property
39 def global_options(self):
40 return directive_options
41
42 def description_table(self, diagram):
43 nodes = diagram.traverse_nodes
44 widths = [25] + [50] * (len(FieldItem.desctable) - 1)
45 headers = [FieldItem.attrname[name] for name in FieldItem.desctable]
46
47 descriptions = [n.to_desctable() for n in nodes()]
48 descriptions.sort(key=cmp_to_key(directives.cmp_node_number))
49
50 for i in range(len(headers) - 1, -1, -1):
51 if any(desc[i] for desc in descriptions):
52 pass
53 else:
54 widths.pop(i)
55 headers.pop(i)
56 for desc in descriptions:
57 desc.pop(i)
58
59 return self._description_table(descriptions, widths, headers)
60
61
62 def setup(**kwargs):
63 for key, value in directive_options_default.items():
64 directive_options[key] = kwargs.get(key, value)
65
66 rst.directives.register_directive("packetdiag", PacketdiagDirective)