diff --git a/utils/nxdl2rst.py b/utils/nxdl2rst.py index dc88ab65a..1d6930e64 100755 --- a/utils/nxdl2rst.py +++ b/utils/nxdl2rst.py @@ -6,18 +6,13 @@ the NeXus NXDL Classes chapter. ''' -########### SVN repository information ################### -# $Date$ -# $Author$ -# $Revision$ -# $URL$ -# $Id$ -########### SVN repository information ################### - import os, sys import lxml.etree -import rst_table +try: + from pyRestTable import rest_table +except: + import rst_table as rest_table TITLE_MARKERS = '# - + ~ ^ * @'.split() # used for underscoring section titles @@ -153,7 +148,7 @@ def printMemberTable(ns, parent, name, xref): :param str xref: cross-referencing label to use with this parent node member table ''' # table(s) describing the specification - t = rst_table.Table() + t = rest_table.Table() t.labels = ('Name\nand\nAttributes', 'Type', 'Units', 'Description\n(and Occurrences)', ) t.alignment = ('p{0.2\linewidth}', 'p{0.2\linewidth}', 'p{0.2\linewidth}', 'p{0.4\linewidth}', ) #t.longtable = True @@ -330,7 +325,7 @@ def main(tree, ns): print '='*len(title) # various metrics and metadata about this specification - t = rst_table.Table() + t = rest_table.Table() t.labels = ['version', 'category', 'extends', ] extends = root.get('extends') if extends is not None: diff --git a/utils/rst_table.py b/utils/rst_table.py index fb4b6f4ab..5ad47e862 100755 --- a/utils/rst_table.py +++ b/utils/rst_table.py @@ -1,75 +1,84 @@ #!/usr/bin/env python +# -*- coding: iso-8859-1 -*- -''' -Read the the NeXus NXDL types specification and find -all the valid data types. Write a restructured -text (.rst) document for use in the NeXus manual in -the NXDL chapter. +# 2014-07-09, copied from: https://pypi.python.org/pypi/pyRestTable +#----------------------------------------------------------------------------- +# :author: Pete R. Jemian +# :email: prjemian@gmail.com +# :copyright: (c) 2014, Pete R. Jemian +# +# Distributed under the terms of the Creative Commons Attribution 4.0 International Public License. +# +# The full license is in the file LICENSE, distributed with this software. +#----------------------------------------------------------------------------- -.. note:: This code is now available in an installable - Python package: pyRestTable.rest_table.Table() ''' +Format a nice table in reST (restructured text) + +Each cell may have multiple lines, separated by a newline. +The content of each cell will be rendered as str(cell). + +EXAMPLE + +These commands:: + + import pyRestTable + t = pyRestTable.Table() + t.labels = ('Name\nand\nAttributes', 'Type', 'Units', 'Description\n(and Occurrences)', ) + t.rows.append( ['one,\ntwo', "buckle my", "shoe.\n\n\nthree,\nfour", "..."] ) + t.rows.append( ['class', 'NX_FLOAT', '', None, ] ) + t.rows.append( range(0,4) ) + t.rows.append( [None, t, 1.234, range(3)] ) + print t.reST(fmt='complex') -########### SVN repository information ################### -# $Date$ -# $Author$ -# $Revision$ -# $URL$ -# $Id$ -########### SVN repository information ################### +build this table source code:: + + +------------+-----------------------------------------+--------+-------------------+ + | Name | Type | Units | Description | + | and | | | (and Occurrences) | + | Attributes | | | | + +============+=========================================+========+===================+ + | one, | buckle my | shoe. | ... | + | two | | | | + | | | | | + | | | three, | | + | | | four | | + +------------+-----------------------------------------+--------+-------------------+ + | class | NX_FLOAT | | None | + +------------+-----------------------------------------+--------+-------------------+ + | 0 | 1 | 2 | 3 | + +------------+-----------------------------------------+--------+-------------------+ + | None | <__main__.Table instance at 0x022B8EE0> | 1.234 | [0, 1, 2] | + +------------+-----------------------------------------+--------+-------------------+ + +''' class Table: ''' Construct a table in reST (no row or column spans). - Each cell may have multiple lines, separated by "\n" - - EXAMPLE - - These commands:: - - t = Table() - t.labels = ('Name\nand\nAttributes', 'Type', 'Units', 'Description\n(and Occurrences)', ) - t.alignment = ('l', 'L', 'l', 'L', ) - t.rows.append( ['one,\ntwo', "buckle my", "shoe.\n\n\nthree,\nfour", "..."] ) - t.rows.append( ['class', 'NX_FLOAT', '..', '..', ] ) - print t.reST(format='complex') - - build this table source code:: - - +------------+-----------+--------+-------------------+ - + Name + Type + Units + Description + - + and + + + (and Occurrences) + - + Attributes + + + + - +============+===========+========+===================+ - + one, + buckle my + shoe. + ... + - + two + + + + - + + + + + - + + + three, + + - + + + four + + - +------------+-----------+--------+-------------------+ - + class + NX_FLOAT + .. + .. + - +------------+-----------+--------+-------------------+ ''' def __init__(self): self.rows = [] self.labels = [] + self.use_tabular_columns = False self.alignment = [] self.longtable = False - def reST(self, indentation = '', format = 'simple'): - '''return the table in reST format''' + def reST(self, indentation = '', fmt = 'simple'): + '''render the table in reST format''' if len(self.alignment) == 0: # set the default column alignments - self.alignment = ['L' for item in self.labels] + self.alignment = str('L '*len(self.labels)).strip().split() if not len(self.labels) == len(self.alignment): - raise "Number of column labels is different from column width specifiers" + msg = "Number of column labels is different from column width specifiers" + raise IndexError, msg return {'simple': self.simple_table, - 'complex': self.complex_table,}[format](indentation) + 'complex': self.complex_table,}[fmt](indentation) def simple_table(self, indentation = ''): - '''return the table in simple rest format''' + '''render the table in simple rest format''' # maximum column widths, considering possible line breaks in each cell width = self.find_widths() @@ -77,11 +86,13 @@ def simple_table(self, indentation = ''): separator = " ".join(['='*w for w in width]) + '\n' fmt = " ".join(["%%-%ds" % w for w in width]) + '\n' - rest = indentation - rest += '.. tabularcolumns:: |%s|' % '|'.join(self.alignment) - if self.longtable: - rest += '\n%s%s' % (' '*4, ':longtable:') - rest += '\n\n' + rest = '' + if self.use_tabular_columns: + rest += indentation + rest += '.. tabularcolumns:: |%s|' % '|'.join(self.alignment) + if self.longtable: + rest += '\n%s%s' % (' '*4, ':longtable:') + rest += '\n\n' rest += '%s%s' % (indentation, separator) # top line of table rest += self._row(self.labels, fmt, indentation) # labels rest += '%s%s' % (indentation, separator) # end of the labels @@ -91,7 +102,7 @@ def simple_table(self, indentation = ''): return rest def complex_table(self, indentation = ''): - '''return the table in complex rest format''' + '''render the table in complex rest format''' # maximum column widths, considering possible line breaks in each cell width = self.find_widths() @@ -100,11 +111,13 @@ def complex_table(self, indentation = ''): label_sep = '+' + "".join(['='*(w+2) + '+' for w in width]) + '\n' fmt = '|' + "".join([" %%-%ds |" % w for w in width]) + '\n' - rest = indentation - rest += '.. tabularcolumns:: |%s|' % '|'.join(self.alignment) - if self.longtable: - rest += '\n%s%s' % (' '*4, ':longtable:') - rest += '\n\n' + rest = '' + if self.use_tabular_columns: + rest += indentation + rest += '.. tabularcolumns:: |%s|' % '|'.join(self.alignment) + if self.longtable: + rest += '\n%s%s' % (' '*4, ':longtable:') + rest += '\n\n' rest += '%s%s' % (indentation, separator) # top line of table rest += self._row(self.labels, fmt, indentation) # labels rest += '%s%s' % (indentation, label_sep) # end of the labels @@ -115,38 +128,38 @@ def complex_table(self, indentation = ''): def list_table(self, indentation = ''): ''' - Demo list-table: - - .. Does this work? - - It was found on this page - http://docutils.sourceforge.net/docs/ref/rst/directives.html - - .. list-table:: Frozen Delights! - :widths: 15 10 30 - :header-rows: 1 - - * - Treat - - Quantity - - Description - * - Albatross - - 2.99 - - On a stick! - * - Crunchy Frog - - 1.49 - - If we took the bones out, it wouldn't be - crunchy, now would it? - * - Gannet Ripple - - 1.99 - - On a stick! - - .. Yes, it _does_ work. - Use it for the tables in the NXDL description. -''' + Demo list-table: (not implemented yet) + + .. Does this work? + + It was found on this page + http://docutils.sourceforge.net/docs/ref/rst/directives.html + + .. list-table:: Frozen Delights! + :widths: 15 10 30 + :header-rows: 1 + + * - Treat + - Quantity + - Description + * - Albatross + - 2.99 + - On a stick! + * - Crunchy Frog + - 1.49 + - If we took the bones out, it wouldn't be + crunchy, now would it? + * - Gannet Ripple + - 1.99 + - On a stick! + + .. Yes, it _does_ work. + ''' + raise NotImplementedError def _row(self, row, fmt, indentation = ''): ''' - Given a list of