This file is indexed.

/usr/share/pyshared/zc/table/README.txt is in python-zc.table 0.9.0-0ubuntu1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
======
Tables
======

Tables are general purpose UI constructs designed to simplify presenting
tabular information.  A table has a column set which collects columns and
manages configuration data.

We must register a faux resource directory in preparation::

    >>> import zope.interface
    >>> import zope.component
    >>> import zope.publisher.interfaces
    >>> @zope.component.adapter(zope.publisher.interfaces.IRequest)
    ... @zope.interface.implementer(zope.interface.Interface)
    ... def dummyResource(request):
    ...     return lambda:'/@@/zc.table'
    ...
    >>> zope.component.provideAdapter(dummyResource, name='zc.table')

Columns
=======

``Columns`` have methods to render a header and the contents of a cell based on
the item that occupies that cell.  Here's a very simple example::

    >>> from zope import interface
    >>> from zc.table import interfaces
    >>> class GetItemColumn:
    ...     interface.implements(interfaces.IColumn)
    ...     def __init__(self, title, name, attr):
    ...         self.title = title
    ...         self.name = name
    ...         self.attr = attr # This isn't part of IColumn
    ...     def renderHeader(self, formatter):
    ...         return self.title
    ...     def renderCell(self, item, formatter):
    ...         return str(getattr(item, self.attr))

Note that the methods do not provide the <th> and <td> tags.

The title is for display, while the name is for identifying the column within
a collection of columns: a column name must be unique within a collection of
columns used for a table formatter.

`renderHeader` takes a formatter--the table formatter introduced in the section
immediately below this one.  It has the responsibility of rendering the
contents of the header for the column.  `renderCell` takes the item to be
rendered and the formatter, and is responsible for returning the cell contents
for the given item.

The formatter is passed because it contains references to a number of useful
values.  The context and request are particularly important.

Columns may also support sorting by implementing the ISortableColumn interface.
This interface is comprised of two methods, `sort` and `reversesort`.  Both
take the same rather large set of arguments: items, formatter, start, stop,
and sorters.  At least two values should be unsurprising: the `items` are the
items to be sorted, the `formatter` is the table formatter.  The `start` and
`stop` values are the values that are needed for the rendering, so some
implementations may be able to optimize to only give precise results for the
given range.  The `sorters` are optional sub-sorters--callables with signatures
identical to `sort` and `reversesort` that are a further sort refinement that
an implementation may optionally ignore.  If a column has two or
more values that will sort identically, the column might take advantage of any
sub-sorters to further sort the data.

The columns.py file has a number of useful base column classes.  The
columns.txt file discusses some of them.  For our examples here, we will use
the relatively simple and versatile zc.table.column.GetterColumn.  It is
instantiated with two required values and two optional values::

    title - (required) the title of the column.

    getter - (required) a callable that is passed the item and the table
             formatter; returns the value used in the cell.

    cell_formatter - (optional) a callable that is passed the result of getter,
                      the item, and the table formatter; returns the formatted
                      HTML.  defaults to a function that returns the result of
                      trying to convert the result to unicode.

    name - (optional) the name of the column.  The title is used if a name is
           not specified.

It includes a reasonably simple implementation of ISortableColumn but does
not declare the interface itself.  It tries to sort on the basis of the getter
value and can be customized simply by overriding the `getSortKey` method.

Let's import the GetterColumn and create some columns that we'll use later,
and then verify that one of the columns fully implements IColumn.  We'll also
then declare that all three of them provide ISortableColumn and verify one of
them::

    >>> from zc.table.column import GetterColumn
    >>> columns = (
    ...     GetterColumn(u'First', lambda i,f: i.a, subsort=True),
    ...     GetterColumn(u'Second', lambda i,f: i.b, subsort=True),
    ...     GetterColumn(u'Third', lambda i,f: i.c, subsort=True),
    ...     )
    >>> import zope.interface.verify
    >>> zope.interface.verify.verifyObject(interfaces.IColumn, columns[0])
    True
    >>> for c in columns:
    ...     interface.directlyProvides(c, interfaces.ISortableColumn)
    ...
    >>> zope.interface.verify.verifyObject(
    ...     interfaces.ISortableColumn, columns[0])
    True

