clone

a browser-first diagram editor with live svg previews, local drafts and encrypted, accountless sharing

main
diagramzip / renderers / python / blockdiag-family / src / packetdiag / utils / rst / directives.py
directives.py 2.3K
1# -*- coding: utf-8 -*-2# Copyright 2012 Takeshi KOMIYA3#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 at7#8# http://www.apache.org/licenses/LICENSE-2.09#10# Unless required by applicable law or agreed to in writing, software11# 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 and14# limitations under the License.1516from blockdiag.utils.compat import cmp_to_key17from blockdiag.utils.rst import directives18from docutils.parsers import rst1920from packetdiag.elements import FieldItem21from packetdiag.utils.rst.nodes import packetdiag as packetdiag_node2223directive_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)31directive_options = {}323334class PacketdiagDirective(directives.BlockdiagDirective):35 name = "packetdiag"36 node_class = packetdiag_node3738 @property39 def global_options(self):40 return directive_options4142 def description_table(self, diagram):43 nodes = diagram.traverse_nodes44 widths = [25] + [50] * (len(FieldItem.desctable) - 1)45 headers = [FieldItem.attrname[name] for name in FieldItem.desctable]4647 descriptions = [n.to_desctable() for n in nodes()]48 descriptions.sort(key=cmp_to_key(directives.cmp_node_number))4950 for i in range(len(headers) - 1, -1, -1):51 if any(desc[i] for desc in descriptions):52 pass53 else:54 widths.pop(i)55 headers.pop(i)56 for desc in descriptions:57 desc.pop(i)5859 return self._description_table(descriptions, widths, headers)606162def setup(**kwargs):63 for key, value in directive_options_default.items():64 directive_options[key] = kwargs.get(key, value)6566 rst.directives.register_directive("packetdiag", PacketdiagDirective)