Salome HOME
0b1302a7609276ec0ec272a52d8cb09dbabe736d
[modules/gui.git] / tools / CurvePlot / src / python / controller / utils.py.in
1 # Copyright (C) 2016-2022  CEA/DEN, EDF R&D
2 #
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
7 #
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 # Lesser General Public License for more details.
12 #
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 #
19
20 class Logger(object):
21     """
22     Debug Info.
23     """
24     LOG_LEVEL = 1 # 0 means all, 1 means all but DEBUG, 2 means all but INFO and DEBUG, 3 only FATAL
25
26     @classmethod
27     def Debug(cls, msg):
28         """
29         Prints an information message to the standard output.
30         
31         :param msg: str -- The message to be printed.
32         
33         """
34         if cls.LOG_LEVEL <= 0:
35             cls.__log("[DEBUG]", msg)
36
37
38     @classmethod
39     def Info(cls, msg):
40         """
41         Prints an information message to the standard output.
42         
43         :param msg: str -- The message to be printed.
44         
45         """
46         if cls.LOG_LEVEL <= 1:
47             cls.__log("[INFO]", msg)
48
49     
50     @classmethod
51     def Warning(cls, msg):
52         """
53         Prints a warning message to the standard output.
54         
55         :param msg: str -- The message to be printed.
56         
57         """
58         if cls.LOG_LEVEL <= 2:
59             cls.__log("[WARNING]", msg)
60         
61     
62     @classmethod
63     def FatalError(cls, msg):
64         """
65         Prints an error message to the standard output.
66         
67         :param msg: str -- The message to be printed.
68         :raises: Exception.
69         
70         """
71         if cls.LOG_LEVEL <= 3:
72             cls.__log("[FATAL]", msg)
73             raise Exception(msg)
74
75     @classmethod
76     def __log(cls, typ, msg):
77         print("%s: %s" % (typ, msg))
78       
79 def trQ(tag, context="CURVEPLOT"):
80   """ @return a QString read from the translation file """
81   from pyqtside.QtWidgets import QApplication 
82   return QApplication.translate(context, tag) 
83
84 def trU(tag, context="CURVEPLOT"):
85   """ @return same as above, but returns a Python unicode string.  """
86   qs = trQ(tag, context)
87   return str(qs, 'utf-8')
88
89 def toUnicodeWithWarning(s, method_name):
90   try: 
91     s = str(s)
92   except:
93     Logger.Warning("%s - warning, passing non-unicode, non-ASCII string '%s'! Trying to convert myself to UTF-8 ..." % (method_name, s))
94     s = str(s, 'utf-8')
95   return s
96
97 def completeResPath(fileName):
98   import os
99   subPath = "@SALOME_CURVEPLOT_INSTALL_PYTHON@"
100   rd = os.environ.get("CURVEPLOT_ROOT_DIR", None)
101   if rd is None:
102     raise Exception("CURVEPLOT_ROOT_DIR is not defined!")
103   if @SALOME_CURVEPLOT_TEST_MODE@:   # do not remove automatically modified in CMake config
104     subPath = "@CRVPLOT_TEST_INSTALL@"
105   filePath = os.path.join(rd, subPath, fileName)
106   return filePath