-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
124 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 <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. | ||
''' | ||
|
@@ -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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters