Package zephir :: Package rapports :: Module rml
[frames] | no frames]

Source Code for Module zephir.rapports.rml

  1  # -*- coding: UTF-8 -*- 
  2  ########################################################################### 
  3  # Eole NG - 2007   
  4  # Copyright Pole de Competence Eole  (Ministere Education - Academie Dijon) 
  5  # Licence CeCill  cf /root/LicenceEole.txt 
  6  # eole@ac-dijon.fr  
  7  #   
  8  # rml.py 
  9  #   
 10  # Feuille de style de traduction au format TRML et différents mappings 
 11  # objets python -> strings trml (http://openreport.org/index.py/trml2pdf) 
 12  #        
 13  ########################################################################### 
 14  from zephir.utils import trml2pdf  
 15  from zephir.config import charset 
 16   
17 -class RML:
18 """Conversion des informations en TRML 19 """ 20
21 - def dump_pdf(self, rml_string):
22 """Conversion du rml en pdf 23 """ 24 return trml2pdf.parseString(rml_string)
25
26 - def page(self, title="", story=""):
27 """Modèle de génération d'une page 28 """ 29 return """<?xml version="1.0" encoding="%s" ?> 30 <!DOCTYPE document SYSTEM "rml_1_0.dtd"> 31 <document filename=""> 32 <template pageSize="(21cm, 29.7cm)" 33 leftMargin="1.5cm" 34 rightMargin="1.5cm" 35 topMargin="1.5cm" 36 bottomMargin="1.5cm" 37 showBoundary="1" 38 allowSplitting="20" 39 > 40 <pageTemplate id="main"> 41 <frame id="first" x1="1.5cm" y1="1.5cm" width="19cm" height="25.5cm"/> 42 </pageTemplate> 43 </template> 44 45 <stylesheet> 46 47 <blockTableStyle id="myBlockTableStyle"> 48 <blockFont name="Helvetica-BoldOblique" size="10" start="0,0" stop="1,0"/> 49 <blockFont name="Courier-Bold" size="10" start="0,1" stop="-1,-1"/> 50 <lineStyle kind="GRID" colorName="silver"/> 51 </blockTableStyle> 52 53 <paraStyle name="bullet" fontName="Helvetica" fontSize="10" leftIndent="10mm" firstLineIndent="-10mm"/> 54 55 </stylesheet> 56 <story> 57 <title>%s</title> 58 <spacer length="1cm"/> 59 %s 60 </story> 61 </document> 62 """ % (charset,title, story)
63 64
65 - def sect(self, title, content):
66 """Formatage 67 d'une section 68 """ 69 return """<h2>%s</h2> <spacer length="15"/> %s """ % (title, content)
70
71 - def page_break(self):
72 """Saut de page 73 """ 74 return """<condPageBreak height="200cm"/>"""
75
76 - def para(self, s):
77 """Formatage 78 d'un paragraphe 79 """ 80 return "<para>%s</para>" % str(s)
81
82 - def list_to_table(self, list_dict):
83 return DictToTable(list_dict)
84
85 - def dict_to_list(self, dict):
86 return DictToList(dict)
87
88 - def list_to_list(self, liste):
89 return ListToList(liste)
90 91 92 # mapping types de base python -> formatage html 93
94 -class DictToTable:
95 """Transforme une liste de dictionnaires python 96 en un tableau TRML 97 """
98 - def __init__(self, list_dict):
99 self.list_dict = list_dict
100
101 - def _dict_to_table_row(self, d):
102 """Mapping 103 dictionnaire python -> table TRML 104 105 exemple 106 ------- 107 108 {'libelle': 'horus', 'id': 13} 109 110 devient : 111 112 <tr><td>horus</td><td>13</td></tr> 113 """ 114 l = [] 115 l.append("<tr>") 116 for s in d.values(): 117 l.append( "<td>%s</td>" % str(s) ) 118 l.append("</tr>") 119 return "".join(l)
120
121 - def _dict_to_table_head(self, d):
122 """Mapping dictionnaire python -> table head 123 """ 124 l = [] 125 l.append("<tr>") 126 for s in d.keys(): 127 l.append( "<td>%s</td>" % s ) 128 l.append("</tr>") 129 return "".join(l)
130
131 - def list_to_table(self,list_dict):
132 """Mapping liste de dictionnaires python -> rml table 133 """ 134 l = ["""<blockTable style="myBlockTableStyle">"""] 135 # renseigne l'en-tête de la table 136 l.append(self._dict_to_table_head(list_dict[0])) 137 # remplis le tableau 138 for d in list_dict: 139 l.append(self._dict_to_table_row(d)) 140 l.append("""</blockTable><spacer length="15"/>""") 141 return "".join(l)
142
143 - def __repr__(self):
144 return self.list_to_table(self.list_dict)
145
146 -class DictToList:
147 """Transforme un dictionnaire python en liste rml 148 """
149 - def __init__(self, dict):
150 self.dict = dict
151
152 - def _dict_to_list(self,dict):
153 """Mapping 154 dictionnaire python -> liste RML 155 """ 156 l=[] 157 for em,item in dict.items(): 158 l.append("""<para style="bullet">- <i> %s </i> : %s </para>""" % (em, item) ) 159 return "".join(l)
160
161 - def __repr__(self):
162 return self._dict_to_list(self.dict)
163
164 -class ListToList:
165 """Transforme une liste python en une liste RML 166 """ 167
168 - def __init__(self, list):
169 self.list = list
170
171 - def list_to_list(self,list):
172 """Mapping 173 liste python -> liste RML 174 """ 175 l = [] 176 177 for item in list : 178 l.append("""<para style="bullet"> - %s </para>""" % item ) 179 180 return "".join(l)
181
182 - def __repr__(self):
183 return self.list_to_list(self.list)
184