pyams-table 1.0.0__py3.5.egg

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. EGG-INFO/PKG-INFO +55 -0
  2. EGG-INFO/SOURCES.txt +32 -0
  3. EGG-INFO/dependency_links.txt +1 -0
  4. EGG-INFO/namespace_packages.txt +1 -0
  5. EGG-INFO/not-zip-safe +1 -0
  6. EGG-INFO/requires.txt +14 -0
  7. EGG-INFO/top_level.txt +1 -0
  8. pyams_table/__init__.py +29 -0
  9. pyams_table/__pycache__/__init__.cpython-35.pyc +0 -0
  10. pyams_table/__pycache__/batch.cpython-35.pyc +0 -0
  11. pyams_table/__pycache__/column.cpython-35.pyc +0 -0
  12. pyams_table/__pycache__/header.cpython-35.pyc +0 -0
  13. pyams_table/__pycache__/include.cpython-35.pyc +0 -0
  14. pyams_table/__pycache__/interfaces.cpython-35.pyc +0 -0
  15. pyams_table/__pycache__/table.cpython-35.pyc +0 -0
  16. pyams_table/__pycache__/testing.cpython-35.pyc +0 -0
  17. pyams_table/__pycache__/value.cpython-35.pyc +0 -0
  18. pyams_table/batch.py +159 -0
  19. pyams_table/column.py +495 -0
  20. pyams_table/doctests/README.rst +668 -0
  21. pyams_table/doctests/batch.rst +660 -0
  22. pyams_table/doctests/column.rst +1274 -0
  23. pyams_table/doctests/miscellaneous.rst +123 -0
  24. pyams_table/doctests/sequence.rst +267 -0
  25. pyams_table/doctests/sort.rst +388 -0
  26. pyams_table/header.py +102 -0
  27. pyams_table/include.py +27 -0
  28. pyams_table/interfaces.py +249 -0
  29. pyams_table/locales/fr/LC_MESSAGES/pyams_table.mo +0 -0
  30. pyams_table/locales/fr/LC_MESSAGES/pyams_table.po +40 -0
  31. pyams_table/locales/pyams_table.pot +41 -0
  32. pyams_table/table.py +383 -0
  33. pyams_table/testing.py +129 -0
  34. pyams_table/tests/__init__.py +29 -0
  35. pyams_table/tests/__pycache__/__init__.cpython-35.pyc +0 -0
  36. pyams_table/tests/__pycache__/test_utilsdocs.cpython-35.pyc +0 -0
  37. pyams_table/tests/__pycache__/test_utilsdocstrings.cpython-35.pyc +0 -0
  38. pyams_table/tests/test_utilsdocs.py +60 -0
  39. pyams_table/tests/test_utilsdocstrings.py +65 -0
  40. pyams_table/value.py +51 -0
