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