1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2022 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 def __init__(self, part="default"):
39 self.__part = str(part)
43 def append(self, content, style="p", position=-1):
45 self.__styles.append(style)
46 self.__content.append(content)
48 self.__styles.insert(position, style)
49 self.__content.insert(position, content)
55 def get_content(self):
58 class _ReportM__(object):
60 Store and retrieve the data for C: internal class
62 def __init__(self, part='default'):
64 self.__document[part] = _ReportPartM__(part)
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)
73 op = list(self.__document.keys()) ; op.sort()
74 return [self.__document[k].get_styles() for k in op]
76 def get_content(self):
77 op = list(self.__document.keys()) ; op.sort()
78 return [self.__document[k].get_content() for k in op]
83 class __ReportC__(object):
85 Get user commands, update M and V: user intertace to create the report
89 def append(self, content="", style="p", position=-1, part="default"):
90 return self.m.append(content, style, position, part)
93 st = self.m.get_styles()
94 ct = self.m.get_content()
100 class __ReportV__(object):
102 Interact with user and C: template for reports
105 default_filename="report.txt"
107 def __init__(self, c):
110 def save(self, filename=None):
112 filename = self.default_filename
113 _filename = os.path.abspath(filename)
116 fid = open(_filename, 'w')
119 return filename, _filename
122 return self.c.retrieve()
131 # ==============================================================================
132 # Classes d'interface utilisateur : ReportStorage, ReportViewIn*
133 # Tags de structure : (title, h1, h2, h3, p, uli, oli, <b></b>, <i></i>)
135 ReportStorage = __ReportC__
137 class ReportViewInHtml(__ReportV__):
142 default_filename="report.html"
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):
157 ii = ps.index("title")
159 pg += "%s\n%s\n%s"%('<hr noshade><h1 align="center">',title,'</h1><hr noshade>')
162 for i,s in enumerate(ps):
164 if s == "uli" and not inuLi:
167 elif s == "oli" and not inoLi:
170 elif s != "uli" and inuLi:
173 elif s != "oli" and inoLi:
179 if s == t: s = self.tags[t]
180 pg += "\n<%s>%s</%s>"%(s,c,s)
181 pg += "\n</body>\n</html>"
184 class ReportViewInRst(__ReportV__):
189 default_filename="report.rst"
209 st, ct = self.retrieve()
210 inuLi, inoLi = False, False
212 for k,ps in enumerate(st):
215 ii = ps.index("title")
217 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
220 for i,s in enumerate(ps):
222 if s == "uli" and not inuLi:
225 elif s == "oli" and not inoLi:
228 elif s != "uli" and inuLi:
231 elif s != "oli" and inoLi:
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])
243 class ReportViewInPlainTxt(__ReportV__):
248 default_filename="report.txt"
268 st, ct = self.retrieve()
269 inuLi, inoLi = False, False
271 for k,ps in enumerate(st):
274 ii = ps.index("title")
276 pg += "%s\n%s\n%s"%("="*80,title,"="*80)
279 for i,s in enumerate(ps):
281 if s == "uli" and not inuLi:
283 elif s == "oli" and not inoLi:
285 elif s != "uli" and inuLi:
287 elif s != "oli" and inoLi:
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])
298 # ==============================================================================
299 if __name__ == "__main__":
300 print('\n AUTODIAGNOSTIC\n')