Salome HOME
Add 'SALOME DEFAULT VALUES' section to the configuration file.
authorOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 17 Nov 2020 09:26:52 +0000 (10:26 +0100)
committerOvidiu Mircescu <ovidiu.mircescu@edf.fr>
Tue, 17 Nov 2020 09:26:52 +0000 (10:26 +0100)
This section can be used to replace the default values used by
Salome, without replacing a value defined by the user in the
environment.
Variables that are already defined in the environment are not
modified when declared in this section.

bin/parseConfigFile.py
bin/salomeContext.py

index 69c3e2acd741bb362f820e32da0306f02c62931d..4c2905574eae5e2856672d7fd81cf146238457df 100644 (file)
@@ -23,6 +23,7 @@ import logging
 import re
 from io import StringIO
 import subprocess
+import collections
 from salomeContextUtils import SalomeContextException #@UnresolvedImport
 
 logging.basicConfig()
@@ -30,7 +31,8 @@ logConfigParser = logging.getLogger(__name__)
 
 ADD_TO_PREFIX = 'ADD_TO_'
 UNSET_KEYWORD = 'UNSET'
-
+# variables defined in this section are set only if not already defined
+DEFAULT_VARS_SECTION_NAME = 'SALOME DEFAULT VALUES'
 
 def _expandSystemVariables(key, val):
   expandedVal = os.path.expandvars(val) # expand environment variables
@@ -193,6 +195,7 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"):
     reserved = []
   unsetVariables = []
   outputVariables = []
+  defaultValues = []
   # Get raw items for each section, and make some processing for environment variables management
   reservedKeys = [ADD_TO_PREFIX+str(x) for x in reserved] # produce [ 'ADD_TO_reserved_1', 'ADD_TO_reserved_2', ..., ADD_TO_reserved_n ]
   reservedValues = dict([str(i),[]] for i in reserved) # create a dictionary in which keys are the 'ADD_TO_reserved_i' and associated values are empty lists: { 'reserved_1':[], 'reserved_2':[], ..., reserved_n:[] }
@@ -217,7 +220,10 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"):
           # remove left&right spaces on each element
           vals = [v.strip(' \t\n\r') for v in vals]
         else:
-          outputVariables.append((key, expandedVal))
+          if DEFAULT_VARS_SECTION_NAME == section.upper():
+            defaultValues.append((key, expandedVal))
+          else:
+            outputVariables.append((key, expandedVal))
           pass
         pass # end if key
       pass # end for key,val
@@ -230,7 +236,12 @@ def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"):
     vals = list(set(vals))
     outVars.append((var, ','.join(vals)))
 
-  return unsetVariables, outVars, reservedValues
+  ConfigInfo = collections.namedtuple("ConfigInfo",
+                                      ["unsetVariables",
+                                       "outputVariables",
+                                       "reservedValues",
+                                       "defaultValues"])
+  return ConfigInfo(unsetVariables, outVars, reservedValues, defaultValues)
 #
 
 def _trimColons(var):
index 689411316ec68168475750204bd2d41bf07d57b0..99fcd113a808e316156248a46c34af6750a1d8a6 100755 (executable)
@@ -183,6 +183,14 @@ class SalomeContext:
     os.environ[name] = value
   #
 
+  def setDefaultValue(self, name, value):
+    """ Set environment variable only if it is undefined."""
+    env = os.getenv(name, '')
+    if not env:
+      value = os.path.expandvars(value) # expand environment variables
+      self.getLogger().debug("Set environment variable: %s=%s", name, value)
+      os.environ[name] = value
+
   """Unset environment variable"""
   def unsetVariable(self, name):
     if os.environ.has_key(name):
@@ -287,7 +295,11 @@ class SalomeContext:
     if reserved is None:
       reserved = []
     try:
-      unsetVars, configVars, reservedDict = parseConfigFile(filename, reserved)
+      configInfo = parseConfigFile(filename, reserved)
+      unsetVars = configInfo.unsetVariables
+      configVars = configInfo.outputVariables
+      reservedDict = configInfo.reservedValues
+      defaultValues = configInfo.defaultValues
     except SalomeContextException as e:
       msg = "%s"%e
       self.getLogger().error(msg)
@@ -312,6 +324,10 @@ class SalomeContext:
       self.setVariable(key, val, overwrite=True)
       pass
 
+    for key,val in defaultValues:
+      self.setDefaultValue(key, val)
+      pass
+
     pythonpath = os.getenv('PYTHONPATH','').split(os.pathsep)
     pythonpath = [ os.path.realpath(x) for x in pythonpath ]
     sys.path[:0] = pythonpath