1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2024 EDF R&D
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.
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.
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
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 # Author: Jean-Philippe Argaud, jean-philippe.argaud@edf.fr, EDF R&D
24 Gestion simple de rapports structurés créés par l'utilisateur.
26 __author__ = "Jean-Philippe ARGAUD"
31 # ==============================================================================
32 # Classes de services non utilisateur
34 class _ReportPartM__(object):
36 Store and retrieve the data for C: internal class
38 __slots__ = ("__part", "__styles", "__content")
40 def __init__(self, part="default"):
41 self.__part = str(part)
45 def append(self, content, style="p", position=-1):
47 self.__styles.append(style)
48 self.__content.append(content)
50 self.__styles.insert(position, style)
51 self.__content.insert(position, content)
57 def get_content(self):
60 class _ReportM__(object):
62 Store and retrieve the data for C: internal class
64 __slots__ = ("__document")
66 def __init__(self, part='default'):
68 self.__document[part] = _ReportPartM__(part)
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)
77 op = list(self.__document.keys())
79 return [self.__document[k].get_styles() for k in op]
81 def get_content(self):
82 op = list(self.__document.keys())
84 return [self.__document[k].get_content() for k in op]
89 class __ReportC__(object):
91 Get user commands, update M and V: user intertace to create the report
97 def append(self, content="", style="p", position=-1, part="default"):
98 return self.m.append(content, style, position, part)
101 st = self.m.get_styles()
102 ct = self.m.get_content()
108 class __ReportV__(object):
110 Interact with user and C: template for reports
114 default_filename = "report.txt"
116 def __init__(self, c):
119 def save(self, filename=None):
121 filename = self.default_filename
122 _filename = os.path.abspath(filename)
125 fid = open(_filename, 'w')
128 return filename, _filename
131 return self.c.retrieve()
140 # ==============================================================================
141 # Classes d'interface utilisateur : ReportViewIn*, ReportStorage
142 # Tags de structure : (title, h1, h2, h3, p, uli, oli, <b></b>, <i></i>)
144 class ReportViewInHtml(__ReportV__):
150 default_filename = "report.html"
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):
165 ii = ps.index("title")
167 pg += "%s\n%s\n%s"%('<hr noshade><h1 align="center">', title, '</h1><hr noshade>')
170 for ip, sp in enumerate(ps):
172 if sp == "uli" and not inuLi:
175 elif sp == "oli" and not inoLi:
178 elif sp != "uli" and inuLi:
181 elif sp != "oli" and inoLi:
189 pg += "\n<%s>%s</%s>"%(sp, cp, sp)
190 pg += "\n</body>\n</html>"
193 class ReportViewInRst(__ReportV__):
199 default_filename = "report.rst"
202 "uli": ["\n - ", ""],
203 "oli": ["\n #. ", ""],
218 st, ct = self.retrieve()
219 inuLi, inoLi = False, False
221 for ks, ps in enumerate(st):
224 ii = ps.index("title")
226 pg += "%s\n%s\n%s"%("=" * 80, title, "=" * 80)
229 for ip, sp in enumerate(ps):
231 if sp == "uli" and not inuLi:
234 elif sp == "oli" and not inoLi:
237 elif sp != "uli" and inuLi:
240 elif sp != "oli" and inoLi:
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])
252 class ReportViewInPlainTxt(__ReportV__):
259 default_filename = "report.txt"
262 "uli": ["\n - ", ""],
263 "oli": ["\n - ", ""],
278 st, ct = self.retrieve()
279 inuLi, inoLi = False, False
281 for ks, ps in enumerate(st):
284 ii = ps.index("title")
286 pg += "%s\n%s\n%s"%("=" * 80, title, "=" * 80)
289 for ip, sp in enumerate(ps):
291 if sp == "uli" and not inuLi:
293 elif sp == "oli" and not inoLi:
295 elif sp != "uli" and inuLi:
297 elif sp != "oli" and inoLi:
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])
308 # Interface utilisateur de stockage des informations
309 ReportStorage = __ReportC__
311 # ==============================================================================
312 if __name__ == "__main__":
313 print("\n AUTODIAGNOSTIC\n")