Formatters
==========

When a sequence of objects are to be turned into an HTML table, a
table.Formatter is used.  The table package includes a simple implementation
of IFormatter as well as a few important variations.

The default Formatter is instantiated with three required arguments--
`context`, `request`, and `items`--and a long string of optional arguments
we'll discuss in a moment.  The first two required arguments are reminiscent
of browser views--and in fact, a table formatter is a specialized browser
view.  The `context` is the object for which the table formatter is being
rendered, and can be important to various columns; and the `request` is the
current request.  The `items` are the full set of items on which the table will
give a view.

The first three optional arguments affect the display::

    visible_column_names=None, batch_start=0, batch_size=0

visible_column_names are a list of column names that should be displayed; note
that even if a column is not visible, it may still affect other behavior such
as sorting, discussed for a couple of Formatter subclasses below.

batch_start is the item position the table should begin to render.  batch_size
is the number of items the table should render; 0 means all.

The next optional argument, `prefix=None`, is particularly important when a
table formatter is used within a form: it sets a prefix for any form fields
and XML identifiers generated for the table or a contained element.

The last optional argument is the full set of columns for the table (not just
the ones curently visible).  It is optional because it may be set instead as
a subclass attribute: the value itself is required on instances.

Lets create some data to format and instantiate the default Formatter.
Our formatters won't need the context, so we'll fake it.  As an
exercise, we'll hide the second column.

    >>> class DataItem:
    ...     def __init__(self, a, b, c):
    ...         self.a = a
    ...         self.b = b
    ...         self.c = c

    >>> items = [DataItem('a0', 'b0', 'c0'),
    ...          DataItem('a2', 'b2', 'c2'),
    ...          DataItem('a1', 'b1', 'c1'),
    ...          ]
    >>> from zc.table import table
    >>> import zope.publisher.browser
    >>> request = zope.publisher.browser.TestRequest()
    >>> context = None
    >>> formatter = table.Formatter(
    ...     context, request, items, visible_column_names=('First', 'Third'),
    ...     columns=columns)
    >>> zope.interface.verify.verifyObject(
    ...     interfaces.IFormatter, formatter)
    True

The simplest way to use a table formatter is to call it, asking the formatter
to render the entire table::

    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>

If you want more control over the output then you may want to call methods
on the formatter that generate various parts of the output piecemeal.  In
particular, getRows, getHeaders, and getCells exist only for this sort of use.
Here is an example of getRows in use to generate even and odd rows and a
column with cells in a special class:

    >>> html = '<table class="my_class">\n'
    >>> html += '<tr class="header">\n'+ formatter.renderHeaders() + '</tr>\n'
    >>> for index, row in enumerate(formatter.getRows()):
    ...     if index % 2:
    ...         html += '<tr class="even">'
    ...     else:
    ...         html += '<tr class="odd">'
    ...     for index, cell in enumerate(row):
    ...         if index == 0:
    ...             html += '<td class="first_column">'
    ...         else:
    ...             html += '<td>'
    ...         html += cell + '<td>'
    ...     html += '</tr>\n'
    >>> html += '</table>'
    >>> print html
    <table class="my_class">
    <tr class="header">
        <th>
          First
        </th>
        <th>
          Third
        </th>
    </tr>
    <tr class="odd"><td class="first_column">a0<td><td>c0<td></tr>
    <tr class="even"><td class="first_column">a2<td><td>c2<td></tr>
    <tr class="odd"><td class="first_column">a1<td><td>c1<td></tr>
    </table>

However, the formatter provides some simple support for style sheets, since it
is the most common form of customization. Each formatter has an attribute
called ``cssClasses``, which is a mapping from HTML elements to CSS
classes. As you saw above, by default there are no CSS classes registered for
the formatter. Let's now register one for the "table" element:

    >>> formatter.cssClasses['table'] = 'list'
    >>> print formatter()
    <table class="list">
    ...
    </table>

