__init__.py (SCons-4.3.0) | : | __init__.py (SCons-4.4.0) | ||
---|---|---|---|---|
skipping to change at line 24 | skipping to change at line 24 | |||
# in all copies or substantial portions of the Software. | # in all copies or substantial portions of the Software. | |||
# | # | |||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY | |||
# KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE | |||
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | |||
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | |||
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | |||
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |||
"""Add user-friendly customizable variables to an SCons build. """ | """Adds user-friendly customizable variables to an SCons build. """ | |||
import os.path | import os.path | |||
import sys | import sys | |||
from functools import cmp_to_key | from functools import cmp_to_key | |||
import SCons.Environment | import SCons.Environment | |||
import SCons.Errors | import SCons.Errors | |||
import SCons.Util | import SCons.Util | |||
import SCons.Warnings | import SCons.Warnings | |||
skipping to change at line 75 | skipping to change at line 75 | |||
if files: | if files: | |||
files = [files,] | files = [files,] | |||
else: | else: | |||
files = [] | files = [] | |||
self.files = files | self.files = files | |||
self.unknown = {} | self.unknown = {} | |||
# create the singleton instance | # create the singleton instance | |||
if is_global: | if is_global: | |||
self = Variables.instance | self = Variables.instance | |||
if not Variables.instance: | if not Variables.instance: | |||
Variables.instance=self | Variables.instance=self | |||
def _do_add(self, key, help="", default=None, validator=None, converter=None , **kwargs) -> None: | def _do_add(self, key, help="", default=None, validator=None, converter=None , **kwargs) -> None: | |||
class Variable: | class Variable: | |||
pass | pass | |||
option = Variable() | option = Variable() | |||
# If we get a list or a tuple, we take the first element as the | # If we get a list or a tuple, we take the first element as the | |||
# option key and store the remaining in aliases. | # option key and store the remaining in aliases. | |||
if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | |||
option.key = key[0] | option.key = key[0] | |||
option.aliases = list(key[1:]) | option.aliases = list(key[1:]) | |||
skipping to change at line 114 | skipping to change at line 114 | |||
# so we remove the key and all its aliases from that dict | # so we remove the key and all its aliases from that dict | |||
for alias in option.aliases + [option.key,]: | for alias in option.aliases + [option.key,]: | |||
if alias in self.unknown: | if alias in self.unknown: | |||
del self.unknown[alias] | del self.unknown[alias] | |||
def keys(self) -> list: | def keys(self) -> list: | |||
"""Returns the keywords for the options.""" | """Returns the keywords for the options.""" | |||
return [o.key for o in self.options] | return [o.key for o in self.options] | |||
def Add(self, key, *args, **kwargs) -> None: | def Add(self, key, *args, **kwargs) -> None: | |||
r""" Add an option. | r""" Adds an option. | |||
Args: | Arguments: | |||
key: the name of the variable, or a 5-tuple (or list). | key: the name of the variable, or a 5-tuple (or list). | |||
If a tuple, and there are no additional arguments, | If a tuple, and there are no additional arguments, | |||
the tuple is unpacked into help, default, validator, converter. | the tuple is unpacked into the four named kwargs from below. | |||
If there are additional arguments, the first word of the tuple | If a tuple and there are additional arguments, the first word | |||
is taken as the key, and the remainder as aliases. | of the tuple is taken as the key, and the remainder as aliases. | |||
\*args: optional positional arguments | *args: optional positional arguments, corresponding to the four | |||
help: optional help text for the options (Default value = "") | named kwargs below. | |||
default: optional default value for option (Default value = None) | ||||
validator: optional function called to validate the option's value | Keyword Args: | |||
(Default value = None) | help: help text for the options (Default value = "") | |||
converter: optional function to be called to convert the option's | default: default value for option (Default value = None) | |||
value before putting it in the environment. (Default value = None) | validator: function called to validate the option's value | |||
\*\*kwargs: keyword args, can be the arguments from \*args or | (Default value = None) | |||
arbitrary kwargs used by a variable itself | converter: function to be called to convert the option's | |||
value before putting it in the environment. (Default value = None) | ||||
**kwargs: arbitrary keyword arguments used by the variable itself. | ||||
""" | """ | |||
if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | if SCons.Util.is_List(key) or SCons.Util.is_Tuple(key): | |||
if not (len(args) or len(kwargs)): | if not (len(args) or len(kwargs)): | |||
return self._do_add(*key) | return self._do_add(*key) | |||
return self._do_add(key, *args, **kwargs) | return self._do_add(key, *args, **kwargs) | |||
def AddVariables(self, *optlist) -> None: | def AddVariables(self, *optlist) -> None: | |||
""" Add a list of options. | """ Adds a list of options. | |||
Each list element is a tuple/list of arguments to be passed on | Each list element is a tuple/list of arguments to be passed on | |||
to the underlying method for adding options. | to the underlying method for adding options. | |||
Example:: | Example:: | |||
opt.AddVariables( | opt.AddVariables( | |||
('debug', '', 0), | ('debug', '', 0), | |||
('CC', 'The C compiler'), | ('CC', 'The C compiler'), | |||
('VALIDATE', 'An option for testing validation', 'notset', valid ator, None), | ('VALIDATE', 'An option for testing validation', 'notset', valid ator, None), | |||
) | ) | |||
""" | """ | |||
for o in optlist: | for o in optlist: | |||
self._do_add(*o) | self._do_add(*o) | |||
def Update(self, env, args=None) -> None: | def Update(self, env, args=None) -> None: | |||
""" Update an environment with the option variables. | """ Updates an environment with the option variables. | |||
Args: | Args: | |||
env: the environment to update. | env: the environment to update. | |||
args: [optional] a dictionary of keys and values to update | args (optional): a dictionary of keys and values to update | |||
in *env*. If omitted, uses the variables from the commandline. | in *env*. If omitted, uses the variables from the commandline. | |||
""" | """ | |||
values = {} | values = {} | |||
# first set the defaults: | # first set the defaults: | |||
for option in self.options: | for option in self.options: | |||
if option.default is not None: | if option.default is not None: | |||
values[option.key] = option.default | values[option.key] = option.default | |||
skipping to change at line 288 | skipping to change at line 290 | |||
defaultVal = option.converter(defaultVal, env) | defaultVal = option.converter(defaultVal, env) | |||
if str(env.subst('${%s}' % option.key)) != str(defaultVa l): | if str(env.subst('${%s}' % option.key)) != str(defaultVa l): | |||
fh.write('%s = %s\n' % (option.key, repr(value))) | fh.write('%s = %s\n' % (option.key, repr(value))) | |||
except KeyError: | except KeyError: | |||
pass | pass | |||
except IOError as x: | except IOError as x: | |||
raise SCons.Errors.UserError('Error writing options to file: %s\n%s' % (filename, x)) | raise SCons.Errors.UserError('Error writing options to file: %s\n%s' % (filename, x)) | |||
def GenerateHelpText(self, env, sort=None) -> str: | def GenerateHelpText(self, env, sort=None) -> str: | |||
""" Generate the help text for the options. | """ Generates the help text for the options. | |||
Args: | Args: | |||
env: an environment that is used to get the current values | env: an environment that is used to get the current values | |||
of the options. | of the options. | |||
cmp: Either a comparison function used for sorting | sort: Either a comparison function used for sorting | |||
(must take two arguments and return -1, 0 or 1) | (must take two arguments and return -1, 0 or 1) | |||
or a boolean to indicate if it should be sorted. | or a boolean to indicate if it should be sorted. | |||
""" | """ | |||
if callable(sort): | if callable(sort): | |||
options = sorted(self.options, key=cmp_to_key(lambda x, y: sort(x.ke y, y.key))) | options = sorted(self.options, key=cmp_to_key(lambda x, y: sort(x.ke y, y.key))) | |||
elif sort is True: | elif sort is True: | |||
options = sorted(self.options, key=lambda x: x.key) | options = sorted(self.options, key=lambda x: x.key) | |||
else: | else: | |||
options = self.options | options = self.options | |||
End of changes. 11 change blocks. | ||||
21 lines changed or deleted | 23 lines changed or added |