sphinxcontrib-nwdiag-0.7.0/0000755000175000017500000000000012224001776017074 5ustar katsuwokatsuwo00000000000000sphinxcontrib-nwdiag-0.7.0/PKG-INFO0000644000175000017500000000365012224001776020175 0ustar katsuwokatsuwo00000000000000Metadata-Version: 1.1 Name: sphinxcontrib-nwdiag Version: 0.7.0 Summary: Sphinx "nwdiag" and "rackdiag" extension Home-page: http://bitbucket.org/birkenfeld/sphinx-contrib Author: Takeshi Komiya Author-email: i.tkomiya@gmail.com License: BSD Download-URL: http://pypi.python.org/pypi/sphinxcontrib-nwdiag Description: This package contains the nwdiag Sphinx extension and rackdiag Sphinx extension. .. _Sphinx: http://sphinx.pocoo.org/ .. _nwdiag: http://blockdiag.com/en/nwdiag/ This extension enable you to insert network diagrams into your Sphinx document. Following code is sample:: .. nwdiag:: diagram { network { web01; web02; } network { web01; web02; db01; } } And insert rack structure diagrams:: .. rackdiag:: diagram { rackheight = 12; 1: UPS [height = 3]; 4: DB Server (Master) 5: DB Server (Mirror) 7: Web Server (1) 8: Web Server (1) 9: Web Server (1) 10: LoadBalancer } This module needs nwdiag_. Platform: any Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Environment :: Web Environment Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 Classifier: Topic :: Documentation Classifier: Topic :: Utilities sphinxcontrib-nwdiag-0.7.0/setup.py0000644000175000017500000000375512224001636020613 0ustar katsuwokatsuwo00000000000000# -*- coding: utf-8 -*- from setuptools import setup, find_packages long_desc = ''' This package contains the nwdiag Sphinx extension and rackdiag Sphinx extension. .. _Sphinx: http://sphinx.pocoo.org/ .. _nwdiag: http://blockdiag.com/en/nwdiag/ This extension enable you to insert network diagrams into your Sphinx document. Following code is sample:: .. nwdiag:: diagram { network { web01; web02; } network { web01; web02; db01; } } And insert rack structure diagrams:: .. rackdiag:: diagram { rackheight = 12; 1: UPS [height = 3]; 4: DB Server (Master) 5: DB Server (Mirror) 7: Web Server (1) 8: Web Server (1) 9: Web Server (1) 10: LoadBalancer } This module needs nwdiag_. ''' requires = ['nwdiag>=1.0.0', 'Sphinx>=0.6', 'setuptools'] setup( name='sphinxcontrib-nwdiag', version='0.7.0', url='http://bitbucket.org/birkenfeld/sphinx-contrib', download_url='http://pypi.python.org/pypi/sphinxcontrib-nwdiag', license='BSD', author='Takeshi Komiya', author_email='i.tkomiya@gmail.com', description='Sphinx "nwdiag" and "rackdiag" extension', long_description=long_desc, zip_safe=False, classifiers=[ 'Development Status :: 4 - Beta', 'Environment :: Console', 'Environment :: Web Environment', 'Intended Audience :: Developers', 'License :: OSI Approved :: BSD License', 'Operating System :: OS Independent', 'Programming Language :: Python', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', 'Topic :: Documentation', 'Topic :: Utilities', ], platforms='any', packages=find_packages(), include_package_data=True, install_requires=requires, namespace_packages=['sphinxcontrib'], ) sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/0000755000175000017500000000000012224001776025011 5ustar katsuwokatsuwo00000000000000sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/PKG-INFO0000644000175000017500000000365012224001776026112 0ustar katsuwokatsuwo00000000000000Metadata-Version: 1.1 Name: sphinxcontrib-nwdiag Version: 0.7.0 Summary: Sphinx "nwdiag" and "rackdiag" extension Home-page: http://bitbucket.org/birkenfeld/sphinx-contrib Author: Takeshi Komiya Author-email: i.tkomiya@gmail.com License: BSD Download-URL: http://pypi.python.org/pypi/sphinxcontrib-nwdiag Description: This package contains the nwdiag Sphinx extension and rackdiag Sphinx extension. .. _Sphinx: http://sphinx.pocoo.org/ .. _nwdiag: http://blockdiag.com/en/nwdiag/ This extension enable you to insert network diagrams into your Sphinx document. Following code is sample:: .. nwdiag:: diagram { network { web01; web02; } network { web01; web02; db01; } } And insert rack structure diagrams:: .. rackdiag:: diagram { rackheight = 12; 1: UPS [height = 3]; 4: DB Server (Master) 5: DB Server (Mirror) 7: Web Server (1) 8: Web Server (1) 9: Web Server (1) 10: LoadBalancer } This module needs nwdiag_. Platform: any Classifier: Development Status :: 4 - Beta Classifier: Environment :: Console Classifier: Environment :: Web Environment Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: BSD License Classifier: Operating System :: OS Independent Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 Classifier: Topic :: Documentation Classifier: Topic :: Utilities sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/dependency_links.txt0000644000175000017500000000000112224001776031057 0ustar katsuwokatsuwo00000000000000 sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/top_level.txt0000644000175000017500000000001612224001776027540 0ustar katsuwokatsuwo00000000000000sphinxcontrib sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/not-zip-safe0000644000175000017500000000000111606004647027242 0ustar katsuwokatsuwo00000000000000 sphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/requires.txt0000644000175000017500000000004412224001776027407 0ustar katsuwokatsuwo00000000000000nwdiag>=1.0.0 Sphinx>=0.6 setuptoolssphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/SOURCES.txt0000644000175000017500000000073412224001776026701 0ustar katsuwokatsuwo00000000000000AUTHORS LICENSE MANIFEST.in README.rst setup.cfg setup.py sphinxcontrib/__init__.py sphinxcontrib/nwdiag.py sphinxcontrib/packetdiag.py sphinxcontrib/rackdiag.py sphinxcontrib_nwdiag.egg-info/PKG-INFO sphinxcontrib_nwdiag.egg-info/SOURCES.txt sphinxcontrib_nwdiag.egg-info/dependency_links.txt sphinxcontrib_nwdiag.egg-info/namespace_packages.txt sphinxcontrib_nwdiag.egg-info/not-zip-safe sphinxcontrib_nwdiag.egg-info/requires.txt sphinxcontrib_nwdiag.egg-info/top_level.txtsphinxcontrib-nwdiag-0.7.0/sphinxcontrib_nwdiag.egg-info/namespace_packages.txt0000644000175000017500000000001612224001776031341 0ustar katsuwokatsuwo00000000000000sphinxcontrib sphinxcontrib-nwdiag-0.7.0/setup.cfg0000644000175000017500000000014112224001776020711 0ustar katsuwokatsuwo00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 [aliases] release = egg_info -RDb '' sphinxcontrib-nwdiag-0.7.0/sphinxcontrib/0000755000175000017500000000000012224001776021766 5ustar katsuwokatsuwo00000000000000sphinxcontrib-nwdiag-0.7.0/sphinxcontrib/packetdiag.py0000644000175000017500000002231712223755012024437 0ustar katsuwokatsuwo00000000000000# -*- coding: utf-8 -*- """ packetdiag.sphinx_ext ~~~~~~~~~~~~~~~~~~~~~~ Allow packetdiag-formatted diagrams to be included in Sphinx-generated documents inline. :copyright: Copyright 2010 by Takeshi Komiya. :license: BSDL. """ import io import os import posixpath import traceback from collections import namedtuple try: from hashlib import sha1 as sha except ImportError: from sha import sha from docutils import nodes from sphinx.errors import SphinxError from sphinx.util.osutil import ensuredir import packetdiag_sphinxhelper as packetdiag class PacketdiagError(SphinxError): category = 'Packetdiag error' class Packetdiag(packetdiag.utils.rst.directives.PacketdiagDirective): def run(self): try: return super(Packetdiag, self).run() except packetdiag.core.parser.ParseException as e: if self.content: msg = '[%s] ParseError: %s\n%s' % (self.name, e, "\n".join(self.content)) else: msg = '[%s] ParseError: %s\n%r' % (self.name, e, self.arguments[0]) reporter = self.state.document.reporter return [reporter.warning(msg, line=self.lineno)] def node2image(self, node, diagram): return node def get_image_filename(self, code, format, options, prefix='packetdiag'): """ Get path of output file. """ if format.upper() not in ('PNG', 'PDF', 'SVG'): raise PacketdiagError('packetdiag error:\nunknown format: %s\n' % format) if format.upper() == 'PDF': try: import reportlab except ImportError: msg = 'packetdiag error:\n' + \ 'colud not output PDF format; Install reportlab\n' raise PacketdiagError(msg) hashkey = (code + str(options)).encode('utf-8') fname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), format.lower()) if hasattr(self.builder, 'imgpath'): # HTML relfn = posixpath.join(self.builder.imgpath, fname) outfn = os.path.join(self.builder.outdir, '_images', fname) else: # LaTeX relfn = fname outfn = os.path.join(self.builder.outdir, fname) if os.path.isfile(outfn): return relfn, outfn ensuredir(os.path.dirname(outfn)) return relfn, outfn def get_fontmap(self): FontMap = packetdiag.utils.fontmap.FontMap try: fontmappath = self.builder.config.packetdiag_fontmap fontmap = FontMap(fontmappath) except: attrname = '_packetdiag_fontmap_warned' if not hasattr(self.builder, attrname): msg = ('packetdiag cannot load "%s" as fontmap file, ' 'check the packetdiag_fontmap setting' % fontmappath) self.builder.warn(msg) setattr(self.builder, attrname, True) fontmap = FontMap(None) try: fontpath = self.builder.config.packetdiag_fontpath if isinstance(fontpath, packetdiag.utils.compat.string_types): fontpath = [fontpath] if fontpath: config = namedtuple('Config', 'font')(fontpath) _fontpath = packetdiag.utils.bootstrap.detectfont(config) fontmap.set_default_font(_fontpath) except: attrname = '_packetdiag_fontpath_warned' if not hasattr(self.builder, attrname): msg = ('packetdiag cannot load "%s" as truetype font, ' 'check the packetdiag_fontpath setting' % fontpath) self.builder.warn(msg) setattr(self.builder, attrname, True) return fontmap def create_packetdiag(self, code, format, filename, options, prefix='packetdiag'): """ Render packetdiag code into a PNG output file. """ draw = None fontmap = get_fontmap(self) try: tree = packetdiag.core.parser.parse_string(code) diagram = packetdiag.core.builder.ScreenNodeBuilder.build(tree) antialias = self.builder.config.packetdiag_antialias draw = packetdiag.core.drawer.DiagramDraw(format, diagram, filename, fontmap=fontmap, antialias=antialias) except Exception as e: if self.builder.config.packetdiag_debug: traceback.print_exc() raise PacketdiagError('packetdiag error:\n%s\n' % e) return draw def make_svgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): svgtag_format = """%s """ code = io.open(outfn, 'r', encoding='utf-8-sig').read() return (svgtag_format % (alt, image_size[0], image_size[1], code)) def make_imgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): result = "" imgtag_format = '%s\n' if trelfn: result += ('' % relfn) result += (imgtag_format % (trelfn, alt, thumb_size[0], thumb_size[1])) result += ('') else: result += (imgtag_format % (relfn, alt, image_size[0], image_size[1])) return result def render_dot_html(self, node, code, options, prefix='packetdiag', imgcls=None, alt=None): trelfn = None thumb_size = None try: format = self.builder.config.packetdiag_html_image_format relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_packetdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() # generate thumbnails image_size = image.pagesize() if 'maxwidth' in options and options['maxwidth'] < image_size[0]: thumb_prefix = prefix + '_thumb' trelfn, toutfn = get_image_filename(self, code, format, options, thumb_prefix) ratio = float(options['maxwidth']) / image_size[0] thumb_size = (options['maxwidth'], image_size[1] * ratio) if not os.path.isfile(toutfn): image.filename = toutfn image.save(thumb_size) except UnicodeEncodeError: msg = ("packetdiag error: UnicodeEncodeError caught " "(check your font settings)") self.builder.warn(msg) raise nodes.SkipNode except PacketdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode self.body.append(self.starttag(node, 'p', CLASS='packetdiag')) if relfn is None: self.body.append(self.encode(code)) else: if alt is None: alt = node.get('alt', self.encode(code).strip()) if format.upper() == 'SVG': tagfunc = make_svgtag else: tagfunc = make_imgtag self.body.append(tagfunc(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size)) self.body.append('