pyams_table/table.py ADDED
@@ -0,0 +1,383 @@
1
+ #
2
+ # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """PyAMS_table.table module
14
+
15
+ This module provides the main table management class.
16
+ """
17
+
18
+ from xml.sax.saxutils import quoteattr
19
+
20
+ from zope.component import getAdapters, getMultiAdapter
21
+ from zope.interface import implementer
22
+ from zope.location import Location
23
+
24
+ from pyams_batching.interfaces import IBatch
25
+ from pyams_table.column import NoneCell
26
+ from pyams_table.interfaces import IBatchProvider, IColumn, INoneCell, ISequenceTable, ITable, \
27
+ IValues
28
+ from pyams_utils.factory import get_object_factory
29
+
30
+
31
+ def get_weight(column):
32
+ """Get sorting weight of a table"""
33
+ try:
34
+ return int(column.weight)
35
+ except AttributeError:
36
+ return 0
37
+
38
+
39
+ def get_sort_method(idx):
40
+ """Get sort key of item at given index position"""
41
+ def get_sort_key(item):
42
+ sublist = item[idx]
43
+
44
+ def get_column_sort_key(sublist):
45
+ return sublist[1].get_sort_key(sublist[0])
46
+
47
+ return get_column_sort_key(sublist)
48
+
49
+ return get_sort_key
50
+
51
+
52
+ def name_column(column, name):
53
+ """Give a column a __name__."""
54
+ column.__name__ = name
55
+ return column
56
+
57
+
58
+ @implementer(ITable)
59
+ class Table(Location):
60
+ # pylint: disable=too-many-instance-attributes,too-many-public-methods
61
+ """Generic usable table implementation."""
62
+
63
+ # customize this part if needed
64
+ prefix = "table"
65
+
66
+ # css classes
67
+ css_classes = {}
68
+ # additional (row) css
69
+ css_class_even = ""
70
+ css_class_odd = ""
71
+ css_class_selected = ""
72
+ # css to show sorting, set to None to turn off
73
+ css_class_sorted_on = "sorted-on"
74
+
75
+ # sort attributes
76
+ sort_on = 0
77
+ sort_order = "ascending"
78
+ reverse_sort_order_names = ["descending", "reverse", "down"]
79
+
80
+ # batch attributes
81
+ batch_provider_name = "batch"
82
+ batch_start = 0
83
+ batch_size = 50
84
+ start_batching_at = 50
85
+
86
+ def __init__(self, context, request):
87
+ self.context = context
88
+ self.request = request
89
+ self.__parent__ = context
90
+ # private variables will be set in update call
91
+ self.batch_provider = None
92
+ self.column_counter = 0
93
+ self.columnindex_by_id = {}
94
+ self.column_by_name = {}
95
+ self.column_by_index = {}
96
+ self.columns = None
97
+ self.rows = []
98
+ self.selected_items = []
99
+
100
+ def init_columns(self):
101
+ """Init table columns"""
102
+ # setup columns
103
+ self.columns = self.setup_columns()
104
+ # order columns
105
+ self.order_columns()
106
+
107
+ @property
108
+ def values(self):
109
+ """Get table values"""
110
+ adapter = getMultiAdapter((self.context, self.request, self), IValues)
111
+ return adapter.values
112
+
113
+ # CSS helpers
114
+
115
+ def get_css_highlight_class(self, column, item, css_class):
116
+ # pylint: disable=no-self-use,unused-argument
117
+ """Provide a highlight option for any cell"""
118
+ return css_class
119
+
120
+ def get_css_sort_class(self, column, css_class):
121
+ """Add CSS class based on current sorting"""
122
+ if self.css_class_sorted_on and self.sort_on is not None:
123
+ try:
124
+ current_sort_id = int(self.sort_on)
125
+ except ValueError:
126
+ current_sort_id = self.sort_on.rsplit("-", 1)[-1]
127
+ sort_id = column.id.rsplit("-", 1)[-1]
128
+
129
+ if int(sort_id) == int(current_sort_id):
130
+ klass = self.css_class_sorted_on + " " + self.sort_order
131
+ if css_class:
132
+ css_class += " " + klass
133
+ else:
134
+ css_class = klass
135
+ return css_class
136
+
137
+ def get_css_class(self, element, css_class=None):
138
+ """Add CSS class based on HTML tag, make a `class=` attribute"""
139
+ klass = self.css_classes.get(element)
140
+ if klass and css_class:
141
+ klass = "%s %s" % (css_class, klass)
142
+ elif css_class:
143
+ klass = css_class
144
+ return " class=%s" % quoteattr(klass) if klass else ""
145
+
146
+ # setup
147
+
148
+ def setup_columns(self):
149
+ """Setup columns"""
150
+ cols = list(getAdapters((self.context, self.request, self), IColumn))
151
+ # use the adapter name as column name
152
+ return [name_column(col, name) for name, col in cols]
153
+
154
+ def update_columns(self):
155
+ """Update columns contents"""
156
+ for col in self.columns:
157
+ col.update()
158
+
159
+ def order_columns(self):
160
+ """Order columns"""
161
+ self.column_counter = 0
162
+ self.columns = sorted(self.columns, key=get_weight)
163
+ for col in self.columns:
164
+ self.column_by_name[col.__name__] = col
165
+ idx = self.column_counter
166
+ col.id = "%s-%s-%s" % (self.prefix, col.__name__, idx)
167
+ self.columnindex_by_id[col.id] = idx
168
+ self.column_counter += 1
169
+
170
+ def setup_row(self, item):
171
+ """Setup row for given item"""
172
+ cols = []
173
+ append = cols.append
174
+ colspan_counter = 0
175
+ countdown = len(self.columns)
176
+ for col in self.columns:
177
+ countdown -= 1
178
+ colspan = 0
179
+ if colspan_counter == 0:
180
+ colspan = colspan_counter = col.get_colspan(item)
181
+ # adjust colspan because we define 0, 2, 3, etc.
182
+ if colspan_counter > 0:
183
+ colspan_counter -= 1
184
+
185
+ if colspan == 0 and colspan_counter > 0:
186
+ # override col if colspan is 0 and colspan coutner not 0
187
+ colspan_counter -= 1
188
+ colspan = 0
189
+ # now we are ready to setup dummy colspan cells
190
+ col = NoneCell(self.context, self.request, self)
191
+
192
+ # we reached the end of the table and have still colspan
193
+ if (countdown - colspan) < 0:
194
+ raise ValueError("Colspan for column '%s' is larger than the table." % col)
195
+
196
+ append((item, col, colspan))
197
+ return cols
198
+
199
+ def setup_rows(self):
200
+ """Setup rows list"""
201
+ return [self.setup_row(item) for item in self.values]
202
+
203
+ # sort
204
+
205
+ def get_sort_on(self):
206
+ """Returns sort on column id"""
207
+ return self.request.params.get(self.prefix + "-sort-on", self.sort_on)
208
+
209
+ def get_sort_order(self):
210
+ """Returns sort order criteria"""
211
+ return self.request.params.get(self.prefix + "-sort-order", self.sort_order)
212
+
213
+ def sort_rows(self):
214
+ """Sort table rows"""
215
+ if self.sort_on is not None and self.rows and self.columns:
216
+ sort_on_idx = self.columnindex_by_id.get(self.sort_on, 0)
217
+ sort_key_getter = get_sort_method(sort_on_idx)
218
+ rows = sorted(self.rows, key=sort_key_getter)
219
+ if self.sort_order in self.reverse_sort_order_names:
220
+ rows.reverse()
221
+ self.rows = rows
222
+
223
+ # batch
224
+
225
+ def get_batch_size(self):
226
+ """Get table batch size"""
227
+ return int(self.request.params.get(self.prefix + "-batch-size", self.batch_size))
228
+
229
+ def get_batch_start(self):
230
+ """Get table batch start position"""
231
+ return int(self.request.params.get(self.prefix + "-batch-start", self.batch_start))
232
+
233
+ def batch_rows(self):
234
+ """Create batch of rows, if required"""
235
+ if len(self.rows) > self.start_batching_at:
236
+ factory = get_object_factory(IBatch)
237
+ if factory is not None:
238
+ self.rows = factory(self.rows, start=self.batch_start, size=self.batch_size)
239
+
240
+ def update_batch(self):
241
+ """Update batch, if required"""
242
+ if IBatch.providedBy(self.rows):
243
+ self.batch_provider = getMultiAdapter((self.context, self.request, self),
244
+ IBatchProvider,
245
+ name=self.batch_provider_name)
246
+ self.batch_provider.update()
247
+
248
+ def is_selected_row(self, row):
249
+ """Check if row is selected"""
250
+ item, col, colspan = row[0] # pylint: disable=unused-variable
251
+ if item in self.selected_items:
252
+ return True
253
+ return False
254
+
255
+ # render
256
+
257
+ def render_batch(self):
258
+ """Render current batch, if any"""
259
+ if self.batch_provider is None:
260
+ return ""
261
+ return self.batch_provider.render()
262
+
263
+ def render_table(self):
264
+ """Render the whole table"""
265
+ if self.columns:
266
+ css_class = self.get_css_class("table")
267
+ head = self.render_head()
268
+ body = self.render_body()
269
+ return '<table%s>%s%s\n</table>' % (css_class, head, body)
270
+ return ''
271
+
272
+ def render_head(self):
273
+ """Render table head"""
274
+ css_class = self.get_css_class("thead")
275
+ head = self.render_head_row()
276
+ return "\n <thead%s>%s\n </thead>" % (css_class, head)
277
+
278
+ def render_head_row(self):
279
+ """Render table head row"""
280
+ css_class = self.get_css_class("tr")
281
+ cells = [self.render_head_cell(col) for col in self.columns]
282
+ return "\n <tr%s>%s\n </tr>" % (css_class, "".join(cells))
283
+
284
+ def render_head_cell(self, column):
285
+ """Render table head cell for given column"""
286
+ css_class = column.css_classes.get("th")
287
+ css_class = self.get_css_sort_class(column, css_class)
288
+ css_class = self.get_css_class("th", css_class)
289
+ return '\n <th%s>%s</th>' % (css_class, column.render_head_cell())
290
+
291
+ def render_body(self):
292
+ """Render table body"""
293
+ css_class = self.get_css_class("tbody")
294
+ body = self.render_rows()
295
+ return '\n <tbody%s>%s\n </tbody>' % (css_class, body)
296
+
297
+ def render_rows(self):
298
+ """Render table rows"""
299
+ counter = 0
300
+ rows = []
301
+ css_classes = (self.css_class_even, self.css_class_odd)
302
+ append = rows.append
303
+ for row in self.rows:
304
+ append(self.render_row(row, css_classes[counter % 2]))
305
+ counter += 1
306
+ return ''.join(rows)
307
+
308
+ def render_row(self, row, css_class=None):
309
+ """Render given row"""
310
+ is_selected = self.is_selected_row(row)
311
+ if is_selected and self.css_class_selected and css_class:
312
+ css_class = "%s %s" % (self.css_class_selected, css_class)
313
+ elif is_selected and self.css_class_selected:
314
+ css_class = self.css_class_selected
315
+ css_class = self.get_css_class("tr", css_class)
316
+ cells = [self.render_cell(item, col, colspan) for item, col, colspan in row]
317
+ return '\n <tr%s>%s\n </tr>' % (css_class, ''.join(cells))
318
+
319
+ def render_cell(self, item, column, colspan=0):
320
+ """Render cell for item and column"""
321
+ if INoneCell.providedBy(column):
322
+ return ""
323
+ css_class = column.css_classes.get("td")
324
+ css_class = self.get_css_highlight_class(column, item, css_class)
325
+ css_class = self.get_css_sort_class(column, css_class)
326
+ css_class = self.get_css_class("td", css_class)
327
+ colspan_str = ' colspan="%s"' % colspan if colspan else ""
328
+ return '\n <td%s%s>%s</td>' % (
329
+ css_class,
330
+ colspan_str,
331
+ column.render_cell(item),
332
+ )
333
+
334
+ def update(self):
335
+ """Update table contents"""
336
+ # reset values
337
+ self.column_counter = 0
338
+ self.column_by_index = {}
339
+ self.selected_items = []
340
+
341
+ # use batch values from request or the existing ones
342
+ self.batch_size = self.get_batch_size()
343
+ self.batch_start = self.get_batch_start()
344
+
345
+ # use sorting values from request or the existing ones
346
+ self.sort_on = self.get_sort_on()
347
+ self.sort_order = self.get_sort_order()
348
+
349
+ # initialize columns
350
+ self.init_columns()
351
+
352
+ # update columns
353
+ self.update_columns()
354
+
355
+ # setup headers based on columns
356
+ self.rows = self.setup_rows()
357
+
358
+ # sort items on columns
359
+ self.sort_rows()
360
+
361
+ # batch sorted rows
362
+ self.batch_rows()
363
+
364
+ self.update_batch()
365
+
366
+ def render(self):
367
+ """Render table contents"""
368
+ # allow to use a template for rendering the table, this will allow
369
+ # to position the batch before and after the table
370
+ return self.render_table()
371
+
372
+ def __repr__(self):
373
+ return "<%s %r>" % (self.__class__.__name__, self.__name__)
374
+
375
+
376
+ @implementer(ISequenceTable)
377
+ class SequenceTable(Table):
378
+ """Sequence table adapts a sequence as context.
379
+
380
+ This table can be used for adapting a z3c.indexer.search.ResultSet or
381
+ pyams_batching.batch.Batch instance as context. Batch which wraps a
382
+ ResultSet sequence.
383
+ """
pyams_table/testing.py ADDED
@@ -0,0 +1,129 @@
1
+ #
2
+ # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """PyAMS_table.testing module
14
+
15
+ This module provides testing helpers.
16
+ """
17
+
18
+ from datetime import datetime
19
+
20
+ from pyramid.interfaces import IRequest
21
+ from zope.container.btree import BTreeContainer
22
+ from zope.container.contained import Contained
23
+ from zope.container.interfaces import IContainer
24
+ from zope.container.ordered import OrderedContainer as BaseOrderedContainer
25
+ from zope.dublincore.interfaces import IZopeDublinCore
26
+ from zope.interface import Interface, implementer
27
+
28
+ from pyams_table.column import Column, add_column
29
+ from pyams_table.interfaces import ISequenceTable, ITable, IValues
30
+ from pyams_table.table import Table
31
+ from pyams_table.value import ValuesForContainer, ValuesForSequence
32
+
33
+
34
+ __docformat__ = "reStructuredText"
35
+
36
+
37
+ class TitleColumn(Column):
38
+ """Title column"""
39
+
40
+ weight = 10
41
+ header = "Title"
42
+
43
+ def render_cell(self, item):
44
+ """Render cell"""
45
+ return u"Title: %s" % item.title
46
+
47
+
48
+ class NumberColumn(Column):
49
+ """Number column"""
50
+
51
+ header = "Number"
52
+ weight = 20
53
+
54
+ def get_sort_key(self, item):
55
+ """Get item sort key"""
56
+ return item.number
57
+
58
+ def render_cell(self, item):
59
+ """Render column cell"""
60
+ return "number: %s" % item.number
61
+
62
+
63
+ class Container(BTreeContainer):
64
+ """Sample container"""
65
+
66
+ __name__ = "container"
67
+
68
+
69
+ class OrderedContainer(BaseOrderedContainer):
70
+ """Sample container."""
71
+
72
+ __name__ = "container"
73
+
74
+
75
+ class Content(Contained):
76
+ """Sample content"""
77
+
78
+ def __init__(self, title, number):
79
+ self.title = title
80
+ self.number = number
81
+
82
+
83
+ class SimpleTable(Table):
84
+ """Simple testing table"""
85
+
86
+ def setup_columns(self):
87
+ return [
88
+ add_column(self, TitleColumn, "title",
89
+ cell_renderer=cell_renderer,
90
+ head_cell_renderer=head_cell_renderer,
91
+ weight=1),
92
+ add_column(self, NumberColumn, name="number", weight=2, header="Number"),
93
+ ]
94
+
95
+
96
+ def head_cell_renderer():
97
+ """Head cell renderer"""
98
+ return "My items"
99
+
100
+
101
+ def cell_renderer(item):
102
+ """Simple cell renderer"""
103
+ return "%s item" % item.title
104
+
105
+
106
+ @implementer(IZopeDublinCore)
107
+ class DublinCoreAdapterStub:
108
+ """Dublin core adapter stub."""
109
+
110
+ def __init__(self, context):
111
+ pass
112
+
113
+ title = "faux title"
114
+ size = 1024
115
+ created = datetime(2001, 1, 1, 1, 1, 1)
116
+ modified = datetime(2002, 2, 2, 2, 2, 2)
117
+
118
+
119
+ def setup_adapters(registry):
120
+ """Adapters registry"""
121
+ registry.registerAdapter(ValuesForContainer,
122
+ (IContainer, IRequest, ITable),
123
+ provided=IValues)
124
+ registry.registerAdapter(ValuesForSequence,
125
+ (Interface, IRequest, ISequenceTable),
126
+ provided=IValues)
127
+ registry.registerAdapter(DublinCoreAdapterStub,
128
+ (Interface,),
129
+ provided=IZopeDublinCore)
@@ -0,0 +1,29 @@
1
+ #
2
+ # Copyright (c) 2015-2019 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """
14
+ Generic test cases for pyams_table doctests
15
+ """
16
+
17
+ __docformat__ = 'restructuredtext'
18
+
19
+ import os
20
+ import sys
21
+
22
+
23
+ def get_package_dir(value):
24
+ """Get package directory"""
25
+
26
+ package_dir = os.path.split(value)[0]
27
+ if package_dir not in sys.path:
28
+ sys.path.append(package_dir)
29
+ return package_dir
@@ -0,0 +1,60 @@
1
+ #
2
+ # Copyright (c) 2015-2019 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """
14
+ Generic test case for pyams_table doctests
15
+ """
16
+
17
+ __docformat__ = 'restructuredtext'
18
+
19
+ import doctest
20
+ import os
21
+ import unittest
22
+
23
+ from pyams_table.tests import get_package_dir
24
+
25
+
26
+ CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
27
+
28
+
29
+ def doc_suite(test_dir, setUp=None, tearDown=None, globs=None): # pylint: disable=invalid-name
30
+ """Returns a test suite, based on doctests found in /doctests"""
31
+ suite = []
32
+ if globs is None:
33
+ globs = globals()
34
+
35
+ flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
36
+ doctest.REPORT_ONLY_FIRST_FAILURE)
37
+
38
+ package_dir = get_package_dir(test_dir)
39
+ doctest_dir = os.path.join(package_dir, 'doctests')
40
+
41
+ # filtering files on extension
42
+ docs = [os.path.join(doctest_dir, doc) for doc in
43
+ os.listdir(doctest_dir) if doc.endswith('.txt') or doc.endswith('.rst')]
44
+
45
+ for test in docs:
46
+ suite.append(doctest.DocFileSuite(test, optionflags=flags,
47
+ globs=globs, setUp=setUp,
48
+ tearDown=tearDown,
49
+ module_relative=False))
50
+
51
+ return unittest.TestSuite(suite)
52
+
53
+
54
+ def test_suite():
55
+ """Returns the test suite"""
56
+ return doc_suite(CURRENT_DIR)
57
+
58
+
59
+ if __name__ == '__main__':
60
+ unittest.main(defaultTest='test_suite')
@@ -0,0 +1,65 @@
1
+ #
2
+ # Copyright (c) 2015-2019 Thierry Florac <tflorac AT ulthar.net>
3
+ # All Rights Reserved.
4
+ #
5
+ # This software is subject to the provisions of the Zope Public License,
6
+ # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
7
+ # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8
+ # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9
+ # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10
+ # FOR A PARTICULAR PURPOSE.
11
+ #
12
+
13
+ """
14
+ Generic test case for pyams_table docstrings
15
+ """
16
+
17
+ __docformat__ = 'restructuredtext'
18
+
19
+ import doctest
20
+ import os
21
+ import unittest
22
+
23
+ from pyams_table.tests import get_package_dir
24
+
25
+
26
+ CURRENT_DIR = os.path.abspath(os.path.dirname(__file__))
27
+
28
+
29
+ def doc_suite(test_dir, globs=None):
30
+ """Returns a test suite, based on doc tests strings found in /*.py"""
31
+ suite = []
32
+ if globs is None:
33
+ globs = globals()
34
+
35
+ flags = (doctest.ELLIPSIS | doctest.NORMALIZE_WHITESPACE |
36
+ doctest.REPORT_ONLY_FIRST_FAILURE)
37
+
38
+ package_dir = get_package_dir(test_dir)
39
+
40
+ # filtering files on extension
41
+ docs = [doc for doc in
42
+ os.listdir(package_dir) if doc.endswith('.py')]
43
+ docs = [doc for doc in docs if not doc.startswith('__')]
44
+
45
+ for test in docs:
46
+ fd = open(os.path.join(package_dir, test)) # pylint: disable=invalid-name
47
+ content = fd.read()
48
+ fd.close()
49
+ if '>>> ' not in content:
50
+ continue
51
+ test = test.replace('.py', '')
52
+ location = 'pyams_table.%s' % test
53
+ suite.append(doctest.DocTestSuite(location, optionflags=flags,
54
+ globs=globs))
55
+
56
+ return unittest.TestSuite(suite)
57
+
58
+
59
+ def test_suite():
60
+ """returns the test suite"""
61
+ return doc_suite(CURRENT_DIR)
62
+
63
+
64
+ if __name__ == '__main__':
65
+ unittest.main(defaultTest='test_suite')