Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# -*- coding: utf-8 -*- 

2""" 

3@file 

4@brief Functions to dump emails 

5""" 

6from jinja2 import Template 

7from pyquickhelper.loghelper import noLOG 

8 

9 

10class Renderer: 

11 """ 

12 defines way to render an email 

13 """ 

14 

15 def __init__(self, tmpl, css, 

16 style_table="dataframe100l", 

17 style_highlight="dataframe100l_hl", 

18 buffer_write=None, fLOG=noLOG): 

19 """ 

20 constructor, defines a template based 

21 on `Jinja2 <http://jinja.pocoo.org/docs/dev/>`_ 

22 

23 @param tmpl template (string or file) 

24 @param css style 

25 @param style_table style for the table 

26 @param style_highlight style for highlighted cells 

27 @param buffer_write instance of class @see cl BufferFilesWriting 

28 """ 

29 self._template = Template(tmpl) 

30 self._css = Template(css) 

31 self._style_table = style_table 

32 self._style_highlight = style_highlight 

33 self._session = None 

34 self._buffer_write = buffer_write 

35 self._raw_template = tmpl 

36 self.fLOG = fLOG 

37 

38 def flush(self): 

39 """ 

40 flushes all files 

41 

42 @return number of bytes written 

43 """ 

44 return self.BufferWrite.flush(None) 

45 

46 @property 

47 def BufferWrite(self): 

48 """ 

49 returns ``self._buffer_write`` 

50 """ 

51 return self._buffer_write 

52 

53 def render(self, location, obj, attachments=None, file_css="mail_style.css"): 

54 """ 

55 render a mail 

56 

57 @paramp location location where this mail should be saved 

58 @param obj instance of an object 

59 @param file_css css file (where it is supposed to be stored) 

60 @param attachments attachments 

61 @return html, css (content) 

62 

63 The mail is stored in object ``message``, ``css`` means the style sheet, 

64 ``render`` means this object, ``location`` means *location*, 

65 ``attachments`` is *attachments*:: 

66 

67 {{ message.get_subject() }} 

68 

69 The value stored in *file_css* will be relative to *location*. 

70 

71 """ 

72 raise NotImplementedError("this methods needs to be overwritten") 

73 

74 def write(self, location, mail, filename, attachments=None, 

75 overwrite=False, file_css="mail_style.css", encoding="utf8"): 

76 """ 

77 writes a mail, the function assumes the attachments were already dumped 

78 

79 @param location location 

80 @param mail instance of an object or an iterator 

81 @param attachments list of attachments (see @see me dump_attachments) 

82 @param overwrite the function does not overwrite 

83 @param file_css css file (where it is supposed to be stored) 

84 @param encoding encoding 

85 @return list of written local files 

86 """ 

87 raise NotImplementedError("this methods needs to be overwritten")