| 1 |
# -*- coding: utf-8 -*- |
| 2 |
# Copyright 2011 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 rackdiag.elements import RackItem |
| 21 |
from rackdiag.utils.rst.nodes import rackdiag as rackdiag_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 RackdiagDirective(directives.BlockdiagDirective): |
| 35 |
name = "rackdiag" |
| 36 |
node_class = rackdiag_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(RackItem.desctable) - 1) |
| 45 |
headers = [RackItem.attrname[name] for name in RackItem.desctable] |
| 46 |
|
| 47 |
descriptions = [n.to_desctable() for n in nodes()] |
| 48 |
descriptions.sort(key=cmp_to_key(directives.cmp_node_number)) |
| 49 |
|
| 50 |
# records for total |
| 51 |
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) |
| 56 |
|
| 57 |
for i in range(len(headers) - 1, -1, -1): |
| 58 |
if any(desc[i] for desc in descriptions): |
| 59 |
pass |
| 60 |
else: |
| 61 |
widths.pop(i) |
| 62 |
headers.pop(i) |
| 63 |
for desc in descriptions: |
| 64 |
desc.pop(i) |
| 65 |
|
| 66 |
return self._description_table(descriptions, widths, headers) |
| 67 |
|
| 68 |
|
| 69 |
def setup(**kwargs): |
| 70 |
for key, value in directive_options_default.items(): |
| 71 |
directive_options[key] = kwargs.get(key, value) |
| 72 |
|
| 73 |
rst.directives.register_directive("rackdiag", RackdiagDirective) |