Salome HOME
Management of scripts+args in SALOME shell and TUI (not yet in GUI)
[modules/kernel.git] / bin / salomeConsole.py
1 #!/usr/bin/env python
2 #  -*- coding: iso-8859-1 -*-
3 # Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
6 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 #
8 # This library is free software; you can redistribute it and/or
9 # modify it under the terms of the GNU Lesser General Public
10 # License as published by the Free Software Foundation; either
11 # version 2.1 of the License.
12 #
13 # This library is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 # Lesser General Public License for more details.
17 #
18 # You should have received a copy of the GNU Lesser General Public
19 # License along with this library; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21 #
22 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #
24
25 import os
26 import sys
27 import glob
28
29 #-------------------------------
30 # Python completion and others if you want
31 # You should have set PYTHONSTARTUP env variable
32 # or import user should try to import $HOME/.pythonrc.py
33 #-------------------------------
34 import user
35
36 #-------------------------------
37 # Get major CORBA objects
38 #-------------------------------
39 import CORBA
40 import CosNaming
41 import salome_utils
42
43 import orbmodule
44
45 def getRunningSession():
46   omniorbUserPath = os.getenv("OMNIORB_USER_PATH")
47   files = glob.glob(os.path.join(omniorbUserPath,".omniORB_"+salome_utils.getUserName()+"_*.cfg"))
48
49   filename=""
50   if len(files)==1:
51     filename=files[0]
52   else:
53     print "You have %d sessions running" % len(files)
54     for f in files:
55       print "Session:",f
56       rep= raw_input("Do you want to connect to this session [y|n]")
57       if rep == "y":
58         filename=f
59         break
60
61   if filename != "":
62     os.environ['OMNIORB_CONFIG']=filename
63   else:
64     rep= raw_input("Do you want to try a local session on port 2810 ? [y|n]")
65     if rep == "y":
66       # Try a local session running on port 2810
67       sys.argv=sys.argv+['-ORBInitRef','NameService=corbaname::localhost:2810']
68     else:
69       sys.exit(1)
70 #
71
72 class client(orbmodule.client):
73    def initNS(self,args):
74       # Obtain a reference to the root naming context
75       obj = self.orb.resolve_initial_references("NameService")
76       try:
77           self.rootContext = obj._narrow(CosNaming.NamingContext)
78           return
79       except (CORBA.TRANSIENT,CORBA.OBJECT_NOT_EXIST,CORBA.COMM_FAILURE):
80           print "It's not a valid naming service"
81           self.rootContext = None
82           raise
83 #
84
85 def startClient():
86   try:
87     clt=client()
88   except Exception:
89     sys.exit(1)
90   #
91   print "Naming Service address: ",clt.orb.object_to_string(clt.rootContext)
92
93   clt.showNS()
94
95   session=clt.waitNS("/Kernel/Session")
96   catalog=clt.waitNS("/Kernel/ModulCatalog")
97   studyMgr=clt.waitNS("/myStudyManager")
98   import salome
99   salome.salome_init()
100   from salome import lcc
101 #
102
103 getRunningSession()
104 startClient()