Beispiel #1
0
    def resolveString(self, value, resolving=None):
        if resolving is None:
            resolving = OrderedDict()
        assert isinstance(resolving, dict)
        resolving = resolving.copy()

        # we do this to deal with non string values (Boolean, integer, ...)
        # as the preg_replace_callback throw an exception when trying
        # a non-string in a parameter value
        match = re.search(r"^%([^%\s]+)%$", value)
        if match:
            key = match.group(1).lower()
            if key in resolving.keys():
                raise ParameterCircularReferenceException(
                    list(resolving.keys()))
            resolving[key] = True
            if self._resolved:
                return self.get(key)
            else:
                return self.resolveValue(self.get(key), resolving)

        def callback(match):
            _resolving = resolving.copy()
            key = match.group(1)
            if not key:
                return "%%"
            key = key.lower()
            if key in _resolving.keys():
                raise ParameterCircularReferenceException(
                    list(_resolving.keys()))
            resolved = self.get(key)
            if not isinstance(resolved, (String, float, int, complex)):
                raise RuntimeException(
                    'A string value must be composed of strings and/or '
                    'numbers, but found parameter "{0}" of type {1} inside '
                    'string value "{2}".'
                    "".format(key,
                              type(resolved).__name__, value))
            resolved = str(resolved)
            _resolving[key] = True
            if self.isResolved():
                return resolved
            else:
                return self.resolveString(resolved, _resolving)

        return re.sub(r"%%|%([^%\s]+)%", callback, value)
Beispiel #2
0
    def resolveValue(self, value, resolving=None):
        if resolving is None:
            resolving = OrderedDict()
        assert isinstance(resolving, dict)
        resolving = resolving.copy()

        if isinstance(value, dict):
            args = dict()
            for k, v in value.items():
                args[self.resolveValue(k, resolving)] = self.resolveValue(
                    v, resolving)
            return args

        elif isinstance(value, list):
            args = list()
            for v in value:
                args.append(self.resolveValue(v, resolving))
            return args

        if not isinstance(value, String):
            return value

        return self.resolveString(value, resolving)