+from salome_utils import verbose, getPortNumber, getHomeDir
+
+
+# names of tags in XML configuration file
+doc_tag = "document"
+sec_tag = "section"
+par_tag = "parameter"
+import_tag = "import"
+
+# names of attributes in XML configuration file
+nam_att = "name"
+val_att = "value"
+
+# certain values in XML configuration file ("launch" section)
+lanch_nam = "launch"
+help_nam = "help"
+gui_nam = "gui"
+splash_nam = "splash"
+logger_nam = "logger"
+xterm_nam = "xterm"
+file_nam = "file"
+portkill_nam = "portkill"
+killall_nam = "killall"
+modules_nam = "modules"
+embedded_nam = "embedded"
+standalone_nam = "standalone"
+key_nam = "key"
+terminal_nam = "terminal"
+interp_nam = "interp"
+except_nam = "noexcepthandler"
+terminal_nam = "terminal"
+pinter_nam = "pinter"
+batch_nam = "batch"
+test_nam = "test"
+play_nam = "play"
+lang_nam = "language"
+gdb_session_nam = "gdb_session"
+ddd_session_nam = "ddd_session"
+valgrind_session_nam = "valgrind_session"
+shutdown_servers_nam = "shutdown_servers"
+foreground_nam = "foreground"
+wake_up_session_nam = "wake_up_session"
+launcher_only_nam = "launcher_only"
+launcher_nam = "launcher"
+
+# values in XML configuration file giving specific module parameters (<module_name> section)
+# which are stored in opts with key <module_name>_<parameter> (eg SMESH_plugins)
+plugins_nam = "plugins"
+
+# values passed as arguments, NOT read from XML config file, but set from within this script
+appname_nam = "appname"
+port_nam = "port"
+useport_nam = "useport"
+salomecfgname = "salome"
+salomeappname = "SalomeApp"
+script_nam = "pyscript"
+
+# possible choices for the "embedded" and "standalone" parameters
+embedded_choices = [ "registry", "study", "moduleCatalog", "cppContainer", "SalomeAppEngine" ]
+standalone_choices = [ "registry", "study", "moduleCatalog", "cppContainer"]
+
+# values of boolean type (must be '0' or '1').
+# xml_parser.boolValue() is used for correct setting
+boolKeys = ( gui_nam, splash_nam, logger_nam, file_nam, xterm_nam, portkill_nam, killall_nam, except_nam, pinter_nam, shutdown_servers_nam, launcher_only_nam )
+intKeys = ( interp_nam, )
+strKeys = ( launcher_nam )
+
+# values of list type
+listKeys = ( embedded_nam, key_nam, modules_nam, standalone_nam, plugins_nam )
+
+###
+# Get the application version
+# Uses GUI_ROOT_DIR (or KERNEL_ROOT_DIR in batch mode) +/bin/salome/VERSION file
+###
+def version():
+ try:
+ filename = None
+ root_dir = os.environ.get( 'KERNEL_ROOT_DIR', '' ) # KERNEL_ROOT_DIR or "" if not found
+ version_file = os.path.join(root_dir, 'bin', 'salome', 'VERSION')
+ if root_dir and os.path.exists( version_file ):
+ filename = version_file
+ root_dir = os.environ.get( 'GUI_ROOT_DIR', '' ) # GUI_ROOT_DIR "" if not found
+ version_file = os.path.join(root_dir, 'bin', 'salome', 'VERSION')
+ if root_dir and os.path.exists( version_file ):
+ filename = version_file
+ if filename:
+ with open(filename, "r") as f:
+ v = f.readline() # v = "THIS IS SALOME - SALOMEGUI VERSION: 3.0.0"
+ match = re.search( r':\s+([a-zA-Z0-9.]+)\s*$', v )
+ if match :
+ return match.group( 1 )
+ except:
+ pass
+ return ''
+
+###
+# Calculate and return configuration file unique ID
+# For example: for SALOME version 3.1.0a1 the id is 300999701
+###
+def version_id(fname):
+ major = minor = release = dev1 = dev2 = 0
+ vers = fname.split(".")
+ if len(vers) > 0:
+ try:
+ major = int(vers[0])
+ except ValueError:
+ # If salome version given is DEV, the call to int('DEV') will fail with
+ # a ValueError exception
+ pass
+ try:
+ if len(vers) > 1: minor = int(vers[1])
+ except ValueError:
+ # If salome version given is 7.DEV, the call to int('DEV') will fail with
+ # a ValueError exception
+ pass
+ if len(vers) > 2:
+ mr = re.search(r'^([0-9]+)([A-Z]|RC)?([0-9]*)',vers[2], re.I)
+ if mr:
+ release = int(mr.group(1))
+ if mr.group(2):
+ tag = mr.group(2).strip().lower()
+ if tag == "rc":
+ dev1 = 49 # release candidate
+ elif tag:
+ dev1 = ord(tag)-ord('a')+1
+ pass
+ if mr.group(3):
+ dev2 = int(mr.group(3).strip())
+ pass
+ pass
+ dev = dev1 * 100 + dev2
+ ver = major
+ ver = ver * 100 + minor
+ ver = ver * 100 + release
+ ver = ver * 10000
+ if dev > 0: ver = ver - dev
+ return ver
+
+###
+# Get default user configuration file name
+# For SALOME, it is:
+# - on Linux: ~/.config/salome/SalomeApprc[.<version>]
+# - on Windows: ~/SalomeApp.xml[.<version>]
+# where <version> is an optional version number
+###
+def defaultUserFile(appname=salomeappname, cfgname=salomecfgname):
+ v = version()
+ filetmpl = sys.platform == "win32" and "{0}.xml.{1}" or "{0}rc.{1}"
+ paths = []
+ paths.append(getHomeDir())
+ paths.append(".config")
+ if cfgname: paths.append(cfgname)
+ paths.append(filetmpl.format(appname, v))
+ return os.path.join(*paths)
+
+###
+# Get user configuration file name
+###
+def userFile(appname, cfgname):
+ # get app version
+ v = version()
+ if not v: return None # unknown version
+
+ # get default user file name
+ filename = defaultUserFile(appname, cfgname)
+ if not filename: return None # default user file name is bad
+
+ # check that default user file exists
+ if os.path.exists(filename): return filename # user file is found
+
+ # otherwise try to detect any appropriate user file
+
+ # ... calculate default version id
+ id0 = version_id(v)
+ if not id0: return None # bad version id -> can't detect appropriate file
+
+ # ... get all existing user preferences files
+ filetmpl1 = sys.platform == "win32" and "{0}.xml.*" or "{0}rc.*"
+ filetmpl2 = sys.platform == "win32" and filetmpl1 or "." + filetmpl1
+ files = []
+ if cfgname:
+ # Since v6.6.0 - in ~/.config/salome directory, without dot prefix
+ files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, filetmpl1.format(appname)))
+ # Since v6.5.0 - in ~/.config/salome directory, dot-prefixed (backward compatibility)
+ if filetmpl2 and filetmpl2 != filetmpl1:
+ files += glob.glob(os.path.join(getHomeDir(), ".config", cfgname, filetmpl2.format(appname)))
+ pass
+ # old style (before v6.5.0) - in ~ directory, dot-prefixed
+ if filetmpl2 and filetmpl2 != filetmpl1:
+ files += glob.glob(os.path.join(getHomeDir(), filetmpl2.format(appname)))
+ pass
+
+ # ... loop through all files and find most appropriate file (with closest id)
+ appr_id = -1
+ appr_file = ""
+ for f in files:
+ ff = os.path.basename( f )
+ if sys.platform == "win32":
+ match = re.search( r'^{0}\.xml\.([a-zA-Z0-9.]+)$'.format(appname), ff )
+ else:
+ match = re.search( r'^\.?{0}rc\.([a-zA-Z0-9.]+)$'.format(appname), ff )
+ if match:
+ ver = version_id(match.group(1))
+ if not ver: continue # bad version id -> skip file
+ if appr_id < 0 or abs(appr_id-id0) > abs(ver-id0):
+ appr_id = ver
+ appr_file = f
+ pass
+ elif abs(appr_id-id0) == abs(ver-id0):
+ if not os.path.basename(f).startswith("."): appr_file = f
+ pass
+ pass
+ pass
+ return appr_file
+
+# --
+
+def process_containers_params( standalone, embedded ):
+ # 1. filter inappropriate containers names
+ if standalone is not None:
+ standalone = [x for x in standalone if x in standalone_choices]
+ if embedded is not None:
+ embedded = [x for x in embedded if x in embedded_choices]
+
+ # 2. remove containers appearing in 'standalone' parameter from the 'embedded'
+ # parameter --> i.e. 'standalone' parameter has higher priority
+ if standalone is not None and embedded is not None:
+ embedded = [x for x in embedded if x not in standalone]
+
+ # 3. return corrected parameters values
+ return standalone, embedded
+