This can be done for every element used in the table. Of course, you can also
unregister the class again:

    >>> del formatter.cssClasses['table']
    >>> print formatter()
    <table>
    ...
    </table>

If you are going to be doing a lot of this sort of thing (or if this approach
is more your style), a subclass of Formatter might be in order--but that
is jumping the gun a bit.  See the section about subclasses below.

Columns are typically defined for a class and reused across requests.
Therefore, they have the request that columns need.  They also have an
`annotations` attribute that allows columns to stash away information that
they need across method calls--for instance, an adapter that every single
cell in a column--and maybe even across multiple columns--will need.

    >>> formatter.annotations
    {}

Batching
========

As discussed above, ``Formatter`` instances can also batch. In order to
batch, `items` must minimally be iterable and ideally support a slice syntax.
batch_size and batch_start, introduced above, are the formatter values to use.
Typically these are passed in on instantiation, but we'll change the attributes
on the existing formatter.

    >>> formatter.batch_size = 1
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>

    >>> formatter.batch_start=1
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
    </tbody>
    </table>

Fancy Columns
=============

It is easy to make columns be more sophisticated.  For example, if we wanted
a column that held content that was especially wide, we could do this::

    >>> class WideColumn(GetterColumn):
    ...     def renderHeader(self, formatter):
    ...         return '<div style="width:200px">%s</div>' % (
    ...             super(WideColumn, self).renderHeader(formatter),)
    >>> fancy_columns = (
    ...     WideColumn(u'First', lambda i,f: i.a),
    ...     GetterColumn(u'Second', lambda i,f: i.b),
    ...     GetterColumn(u'Third', lambda i,f: i.c),
    ...     )
    >>> formatter = table.Formatter(
    ...     context, request, items, visible_column_names=('First', 'Third'),
    ...     columns=fancy_columns)
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          <div style="width:200px">First</div>
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>

This level of control over the way columns are rendered allows for creating
advanced column types.

Formatter Subclasses
====================

The Formatter is useful, but lacks some features you may need.  The
factoring is such that, typically, overriding just a few methods can easily
provide what you need.  The table module provides a few examples of these
subclasses.  While the names are sometimes a bit unwieldy, the functionality is
useful.

AlternatingRowFormatter
-----------------------

The AlternatingRowFormatter is the simplest subclass, offering an
odd-even row formatter that's very easy to use::

    >>> formatter = table.AlternatingRowFormatter(
    ...     context, request, items, ('First', 'Third'), columns=columns)
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr class="odd">
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr class="even">
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr class="odd">
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>

If you want different classes other than "even" and "odd" then simply
define `row_classes` on your instance: the default is a tuple of "even" and
"odd", but "green" and "red" will work as well:

    >>> formatter.row_classes = ("red", "green")
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr class="green">
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr class="red">
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr class="green">
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>

Note that this formatter also plays nicely with the other CSS classes defined
by the formatter:

    >>> formatter.cssClasses['tr'] = 'list'
    >>> print formatter()
    <table>
      <thead>
        <tr class="list">
          <th>
            First
          </th>
          <th>
            Third
          </th>
        </tr>
      </thead>
      <tbody>
      <tr class="list green">
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr class="list red">
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr class="list green">
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      </tbody>
    </table>


SortingFormatter
----------------

``SortingFormatter`` supports ``ISortableColumn`` instances by asking them to
sort using the ``ISortableColumn`` interface described above.  Instantiating
one takes a new final optional argument, ``sort_on``, which is a sequence of
tuple pairs of (column name string, reverse sort boolean) in which the first
pair is the primary sort.  Here's an example.  Notice that we are sorting on
the hidden column--this is acceptable, and not even all that unlikely to
encounter.

    >>> formatter = table.SortingFormatter(
    ...     context, request, items, ('First', 'Third'), columns=columns,
    ...     sort_on=(('Second', True),))
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>

