Salome HOME
Updating module version information
[modules/adao.git] / src / daComposant / daCore / Reporting.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2022 EDF R&D
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
22
23 """
24     Gestion simple de rapports structurés créés par l'utilisateur.
25 """
26 __author__ = "Jean-Philippe ARGAUD"
27 __all__ = []
28
29 import os.path
30
31 # ==============================================================================
32 # Classes de services non utilisateur
33
34 class _ReportPartM__(object):
35     """
36     Store and retrieve the data for C: internal class
37     """
38     def __init__(self, part="default"):
39         self.__part    = str(part)
40         self.__styles  = []
41         self.__content = []
42
43     def append(self, content, style="p", position=-1):
44         if position == -1:
45             self.__styles.append(style)
46             self.__content.append(content)
47         else:
48             self.__styles.insert(position, style)
49             self.__content.insert(position, content)
50         return 0
51
52     def get_styles(self):
53         return self.__styles
54
55     def get_content(self):
56         return self.__content
57
58 class _ReportM__(object):
59     """
60     Store and retrieve the data for C: internal class
61     """
62     def __init__(self, part='default'):
63         self.__document = {}
64         self.__document[part] = _ReportPartM__(part)
65
66     def append(self,  content, style="p", position=-1, part='default'):
67         if part not in self.__document:
68             self.__document[part] = _ReportPartM__(part)
69         self.__document[part].append(content, style, position)
70         return 0
71
72     def get_styles(self):
73         op = list(self.__document.keys()) ; op.sort()
74         return [self.__document[k].get_styles() for k in op]
75
76     def get_content(self):
77         op = list(self.__document.keys()) ; op.sort()
78         return [self.__document[k].get_content() for k in op]
79
80     def clear(self):
81         self.__init__()
82
83 class __ReportC__(object):
84     """
85     Get user commands, update M and V: user intertace to create the report
86     """
87     m = _ReportM__()
88
89     def append(self, content="", style="p", position=-1, part="default"):
90         return self.m.append(content, style, position, part)
91
92     def retrieve(self):
93         st = self.m.get_styles()
94         ct = self.m.get_content()
95         return st, ct
96
97     def clear(self):
98         self.m.clear()
99
100 class __ReportV__(object):
101     """
102     Interact with user and C: template for reports
103     """
104
105     default_filename="report.txt"
106
107     def __init__(self, c):
108         self.c = c
109
110     def save(self, filename=None):
111         if filename is None:
112             filename = self.default_filename
113         _filename = os.path.abspath(filename)
114         #
115         h = self.get()
116         fid = open(_filename, 'w')
117         fid.write(h)
118         fid.close()
119         return filename, _filename
120
121     def retrieve(self):
122         return self.c.retrieve()
123
124     def __str__(self):
125         return self.get()
126
127     def close(self):
128         del self.c
129         return 0
130
131 # ==============================================================================
132 # Classes d'interface utilisateur : ReportStorage, ReportViewIn*
133 # Tags de structure : (title, h1, h2, h3, p, uli, oli, <b></b>, <i></i>)
134
135 ReportStorage = __ReportC__
136
137 class ReportViewInHtml(__ReportV__):
138     """
139     Report in HTML
140     """
141
142     default_filename="report.html"
143     tags = {
144         "oli":"li",
145         "uli":"li",
146         }
147
148     def get(self):
149         st, ct = self.retrieve()
150         inuLi, inoLi = False, False
151         pg = "<html>\n<head>"
152         pg += "\n<title>Report in HTML</title>"
153         pg += "\n</head>\n<body>"
154         for k,ps in enumerate(st):
155             pc = ct[k]
156             try:
157                 ii = ps.index("title")
158                 title = pc[ii]
159                 pg += "%s\n%s\n%s"%('<hr noshade><h1 align="center">',title,'</h1><hr noshade>')
160             except Exception:
161                 pass
162             for i,s in enumerate(ps):
163                 c = pc[i]
164                 if s == "uli" and not inuLi:
165                     pg += "\n<ul>"
166                     inuLi = True
167                 elif s == "oli" and not inoLi:
168                     pg += "\n<ol>"
169                     inoLi = True
170                 elif s != "uli" and inuLi:
171                     pg += "\n</ul>"
172                     inuLi = False
173                 elif s != "oli" and inoLi:
174                     pg += "\n</ol>"
175                     inoLi = False
176                 elif s == "title":
177                     continue
178                 for t in self.tags:
179                     if s == t: s = self.tags[t]
180                 pg += "\n<%s>%s</%s>"%(s,c,s)
181         pg += "\n</body>\n</html>"
182         return pg
183
184 class ReportViewInRst(__ReportV__):
185     """
186     Report in RST
187     """
188
189     default_filename="report.rst"
190     tags = {
191         "p":["\n\n",""],
192         "uli":["\n  - ",""],
193         "oli":["\n  #. ",""],
194         }
195     titles = {
196         # "title":["=","="],
197         "h1":["","-"],
198         "h2":["","+"],
199         "h3":["","*"],
200         }
201     translation = {
202         "<b>":"**",
203         "<i>":"*",
204         "</b>":"**",
205         "</i>":"*",
206         }
207
208     def get(self):
209         st, ct = self.retrieve()
210         inuLi, inoLi = False, False
211         pg = ""
212         for k,ps in enumerate(st):
213             pc = ct[k]
214             try:
215                 ii = ps.index("title")
216                 title = pc[ii]
217                 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
218             except Exception:
219                 pass
220             for i,s in enumerate(ps):
221                 c = pc[i]
222                 if s == "uli" and not inuLi:
223                     pg += "\n"
224                     inuLi = True
225                 elif s == "oli" and not inoLi:
226                     pg += "\n"
227                     inoLi = True
228                 elif s != "uli" and inuLi:
229                     pg += "\n"
230                     inuLi = False
231                 elif s != "oli" and inoLi:
232                     pg += "\n"
233                     inoLi = False
234                 for t in self.translation:
235                     c = c.replace(t,self.translation[t])
236                 if s in self.titles.keys():
237                     pg += "\n%s\n%s\n%s"%(self.titles[s][0]*len(c),c,self.titles[s][1]*len(c))
238                 elif s in self.tags.keys():
239                     pg += "%s%s%s"%(self.tags[s][0],c,self.tags[s][1])
240             pg += "\n"
241         return pg
242
243 class ReportViewInPlainTxt(__ReportV__):
244     """
245     Report in plain TXT
246     """
247
248     default_filename="report.txt"
249     tags = {
250         "p":["\n",""],
251         "uli":["\n  - ",""],
252         "oli":["\n  - ",""],
253         }
254     titles = {
255         # "title":["=","="],
256         "h1":["",""],
257         "h2":["",""],
258         "h3":["",""],
259         }
260     translation = {
261         "<b>":"",
262         "<i>":"",
263         "</b>":"",
264         "</i>":"",
265         }
266
267     def get(self):
268         st, ct = self.retrieve()
269         inuLi, inoLi = False, False
270         pg = ""
271         for k,ps in enumerate(st):
272             pc = ct[k]
273             try:
274                 ii = ps.index("title")
275                 title = pc[ii]
276                 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
277             except Exception:
278                 pass
279             for i,s in enumerate(ps):
280                 c = pc[i]
281                 if s == "uli" and not inuLi:
282                     inuLi = True
283                 elif s == "oli" and not inoLi:
284                     inoLi = True
285                 elif s != "uli" and inuLi:
286                     inuLi = False
287                 elif s != "oli" and inoLi:
288                     inoLi = False
289                 for t in self.translation:
290                     c = c.replace(t,self.translation[t])
291                 if s in self.titles.keys():
292                     pg += "\n%s\n%s\n%s"%(self.titles[s][0]*len(c),c,self.titles[s][1]*len(c))
293                 elif s in self.tags.keys():
294                     pg += "\n%s%s%s"%(self.tags[s][0],c,self.tags[s][1])
295             pg += "\n"
296         return pg
297
298 # ==============================================================================
299 if __name__ == "__main__":
300     print('\n AUTODIAGNOSTIC\n')