ImprovedTableParser

Changeset

35:69590f054c12
2012-07-16 Paul Boddie raw files shortlog changelog graph Added a write parameter to the formatTable function for configurable output. Added formatting according to content type, supporting an extended parser API.
ImprovedTableParser.py (file) parsers/table.py (file)
     1.1 --- a/ImprovedTableParser.py	Fri Jul 13 01:13:08 2012 +0200
     1.2 +++ b/ImprovedTableParser.py	Mon Jul 16 00:36:12 2012 +0200
     1.3 @@ -401,17 +401,20 @@
     1.4  
     1.5          return 0
     1.6  
     1.7 -def write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, start=0):
     1.8 +def write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, start=0, write=None):
     1.9  
    1.10      """
    1.11      Using the 'request', write a sort control for the given 'columnnumber' in
    1.12      the collection of 'columns', using the existing 'sort_columns' and
    1.13      'column_types' to construct labels and links that modify the sort criteria,
    1.14      and using the given 'table_name' to parameterise the links.
    1.15 +
    1.16 +    If the 'write' parameter is specified, use it to write output; otherwise,
    1.17 +    write output using the request.
    1.18      """
    1.19  
    1.20      fmt = request.formatter
    1.21 -    write = request.write
    1.22 +    write = write or request.write
    1.23      _ = request.getText
    1.24  
    1.25      write(fmt.div(1, css_class="sortcolumns"))
    1.26 @@ -556,11 +559,14 @@
    1.27  
    1.28  # Common formatting functions.
    1.29  
    1.30 -def formatTable(text, request, fmt, attrs=None):
    1.31 +def formatTable(text, request, fmt, attrs=None, write=None):
    1.32  
    1.33      """
    1.34      Format the given 'text' using the specified 'request' and formatter 'fmt'.
    1.35      The optional 'attrs' can be used to control the presentation of the table.
    1.36 +
    1.37 +    If the 'write' parameter is specified, use it to write output; otherwise,
    1.38 +    write output using the request.
    1.39      """
    1.40  
    1.41      # Parse the table region.
    1.42 @@ -625,7 +631,7 @@
    1.43  
    1.44      # Write the table.
    1.45  
    1.46 -    write = request.write
    1.47 +    write = write or request.write
    1.48      write(fmt.table(1, table_attrs))
    1.49  
    1.50      for rownumber, (row_attrs, columns) in enumerate(table):
    1.51 @@ -668,7 +674,7 @@
    1.52              # Add sorting controls, if appropriate.
    1.53  
    1.54              if sortable_heading:
    1.55 -                write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name)
    1.56 +                write_sort_control(request, columnnumber, columns, sort_columns, column_types, table_name, write=write)
    1.57                  write(fmt.div(0))
    1.58  
    1.59              write(fmt.table_cell(0))
    1.60 @@ -677,4 +683,27 @@
    1.61  
    1.62      write(fmt.table(0))
    1.63  
    1.64 +def formatTableForOutputType(text, request, mimetype, attrs=None, write=None):
    1.65 +
    1.66 +    """
    1.67 +    Format the given 'text' using the specified 'request' for the given output
    1.68 +    'mimetype'.
    1.69 +
    1.70 +    The optional 'attrs' can be used to control the presentation of the table.
    1.71 +
    1.72 +    If the 'write' parameter is specified, use it to write output; otherwise,
    1.73 +    write output using the request.
    1.74 +    """
    1.75 +
    1.76 +    write = write or request.write
    1.77 +
    1.78 +    if mimetype == "text/html":
    1.79 +        write('<html>')
    1.80 +        write('<body>')
    1.81 +        fmt = request.html_formatter
    1.82 +        fmt.setPage(request.page)
    1.83 +        formatTable(text, request, fmt, attrs, write)
    1.84 +        write('</body>')
    1.85 +        write('</html>')
    1.86 +
    1.87  # vim: tabstop=4 expandtab shiftwidth=4
     2.1 --- a/parsers/table.py	Fri Jul 13 01:13:08 2012 +0200
     2.2 +++ b/parsers/table.py	Mon Jul 16 00:36:12 2012 +0200
     2.3 @@ -19,6 +19,10 @@
     2.4      Dependencies = Dependencies
     2.5      extensions = []
     2.6  
     2.7 +    # Output content types preferred by this parser.
     2.8 +
     2.9 +    output_mimetypes = ["text/html"]
    2.10 +
    2.11      def __init__(self, raw, request, **kw):
    2.12  
    2.13          """
    2.14 @@ -30,10 +34,33 @@
    2.15          self.request = request
    2.16          self.attrs = parseAttributes(kw.get("format_args", ""), False)
    2.17  
    2.18 -    def format(self, fmt):
    2.19 +    def format(self, fmt, write=None):
    2.20 +
    2.21 +        """
    2.22 +        Format a table using the given formatter 'fmt'. If the 'write' parameter
    2.23 +        is specified, use it to write output; otherwise, write output using the
    2.24 +        request.
    2.25 +        """
    2.26 +
    2.27 +        formatTable(self.raw, self.request, fmt, self.attrs, write=write)
    2.28 +
    2.29 +    # Extra API methods.
    2.30 +
    2.31 +    def formatForOutputType(self, mimetype, write=None):
    2.32  
    2.33 -        "Format a table using the given formatter 'fmt'."
    2.34 +        """
    2.35 +        Format a table for the given 'mimetype'. If the 'write' parameter is
    2.36 +        specified, use it to write output; otherwise, write output using the
    2.37 +        request.
    2.38 +        """
    2.39  
    2.40 -        formatTable(self.raw, self.request, fmt, self.attrs)
    2.41 +        formatTableForOutputType(self.raw, self.request, mimetype, self.attrs, write=write)
    2.42 +
    2.43 +    # Class methods.
    2.44 +
    2.45 +    def getOutputTypes(self):
    2.46 +        return self.output_mimetypes
    2.47 +
    2.48 +    getOutputTypes = classmethod(getOutputTypes)
    2.49  
    2.50  # vim: tabstop=4 expandtab shiftwidth=4