Sorting can also be done on multiple columns.  This has the effect of
subsorting.  It is up to a column to support the subsorting: it is not a
required behavior.  The default GetterColumns we have been using it support it
at the expense of possibly doing a lot of wasted work; the behavior will come
in handy for some examples, though.

First, we'll add some data items that have the same value in the "First"
column. Then we'll configure the sort to sort with "First" being the primary
key and "Third" being the secondary key (you can provide more than two if you
wish). Note that, unlike some of the values examined up to this point, the
sort columns will only be honored when passed to the class on instanciation.

    >>> big_items = items[:]
    >>> big_items.append(DataItem('a1', 'b1', 'c9'))
    >>> big_items.append(DataItem('a1', 'b1', 'c7'))
    >>> big_items.append(DataItem('a1', 'b1', 'c8'))
    >>> formatter = table.SortingFormatter(
    ...     context, request, big_items, ('First', 'Third'), columns=columns,
    ...     sort_on=(('First', True), ('Third', False)))
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c7
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c8
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c9
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>

If the direction of the primary sort is changed, it doesn't effect the sub
sort::

    >>> formatter = table.SortingFormatter(
    ...     context, request, big_items, ('First', 'Third'), columns=columns,
    ...     sort_on=(('First', False), ('Third', False)))
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c7
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c8
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c9
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
    </tbody>
    </table>

When batching sorted tables, the sorting is applied first, then the batching::

    >>> formatter = table.SortingFormatter(
    ...     context, request, items, ('First', 'Third'), columns=columns,
    ...     batch_start=1, sort_on=(('Second', True),))
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>

StandaloneSortFormatter and FormSortFormatter
---------------------------------------------

The sorting table formatter takes care of the sorting back end, but it's
convenient to encapsulate a bit of the front end logic as well, to provide
columns with clickable headers for sorting and so on without having to write
the code every time you need the behavior.  Two subclasses of
SortingFormatter provide this capability.  The
StandaloneSortFormatter is useful for tables that are not parts of a
form, while the FormSortFormatter is designed to fit within a form.

Both versions look at the request to examine what the user has requested be
sorted, and draw UI on the sortable column headers to enable sorting.  The
standalone version uses javascript to put the information in the url, and
the form version puts the information in a hidden field.

Let's take a look at the output of one of these formatters.  First there will
be no sorting information.

    >>> request = zope.publisher.browser.TestRequest()
    >>> formatter = table.FormSortFormatter(
    ...     context, request, items, ('First', 'Third'), columns=columns)
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'First', 'sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    First</span>...
        </th>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'Third', 'sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    Third</span>...
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>
    ...

Setting a prefix also affects the value used to store the sorting information.

    >>> formatter = table.FormSortFormatter(
    ...     context, request, items, ('First', 'Third'),
    ...     prefix='slot.first', columns=columns)
    >>> sort_on_name = table.getSortOnName(formatter.prefix)
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'First', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    First</span>...
        </th>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'Third', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    Third</span>...
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>
    ...

Now we'll add information in the request about the sort, and use a prefix.
The value given in the request indicates that the form should be sorted by
the second column in reverse order.

    >>> request.form[sort_on_name] = ['Second', 'Second']
    >>> formatter = table.FormSortFormatter(
    ...     context, request, items, ('First', 'Third'),
    ...     prefix='slot.first', columns=columns)
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'First', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    First</span>...
        </th>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'Third', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    Third</span>...
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>
    ...

Note that sort_on value explicitly passed to a FormSortFormatter is only an
initial value: if the request contains sort information, then the sort_on
value is ignored.  This is correct behavior because the initial sort_on value
is recorded in the form, and does not need to be repeated.

