1# -*- coding: utf-8 -*-2# Copyright 2011 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 rackdiag.elements import RackItem21from rackdiag.utils.rst.nodes import rackdiag as rackdiag_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 RackdiagDirective(directives.BlockdiagDirective):35 name = "rackdiag"36 node_class = rackdiag_node373839 def global_options(self):40 return directive_options4142 def description_table(self, diagram):43 nodes = diagram.traverse_nodes44 widths = [25] + [50] * (len(RackItem.desctable) - 1)45 headers = [RackItem.attrname[name] for name in RackItem.desctable]4647 descriptions = [n.to_desctable() for n in nodes()]48 descriptions.sort(key=cmp_to_key(directives.cmp_node_number))4950 # records for total51 total = ['-', 'Total'] + [''] * (len(RackItem.desctable) - 2)52 total[2] = "%dU" % sum(n.colheight for n in nodes() if n.colheight)53 total[3] = "%.1fA" % sum(n.ampere for n in nodes() if n.ampere)54 total[4] = "%.1fkg" % sum(n.weight for n in nodes() if n.weight)55 descriptions.append(total)5657 for i in range(len(headers) - 1, -1, -1):58 if any(desc[i] for desc in descriptions):59 pass60 else:61 widths.pop(i)62 headers.pop(i)63 for desc in descriptions:64 desc.pop(i)6566 return self._description_table(descriptions, widths, headers)676869def setup(**kwargs):70 for key, value in directive_options_default.items():71 directive_options[key] = kwargs.get(key, value)7273 rst.directives.register_directive("rackdiag", RackdiagDirective)