예제 #1
0
    def __coerce_value(initial_value, new_value):
        """Coerce the new_value to the same type as the initial_value.

        :param initial_value: initial value from the device
        :param new_value: new value to set, coerced into the right type
        :return: new value, coerced into the right type
        """
        # pylint: disable=redefined-variable-type
        # In order to return the right type the return value has to be
        # redefined.

        # Types from RFC-1902
        if isinstance(initial_value, rfc1902.Counter32):
            set_value = rfc1902.Counter32(str(new_value))
        elif isinstance(initial_value, rfc1902.Counter64):
            set_value = rfc1902.Counter64(str(new_value))
        elif isinstance(initial_value, rfc1902.Gauge32):
            set_value = rfc1902.Gauge32(str(new_value))
        elif isinstance(initial_value, rfc1902.Integer):
            set_value = rfc1902.Integer(str(new_value))
        elif isinstance(initial_value, rfc1902.Integer32):
            set_value = rfc1902.Integer32(str(new_value))
        elif isinstance(initial_value, rfc1902.IpAddress):
            set_value = rfc1902.IpAddress(str(new_value))
        elif isinstance(initial_value, rfc1902.OctetString):
            set_value = rfc1902.OctetString(str(new_value))
        elif isinstance(initial_value, rfc1902.TimeTicks):
            set_value = rfc1902.TimeTicks(str(new_value))
        elif isinstance(initial_value, rfc1902.Unsigned32):
            set_value = rfc1902.Unsigned32(str(new_value))
        else:
            raise RuntimeError("Unknown type %s" % type(initial_value))

        return set_value
예제 #2
0
 def _to_pysnmp(self, value):
     """ Convert connection plugin object into pysnmp objects """
     if value is None:
         return None
     if isinstance(value, OctetString):
         return rfc1902.OctetString(str(value.value))
     if isinstance(value, ObjectIdentifier):
         return rfc1902.ObjectName(str(value.value))
     if isinstance(value, Integer32):
         return rfc1902.Integer32(int(value.value))
     if isinstance(value, Counter32):
         return rfc1902.Counter32(long(value.value))
     if isinstance(value, IpAddress):
         return rfc1902.IpAddress(str(value.value))
     if isinstance(value, Gauge32):
         return rfc1902.Gauge32(long(value.value))
     if isinstance(value, TimeTicks):
         return rfc1902.TimeTicks(long(value.value))
     if isinstance(value, Opaque):
         return rfc1902.Opaque(value.value)  # FIXME
     if isinstance(value, Counter64):
         return rfc1902.Counter64(str(value.value))
     raise SnmpError('Invalid type: %s' % value.__class__.__name__)
 def convert_to_counter32(self, value):
     """Converts a value to a SNMP Counter32 object."""
     return rfc1902.Counter32(value)