For instance, if we re-use the big_items collection from above and pass a
sort_on but modify the request to effectively get a sort_on of
(('First', True), ('Third', False)), then the code will look something like
this--notice that we draw arrows indicating the direction of the primary
search.

    >>> request = zope.publisher.browser.TestRequest()
    >>> request.form[sort_on_name] = ['Third', 'First', 'First'] # LIFO
    >>> formatter = table.FormSortFormatter(
    ...     context, request, big_items, ('First', 'Third'), columns=columns,
    ...     prefix='slot.first', sort_on=(('Second', False), ('Third', True)))
    >>> interfaces.IColumnSortedItems.providedBy(formatter.items)
    True
    >>> zope.interface.verify.verifyObject(interfaces.IColumnSortedItems,
    ...                                    formatter.items)
    True
    >>> formatter.items.sort_on
    [['First', True], ['Third', False]]
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'First', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    First</span>...
                    <img src="/@@/zc.table/sort_arrows_up.gif".../>
        </th>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickForm(
                            'Third', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    Third</span>...
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c7
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c8
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c9
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>
    ...

The standalone non-form version uses almost all the same code but doesn't
draw the hidden field and calls a different JavaScript function (which puts the
sorting information in the query string rather than in a form field).  Here's a
quick copy of the example above, modified to use the standalone version.
Because of the way the query string is used, more than two instances of a
column name may appear in the form field, so this is emulated in the example.

Because the standalone version doesn't have a form to record the initial
sort_on values, they are honored even if sort_on values exist in the request.
This is in direct contrast to the form-based formatter discussed immediately
above.

    >>> request = zope.publisher.browser.TestRequest()
    >>> request.form[sort_on_name] = [
    ...     'Third', 'First', 'Second', 'Third', 'Second', 'Third', 'First']
    ... # == First True, Third False, Second True
    >>> formatter = table.StandaloneSortFormatter(
    ...     context, request, big_items, ('First', 'Third'), columns=columns,
    ...     prefix='slot.first', sort_on=(('Second', False), ('Third', True)))
    >>> formatter.items.sort_on
    [['First', True], ['Third', False], ['Second', False]]
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickStandalone(
                            'First', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    First</span> <img src="/@@/zc.table/sort_arrows_up.gif".../>
        </th>
        <th>
                <span class="zc-table-sortable"
                      onclick="javascript: onSortClickStandalone(
                            'Third', 'slot.first.sort_on')"
                        onMouseOver="javascript: this.className='sortable zc-table-sortable'"
                        onMouseOut="javascript: this.className='zc-table-sortable'">
                    Third</span>...
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c7
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c8
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c9
        </td>
      </tr>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
    </tbody>
    </table>

The sorting code is to be able to accept iterators as items, and only iterate
through them as much as necessary to accomplish the tasks.  This needs to
support multiple simultaneous iterations.  Another goal is to use the slice
syntax to let sort implementations be guided as to where precise sorting is
needed, in case n-best or other approaches can be used.

There is some trickiness about this in the implementation, and this part of
the document tries to explore some of the edge cases that have proved
problematic in the field.

In particular, we should examine using an iterator in sorted and unsorted
configurations within a sorting table formatter, with batching.

Unsorted:

    >>> formatter = table.SortingFormatter(
    ...     context, request, iter(items), ('First', 'Third'),
    ...     columns=columns, batch_size=2)
    >>> formatter.items[0] is not None # artifically provoke error :-(
    True
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a0
        </td>
        <td>
          c0
        </td>
      </tr>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
    </tbody>
    </table>

Sorted:

    >>> formatter = table.SortingFormatter(
    ...     context, request, iter(items), ('First', 'Third'),
    ...     columns=columns, sort_on=(('Second', True),), batch_size=2)
    >>> formatter.items[0] is not None # artifically provoke error :-(
    True
    >>> print formatter()
    <table>
    <thead>
      <tr>
        <th>
          First
        </th>
        <th>
          Third
        </th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          a2
        </td>
        <td>
          c2
        </td>
      </tr>
      <tr>
        <td>
          a1
        </td>
        <td>
          c1
        </td>
      </tr>
    </tbody>
    </table>