#!/usr/bin/python
#
# --------------------------------------------------------
# license
# --------------------------------------------------------
#
# Copyright (c) 2007 Craig J M Turner
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
#
# --------------------------------------------------------
# overview
# --------------------------------------------------------
#
# This file is designed to be used from the command line
# and as a library.
#
#
# --------------------------------------------------------
# imports
# --------------------------------------------------------
try:
import locale
locale.setlocale(locale.LC_ALL, '')
except:
pass
import docutils.core
import os
import sys
# --------------------------------------------------------
# fn
# --------------------------------------------------------
def read_file(fname):
f_ptr = None
try:
f_ptr = open(fname)
data = f_ptr.read()
return data
finally:
if None != f_ptr:
f_ptr.close()
def write_file(fname, data):
f_ptr = None
try:
f_ptr = open(fname, 'w+')
f_ptr.write(data)
finally:
if None != f_ptr:
f_ptr.close()
def render_dot(dot_data, dir_to_create_png_in):
"""Reads the dot file and creates a PNG file of it. Nayce.
Note that this returns the filename of the new PNG file.
Will throw an exception if it can't render the diagram.
I've found this functionality to be highly unstable - it
relies on a C library which crashes the python vm unless
everything is perfect.
Even if it did work, the mechanism it uses is extremely
inelegant - it generates a file with a fixed filename.
Avoid using this functionality. We'll find a more
respectable graphing solution later, probably after the
Word frontend is done so we can work out a good interface
for generating that data from the frontend perspective.
"""
dot_data = dot_data.strip()
if 0 == len(dot_data):
# If we don't catch this, the python VM dies
# shortly afterwards.
raise Exception, "No data supplied for diagram."
import pygraphviz
diagram = pygraphviz.AGraph()
diagram.from_string(dot_data)
png_fname = "example.png"
diagram.layout()
diagram.draw("%s%s%s"%(dir_to_create_png_in, os.sep, png_fname))
return png_fname
def render_html(rst_data):
return docutils.core.publish_string(rst_data, writer_name='html')
def render_latex(rst_data):
return docutils.core.publish_string(rst_data, writer_name='latex')
def render_pdf(rst_data):
latex = render_latex( rst_data )
raise Exception, "Haven't yet bridged to PDF."
def render_xml(rst_data):
return docutils.core.publish_string(rst_data, writer_name='xml')
def format_mapping():
d = {}
d['-html'] = render_html
d['-latex'] = render_latex
d['-pdf'] = render_pdf
d['-xml'] = render_xml
return d
# --------------------------------------------------------
# bootstrap
# --------------------------------------------------------
def usage():
print "Usage:"
print " %s COMMAND FILENAME.rst [-|OUTPUT_FNAME.html]"%sys.argv[0]
print
print "Available commands:",
lst = format_mapping().keys()
lst.sort()
for c in lst:
print "'%s'"%c,
print
sys.exit(1)
def go(command, fname, out_directive):
rst_data = read_file(fname)
render_fn = format_mapping()[command]
string_rendered_content = render_fn( rst_data )
if out_directive == '-':
print string_rendered_content
else:
write_file(out_directive, string_rendered_content)
if len(sys.argv) != 4:
usage()
def main():
if len(sys.argv) == 2 and sys.argv[1] == '--help':
usage()
elif len(sys.argv) < 3:
usage()
cmd = sys.argv[1].lower()
fname = sys.argv[2]
out_directive = sys.argv[3]
if cmd not in format_mapping().keys():
usage()
go(cmd, fname, out_directive)
if __name__ == '__main__':
main()