Salome HOME
684858174ef83b5b2aae16de2e57d52cb7cea9bd
[modules/gui.git] / tools / CurvePlot / src / python / controller / utils.py.in
1 class Logger(object):
2     """
3     Debug Info.
4     """
5     LOG_LEVEL = 1 # 0 means all, 1 means all but DEBUG, 2 means all but INFO and DEBUG, 3 only FATAL
6
7     @classmethod
8     def Debug(cls, msg):
9         """
10         Prints an information message to the standard output.
11         
12         :param msg: str -- The message to be printed.
13         
14         """
15         if cls.LOG_LEVEL <= 0:
16             cls.__log("[DEBUG]", msg)
17
18
19     @classmethod
20     def Info(cls, msg):
21         """
22         Prints an information message to the standard output.
23         
24         :param msg: str -- The message to be printed.
25         
26         """
27         if cls.LOG_LEVEL <= 1:
28             cls.__log("[INFO]", msg)
29
30     
31     @classmethod
32     def Warning(cls, msg):
33         """
34         Prints a warning message to the standard output.
35         
36         :param msg: str -- The message to be printed.
37         
38         """
39         if cls.LOG_LEVEL <= 2:
40             cls.__log("[WARNING]", msg)
41         
42     
43     @classmethod
44     def FatalError(cls, msg):
45         """
46         Prints an error message to the standard output.
47         
48         :param msg: str -- The message to be printed.
49         :raises: Exception.
50         
51         """
52         if cls.LOG_LEVEL <= 3:
53             cls.__log("[FATAL]", msg)
54             raise Exception(msg)
55
56     @classmethod
57     def __log(cls, typ, msg):
58         print("%s: %s" % (typ, msg))
59       
60 def trQ(tag, context="CURVEPLOT"):
61   """ @return a QString read from the translation file """
62   from pyqtside.QtGui import QApplication 
63   return QApplication.translate(context, tag) 
64
65 def trU(tag, context="CURVEPLOT"):
66   """ @return same as above, but returns a Python unicode string.  """
67   qs = trQ(tag, context)
68   return unicode(qs, 'utf-8')
69
70 def toUnicodeWithWarning(s, method_name):
71   try: 
72     s = unicode(s)
73   except:
74     Logger.Warning("%s - warning, passing non-unicode, non-ASCII string '%s'! Trying to convert myself to UTF-8 ..." % (method_name, s))
75     s = unicode(s, 'utf-8')
76   return s
77
78 def completeResPath(fileName):
79   import os
80   subPath = "@SALOME_CURVEPLOT_INSTALL_PYTHON@"
81   rd = os.environ.get("CURVEPLOT_ROOT_DIR", None)
82   if rd is None:
83     raise Exception("CURVEPLOT_ROOT_DIR is not defined!")
84   filePath = os.path.join(rd, subPath, fileName)
85   return filePath