예제 #1
0
파일: property.py 프로젝트: pwgen/alignak
    def pythonize(val):
        """Convert value into a boolean

        :param val: value to convert
        :type val:
        :return: boolean corresponding to value ::

        {'1': True, 'yes': True, 'true': True, 'on': True,
         '0': False, 'no': False, 'false': False, 'off': False}

        :rtype: bool
        """
        __boolean_states__ = {
            '1': True,
            'yes': True,
            'true': True,
            'on': True,
            '0': False,
            'no': False,
            'false': False,
            'off': False
        }

        if isinstance(val, bool):
            return val
        val = unique_value(val).lower()
        if val in __boolean_states__.keys():
            return __boolean_states__[val]
        else:
            raise PythonizeError("Cannot convert '%s' to a boolean value" %
                                 val)
예제 #2
0
    def pythonize(self, val):
        """Convert value into a string::

        * If value is a list, try to take the last element

        :param val: value to convert
        :type val:
        :return: str corresponding to value
        :rtype: str
        """
        return unique_value(val).strip()
예제 #3
0
    def pythonize(self, val):
        """Convert value into a string::

        * If value is a list, try to take the last element

        :param val: value to convert
        :type val:
        :return: str corresponding to value
        :rtype: str
        """
        return unique_value(val).strip()
예제 #4
0
    def pythonize(self, val):
        """Convert value into a log level property::

        * If value is a list, try to take the last element
        * get logging level base on the value

        :param val: value to convert
        :type val:
        :return: log level corresponding to value
        :rtype: str
        """
        return logging.getLevelName(unique_value(val))
예제 #5
0
    def pythonize(self, val):
        """Convert value into a char ::

        * If value is a list try, to take the last element
        * Then take the first char of val (first elem)

        :param val: value to convert
        :type val:
        :return: char corresponding to value
        :rtype: str
        """
        return to_char(unique_value(val))
예제 #6
0
    def pythonize(self, val):
        """Convert value into a float::

        * If value is a list, try to take the last element
        * Then call float(val)

        :param val: value to convert
        :type val:
        :return: float corresponding to value
        :rtype: float
        """
        return to_float(unique_value(val))
예제 #7
0
    def pythonize(self, val):
        """Convert value into a log level property::

        * If value is a list, try to take the last element
        * get logging level base on the value

        :param val: value to convert
        :type val:
        :return: log level corresponding to value
        :rtype: str
        """
        return logging.getLevelName(unique_value(val))
예제 #8
0
    def pythonize(self, val):
        """Convert value into a char ::

        * If value is a list try, to take the last element
        * Then take the first char of val (first elem)

        :param val: value to convert
        :type val:
        :return: char corresponding to value
        :rtype: str
        """
        return to_char(unique_value(val))
예제 #9
0
    def pythonize(self, val):
        """Convert value into a float::

        * If value is a list, try to take the last element
        * Then call float(val)

        :param val: value to convert
        :type val:
        :return: float corresponding to value
        :rtype: float
        """
        return to_float(unique_value(val))
예제 #10
0
파일: property.py 프로젝트: pwgen/alignak
    def pythonize(self, val):
        """Convert value into an integer::

        * If value is a list, try to take the last element
        * Then call float(int(val))

        :param val: value to convert
        :type val:
        :return: integer corresponding to value
        :rtype: int
        """
        val = unique_value(val)
        return to_int(val)
예제 #11
0
파일: property.py 프로젝트: LionelR/alignak
    def pythonize(self, val):
        """Convert value into an integer::

        * If value is a list, try to take the last element
        * Then call float(int(val))

        :param val: value to convert
        :type val:
        :return: integer corresponding to value
        :rtype: int
        """
        val = unique_value(val)
        return to_int(val)
