Salome HOME
Minor documentation and code review corrections (32)
[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     __slots__ = ("__part", "__styles", "__content")
39     #
40     def __init__(self, part="default"):
41         self.__part    = str(part)
42         self.__styles  = []
43         self.__content = []
44     #
45     def append(self, content, style="p", position=-1):
46         if position == -1:
47             self.__styles.append(style)
48             self.__content.append(content)
49         else:
50             self.__styles.insert(position, style)
51             self.__content.insert(position, content)
52         return 0
53     #
54     def get_styles(self):
55         return self.__styles
56     #
57     def get_content(self):
58         return self.__content
59
60 class _ReportM__(object):
61     """
62     Store and retrieve the data for C: internal class
63     """
64     __slots__ = ("__document")
65     #
66     def __init__(self, part='default'):
67         self.__document = {}
68         self.__document[part] = _ReportPartM__(part)
69     #
70     def append(self,  content, style="p", position=-1, part='default'):
71         if part not in self.__document:
72             self.__document[part] = _ReportPartM__(part)
73         self.__document[part].append(content, style, position)
74         return 0
75     #
76     def get_styles(self):
77         op = list(self.__document.keys()) ; op.sort()
78         return [self.__document[k].get_styles() for k in op]
79     #
80     def get_content(self):
81         op = list(self.__document.keys()) ; op.sort()
82         return [self.__document[k].get_content() for k in op]
83     #
84     def clear(self):
85         self.__init__()
86
87 class __ReportC__(object):
88     """
89     Get user commands, update M and V: user intertace to create the report
90     """
91     __slots__ = ()
92     #
93     m = _ReportM__()
94     #
95     def append(self, content="", style="p", position=-1, part="default"):
96         return self.m.append(content, style, position, part)
97     #
98     def retrieve(self):
99         st = self.m.get_styles()
100         ct = self.m.get_content()
101         return st, ct
102     #
103     def clear(self):
104         self.m.clear()
105
106 class __ReportV__(object):
107     """
108     Interact with user and C: template for reports
109     """
110     __slots__ = ("c")
111     #
112     default_filename="report.txt"
113     #
114     def __init__(self, c):
115         self.c = c
116     #
117     def save(self, filename=None):
118         if filename is None:
119             filename = self.default_filename
120         _filename = os.path.abspath(filename)
121         #
122         h = self.get()
123         fid = open(_filename, 'w')
124         fid.write(h)
125         fid.close()
126         return filename, _filename
127     #
128     def retrieve(self):
129         return self.c.retrieve()
130     #
131     def __str__(self):
132         return self.get()
133     #
134     def close(self):
135         del self.c
136         return 0
137
138 # ==============================================================================
139 # Classes d'interface utilisateur : ReportViewIn*, ReportStorage
140 # Tags de structure : (title, h1, h2, h3, p, uli, oli, <b></b>, <i></i>)
141
142 class ReportViewInHtml(__ReportV__):
143     """
144     Report in HTML
145     """
146     __slots__ = ()
147     #
148     default_filename="report.html"
149     tags = {
150         "oli":"li",
151         "uli":"li",
152         }
153     #
154     def get(self):
155         st, ct = self.retrieve()
156         inuLi, inoLi = False, False
157         pg = "<html>\n<head>"
158         pg += "\n<title>Report in HTML</title>"
159         pg += "\n</head>\n<body>"
160         for k,ps in enumerate(st):
161             pc = ct[k]
162             try:
163                 ii = ps.index("title")
164                 title = pc[ii]
165                 pg += "%s\n%s\n%s"%('<hr noshade><h1 align="center">',title,'</h1><hr noshade>')
166             except Exception:
167                 pass
168             for i,s in enumerate(ps):
169                 c = pc[i]
170                 if s == "uli" and not inuLi:
171                     pg += "\n<ul>"
172                     inuLi = True
173                 elif s == "oli" and not inoLi:
174                     pg += "\n<ol>"
175                     inoLi = True
176                 elif s != "uli" and inuLi:
177                     pg += "\n</ul>"
178                     inuLi = False
179                 elif s != "oli" and inoLi:
180                     pg += "\n</ol>"
181                     inoLi = False
182                 elif s == "title":
183                     continue
184                 for t in self.tags:
185                     if s == t: s = self.tags[t]
186                 pg += "\n<%s>%s</%s>"%(s,c,s)
187         pg += "\n</body>\n</html>"
188         return pg
189
190 class ReportViewInRst(__ReportV__):
191     """
192     Report in RST
193     """
194     __slots__ = ()
195     #
196     default_filename="report.rst"
197     tags = {
198         "p":["\n\n",""],
199         "uli":["\n  - ",""],
200         "oli":["\n  #. ",""],
201         }
202     titles = {
203         # "title":["=","="],
204         "h1":["","-"],
205         "h2":["","+"],
206         "h3":["","*"],
207         }
208     translation = {
209         "<b>":"**",
210         "<i>":"*",
211         "</b>":"**",
212         "</i>":"*",
213         }
214     #
215     def get(self):
216         st, ct = self.retrieve()
217         inuLi, inoLi = False, False
218         pg = ""
219         for k,ps in enumerate(st):
220             pc = ct[k]
221             try:
222                 ii = ps.index("title")
223                 title = pc[ii]
224                 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
225             except Exception:
226                 pass
227             for i,s in enumerate(ps):
228                 c = pc[i]
229                 if s == "uli" and not inuLi:
230                     pg += "\n"
231                     inuLi = True
232                 elif s == "oli" and not inoLi:
233                     pg += "\n"
234                     inoLi = True
235                 elif s != "uli" and inuLi:
236                     pg += "\n"
237                     inuLi = False
238                 elif s != "oli" and inoLi:
239                     pg += "\n"
240                     inoLi = False
241                 for t in self.translation:
242                     c = c.replace(t,self.translation[t])
243                 if s in self.titles.keys():
244                     pg += "\n%s\n%s\n%s"%(self.titles[s][0]*len(c),c,self.titles[s][1]*len(c))
245                 elif s in self.tags.keys():
246                     pg += "%s%s%s"%(self.tags[s][0],c,self.tags[s][1])
247             pg += "\n"
248         return pg
249
250 class ReportViewInPlainTxt(__ReportV__):
251     """
252     Report in plain TXT
253     """
254     #
255     default_filename="report.txt"
256     tags = {
257         "p":["\n",""],
258         "uli":["\n  - ",""],
259         "oli":["\n  - ",""],
260         }
261     titles = {
262         # "title":["=","="],
263         "h1":["",""],
264         "h2":["",""],
265         "h3":["",""],
266         }
267     translation = {
268         "<b>":"",
269         "<i>":"",
270         "</b>":"",
271         "</i>":"",
272         }
273     #
274     def get(self):
275         st, ct = self.retrieve()
276         inuLi, inoLi = False, False
277         pg = ""
278         for k,ps in enumerate(st):
279             pc = ct[k]
280             try:
281                 ii = ps.index("title")
282                 title = pc[ii]
283                 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
284             except Exception:
285                 pass
286             for i,s in enumerate(ps):
287                 c = pc[i]
288                 if s == "uli" and not inuLi:
289                     inuLi = True
290                 elif s == "oli" and not inoLi:
291                     inoLi = True
292                 elif s != "uli" and inuLi:
293                     inuLi = False
294                 elif s != "oli" and inoLi:
295                     inoLi = False
296                 for t in self.translation:
297                     c = c.replace(t,self.translation[t])
298                 if s in self.titles.keys():
299                     pg += "\n%s\n%s\n%s"%(self.titles[s][0]*len(c),c,self.titles[s][1]*len(c))
300                 elif s in self.tags.keys():
301                     pg += "\n%s%s%s"%(self.tags[s][0],c,self.tags[s][1])
302             pg += "\n"
303         return pg
304
305 # Interface utilisateur de stockage des informations
306 ReportStorage = __ReportC__
307
308 # ==============================================================================
309 if __name__ == "__main__":
310     print('\n AUTODIAGNOSTIC\n')