1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2023 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()) ; op.sort()
78 return [self.__document[k].get_styles() for k in op]
80 def get_content(self):
81 op = list(self.__document.keys()) ; op.sort()
82 return [self.__document[k].get_content() for k in op]
87 class __ReportC__(object):
89 Get user commands, update M and V: user intertace to create the report
95 def append(self, content="", style="p", position=-1, part="default"):
96 return self.m.append(content, style, position, part)
99 st = self.m.get_styles()
100 ct = self.m.get_content()
106 class __ReportV__(object):
108 Interact with user and C: template for reports
112 default_filename="report.txt"
114 def __init__(self, c):
117 def save(self, filename=None):
119 filename = self.default_filename
120 _filename = os.path.abspath(filename)
123 fid = open(_filename, 'w')
126 return filename, _filename
129 return self.c.retrieve()
138 # ==============================================================================
139 # Classes d'interface utilisateur : ReportViewIn*, ReportStorage
140 # Tags de structure : (title, h1, h2, h3, p, uli, oli, <b></b>, <i></i>)
142 class ReportViewInHtml(__ReportV__):
148 default_filename="report.html"
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):
163 ii = ps.index("title")
165 pg += "%s\n%s\n%s"%('<hr noshade><h1 align="center">',title,'</h1><hr noshade>')
168 for i,s in enumerate(ps):
170 if s == "uli" and not inuLi:
173 elif s == "oli" and not inoLi:
176 elif s != "uli" and inuLi:
179 elif s != "oli" and inoLi:
185 if s == t: s = self.tags[t]
186 pg += "\n<%s>%s</%s>"%(s,c,s)
187 pg += "\n</body>\n</html>"
190 class ReportViewInRst(__ReportV__):
196 default_filename="report.rst"
216 st, ct = self.retrieve()
217 inuLi, inoLi = False, False
219 for k,ps in enumerate(st):
222 ii = ps.index("title")
224 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
227 for i,s in enumerate(ps):
229 if s == "uli" and not inuLi:
232 elif s == "oli" and not inoLi:
235 elif s != "uli" and inuLi:
238 elif s != "oli" and inoLi:
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])
250 class ReportViewInPlainTxt(__ReportV__):
257 default_filename="report.txt"
277 st, ct = self.retrieve()
278 inuLi, inoLi = False, False
280 for k,ps in enumerate(st):
283 ii = ps.index("title")
285 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
288 for i,s in enumerate(ps):
290 if s == "uli" and not inuLi:
292 elif s == "oli" and not inoLi:
294 elif s != "uli" and inuLi:
296 elif s != "oli" and inoLi:
298 for t in self.translation:
299 c = c.replace(t,self.translation[t])
300 if s in self.titles.keys():
301 pg += "\n%s\n%s\n%s"%(self.titles[s][0]*len(c),c,self.titles[s][1]*len(c))
302 elif s in self.tags.keys():
303 pg += "\n%s%s%s"%(self.tags[s][0],c,self.tags[s][1])
307 # Interface utilisateur de stockage des informations
308 ReportStorage = __ReportC__
310 # ==============================================================================
311 if __name__ == "__main__":
312 print("\n AUTODIAGNOSTIC\n")