OLD | NEW |
1 # copyright 2003-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 1 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
3 # | 3 # |
4 # This file is part of logilab-common. | 4 # This file is part of logilab-common. |
5 # | 5 # |
6 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 6 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
7 # the terms of the GNU Lesser General Public License as published by the Free | 7 # the terms of the GNU Lesser General Public License as published by the Free |
8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 8 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
9 # later version. | 9 # later version. |
10 # | 10 # |
11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT | 11 # logilab-common is distributed in the hope that it will be useful, but WITHOUT |
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | 12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | 13 # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
14 # details. | 14 # details. |
15 # | 15 # |
16 # You should have received a copy of the GNU Lesser General Public License along | 16 # You should have received a copy of the GNU Lesser General Public License along |
17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. | 17 # with logilab-common. If not, see <http://www.gnu.org/licenses/>. |
18 """Table management module.""" | 18 """Table management module.""" |
19 | |
20 from __future__ import print_function | |
21 | |
22 __docformat__ = "restructuredtext en" | 19 __docformat__ = "restructuredtext en" |
23 | 20 |
24 from six.moves import range | |
25 | 21 |
26 class Table(object): | 22 class Table(object): |
27 """Table defines a data table with column and row names. | 23 """Table defines a data table with column and row names. |
28 inv: | 24 inv: |
29 len(self.data) <= len(self.row_names) | 25 len(self.data) <= len(self.row_names) |
30 forall(self.data, lambda x: len(x) <= len(self.col_names)) | 26 forall(self.data, lambda x: len(x) <= len(self.col_names)) |
31 """ | 27 """ |
32 | 28 |
33 def __init__(self, default_value=0, col_names=None, row_names=None): | 29 def __init__(self, default_value=0, col_names=None, row_names=None): |
34 self.col_names = [] | 30 self.col_names = [] |
(...skipping 10 matching lines...) Expand all Loading... |
45 | 41 |
46 def __iter__(self): | 42 def __iter__(self): |
47 return iter(self.data) | 43 return iter(self.data) |
48 | 44 |
49 def __eq__(self, other): | 45 def __eq__(self, other): |
50 if other is None: | 46 if other is None: |
51 return False | 47 return False |
52 else: | 48 else: |
53 return list(self) == list(other) | 49 return list(self) == list(other) |
54 | 50 |
55 __hash__ = object.__hash__ | |
56 | |
57 def __ne__(self, other): | 51 def __ne__(self, other): |
58 return not self == other | 52 return not self == other |
59 | 53 |
60 def __len__(self): | 54 def __len__(self): |
61 return len(self.row_names) | 55 return len(self.row_names) |
62 | 56 |
63 ## Rows / Columns creation ################################################# | 57 ## Rows / Columns creation ################################################# |
64 def create_rows(self, row_names): | 58 def create_rows(self, row_names): |
65 """Appends row_names to the list of existing rows | 59 """Appends row_names to the list of existing rows |
66 """ | 60 """ |
(...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
439 for row_name in self.row_names: | 433 for row_name in self.row_names: |
440 if len(row_name) > max_row_name: | 434 if len(row_name) > max_row_name: |
441 max_row_name = len(row_name) | 435 max_row_name = len(row_name) |
442 col_start = max_row_name + 5 | 436 col_start = max_row_name + 5 |
443 | 437 |
444 lines = [] | 438 lines = [] |
445 # Build the 'first' line <=> the col_names one | 439 # Build the 'first' line <=> the col_names one |
446 # The first cell <=> an empty one | 440 # The first cell <=> an empty one |
447 col_names_line = [' '*col_start] | 441 col_names_line = [' '*col_start] |
448 for col_name in self.col_names: | 442 for col_name in self.col_names: |
449 col_names_line.append(col_name + ' '*5) | 443 col_names_line.append(col_name.encode('iso-8859-1') + ' '*5) |
450 lines.append('|' + '|'.join(col_names_line) + '|') | 444 lines.append('|' + '|'.join(col_names_line) + '|') |
451 max_line_length = len(lines[0]) | 445 max_line_length = len(lines[0]) |
452 | 446 |
453 # Build the table | 447 # Build the table |
454 for row_index, row in enumerate(self.data): | 448 for row_index, row in enumerate(self.data): |
455 line = [] | 449 line = [] |
456 # First, build the row_name's cell | 450 # First, build the row_name's cell |
457 row_name = self.row_names[row_index] | 451 row_name = self.row_names[row_index].encode('iso-8859-1') |
458 line.append(row_name + ' '*(col_start-len(row_name))) | 452 line.append(row_name + ' '*(col_start-len(row_name))) |
459 | 453 |
460 # Then, build all the table's cell for this line. | 454 # Then, build all the table's cell for this line. |
461 for col_index, cell in enumerate(row): | 455 for col_index, cell in enumerate(row): |
462 col_name_length = len(self.col_names[col_index]) + 5 | 456 col_name_length = len(self.col_names[col_index]) + 5 |
463 data = str(cell) | 457 data = str(cell) |
464 line.append(data + ' '*(col_name_length - len(data))) | 458 line.append(data + ' '*(col_name_length - len(data))) |
465 lines.append('|' + '|'.join(line) + '|') | 459 lines.append('|' + '|'.join(line) + '|') |
466 if len(lines[-1]) > max_line_length: | 460 if len(lines[-1]) > max_line_length: |
467 max_line_length = len(lines[-1]) | 461 max_line_length = len(lines[-1]) |
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
647 def add_rule(self, rule): | 641 def add_rule(self, rule): |
648 """Adds a rule to the stylesheet rules | 642 """Adds a rule to the stylesheet rules |
649 """ | 643 """ |
650 try: | 644 try: |
651 source_code = ['from math import *'] | 645 source_code = ['from math import *'] |
652 source_code.append(CELL_PROG.sub(r'self.data[\1][\2]', rule)) | 646 source_code.append(CELL_PROG.sub(r'self.data[\1][\2]', rule)) |
653 self.instructions.append(compile('\n'.join(source_code), | 647 self.instructions.append(compile('\n'.join(source_code), |
654 'table.py', 'exec')) | 648 'table.py', 'exec')) |
655 self.rules.append(rule) | 649 self.rules.append(rule) |
656 except SyntaxError: | 650 except SyntaxError: |
657 print("Bad Stylesheet Rule : %s [skipped]" % rule) | 651 print "Bad Stylesheet Rule : %s [skipped]"%rule |
658 | 652 |
659 | 653 |
660 def add_rowsum_rule(self, dest_cell, row_index, start_col, end_col): | 654 def add_rowsum_rule(self, dest_cell, row_index, start_col, end_col): |
661 """Creates and adds a rule to sum over the row at row_index from | 655 """Creates and adds a rule to sum over the row at row_index from |
662 start_col to end_col. | 656 start_col to end_col. |
663 dest_cell is a tuple of two elements (x,y) of the destination cell | 657 dest_cell is a tuple of two elements (x,y) of the destination cell |
664 No check is done for indexes ranges. | 658 No check is done for indexes ranges. |
665 pre: | 659 pre: |
666 start_col >= 0 | 660 start_col >= 0 |
667 end_col > start_col | 661 end_col > start_col |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 cell_value = table.data[row_index][col_index] | 736 cell_value = table.data[row_index][col_index] |
743 final_content = self._make_cell_content(cell_value, | 737 final_content = self._make_cell_content(cell_value, |
744 table_style, col_index +1) | 738 table_style, col_index +1) |
745 return self._render_cell_content(final_content, | 739 return self._render_cell_content(final_content, |
746 table_style, col_index + 1) | 740 table_style, col_index + 1) |
747 | 741 |
748 | 742 |
749 def render_row_cell(self, row_name, table, table_style): | 743 def render_row_cell(self, row_name, table, table_style): |
750 """Renders the cell for 'row_id' row | 744 """Renders the cell for 'row_id' row |
751 """ | 745 """ |
752 cell_value = row_name | 746 cell_value = row_name.encode('iso-8859-1') |
753 return self._render_cell_content(cell_value, table_style, 0) | 747 return self._render_cell_content(cell_value, table_style, 0) |
754 | 748 |
755 | 749 |
756 def render_col_cell(self, col_name, table, table_style): | 750 def render_col_cell(self, col_name, table, table_style): |
757 """Renders the cell for 'col_id' row | 751 """Renders the cell for 'col_id' row |
758 """ | 752 """ |
759 cell_value = col_name | 753 cell_value = col_name.encode('iso-8859-1') |
760 col_index = table.col_names.index(col_name) | 754 col_index = table.col_names.index(col_name) |
761 return self._render_cell_content(cell_value, table_style, col_index +1) | 755 return self._render_cell_content(cell_value, table_style, col_index +1) |
762 | 756 |
763 | 757 |
764 | 758 |
765 def _render_cell_content(self, content, table_style, col_index): | 759 def _render_cell_content(self, content, table_style, col_index): |
766 """Makes the appropriate rendering for this cell content. | 760 """Makes the appropriate rendering for this cell content. |
767 Rendering properties will be searched using the | 761 Rendering properties will be searched using the |
768 *table_style.get_xxx_by_index(col_index)' methods | 762 *table_style.get_xxx_by_index(col_index)' methods |
769 | 763 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
920 self._stream.write('<table>\n<title>%s></title>\n'%(title)) | 914 self._stream.write('<table>\n<title>%s></title>\n'%(title)) |
921 self._stream.write( | 915 self._stream.write( |
922 '<tgroup cols="%d" align="left" colsep="1" rowsep="1">\n'% | 916 '<tgroup cols="%d" align="left" colsep="1" rowsep="1">\n'% |
923 (len(self._table.col_names)+1)) | 917 (len(self._table.col_names)+1)) |
924 self._write_headers() | 918 self._write_headers() |
925 self._write_body() | 919 self._write_body() |
926 | 920 |
927 self._stream.write('</tgroup>\n</table>\n') | 921 self._stream.write('</tgroup>\n</table>\n') |
928 | 922 |
929 | 923 |
OLD | NEW |