\n') raise nodes.SkipNode def html_visit_packetdiag(self, node): render_dot_html(self, node, node['code'], node['options']) def render_dot_latex(self, node, code, options, prefix='packetdiag'): try: format = self.builder.config.packetdiag_tex_image_format fname, outfn = get_image_filename(self, code, format, options, prefix) image = create_packetdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() except PacketdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode if fname is not None: self.body.append('\\par\\includegraphics{%s}\\par' % fname) raise nodes.SkipNode def latex_visit_packetdiag(self, node): render_dot_latex(self, node, node['code'], node['options']) def on_doctree_resolved(self, doctree, docname): if self.builder.name in ('gettext', 'singlehtml', 'html', 'latex', 'epub'): return for node in doctree.traverse(packetdiag.utils.rst.nodes.packetdiag): code = node['code'] prefix = 'packetdiag' format = 'PNG' options = node['options'] relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_packetdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() candidates = {'image/png': outfn} image = nodes.image(uri=outfn, candidates=candidates) node.parent.replace(node, image) def setup(app): app.add_node(packetdiag.utils.rst.nodes.packetdiag, html=(html_visit_packetdiag, None), latex=(latex_visit_packetdiag, None)) app.add_directive('packetdiag', Packetdiag) app.add_config_value('packetdiag_fontpath', None, 'html') app.add_config_value('packetdiag_fontmap', None, 'html') app.add_config_value('packetdiag_antialias', False, 'html') app.add_config_value('packetdiag_debug', False, 'html') app.add_config_value('packetdiag_html_image_format', 'PNG', 'html') app.add_config_value('packetdiag_tex_image_format', 'PNG', 'html') app.connect("doctree-resolved", on_doctree_resolved) sphinxcontrib-nwdiag-0.7.0/sphinxcontrib/__init__.py0000644000175000017500000000055612223754375024116 0ustar katsuwokatsuwo00000000000000# -*- coding: utf-8 -*- """ sphinxcontrib ~~~~~~~~~~~~~ This package is a namespace package that contains all extensions distributed in the ``sphinx-contrib`` distribution. :copyright: Copyright 2007-2009 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ __import__('pkg_resources').declare_namespace(__name__) sphinxcontrib-nwdiag-0.7.0/sphinxcontrib/rackdiag.py0000644000175000017500000002211112223755012024100 0ustar katsuwokatsuwo00000000000000# -*- coding: utf-8 -*- """ rackdiag.sphinx_ext ~~~~~~~~~~~~~~~~~~~~ Allow rackdiag-formatted diagrams to be included in Sphinx-generated documents inline. :copyright: Copyright 2010 by Takeshi Komiya. :license: BSDL. """ import io import os import posixpath import traceback from collections import namedtuple try: from hashlib import sha1 as sha except ImportError: from sha import sha from docutils import nodes from sphinx.errors import SphinxError from sphinx.util.osutil import ensuredir import rackdiag_sphinxhelper as rackdiag class RackdiagError(SphinxError): category = 'Rackdiag error' class Rackdiag(rackdiag.utils.rst.directives.RackdiagDirective): def run(self): try: return super(Rackdiag, self).run() except rackdiag.core.parser.ParseException as e: if self.content: msg = '[%s] ParseError: %s\n%s' % (self.name, e, "\n".join(self.content)) else: msg = '[%s] ParseError: %s\n%r' % (self.name, e, self.arguments[0]) reporter = self.state.document.reporter return [reporter.warning(msg, line=self.lineno)] def node2image(self, node, diagram): return node def get_image_filename(self, code, format, options, prefix='rackdiag'): """ Get path of output file. """ if format.upper() not in ('PNG', 'PDF', 'SVG'): raise RackdiagError('rackdiag error:\nunknown format: %s\n' % format) if format.upper() == 'PDF': try: import reportlab except ImportError: msg = 'rackdiag error:\n' + \ 'colud not output PDF format; Install reportlab\n' raise RackdiagError(msg) hashkey = (code + str(options)).encode('utf-8') fname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), format.lower()) if hasattr(self.builder, 'imgpath'): # HTML relfn = posixpath.join(self.builder.imgpath, fname) outfn = os.path.join(self.builder.outdir, '_images', fname) else: # LaTeX relfn = fname outfn = os.path.join(self.builder.outdir, fname) if os.path.isfile(outfn): return relfn, outfn ensuredir(os.path.dirname(outfn)) return relfn, outfn def get_fontmap(self): FontMap = rackdiag.utils.fontmap.FontMap try: fontmappath = self.builder.config.rackdiag_fontmap fontmap = FontMap(fontmappath) except: attrname = '_rackdiag_fontmap_warned' if not hasattr(self.builder, attrname): msg = ('rackdiag cannot load "%s" as fontmap file, ' 'check the rackdiag_fontmap setting' % fontmappath) self.builder.warn(msg) setattr(self.builder, attrname, True) fontmap = FontMap(None) try: fontpath = self.builder.config.rackdiag_fontpath if isinstance(fontpath, rackdiag.utils.compat.string_types): fontpath = [fontpath] if fontpath: config = namedtuple('Config', 'font')(fontpath) _fontpath = rackdiag.utils.bootstrap.detectfont(config) fontmap.set_default_font(_fontpath) except: attrname = '_rackdiag_fontpath_warned' if not hasattr(self.builder, attrname): msg = ('rackdiag cannot load "%s" as truetype font, ' 'check the rackdiag_fontpath setting' % fontpath) self.builder.warn(msg) setattr(self.builder, attrname, True) return fontmap def create_rackdiag(self, code, format, filename, options, prefix='rackdiag'): """ Render rackdiag code into a PNG output file. """ draw = None fontmap = get_fontmap(self) try: tree = rackdiag.core.parser.parse_string(code) diagram = rackdiag.core.builder.ScreenNodeBuilder.build(tree) antialias = self.builder.config.rackdiag_antialias draw = rackdiag.core.drawer.DiagramDraw(format, diagram, filename, fontmap=fontmap, antialias=antialias) except Exception as e: if self.builder.config.rackdiag_debug: traceback.print_exc() raise RackdiagError('rackdiag error:\n%s\n' % e) return draw def make_svgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): svgtag_format = """%s """ code = io.open(outfn, 'r', encoding='utf-8-sig').read() return (svgtag_format % (alt, image_size[0], image_size[1], code)) def make_imgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): result = "" imgtag_format = '%s\n' if trelfn: result += ('' % relfn) result += (imgtag_format % (trelfn, alt, thumb_size[0], thumb_size[1])) result += ('') else: result += (imgtag_format % (relfn, alt, image_size[0], image_size[1])) return result def render_dot_html(self, node, code, options, prefix='rackdiag', imgcls=None, alt=None): trelfn = None thumb_size = None try: format = self.builder.config.rackdiag_html_image_format relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_rackdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() # generate thumbnails image_size = image.pagesize() if 'maxwidth' in options and options['maxwidth'] < image_size[0]: thumb_prefix = prefix + '_thumb' trelfn, toutfn = get_image_filename(self, code, format, options, thumb_prefix) ratio = float(options['maxwidth']) / image_size[0] thumb_size = (options['maxwidth'], image_size[1] * ratio) if not os.path.isfile(toutfn): image.filename = toutfn image.save(thumb_size) except UnicodeEncodeError: msg = ("rackdiag error: UnicodeEncodeError caught " "(check your font settings)") self.builder.warn(msg) raise nodes.SkipNode except RackdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode self.body.append(self.starttag(node, 'p', CLASS='rackdiag')) if relfn is None: self.body.append(self.encode(code)) else: if alt is None: alt = node.get('alt', self.encode(code).strip()) if format.upper() == 'SVG': tagfunc = make_svgtag else: tagfunc = make_imgtag self.body.append(tagfunc(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size)) self.body.append('

