1 # Copyright (C) 2013-2024 CEA, EDF, OPEN CASCADE
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.
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.
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
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 from io import StringIO
27 from salomeContextUtils import SalomeContextException #@UnresolvedImport
30 logConfigParser = logging.getLogger(__name__)
32 ADD_TO_PREFIX = 'ADD_TO_'
33 UNSET_KEYWORD = 'UNSET'
34 # variables defined in this section are set only if not already defined
35 DEFAULT_VARS_SECTION_NAME = 'SALOME DEFAULT VALUES'
37 def _expandSystemVariables(key, val):
38 expandedVal = os.path.expandvars(val) # expand environment variables
39 # Search for not expanded variables (i.e. non-existing environment variables)
40 pattern = re.compile('\${ ( [^}]* ) }', re.VERBOSE) # string enclosed in ${ and }
41 expandedVal = pattern.sub(r'', expandedVal) # remove matching patterns
43 if not "DLIM8VAR" in key: # special case: DISTENE licence key can contain double clons (::)
44 expandedVal = _trimColons(expandedVal)
48 # :TRICKY: So ugly solution...
49 class MultiOptSafeConfigParser(configparser.SafeConfigParser):
51 configparser.SafeConfigParser.__init__(self)
53 # copied from python 2.6.8 Lib.ConfigParser.py
54 # modified (see code comments) to handle duplicate keys
55 def _read(self, fp, fpname):
56 """Parse a sectioned setup file.
58 The sections in setup file contains a title line at the top,
59 indicated by a name in square brackets (`[]'), plus key/value
60 options lines, indicated by `name: value' format lines.
61 Continuations are represented by an embedded newline then
62 leading whitespace. Blank lines, lines beginning with a '#',
63 and just about everything else are ignored.
65 cursect = None # None, or a dictionary
68 e = None # None, or an exception
74 # comment or blank line?
75 if line.strip() == '' or line[0] in '#;':
77 if line.split(None, 1)[0].lower() == 'rem' and line[0] in "rR":
78 # no leading whitespace
81 if line[0].isspace() and cursect is not None and optname:
84 cursect[optname].append(value)
85 # a section header or option header?
87 # is it a section header?
88 mo = self.SECTCRE.match(line)
90 sectname = mo.group('header')
91 if sectname in self._sections:
92 cursect = self._sections[sectname]
93 elif sectname == configparser.DEFAULTSECT:
94 cursect = self._defaults
96 cursect = self._dict()
97 cursect['__name__'] = sectname
98 self._sections[sectname] = cursect
99 # So sections can't start with a continuation line
101 # no section header in the file?
102 elif cursect is None:
103 raise configparser.MissingSectionHeaderError(fpname, lineno, line)
106 mo = self.OPTCRE.match(line)
108 optname, vi, optval = mo.group('option', 'vi', 'value')
109 optname = self.optionxform(optname.rstrip())
110 # This check is fine because the OPTCRE cannot
111 # match if it would set optval to None
112 if optval is not None:
113 if vi in ('=', ':') and ';' in optval:
114 # ';' is a comment delimiter only if it follows
115 # a spacing character
116 pos = optval.find(';')
117 if pos != -1 and optval[pos-1].isspace():
118 optval = optval[:pos]
119 optval = optval.strip()
121 splittedComments = optval.split('#')
122 s = _expandSystemVariables(optname, splittedComments[0])
123 optval = s.strip().strip("'").strip('"')
124 #if len(splittedComments) > 1:
125 # optval += " #" + " ".join(splittedComments[1:])
130 # REPLACE following line (original):
131 #cursect[optname] = [optval]
133 # Check if this optname already exists
134 if (optname in cursect) and (cursect[optname] is not None):
135 cursect[optname][0] += ','+optval
137 cursect[optname] = [optval]
138 # END OF SUBSTITUTION
140 # valueless option handling
141 cursect[optname] = optval
143 # a non-fatal parsing error occurred. set up the
144 # exception but keep going. the exception will be
145 # raised at the end of the file and will contain a
146 # list of all bogus lines
148 e = configparser.ParsingError(fpname)
149 e.append(lineno, repr(line))
150 # if any parsing errors occurred, raise an exception
154 # join the multi-line values collected while reading
155 all_sections = [self._defaults]
156 all_sections.extend(list(self._sections.values()))
157 for options in all_sections:
158 for name, val in list(options.items()):
159 if isinstance(val, list):
160 options[name] = '\n'.join(val)
164 # Parse configuration file
165 # Input: filename, and a list of reserved keywords (environment variables)
166 # Output: a list of pairs (variable, value), and a dictionary associating a list of user-defined values to each reserved keywords
167 # Note: Does not support duplicate keys in a same section
168 def parseConfigFile(filename, reserved = None):
171 config = MultiOptSafeConfigParser()
172 config.optionxform = str # case sensitive
174 # :TODO: test file existence
178 config.read(filename)
179 except configparser.MissingSectionHeaderError:
180 logConfigParser.error("No section found in file: %s"%(filename))
184 return __processConfigFile(config, reserved, filename)
185 except configparser.InterpolationMissingOptionError as e:
186 msg = "A variable may be undefined in SALOME context file: %s\nParser error is: %s\n"%(filename, e)
187 raise SalomeContextException(msg)
190 def __processConfigFile(config, reserved = None, filename="UNKNOWN FILENAME"):
191 # :TODO: may detect duplicated variables in the same section (raise a warning)
192 # or even duplicate sections
199 # Get raw items for each section, and make some processing for environment variables management
200 reservedKeys = [ADD_TO_PREFIX+str(x) for x in reserved] # produce [ 'ADD_TO_reserved_1', 'ADD_TO_reserved_2', ..., ADD_TO_reserved_n ]
201 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:[] }
202 sections = config.sections()
203 for section in sections:
204 entries = config.items(section, raw=False) # use interpolation
205 if len(entries) == 0: # empty section
206 logConfigParser.warning("Empty section: %s in file: %s"%(section, filename))
208 for key,val in entries:
210 logConfigParser.error("Invalid use of reserved variable: %s in file: %s"%(key, filename))
211 elif key == UNSET_KEYWORD:
212 unsetVariables += val.replace(',', ' ').split()
214 expandedVal = _expandSystemVariables(key, val)
216 if key in reservedKeys:
217 shortKey = key[len(ADD_TO_PREFIX):]
218 vals = expandedVal.split(',')
219 reservedValues[shortKey] += vals
220 # remove left&right spaces on each element
221 vals = [v.strip(' \t\n\r') for v in vals]
223 if DEFAULT_VARS_SECTION_NAME == section.upper():
224 defaultValues.append((key, expandedVal))
226 outputVariables.append((key, expandedVal))
229 pass # end for key,val
230 pass # end for section
232 # remove duplicate values
234 for (var, values) in outputVariables:
235 vals = values.split(',')
236 vals = list(set(vals))
237 outVars.append((var, ','.join(vals)))
239 ConfigInfo = collections.namedtuple("ConfigInfo",
244 return ConfigInfo(unsetVariables, outVars, reservedValues, defaultValues)
247 def _trimColons(var):
249 # Remove leading and trailing colons (:)
250 pattern = re.compile('^:+ | :+$', re.VERBOSE)
251 v = pattern.sub(r'', v) # remove matching patterns
252 # Remove multiple colons
253 pattern = re.compile('::+', re.VERBOSE)
254 v = pattern.sub(r':', v) # remove matching patterns