Skip to content

Commit

Permalink
fixes #287
Browse files Browse the repository at this point in the history
  • Loading branch information
prjemian committed Jul 9, 2014
1 parent 03b7582 commit ff09f29
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 114 deletions.
17 changes: 6 additions & 11 deletions utils/nxdl2rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
212 changes: 118 additions & 94 deletions utils/rst_table.py
Original file line number Diff line number Diff line change
@@ -1,87 +1,98 @@
#!/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: [email protected]
# :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()

# build the row separators
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
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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 <entry nodes in this table <row,
Given a list of entry nodes in this table row,
build one line of the table with one text from each entry element.
The lines are separated by line breaks.
'''
Expand Down Expand Up @@ -183,12 +196,23 @@ def find_widths(self):
return width


if __name__ == '__main__':
def main():
'''test routine used to demo the code'''
t = 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', '..', '..', ] )
print t.reST()
print '\n'*3
t.alignment = ('l', 'L', 'l', 'L', )
print t.reST(format='complex')
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='simple')
print ""
print t.reST(fmt='complex')
print ""
t.longtable = True
t.use_tabular_columns = True
t.alignment = 'l L c r'.split()
print t.reST(fmt='complex')


if __name__ == '__main__':
main()
9 changes: 0 additions & 9 deletions utils/units2rst.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,9 @@
the NXDL chapter.
'''

########### SVN repository information ###################
# $Date$
# $Author$
# $Revision$
# $URL$
# $Id$
########### SVN repository information ###################


import os, sys
import lxml.etree
import rst_table


def worker(nodeMatchString, section = 'units'):
Expand Down

0 comments on commit ff09f29

Please sign in to comment.