\n') raise nodes.SkipNode def html_visit_rackdiag(self, node): render_dot_html(self, node, node['code'], node['options']) def render_dot_latex(self, node, code, options, prefix='rackdiag'): try: format = self.builder.config.rackdiag_tex_image_format fname, outfn = get_image_filename(self, code, format, options, prefix) image = create_rackdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() except RackdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode if fname is not None: self.body.append('\\par\\includegraphics{%s}\\par' % fname) raise nodes.SkipNode def latex_visit_rackdiag(self, node): render_dot_latex(self, node, node['code'], node['options']) def on_doctree_resolved(self, doctree, docname): if self.builder.name in ('gettext', 'singlehtml', 'html', 'latex', 'epub'): return for node in doctree.traverse(rackdiag.utils.rst.nodes.rackdiag): code = node['code'] prefix = 'rackdiag' format = 'PNG' options = node['options'] relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_rackdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() candidates = {'image/png': outfn} image = nodes.image(uri=outfn, candidates=candidates) node.parent.replace(node, image) def setup(app): app.add_node(rackdiag.utils.rst.nodes.rackdiag, html=(html_visit_rackdiag, None), latex=(latex_visit_rackdiag, None)) app.add_directive('rackdiag', Rackdiag) app.add_config_value('rackdiag_fontpath', None, 'html') app.add_config_value('rackdiag_fontmap', None, 'html') app.add_config_value('rackdiag_antialias', False, 'html') app.add_config_value('rackdiag_debug', False, 'html') app.add_config_value('rackdiag_html_image_format', 'PNG', 'html') app.add_config_value('rackdiag_tex_image_format', 'PNG', 'html') app.connect("doctree-resolved", on_doctree_resolved) sphinxcontrib-nwdiag-0.7.0/sphinxcontrib/nwdiag.py0000644000175000017500000002170512223755012023614 0ustar katsuwokatsuwo00000000000000# -*- coding: utf-8 -*- """ nwdiag.sphinx_ext ~~~~~~~~~~~~~~~~~~~~ Allow nwdiag-formatted diagrams to be included in Sphinx-generated documents inline. :copyright: Copyright 2010 by Takeshi Komiya. :license: BSDL. """ import io import os import posixpath import traceback from collections import namedtuple try: from hashlib import sha1 as sha except ImportError: from sha import sha from docutils import nodes from sphinx.errors import SphinxError from sphinx.util.osutil import ensuredir import nwdiag_sphinxhelper as nwdiag class NwdiagError(SphinxError): category = 'Nwdiag error' class Nwdiag(nwdiag.utils.rst.directives.NwdiagDirective): def run(self): try: return super(Nwdiag, self).run() except nwdiag.core.parser.ParseException as e: if self.content: msg = '[%s] ParseError: %s\n%s' % (self.name, e, "\n".join(self.content)) else: msg = '[%s] ParseError: %s\n%r' % (self.name, e, self.arguments[0]) reporter = self.state.document.reporter return [reporter.warning(msg, line=self.lineno)] def node2image(self, node, diagram): return node def get_image_filename(self, code, format, options, prefix='nwdiag'): """ Get path of output file. """ if format.upper() not in ('PNG', 'PDF', 'SVG'): raise NwdiagError('nwdiag error:\nunknown format: %s\n' % format) if format.upper() == 'PDF': try: import reportlab except ImportError: msg = 'nwdiag error:\n' + \ 'colud not output PDF format; Install reportlab\n' raise NwdiagError(msg) hashkey = (code + str(options)).encode('utf-8') fname = '%s-%s.%s' % (prefix, sha(hashkey).hexdigest(), format.lower()) if hasattr(self.builder, 'imgpath'): # HTML relfn = posixpath.join(self.builder.imgpath, fname) outfn = os.path.join(self.builder.outdir, '_images', fname) else: # LaTeX relfn = fname outfn = os.path.join(self.builder.outdir, fname) if os.path.isfile(outfn): return relfn, outfn ensuredir(os.path.dirname(outfn)) return relfn, outfn def get_fontmap(self): FontMap = nwdiag.utils.fontmap.FontMap try: fontmappath = self.builder.config.nwdiag_fontmap fontmap = FontMap(fontmappath) except: attrname = '_nwdiag_fontmap_warned' if not hasattr(self.builder, attrname): msg = ('nwdiag cannot load "%s" as fontmap file, ' 'check the nwdiag_fontmap setting' % fontmappath) self.builder.warn(msg) setattr(self.builder, attrname, True) fontmap = FontMap(None) try: fontpath = self.builder.config.nwdiag_fontpath if isinstance(fontpath, nwdiag.utils.compat.string_types): fontpath = [fontpath] if fontpath: config = namedtuple('Config', 'font')(fontpath) _fontpath = nwdiag.utils.bootstrap.detectfont(config) fontmap.set_default_font(_fontpath) except: attrname = '_nwdiag_fontpath_warned' if not hasattr(self.builder, attrname): msg = ('nwdiag cannot load "%s" as truetype font, ' 'check the nwdiag_fontpath setting' % fontpath) self.builder.warn(msg) setattr(self.builder, attrname, True) return fontmap def create_nwdiag(self, code, format, filename, options, prefix='nwdiag'): """ Render nwdiag code into a PNG output file. """ draw = None fontmap = get_fontmap(self) try: tree = nwdiag.core.parser.parse_string(code) diagram = nwdiag.core.builder.ScreenNodeBuilder.build(tree) antialias = self.builder.config.nwdiag_antialias draw = nwdiag.core.drawer.DiagramDraw(format, diagram, filename, fontmap=fontmap, antialias=antialias) except Exception as e: if self.builder.config.nwdiag_debug: traceback.print_exc() raise NwdiagError('nwdiag error:\n%s\n' % e) return draw def make_svgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): svgtag_format = """%s """ code = io.open(outfn, 'r', encoding='utf-8-sig').read() return (svgtag_format % (alt, image_size[0], image_size[1], code)) def make_imgtag(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size): result = "" imgtag_format = '%s\n' if trelfn: result += ('' % relfn) result += (imgtag_format % (trelfn, alt, thumb_size[0], thumb_size[1])) result += ('') else: result += (imgtag_format % (relfn, alt, image_size[0], image_size[1])) return result def render_dot_html(self, node, code, options, prefix='nwdiag', imgcls=None, alt=None): trelfn = None thumb_size = None try: format = self.builder.config.nwdiag_html_image_format relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_nwdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() # generate thumbnails image_size = image.pagesize() if 'maxwidth' in options and options['maxwidth'] < image_size[0]: thumb_prefix = prefix + '_thumb' trelfn, toutfn = get_image_filename(self, code, format, options, thumb_prefix) ratio = float(options['maxwidth']) / image_size[0] thumb_size = (options['maxwidth'], image_size[1] * ratio) if not os.path.isfile(toutfn): image.filename = toutfn image.save(thumb_size) except UnicodeEncodeError: msg = ("nwdiag error: UnicodeEncodeError caught " "(check your font settings)") self.builder.warn(msg) raise nodes.SkipNode except NwdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode self.body.append(self.starttag(node, 'p', CLASS='nwdiag')) if relfn is None: self.body.append(self.encode(code)) else: if alt is None: alt = node.get('alt', self.encode(code).strip()) if format.upper() == 'SVG': tagfunc = make_svgtag else: tagfunc = make_imgtag self.body.append(tagfunc(self, image, relfn, trelfn, outfn, alt, thumb_size, image_size)) self.body.append('

