table.py 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
  2. # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
  3. #
  4. # This file is part of logilab-common.
  5. #
  6. # logilab-common is free software: you can redistribute it and/or modify it under
  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) any
  9. # later version.
  10. #
  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
  13. # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  14. # details.
  15. #
  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/>.
  18. """Table management module."""
  19. __docformat__ = "restructuredtext en"
  20. class Table(object):
  21. """Table defines a data table with column and row names.
  22. inv:
  23. len(self.data) <= len(self.row_names)
  24. forall(self.data, lambda x: len(x) <= len(self.col_names))
  25. """
  26. def __init__(self, default_value=0, col_names=None, row_names=None):
  27. self.col_names = []
  28. self.row_names = []
  29. self.data = []
  30. self.default_value = default_value
  31. if col_names:
  32. self.create_columns(col_names)
  33. if row_names:
  34. self.create_rows(row_names)
  35. def _next_row_name(self):
  36. return 'row%s' % (len(self.row_names)+1)
  37. def __iter__(self):
  38. return iter(self.data)
  39. def __eq__(self, other):
  40. if other is None:
  41. return False
  42. else:
  43. return list(self) == list(other)
  44. def __ne__(self, other):
  45. return not self == other
  46. def __len__(self):
  47. return len(self.row_names)
  48. ## Rows / Columns creation #################################################
  49. def create_rows(self, row_names):
  50. """Appends row_names to the list of existing rows
  51. """
  52. self.row_names.extend(row_names)
  53. for row_name in row_names:
  54. self.data.append([self.default_value]*len(self.col_names))
  55. def create_columns(self, col_names):
  56. """Appends col_names to the list of existing columns
  57. """
  58. for col_name in col_names:
  59. self.create_column(col_name)
  60. def create_row(self, row_name=None):
  61. """Creates a rowname to the row_names list
  62. """
  63. row_name = row_name or self._next_row_name()
  64. self.row_names.append(row_name)
  65. self.data.append([self.default_value]*len(self.col_names))
  66. def create_column(self, col_name):
  67. """Creates a colname to the col_names list
  68. """
  69. self.col_names.append(col_name)
  70. for row in self.data:
  71. row.append(self.default_value)
  72. ## Sort by column ##########################################################
  73. def sort_by_column_id(self, col_id, method = 'asc'):
  74. """Sorts the table (in-place) according to data stored in col_id
  75. """
  76. try:
  77. col_index = self.col_names.index(col_id)
  78. self.sort_by_column_index(col_index, method)
  79. except ValueError:
  80. raise KeyError("Col (%s) not found in table" % (col_id))
  81. def sort_by_column_index(self, col_index, method = 'asc'):
  82. """Sorts the table 'in-place' according to data stored in col_index
  83. method should be in ('asc', 'desc')
  84. """
  85. sort_list = sorted([(row[col_index], row, row_name)
  86. for row, row_name in zip(self.data, self.row_names)])
  87. # Sorting sort_list will sort according to col_index
  88. # If we want reverse sort, then reverse list
  89. if method.lower() == 'desc':
  90. sort_list.reverse()
  91. # Rebuild data / row names
  92. self.data = []
  93. self.row_names = []
  94. for val, row, row_name in sort_list:
  95. self.data.append(row)
  96. self.row_names.append(row_name)
  97. def groupby(self, colname, *others):
  98. """builds indexes of data
  99. :returns: nested dictionaries pointing to actual rows
  100. """
  101. groups = {}
  102. colnames = (colname,) + others
  103. col_indexes = [self.col_names.index(col_id) for col_id in colnames]
  104. for row in self.data:
  105. ptr = groups
  106. for col_index in col_indexes[:-1]:
  107. ptr = ptr.setdefault(row[col_index], {})
  108. ptr = ptr.setdefault(row[col_indexes[-1]],
  109. Table(default_value=self.default_value,
  110. col_names=self.col_names))
  111. ptr.append_row(tuple(row))
  112. return groups
  113. def select(self, colname, value):
  114. grouped = self.groupby(colname)
  115. try:
  116. return grouped[value]
  117. except KeyError:
  118. return []
  119. def remove(self, colname, value):
  120. col_index = self.col_names.index(colname)
  121. for row in self.data[:]:
  122. if row[col_index] == value:
  123. self.data.remove(row)
  124. ## The 'setter' part #######################################################
  125. def set_cell(self, row_index, col_index, data):
  126. """sets value of cell 'row_indew', 'col_index' to data
  127. """
  128. self.data[row_index][col_index] = data
  129. def set_cell_by_ids(self, row_id, col_id, data):
  130. """sets value of cell mapped by row_id and col_id to data
  131. Raises a KeyError if row_id or col_id are not found in the table
  132. """
  133. try:
  134. row_index = self.row_names.index(row_id)
  135. except ValueError:
  136. raise KeyError("Row (%s) not found in table" % (row_id))
  137. else:
  138. try:
  139. col_index = self.col_names.index(col_id)
  140. self.data[row_index][col_index] = data
  141. except ValueError:
  142. raise KeyError("Column (%s) not found in table" % (col_id))
  143. def set_row(self, row_index, row_data):
  144. """sets the 'row_index' row
  145. pre:
  146. type(row_data) == types.ListType
  147. len(row_data) == len(self.col_names)
  148. """
  149. self.data[row_index] = row_data
  150. def set_row_by_id(self, row_id, row_data):
  151. """sets the 'row_id' column
  152. pre:
  153. type(row_data) == types.ListType
  154. len(row_data) == len(self.row_names)
  155. Raises a KeyError if row_id is not found
  156. """
  157. try:
  158. row_index = self.row_names.index(row_id)
  159. self.set_row(row_index, row_data)
  160. except ValueError:
  161. raise KeyError('Row (%s) not found in table' % (row_id))
  162. def append_row(self, row_data, row_name=None):
  163. """Appends a row to the table
  164. pre:
  165. type(row_data) == types.ListType
  166. len(row_data) == len(self.col_names)
  167. """
  168. row_name = row_name or self._next_row_name()
  169. self.row_names.append(row_name)
  170. self.data.append(row_data)
  171. return len(self.data) - 1
  172. def insert_row(self, index, row_data, row_name=None):
  173. """Appends row_data before 'index' in the table. To make 'insert'
  174. behave like 'list.insert', inserting in an out of range index will
  175. insert row_data to the end of the list
  176. pre:
  177. type(row_data) == types.ListType
  178. len(row_data) == len(self.col_names)
  179. """
  180. row_name = row_name or self._next_row_name()
  181. self.row_names.insert(index, row_name)
  182. self.data.insert(index, row_data)
  183. def delete_row(self, index):
  184. """Deletes the 'index' row in the table, and returns it.
  185. Raises an IndexError if index is out of range
  186. """
  187. self.row_names.pop(index)
  188. return self.data.pop(index)
  189. def delete_row_by_id(self, row_id):
  190. """Deletes the 'row_id' row in the table.
  191. Raises a KeyError if row_id was not found.
  192. """
  193. try:
  194. row_index = self.row_names.index(row_id)
  195. self.delete_row(row_index)
  196. except ValueError:
  197. raise KeyError('Row (%s) not found in table' % (row_id))
  198. def set_column(self, col_index, col_data):
  199. """sets the 'col_index' column
  200. pre:
  201. type(col_data) == types.ListType
  202. len(col_data) == len(self.row_names)
  203. """
  204. for row_index, cell_data in enumerate(col_data):
  205. self.data[row_index][col_index] = cell_data
  206. def set_column_by_id(self, col_id, col_data):
  207. """sets the 'col_id' column
  208. pre:
  209. type(col_data) == types.ListType
  210. len(col_data) == len(self.col_names)
  211. Raises a KeyError if col_id is not found
  212. """
  213. try:
  214. col_index = self.col_names.index(col_id)
  215. self.set_column(col_index, col_data)
  216. except ValueError:
  217. raise KeyError('Column (%s) not found in table' % (col_id))
  218. def append_column(self, col_data, col_name):
  219. """Appends the 'col_index' column
  220. pre:
  221. type(col_data) == types.ListType
  222. len(col_data) == len(self.row_names)
  223. """
  224. self.col_names.append(col_name)
  225. for row_index, cell_data in enumerate(col_data):
  226. self.data[row_index].append(cell_data)
  227. def insert_column(self, index, col_data, col_name):
  228. """Appends col_data before 'index' in the table. To make 'insert'
  229. behave like 'list.insert', inserting in an out of range index will
  230. insert col_data to the end of the list
  231. pre:
  232. type(col_data) == types.ListType
  233. len(col_data) == len(self.row_names)
  234. """
  235. self.col_names.insert(index, col_name)
  236. for row_index, cell_data in enumerate(col_data):
  237. self.data[row_index].insert(index, cell_data)
  238. def delete_column(self, index):
  239. """Deletes the 'index' column in the table, and returns it.
  240. Raises an IndexError if index is out of range
  241. """
  242. self.col_names.pop(index)
  243. return [row.pop(index) for row in self.data]
  244. def delete_column_by_id(self, col_id):
  245. """Deletes the 'col_id' col in the table.
  246. Raises a KeyError if col_id was not found.
  247. """
  248. try:
  249. col_index = self.col_names.index(col_id)
  250. self.delete_column(col_index)
  251. except ValueError:
  252. raise KeyError('Column (%s) not found in table' % (col_id))
  253. ## The 'getter' part #######################################################
  254. def get_shape(self):
  255. """Returns a tuple which represents the table's shape
  256. """
  257. return len(self.row_names), len(self.col_names)
  258. shape = property(get_shape)
  259. def __getitem__(self, indices):
  260. """provided for convenience"""
  261. rows, multirows = None, False
  262. cols, multicols = None, False
  263. if isinstance(indices, tuple):
  264. rows = indices[0]
  265. if len(indices) > 1:
  266. cols = indices[1]
  267. else:
  268. rows = indices
  269. # define row slice
  270. if isinstance(rows, str):
  271. try:
  272. rows = self.row_names.index(rows)
  273. except ValueError:
  274. raise KeyError("Row (%s) not found in table" % (rows))
  275. if isinstance(rows, int):
  276. rows = slice(rows, rows+1)
  277. multirows = False
  278. else:
  279. rows = slice(None)
  280. multirows = True
  281. # define col slice
  282. if isinstance(cols, str):
  283. try:
  284. cols = self.col_names.index(cols)
  285. except ValueError:
  286. raise KeyError("Column (%s) not found in table" % (cols))
  287. if isinstance(cols, int):
  288. cols = slice(cols, cols+1)
  289. multicols = False
  290. else:
  291. cols = slice(None)
  292. multicols = True
  293. # get sub-table
  294. tab = Table()
  295. tab.default_value = self.default_value
  296. tab.create_rows(self.row_names[rows])
  297. tab.create_columns(self.col_names[cols])
  298. for idx, row in enumerate(self.data[rows]):
  299. tab.set_row(idx, row[cols])
  300. if multirows :
  301. if multicols:
  302. return tab
  303. else:
  304. return [item[0] for item in tab.data]
  305. else:
  306. if multicols:
  307. return tab.data[0]
  308. else:
  309. return tab.data[0][0]
  310. def get_cell_by_ids(self, row_id, col_id):
  311. """Returns the element at [row_id][col_id]
  312. """
  313. try:
  314. row_index = self.row_names.index(row_id)
  315. except ValueError:
  316. raise KeyError("Row (%s) not found in table" % (row_id))
  317. else:
  318. try:
  319. col_index = self.col_names.index(col_id)
  320. except ValueError:
  321. raise KeyError("Column (%s) not found in table" % (col_id))
  322. return self.data[row_index][col_index]
  323. def get_row_by_id(self, row_id):
  324. """Returns the 'row_id' row
  325. """
  326. try:
  327. row_index = self.row_names.index(row_id)
  328. except ValueError:
  329. raise KeyError("Row (%s) not found in table" % (row_id))
  330. return self.data[row_index]
  331. def get_column_by_id(self, col_id, distinct=False):
  332. """Returns the 'col_id' col
  333. """
  334. try:
  335. col_index = self.col_names.index(col_id)
  336. except ValueError:
  337. raise KeyError("Column (%s) not found in table" % (col_id))
  338. return self.get_column(col_index, distinct)
  339. def get_columns(self):
  340. """Returns all the columns in the table
  341. """
  342. return [self[:, index] for index in range(len(self.col_names))]
  343. def get_column(self, col_index, distinct=False):
  344. """get a column by index"""
  345. col = [row[col_index] for row in self.data]
  346. if distinct:
  347. col = list(set(col))
  348. return col
  349. def apply_stylesheet(self, stylesheet):
  350. """Applies the stylesheet to this table
  351. """
  352. for instruction in stylesheet.instructions:
  353. eval(instruction)
  354. def transpose(self):
  355. """Keeps the self object intact, and returns the transposed (rotated)
  356. table.
  357. """
  358. transposed = Table()
  359. transposed.create_rows(self.col_names)
  360. transposed.create_columns(self.row_names)
  361. for col_index, column in enumerate(self.get_columns()):
  362. transposed.set_row(col_index, column)
  363. return transposed
  364. def pprint(self):
  365. """returns a string representing the table in a pretty
  366. printed 'text' format.
  367. """
  368. # The maximum row name (to know the start_index of the first col)
  369. max_row_name = 0
  370. for row_name in self.row_names:
  371. if len(row_name) > max_row_name:
  372. max_row_name = len(row_name)
  373. col_start = max_row_name + 5
  374. lines = []
  375. # Build the 'first' line <=> the col_names one
  376. # The first cell <=> an empty one
  377. col_names_line = [' '*col_start]
  378. for col_name in self.col_names:
  379. col_names_line.append(col_name.encode('iso-8859-1') + ' '*5)
  380. lines.append('|' + '|'.join(col_names_line) + '|')
  381. max_line_length = len(lines[0])
  382. # Build the table
  383. for row_index, row in enumerate(self.data):
  384. line = []
  385. # First, build the row_name's cell
  386. row_name = self.row_names[row_index].encode('iso-8859-1')
  387. line.append(row_name + ' '*(col_start-len(row_name)))
  388. # Then, build all the table's cell for this line.
  389. for col_index, cell in enumerate(row):
  390. col_name_length = len(self.col_names[col_index]) + 5
  391. data = str(cell)
  392. line.append(data + ' '*(col_name_length - len(data)))
  393. lines.append('|' + '|'.join(line) + '|')
  394. if len(lines[-1]) > max_line_length:
  395. max_line_length = len(lines[-1])
  396. # Wrap the table with '-' to make a frame
  397. lines.insert(0, '-'*max_line_length)
  398. lines.append('-'*max_line_length)
  399. return '\n'.join(lines)
  400. def __repr__(self):
  401. return repr(self.data)
  402. def as_text(self):
  403. data = []
  404. # We must convert cells into strings before joining them
  405. for row in self.data:
  406. data.append([str(cell) for cell in row])
  407. lines = ['\t'.join(row) for row in data]
  408. return '\n'.join(lines)
  409. class TableStyle:
  410. """Defines a table's style
  411. """
  412. def __init__(self, table):
  413. self._table = table
  414. self.size = dict([(col_name, '1*') for col_name in table.col_names])
  415. # __row_column__ is a special key to define the first column which
  416. # actually has no name (<=> left most column <=> row names column)
  417. self.size['__row_column__'] = '1*'
  418. self.alignment = dict([(col_name, 'right')
  419. for col_name in table.col_names])
  420. self.alignment['__row_column__'] = 'right'
  421. # We shouldn't have to create an entry for
  422. # the 1st col (the row_column one)
  423. self.units = dict([(col_name, '') for col_name in table.col_names])
  424. self.units['__row_column__'] = ''
  425. # XXX FIXME : params order should be reversed for all set() methods
  426. def set_size(self, value, col_id):
  427. """sets the size of the specified col_id to value
  428. """
  429. self.size[col_id] = value
  430. def set_size_by_index(self, value, col_index):
  431. """Allows to set the size according to the column index rather than
  432. using the column's id.
  433. BE CAREFUL : the '0' column is the '__row_column__' one !
  434. """
  435. if col_index == 0:
  436. col_id = '__row_column__'
  437. else:
  438. col_id = self._table.col_names[col_index-1]
  439. self.size[col_id] = value
  440. def set_alignment(self, value, col_id):
  441. """sets the alignment of the specified col_id to value
  442. """
  443. self.alignment[col_id] = value
  444. def set_alignment_by_index(self, value, col_index):
  445. """Allows to set the alignment according to the column index rather than
  446. using the column's id.
  447. BE CAREFUL : the '0' column is the '__row_column__' one !
  448. """
  449. if col_index == 0:
  450. col_id = '__row_column__'
  451. else:
  452. col_id = self._table.col_names[col_index-1]
  453. self.alignment[col_id] = value
  454. def set_unit(self, value, col_id):
  455. """sets the unit of the specified col_id to value
  456. """
  457. self.units[col_id] = value
  458. def set_unit_by_index(self, value, col_index):
  459. """Allows to set the unit according to the column index rather than
  460. using the column's id.
  461. BE CAREFUL : the '0' column is the '__row_column__' one !
  462. (Note that in the 'unit' case, you shouldn't have to set a unit
  463. for the 1st column (the __row__column__ one))
  464. """
  465. if col_index == 0:
  466. col_id = '__row_column__'
  467. else:
  468. col_id = self._table.col_names[col_index-1]
  469. self.units[col_id] = value
  470. def get_size(self, col_id):
  471. """Returns the size of the specified col_id
  472. """
  473. return self.size[col_id]
  474. def get_size_by_index(self, col_index):
  475. """Allows to get the size according to the column index rather than
  476. using the column's id.
  477. BE CAREFUL : the '0' column is the '__row_column__' one !
  478. """
  479. if col_index == 0:
  480. col_id = '__row_column__'
  481. else:
  482. col_id = self._table.col_names[col_index-1]
  483. return self.size[col_id]
  484. def get_alignment(self, col_id):
  485. """Returns the alignment of the specified col_id
  486. """
  487. return self.alignment[col_id]
  488. def get_alignment_by_index(self, col_index):
  489. """Allors to get the alignment according to the column index rather than
  490. using the column's id.
  491. BE CAREFUL : the '0' column is the '__row_column__' one !
  492. """
  493. if col_index == 0:
  494. col_id = '__row_column__'
  495. else:
  496. col_id = self._table.col_names[col_index-1]
  497. return self.alignment[col_id]
  498. def get_unit(self, col_id):
  499. """Returns the unit of the specified col_id
  500. """
  501. return self.units[col_id]
  502. def get_unit_by_index(self, col_index):
  503. """Allors to get the unit according to the column index rather than
  504. using the column's id.
  505. BE CAREFUL : the '0' column is the '__row_column__' one !
  506. """
  507. if col_index == 0:
  508. col_id = '__row_column__'
  509. else:
  510. col_id = self._table.col_names[col_index-1]
  511. return self.units[col_id]
  512. import re
  513. CELL_PROG = re.compile("([0-9]+)_([0-9]+)")
  514. class TableStyleSheet:
  515. """A simple Table stylesheet
  516. Rules are expressions where cells are defined by the row_index
  517. and col_index separated by an underscore ('_').
  518. For example, suppose you want to say that the (2,5) cell must be
  519. the sum of its two preceding cells in the row, you would create
  520. the following rule :
  521. 2_5 = 2_3 + 2_4
  522. You can also use all the math.* operations you want. For example:
  523. 2_5 = sqrt(2_3**2 + 2_4**2)
  524. """
  525. def __init__(self, rules = None):
  526. rules = rules or []
  527. self.rules = []
  528. self.instructions = []
  529. for rule in rules:
  530. self.add_rule(rule)
  531. def add_rule(self, rule):
  532. """Adds a rule to the stylesheet rules
  533. """
  534. try:
  535. source_code = ['from math import *']
  536. source_code.append(CELL_PROG.sub(r'self.data[\1][\2]', rule))
  537. self.instructions.append(compile('\n'.join(source_code),
  538. 'table.py', 'exec'))
  539. self.rules.append(rule)
  540. except SyntaxError:
  541. print "Bad Stylesheet Rule : %s [skipped]"%rule
  542. def add_rowsum_rule(self, dest_cell, row_index, start_col, end_col):
  543. """Creates and adds a rule to sum over the row at row_index from
  544. start_col to end_col.
  545. dest_cell is a tuple of two elements (x,y) of the destination cell
  546. No check is done for indexes ranges.
  547. pre:
  548. start_col >= 0
  549. end_col > start_col
  550. """
  551. cell_list = ['%d_%d'%(row_index, index) for index in range(start_col,
  552. end_col + 1)]
  553. rule = '%d_%d=' % dest_cell + '+'.join(cell_list)
  554. self.add_rule(rule)
  555. def add_rowavg_rule(self, dest_cell, row_index, start_col, end_col):
  556. """Creates and adds a rule to make the row average (from start_col
  557. to end_col)
  558. dest_cell is a tuple of two elements (x,y) of the destination cell
  559. No check is done for indexes ranges.
  560. pre:
  561. start_col >= 0
  562. end_col > start_col
  563. """
  564. cell_list = ['%d_%d'%(row_index, index) for index in range(start_col,
  565. end_col + 1)]
  566. num = (end_col - start_col + 1)
  567. rule = '%d_%d=' % dest_cell + '('+'+'.join(cell_list)+')/%f'%num
  568. self.add_rule(rule)
  569. def add_colsum_rule(self, dest_cell, col_index, start_row, end_row):
  570. """Creates and adds a rule to sum over the col at col_index from
  571. start_row to end_row.
  572. dest_cell is a tuple of two elements (x,y) of the destination cell
  573. No check is done for indexes ranges.
  574. pre:
  575. start_row >= 0
  576. end_row > start_row
  577. """
  578. cell_list = ['%d_%d'%(index, col_index) for index in range(start_row,
  579. end_row + 1)]
  580. rule = '%d_%d=' % dest_cell + '+'.join(cell_list)
  581. self.add_rule(rule)
  582. def add_colavg_rule(self, dest_cell, col_index, start_row, end_row):
  583. """Creates and adds a rule to make the col average (from start_row
  584. to end_row)
  585. dest_cell is a tuple of two elements (x,y) of the destination cell
  586. No check is done for indexes ranges.
  587. pre:
  588. start_row >= 0
  589. end_row > start_row
  590. """
  591. cell_list = ['%d_%d'%(index, col_index) for index in range(start_row,
  592. end_row + 1)]
  593. num = (end_row - start_row + 1)
  594. rule = '%d_%d=' % dest_cell + '('+'+'.join(cell_list)+')/%f'%num
  595. self.add_rule(rule)
  596. class TableCellRenderer:
  597. """Defines a simple text renderer
  598. """
  599. def __init__(self, **properties):
  600. """keywords should be properties with an associated boolean as value.
  601. For example :
  602. renderer = TableCellRenderer(units = True, alignment = False)
  603. An unspecified property will have a 'False' value by default.
  604. Possible properties are :
  605. alignment, unit
  606. """
  607. self.properties = properties
  608. def render_cell(self, cell_coord, table, table_style):
  609. """Renders the cell at 'cell_coord' in the table, using table_style
  610. """
  611. row_index, col_index = cell_coord
  612. cell_value = table.data[row_index][col_index]
  613. final_content = self._make_cell_content(cell_value,
  614. table_style, col_index +1)
  615. return self._render_cell_content(final_content,
  616. table_style, col_index + 1)
  617. def render_row_cell(self, row_name, table, table_style):
  618. """Renders the cell for 'row_id' row
  619. """
  620. cell_value = row_name.encode('iso-8859-1')
  621. return self._render_cell_content(cell_value, table_style, 0)
  622. def render_col_cell(self, col_name, table, table_style):
  623. """Renders the cell for 'col_id' row
  624. """
  625. cell_value = col_name.encode('iso-8859-1')
  626. col_index = table.col_names.index(col_name)
  627. return self._render_cell_content(cell_value, table_style, col_index +1)
  628. def _render_cell_content(self, content, table_style, col_index):
  629. """Makes the appropriate rendering for this cell content.
  630. Rendering properties will be searched using the
  631. *table_style.get_xxx_by_index(col_index)' methods
  632. **This method should be overridden in the derived renderer classes.**
  633. """
  634. return content
  635. def _make_cell_content(self, cell_content, table_style, col_index):
  636. """Makes the cell content (adds decoration data, like units for
  637. example)
  638. """
  639. final_content = cell_content
  640. if 'skip_zero' in self.properties:
  641. replacement_char = self.properties['skip_zero']
  642. else:
  643. replacement_char = 0
  644. if replacement_char and final_content == 0:
  645. return replacement_char
  646. try:
  647. units_on = self.properties['units']
  648. if units_on:
  649. final_content = self._add_unit(
  650. cell_content, table_style, col_index)
  651. except KeyError:
  652. pass
  653. return final_content
  654. def _add_unit(self, cell_content, table_style, col_index):
  655. """Adds unit to the cell_content if needed
  656. """
  657. unit = table_style.get_unit_by_index(col_index)
  658. return str(cell_content) + " " + unit
  659. class DocbookRenderer(TableCellRenderer):
  660. """Defines how to render a cell for a docboook table
  661. """
  662. def define_col_header(self, col_index, table_style):
  663. """Computes the colspec element according to the style
  664. """
  665. size = table_style.get_size_by_index(col_index)
  666. return '<colspec colname="c%d" colwidth="%s"/>\n' % \
  667. (col_index, size)
  668. def _render_cell_content(self, cell_content, table_style, col_index):
  669. """Makes the appropriate rendering for this cell content.
  670. Rendering properties will be searched using the
  671. table_style.get_xxx_by_index(col_index)' methods.
  672. """
  673. try:
  674. align_on = self.properties['alignment']
  675. alignment = table_style.get_alignment_by_index(col_index)
  676. if align_on:
  677. return "<entry align='%s'>%s</entry>\n" % \
  678. (alignment, cell_content)
  679. except KeyError:
  680. # KeyError <=> Default alignment
  681. return "<entry>%s</entry>\n" % cell_content
  682. class TableWriter:
  683. """A class to write tables
  684. """
  685. def __init__(self, stream, table, style, **properties):
  686. self._stream = stream
  687. self.style = style or TableStyle(table)
  688. self._table = table
  689. self.properties = properties
  690. self.renderer = None
  691. def set_style(self, style):
  692. """sets the table's associated style
  693. """
  694. self.style = style
  695. def set_renderer(self, renderer):
  696. """sets the way to render cell
  697. """
  698. self.renderer = renderer
  699. def update_properties(self, **properties):
  700. """Updates writer's properties (for cell rendering)
  701. """
  702. self.properties.update(properties)
  703. def write_table(self, title = ""):
  704. """Writes the table
  705. """
  706. raise NotImplementedError("write_table must be implemented !")
  707. class DocbookTableWriter(TableWriter):
  708. """Defines an implementation of TableWriter to write a table in Docbook
  709. """
  710. def _write_headers(self):
  711. """Writes col headers
  712. """
  713. # Define col_headers (colstpec elements)
  714. for col_index in range(len(self._table.col_names)+1):
  715. self._stream.write(self.renderer.define_col_header(col_index,
  716. self.style))
  717. self._stream.write("<thead>\n<row>\n")
  718. # XXX FIXME : write an empty entry <=> the first (__row_column) column
  719. self._stream.write('<entry></entry>\n')
  720. for col_name in self._table.col_names:
  721. self._stream.write(self.renderer.render_col_cell(
  722. col_name, self._table,
  723. self.style))
  724. self._stream.write("</row>\n</thead>\n")
  725. def _write_body(self):
  726. """Writes the table body
  727. """
  728. self._stream.write('<tbody>\n')
  729. for row_index, row in enumerate(self._table.data):
  730. self._stream.write('<row>\n')
  731. row_name = self._table.row_names[row_index]
  732. # Write the first entry (row_name)
  733. self._stream.write(self.renderer.render_row_cell(row_name,
  734. self._table,
  735. self.style))
  736. for col_index, cell in enumerate(row):
  737. self._stream.write(self.renderer.render_cell(
  738. (row_index, col_index),
  739. self._table, self.style))
  740. self._stream.write('</row>\n')
  741. self._stream.write('</tbody>\n')
  742. def write_table(self, title = ""):
  743. """Writes the table
  744. """
  745. self._stream.write('<table>\n<title>%s></title>\n'%(title))
  746. self._stream.write(
  747. '<tgroup cols="%d" align="left" colsep="1" rowsep="1">\n'%
  748. (len(self._table.col_names)+1))
  749. self._write_headers()
  750. self._write_body()
  751. self._stream.write('</tgroup>\n</table>\n')