Salome HOME
Merge remote-tracking branch 'origin/vsr/qtesting'
[modules/med.git] / src / MEDCalc / tui / medconsole.py
1 # Copyright (C) 2012-2016  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 import medcalc
21
22 dataManager = medcalc.medcorba.factory.getDataManager()
23
24 # IMPORTANT NOTE:
25 # the pyConsoleGlobals variable should hold the globals() dictionnary of
26 # the python console context
27 pyConsoleGlobals = None
28
29 #-----
30 # This function is to be called from the working python console to
31 # specify the globals() dictionnary (used in fieldtools for stat analysis)
32 #
33 # >>> medcalc.setConsoleGlobals(globals())
34 #
35 def setConsoleGlobals(pyconsole_globals):
36   global pyConsoleGlobals
37   pyConsoleGlobals = pyconsole_globals
38 #
39
40 import SALOME
41 def saveWorkspace(filename):
42   """
43   Dump your job in a med file. Only the fields marked as persistent
44   are saved in the specified file.
45   """
46   try:
47     dataManager.savePersistentFields(filename)
48   except SALOME.SALOME_Exception, ex:
49     medcalc.err(ex.details.text)
50 #
51
52 # Clean workspace
53 from medevents import notifyGui_cleanWorkspace
54 def cleanWorkspace():
55   dvars = pyConsoleGlobals
56   if dvars is None:
57     return
58   all_keys = []
59   for varkey, var in dvars.items():
60     if isinstance(var, medcalc.FieldProxy):
61       all_keys.append("%s"%varkey)
62   if len(all_keys) > 0:
63     exec "del "+",".join(all_keys) in pyConsoleGlobals
64   notifyGui_cleanWorkspace()
65 #
66
67 # Remove variable from console
68 from medevents import notifyGui_removeFromWorkspace
69 def removeFromWorkspace(fieldProxy):
70   dvars = pyConsoleGlobals
71   if dvars is None:
72     return
73   for varkey, var in dvars.items():
74     if isinstance(var, medcalc.FieldProxy) and var.id == fieldProxy.id:
75       exec("del %s"%varkey) in pyConsoleGlobals
76   notifyGui_removeFromWorkspace(fieldProxy.id)
77 #
78
79 # Get list a field with information using the commands ls and/or la
80 def getEnvironment(local=True, remote=False):
81   """
82   This function return the status of the medcalc context, i.e. the
83   list of fields defined in this python session.
84   """
85   status=""
86   if local is True:
87     dvars = pyConsoleGlobals
88     if dvars is None:
89       medcalc.wrn("The stat function required the specification of the python context")
90       medcalc.inf("Type this command \"import medcalc; medcalc.setConsoleGlobals(globals())")
91     if remote is True:
92       status="========= Fields used in the current context ===\n"
93     for varkey in dvars.keys():
94       var = dvars[varkey]
95       if isinstance(var, medcalc.FieldProxy):
96         status+="%s \t(id=%s, name=%s)\n"%(varkey,var.id,var.fieldname)
97
98   if remote is True:
99     if local is True:
100       status+="\n========= Fields available in the data manager ===\n"
101     fieldHandlerList = dataManager.getFieldHandlerList()
102     for fieldHandler in fieldHandlerList:
103       status+="id=%s\tname\t= %s\n\tmesh\t= %s\n\t(it,dt)\t= (%s,%s)\n\tsource\t= %s\n"%(
104         fieldHandler.id,
105         fieldHandler.fieldname,
106         fieldHandler.meshname,
107         fieldHandler.iteration,
108         fieldHandler.order,
109         fieldHandler.source)
110       status+="---------\n"
111
112     if len(fieldHandlerList) > 0:
113       status+="(use 'f=accessField(id)' to get a field in the current context)"
114
115   return status
116 #
117
118 # For simpler typing, one can create a python command for status
119 # (avoid to type "print getEnvironment()")
120 class ListFields(object):
121   """
122   A stat object displays the status of the med operating context, i.e. the
123   list of fields defined in this python session.
124   """
125   def __init__(self, all=False):
126     self.__local  = True
127     self.__remote = all
128     # all = True means that the local metadata (fieldproxy) and the
129     # remote metadata on the engine (MEDCouplingFieldDouble) are
130     # displayed by the stat command. Otherwise, only the local
131     # metadata are displayed.
132   #
133   def __repr__(self):
134     return getEnvironment(self.__local, self.__remote)
135   #
136 #
137 # Creating the commands list (ls) and list all (la)
138 ls = ListFields(all=False)
139 la = ListFields(all=True)
140
141 #
142 # Add variable to workspace
143 from medevents import notifyGui_putInWorkspace
144 def putInWorkspace(fieldProxy):
145   """
146   This function puts a reference to this field in the GUI data
147   model. When a field is referenced in the GUI data model, then it
148   belongs to the workspace. When the workspace is saved, all the
149   field that belongs to the workspace are saved.
150   """
151   dataManager.markAsPersistent(fieldProxy.id, True)
152   notifyGui_putInWorkspace(fieldProxy.id)
153 #
154
155 def accessField(fieldHandlerId):
156   """
157   This return a field proxy on the field identified by the specified
158   field handler id.
159   """
160   return medcalc.newFieldProxy(fieldHandlerId)
161 #