\n') raise nodes.SkipNode def html_visit_nwdiag(self, node): render_dot_html(self, node, node['code'], node['options']) def render_dot_latex(self, node, code, options, prefix='nwdiag'): try: format = self.builder.config.nwdiag_tex_image_format fname, outfn = get_image_filename(self, code, format, options, prefix) image = create_nwdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() except NwdiagError as exc: self.builder.warn('dot code %r: ' % code + str(exc)) raise nodes.SkipNode if fname is not None: self.body.append('\\par\\includegraphics{%s}\\par' % fname) raise nodes.SkipNode def latex_visit_nwdiag(self, node): render_dot_latex(self, node, node['code'], node['options']) def on_doctree_resolved(self, doctree, docname): if self.builder.name in ('gettext', 'singlehtml', 'html', 'latex', 'epub'): return for node in doctree.traverse(nwdiag.utils.rst.nodes.nwdiag): code = node['code'] prefix = 'nwdiag' format = 'PNG' options = node['options'] relfn, outfn = get_image_filename(self, code, format, options, prefix) image = create_nwdiag(self, code, format, outfn, options, prefix) if not os.path.isfile(outfn): image.draw() image.save() candidates = {'image/png': outfn} image = nodes.image(uri=outfn, candidates=candidates) node.parent.replace(node, image) def setup(app): app.add_node(nwdiag.utils.rst.nodes.nwdiag, html=(html_visit_nwdiag, None), latex=(latex_visit_nwdiag, None)) app.add_directive('nwdiag', Nwdiag) app.add_config_value('nwdiag_fontpath', None, 'html') app.add_config_value('nwdiag_fontmap', None, 'html') app.add_config_value('nwdiag_antialias', False, 'html') app.add_config_value('nwdiag_debug', False, 'html') app.add_config_value('nwdiag_html_image_format', 'PNG', 'html') app.add_config_value('nwdiag_tex_image_format', 'PNG', 'html') app.connect("doctree-resolved", on_doctree_resolved) sphinxcontrib-nwdiag-0.7.0/MANIFEST.in0000644000175000017500000000010111571600560020622 0ustar katsuwokatsuwo00000000000000include AUTHORS include LICENSE include README include CHANGES.* sphinxcontrib-nwdiag-0.7.0/README.rst0000644000175000017500000000414412223716543020572 0ustar katsuwokatsuwo00000000000000nwdiag extension README ========================== This is a sphinx extension which render network diagrams by using `nwdiag `_ . rendered: .. nwdiag:: diagram { network { web01; web02; } network { web01; web02; db01; } } source: .. code-block:: text .. nwdiag:: diagram { network { web01; web02; } network { web01; web02; db01; } } Setting ======= .. You can see available package at `PyPI `_. You can get archive file at http://bitbucket.org/birkenfeld/sphinx-contrib/ Required components ------------------- * `nwdiag `_ . Install ------- .. code-block:: bash > easy_install sphinxcontrib-nwdiag Configure Sphinx ---------------- To enable this extension, add ``sphinxcontrib.nwdiag`` module to extensions option at :file:`conf.py`. .. code-block:: python import os, sys # Path to the folder where nwdiag.py is # NOTE: not needed if the package is installed in traditional way # using setup.py or easy_install sys.path.append(os.path.abspath('/path/to/sphinxcontrib.nwdiag')) # Enabled extensions extensions = ['sphinxcontrib.nwdiag'] Directive ========= .. describe:: .. nwdiag:: [filename] This directive insert a network diagram into the generated document. If filename is specified, sphinx reads external file as source script of blockfile. In another case, nwdiag directive takes code block as source script. Examples:: .. nwdiag:: foobar.diag .. nwdiag:: diagram { // some diagrams are here. } Configuration File Options ========================== .. confval:: nwdiag_fontpath This is a path for renderring fonts. You can use truetype font (.ttf) file path. .. confval:: nwdiag_antialias If :confval:`nwdiag_antialias: is True, nwdiag generates images with anti-alias filter. Repository ========== This code is hosted by Bitbucket. http://bitbucket.org/birkenfeld/sphinx-contrib/ sphinxcontrib-nwdiag-0.7.0/AUTHORS0000644000175000017500000000004511571600560020143 0ustar katsuwokatsuwo00000000000000Takeshi KOMIYA sphinxcontrib-nwdiag-0.7.0/LICENSE0000644000175000017500000000261211571600560020102 0ustar katsuwokatsuwo00000000000000If not otherwise noted, the extensions in this package are licensed under the following license. Copyright (c) 2009 by the contributors (see AUTHORS file). All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.