Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright (c) 2015-2016 Claudiu Popa <pcmanticore@gmail.com>
  2. # Licensed under the GPL: https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  3. # For details: https://github.com/PyCQA/pylint/blob/master/COPYING
  4. """Universal report objects and some formatting drivers.
  5. A way to create simple reports using python objects, primarily designed to be
  6. formatted as text and html.
  7. """
  8. import os
  9. import sys
  10. import six
  11. class BaseWriter(object):
  12. """base class for ureport writers"""
  13. def format(self, layout, stream=None, encoding=None):
  14. """format and write the given layout into the stream object
  15. unicode policy: unicode strings may be found in the layout;
  16. try to call stream.write with it, but give it back encoded using
  17. the given encoding if it fails
  18. """
  19. if stream is None:
  20. stream = sys.stdout
  21. if not encoding:
  22. encoding = getattr(stream, 'encoding', 'UTF-8')
  23. self.encoding = encoding or 'UTF-8'
  24. self.out = stream
  25. self.begin_format()
  26. layout.accept(self)
  27. self.end_format()
  28. def format_children(self, layout):
  29. """recurse on the layout children and call their accept method
  30. (see the Visitor pattern)
  31. """
  32. for child in getattr(layout, 'children', ()):
  33. child.accept(self)
  34. def writeln(self, string=u''):
  35. """write a line in the output buffer"""
  36. self.write(string + os.linesep)
  37. def write(self, string):
  38. """write a string in the output buffer"""
  39. self.out.write(string)
  40. def begin_format(self):
  41. """begin to format a layout"""
  42. self.section = 0
  43. def end_format(self):
  44. """finished to format a layout"""
  45. def get_table_content(self, table):
  46. """trick to get table content without actually writing it
  47. return an aligned list of lists containing table cells values as string
  48. """
  49. result = [[]]
  50. cols = table.cols
  51. for cell in self.compute_content(table):
  52. if cols == 0:
  53. result.append([])
  54. cols = table.cols
  55. cols -= 1
  56. result[-1].append(cell)
  57. # fill missing cells
  58. while len(result[-1]) < cols:
  59. result[-1].append(u'')
  60. return result
  61. def compute_content(self, layout):
  62. """trick to compute the formatting of children layout before actually
  63. writing it
  64. return an iterator on strings (one for each child element)
  65. """
  66. # Patch the underlying output stream with a fresh-generated stream,
  67. # which is used to store a temporary representation of a child
  68. # node.
  69. out = self.out
  70. try:
  71. for child in layout.children:
  72. stream = six.StringIO()
  73. self.out = stream
  74. child.accept(self)
  75. yield stream.getvalue()
  76. finally:
  77. self.out = out