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