예제 #12
0
파일: property.py 프로젝트: LionelR/alignak
    def pythonize(self, val):
        """Convert value into a dict::

        * If value is a list, try to take the last element
        * split "key=value" string and convert to { key:value }

        :param val: value to convert
        :type val:
        :return: log level corresponding to value
        :rtype: str
        """
        val = unique_value(val)

        def split(keyval):
            """Split key-value string into (key,value)

            :param keyval: key value string
            :return: key, value
            :rtype: tuple
            """
            matches = re.match(r"^\s*([^\s]+)\s*=\s*([^\s]+)\s*$", keyval)
            if matches is None:
                raise ValueError

            return (
                matches.group(1),
                # >2.4 only. we keep it for later. m.group(2) if self.elts_prop is None
                # else self.elts_prop.pythonize(m.group(2))
                (self.elts_prop.pythonize(matches.group(2)),
                 matches.group(2))[self.elts_prop is None]
            )

        if val is None:
            return dict()

        if self.elts_prop is None:
            return val

        # val is in the form "key1=addr:[port],key2=addr:[port],..."
        print ">>>", dict([split(kv) for kv in to_split(val)])
        return dict([split(kv) for kv in to_split(val)])
예제 #13
0
파일: property.py 프로젝트: pwgen/alignak
    def pythonize(self, val):
        """Convert value into a address ip format::

        * If value is a list, try to take the last element
        * match ip address and port (if available)

        :param val: value to convert
        :type val:
        :return: address/port corresponding to value
        :rtype: dict
        """
        val = unique_value(val)
        matches = re.match(r"^([^:]*)(?::(\d+))?$", val)
        if matches is None:
            raise ValueError

        addr = {'address': matches.group(1)}
        if matches.group(2) is not None:
            addr['port'] = int(matches.group(2))

        return addr
예제 #14
0
파일: property.py 프로젝트: LionelR/alignak
    def pythonize(self, val):
        """Convert value into a address ip format::

        * If value is a list, try to take the last element
        * match ip address and port (if available)

        :param val: value to convert
        :type val:
        :return: address/port corresponding to value
        :rtype: dict
        """
        val = unique_value(val)
        matches = re.match(r"^([^:]*)(?::(\d+))?$", val)
        if matches is None:
            raise ValueError

        addr = {'address': matches.group(1)}
        if matches.group(2) is not None:
            addr['port'] = int(matches.group(2))

        return addr
예제 #15
0
파일: property.py 프로젝트: pwgen/alignak
    def pythonize(self, val):
        """Convert value into a dict::

        * If value is a list, try to take the last element
        * split "key=value" string and convert to { key:value }

        :param val: value to convert
        :type val:
        :return: log level corresponding to value
        :rtype: str
        """
        val = unique_value(val)

        def split(keyval):
            """Split key-value string into (key,value)

            :param keyval: key value string
            :return: key, value
            :rtype: tuple
            """
            matches = re.match(r"^\s*([^\s]+)\s*=\s*([^\s]+)\s*$", keyval)
            if matches is None:
                raise ValueError

            return (
                matches.group(1),
                # >2.4 only. we keep it for later. m.group(2) if self.elts_prop is None
                # else self.elts_prop.pythonize(m.group(2))
                (self.elts_prop.pythonize(matches.group(2)), matches.group(2)
                 )[self.elts_prop is None])

        if val is None:
            return dict()

        if self.elts_prop is None:
            return val

        # val is in the form "key1=addr:[port],key2=addr:[port],..."
        print ">>>", dict([split(kv) for kv in to_split(val)])
        return dict([split(kv) for kv in to_split(val)])
예제 #16
0
파일: property.py 프로젝트: LionelR/alignak
    def pythonize(val):
        """Convert value into a boolean

        :param val: value to convert
        :type val:
        :return: boolean corresponding to value ::

        {'1': True, 'yes': True, 'true': True, 'on': True,
         '0': False, 'no': False, 'false': False, 'off': False}

        :rtype: bool
        """
        __boolean_states__ = {'1': True, 'yes': True, 'true': True, 'on': True,
                              '0': False, 'no': False, 'false': False, 'off': False}

        if isinstance(val, bool):
            return val
        val = unique_value(val).lower()
        if val in __boolean_states__.keys():
            return __boolean_states__[val]
        else:
            raise PythonizeError("Cannot convert '%s' to a boolean value" % val)