Beispiel #1
0
    def _parse_string_as_bool(self, bool_string):
        """
        'private' function performing parse of a string containing
        a bool representation as defined in the parameter set/otdb
        implementation
        """
        true_chars = ['t', 'T', 'y', 'Y', '1']
        false_chars = ['f', 'F', 'n', 'N', '0']
        if bool_string[0] in true_chars:
            return True
        if bool_string[0] in false_chars:
            return False

        raise tcError(
            "Supplied string cannot be parsed as a bool: {0}".format(bool_string))
Beispiel #2
0
    def set_opts(self, opts):
        """Set multiple variables at once.

        opts should be dictionary of name->value
        """
        opts = dict(opts)
        for k, v in opts.iteritems():
            try:
                # Fix for lofar parameter set integration:
                # If the attribute is a bool, test if it is a string.
                # and then try to parse it
                if hasattr(self, k):
                    if isinstance(self.__getattribute__(k), bool):
                        if isinstance(v, bool) or v == None:
                            # just enter the bool into the parameter
                            pass
                        elif isinstance(v, basestring):
                            # Try parse it as a parameter set bool string
                            v = self._parse_string_as_bool(v)
                        else:
                            # raise error
                            raise tcError("unknown type for bool variable")
                if k == 'sb':
                    # quick check on the sb sintax
                    import re
                    def sb_re(strg):
                        return bool(re.match("^[0-9,.]*$", strg))
                    if not sb_re(v): raise RuntimeError('Parameter "{0}" is not defined properly.'.format(k))

                if v == "none":
                    v = None
                self.__setattr__(k, v)
            except tcError, e:
                # Catch and re-raise as a RuntimeError
                raise RuntimeError('Parameter "{0}" is not defined properly.\
                         \n {1}'.format(k, str(e)))