How to use the lbuild.option.Option function in lbuild

To help you get started, we’ve selected a few lbuild examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github modm-io / lbuild / lbuild / option.py View on Github external
values = self.values
        values.sort(key=lambda v: (float(v) if v.isdigit() else 0, v))
        return values

    def format_values(self):
        values = [_cw(v).wrap("underlined") if v == self._default else _cw(v)
                  for v in self._format_values()]
        return _cw(", ").join(values)

    def as_enumeration(self, value):
        try:
            return self._enumeration[self._obj_to_str(value)]
        except KeyError:
            raise TypeError("Input must be an element of the enumeration!")

class OptionSet(Option):

    def __init__(self, option, default=None):
        if isinstance(option, (SetOption, StringOption)):
            raise le.LbuildOptionConstructionException(self, "StringOption cannot be used as a set option!")

        Option.__init__(self, option.name, option._description,
                        convert_input=self.to_set,
                        convert_output=self.as_set)
        self._option = option
        self._dependency_handler = option._dependency_handler
        if default is not None:
            self._set_default(default)
        elif not option.is_default():
            self._set_default(option._default)

    def format_value(self):
github modm-io / lbuild / lbuild / option.py View on Github external
    @staticmethod
    def as_boolean(value):
        if value is None:
            return value
        if isinstance(value, bool):
            return value
        if str(value).lower() in ['true', 'yes', '1']:
            return True
        if str(value).lower() in ['false', 'no', '0']:
            return False

        raise TypeError("Input must be boolean!")


class NumericOption(Option):

    def __init__(self, name, description, minimum=None, maximum=None,
                 default=None, dependencies=None):
        Option.__init__(self, name, description, default, dependencies,
                        convert_input=str,
                        convert_output=self.as_numeric_value)
        self.minimum_input = str(minimum)
        self.maximum_input = str(maximum)
        self.minimum = None
        self.maximum = None

        try:
            self.minimum = self.as_numeric_value(minimum)
        except (TypeError, ValueError) as error:
            raise le.LbuildOptionConstructionException(self,
                    "Minimum '{}' is invalid! {}".format(minimum, error))
github modm-io / lbuild / lbuild / option.py View on Github external
class StringOption(Option):

    def __init__(self, name, description, default=None, dependencies=None, validate=None):
        Option.__init__(self, name, description, None, dependencies,
                        convert_input=self._validate_string)
        self._validate = validate
        self._set_default(default)

    def _validate_string(self, value):
        value = str(value)
        if self._validate:
            self._validate(value)
        return value


class PathOption(Option):

    def __init__(self, name, description, default=None, dependencies=None, empty_ok=False, absolute=False):
        self._empty_ok = empty_ok
        self._absolute = absolute
        Option.__init__(self, name, description, default, dependencies,
                        convert_input=self._validate_path,
                        convert_output=self._relocate_path)

    def _relocate(self, path):
        return self._absolute and (self._filename is not None) and (path != "")

    def _relocate_path(self, path):
        path = str(path).strip()
        # relocate path relative to the option filepath to absolute
        if self._relocate(path):
            path = os.path.abspath(os.path.join(self._filepath, path))
github modm-io / lbuild / lbuild / option.py View on Github external
if value is None:
            return value
        if isinstance(value, (int, float)):
            return value
        if isinstance(value, str):
            try:
                value = eval(value)
                if isinstance(value, (int, float)):
                    return value
            except:
                pass

        raise TypeError("Input must be numeric!")


class EnumerationOption(Option):

    def __init__(self, name, description, enumeration, default=None, dependencies=None):
        Option.__init__(self, name, description, None, dependencies,
                        convert_input=self._obj_to_str,
                        convert_output=self.as_enumeration)
        if self._is_enum(enumeration):
            self._enumeration = {self._obj_to_str(entry): entry.value for entry in enumeration}
        elif isinstance(enumeration, (list, tuple, set, range)) and \
           len(enumeration) == len(set(enumeration)):
            # If the argument is a list and the items in the list are unique,
            # convert it so that the value of the enum equals its name.
            self._enumeration = {self._obj_to_str(entry): entry for entry in enumeration}
        elif isinstance(enumeration, dict):
            self._enumeration = enumeration
            for key in self._enumeration.keys():
                if not isinstance(key, str):
github modm-io / lbuild / lbuild / option.py View on Github external
def values(self):
        return ["Path"]

    def format_value(self):
        value = str(self._input).strip()
        if value == "":
            value = "[]"
        return value

    def format_values(self):
        if self.is_default() or self._default == "":
            return _cw("Path")
        return _cw("Path: ") + _cw(str(self._default)).wrap("underlined")


class BooleanOption(Option):

    def __init__(self, name, description, default=False, dependencies=None):
        Option.__init__(self, name, description, default, dependencies,
                        convert_input=self.as_boolean,
                        convert_output=self.as_boolean)

    @property
    def values(self):
        return ["True", "False"]

    def format_values(self):
        if self._default:
            return _cw("True").wrap("underlined") + _cw(", False")
        return _cw("True, ") + _cw("False").wrap("underlined")

    @staticmethod
github modm-io / lbuild / lbuild / option.py View on Github external
    @Option.value.setter
    def value(self, value):
        try:
            numeric_value = self.as_numeric_value(value)
            if self.minimum is not None and numeric_value < self.minimum:
                raise ValueError("Input must be greater or equal to '{}'".format(self.minimum))
            if self.maximum is not None and numeric_value > self.maximum:
                raise ValueError("Input must be smaller or equal to '{}'".format(self.maximum))
        except (TypeError, ValueError) as error:
            raise le.LbuildOptionInputException(self, value, error)
        self._set_value(value)
github modm-io / lbuild / lbuild / option.py View on Github external
def __init__(self, name, description, minimum=None, maximum=None,
                 default=None, dependencies=None):
        Option.__init__(self, name, description, default, dependencies,
                        convert_input=str,
                        convert_output=self.as_numeric_value)
        self.minimum_input = str(minimum)
        self.maximum_input = str(maximum)
        self.minimum = None
        self.maximum = None

        try:
            self.minimum = self.as_numeric_value(minimum)
        except (TypeError, ValueError) as error:
            raise le.LbuildOptionConstructionException(self,
                    "Minimum '{}' is invalid! {}".format(minimum, error))
        try:
            self.maximum = self.as_numeric_value(maximum)
        except (TypeError, ValueError) as error:
            raise le.LbuildOptionConstructionException(self,
github modm-io / lbuild / lbuild / option.py View on Github external
def is_default(self):
        return self._default is None or self._input == self._default

    def format_value(self):
        value = str(self._input).strip()
        if value == "":
            value = '""'
        return value

    def format_values(self):
        if self.is_default() or self._default == "":
            return _cw("String")
        return _cw("String: ") + _cw(str(self._default)).wrap("underlined")


class StringOption(Option):

    def __init__(self, name, description, default=None, dependencies=None, validate=None):
        Option.__init__(self, name, description, None, dependencies,
                        convert_input=self._validate_string)
        self._validate = validate
        self._set_default(default)

    def _validate_string(self, value):
        value = str(value)
        if self._validate:
            self._validate(value)
        return value


class PathOption(Option):