コード例 #1
0
    def getExactOID(self, base):
        """Get the given OID,value pair for the given base

		This method is responsible for implementing the GET
		request, (or a GETBULK request which specifies
		inclusive operation).
		"""
        base = oidToSortable(base)
        start = bisect.bisect(self.OIDs, (base, ))
        try:
            oid, result = self.OIDs[start]
        except (IndexError, KeyError):
            # do error reporting here
            raise errors.OIDNameError(
                base,
                message="OID not found in database",
            )
        else:
            if oid != base:
                try:
                    oid, result = self.OIDs[start - 1]
                except IndexError, err:
                    pass
                else:
                    if (oidstore.dumbPrefix(oid, base)
                            and hasattr(result, 'getExactOID')):
                        return result.getExactOID(sortableToOID(base))
                raise errors.OIDNameError(
                    base,
                    message="OID not found in database",
                )
            return sortableToOID(base), self.returnValue(result, base)
コード例 #2
0
    def getExactOID(self, base):
        """Get the given OID,value pair for the given base

		This method is responsible for implementing the GET
		request, (or a GETBULK request which specifies
		inclusive operation).
		"""
        encoded = oidToSortable(base)
        if self.btree.has_key(encoded):
            return base, self.btree[encoded]
        raise errors.OIDNameError(base, message="No such OID")
コード例 #3
0
    def nextOID(self, base):
        """Get next OID,value pair after given base OID

		This method is responsible for implementing GETNEXT,
		and GETBULK requests.
		"""
        try:
            encoded = oidToSortable(base)
            oid, value = self.btree.set_location(encoded)
            oid = sortableToOID(oid)
        except KeyError, err:
            raise errors.OIDNameError(base,
                                      message="OID not found in database")
コード例 #4
0
    def firstOID(self):
        """Retrieve the first OID,value pair for the storage

		Raises OIDNameError if there are no pairs available
		"""
        try:
            oid, value = self.btree.first()
            return sortableToOID(oid), value
        except bsddb.error:
            raise errors.OIDNameError(
                (),
                message="""No OIDs available in this storage""",
            )
コード例 #5
0
    def nextOID(self, base):
        """Get next OID,value pair after given base OID

		This method is responsible for implementing GETNEXT,
		and GETBULK requests.
		"""
        base = oidToSortable(base)
        start = bisect.bisect(self.OIDs, (base, ))
        # if we have pysnmp-se optimised isaprefix support, use that,
        # as it saves copying the OIDs using slice operations
        if hasattr(base.__class__, 'isaprefix'):
            dumbPrefix = base.__class__.isaprefix
        else:
            dumbPrefix = oidstore.dumbPrefix
        if start < len(self.OIDs):
            # require for all OIDs that they precisely match
            # an OID in the OID set we publish...
            oid, value = self.OIDs[start]
            if oid != base and not dumbPrefix(base, oid):
                raise errors.OIDNameError(
                    base,
                    message="Could not find OID in database",
                )
            elif oid != base and dumbPrefix(base, oid):
                # if the found OID is prefixed by key, we want to return this OID
                # otherwise we want to return the *next* item

                # iff the value is a sub-storage, we need to get it to
                # see if it's got the proper value within it, if not, then
                # try the next oidstore/value
                if hasattr(value, 'nextOID'):
                    try:
                        return value.nextOID(sortableToOID(base))
                    except errors.OIDNameError, err:
                        start += 1
            else:
                # otherwise return the item *after* the found OID (exact match)
                # again, if the value is a sub-storage, then we need to search
                # forward from the item after...
                start += 1
            # now get the real value...
            while start < len(self.OIDs):
                key, value = self.OIDs[start]
                if hasattr(value, 'firstOID'):
                    try:
                        return value.firstOID()
                    except errors.OIDNameError, err:
                        pass
                else:
                    return sortableToOID(key), self.returnValue(value, key)
                start += 1
コード例 #6
0
    def firstOID(self):
        """Retrieve the first OID,value pair for the storage

		Raises OIDNameError if there are no pairs available
		"""
        if self.OIDs:
            oid, value = self.OIDs[0]
            if hasattr(value, 'firstOID'):
                index = 0
                while hasattr(value, 'firstOID'):
                    try:
                        return value.firstOID()
                    except errors.OIDNameError, err:
                        index += 1
                        try:
                            oid, value = self.OIDs[index]
                        except IndexError:
                            raise errors.OIDNameError(
                                (),
                                message="""No OIDs available in this storage""",
                            )
            return sortableToOID(oid), self.returnValue(value, oid)
コード例 #7
0
                while hasattr(value, 'firstOID'):
                    try:
                        return value.firstOID()
                    except errors.OIDNameError, err:
                        index += 1
                        try:
                            oid, value = self.OIDs[index]
                        except IndexError:
                            raise errors.OIDNameError(
                                (),
                                message="""No OIDs available in this storage""",
                            )
            return sortableToOID(oid), self.returnValue(value, oid)
        else:
            raise errors.OIDNameError(
                (),
                message="""No OIDs available in this storage""",
            )

    def nextOID(self, base):
        """Get next OID,value pair after given base OID

		This method is responsible for implementing GETNEXT,
		and GETBULK requests.
		"""
        base = oidToSortable(base)
        start = bisect.bisect(self.OIDs, (base, ))
        # if we have pysnmp-se optimised isaprefix support, use that,
        # as it saves copying the OIDs using slice operations
        if hasattr(base.__class__, 'isaprefix'):
            dumbPrefix = base.__class__.isaprefix
        else:
コード例 #8
0
class BSDOIDStore(oidstore.OIDStore):
    """OIDStore implemented using (on-disk) BSDDB files

	This OID store is appropriate for middle-sized OID
	tables which require persistence across Python sessions.
	
	The store uses a BSDDB BTree database provided by the
	Python optional bsddb module wrapped by the standard
	shelve module.
	"""
    def __init__(self, filename, OIDs=None):
        """Initialise the storage with appropriate OIDs"""
        self.btree = self.open(filename)
        self.update(OIDs)
        self.close = Closer(self)

    def open(self, filename, mode='c'):
        """Open the given shelf as a BSDDB btree shelf

		XXX patches bug in Python 2.3.x set_location for
		bsddb objects as a side-effect
		"""
        if isinstance(filename, (str, unicode)):
            filename = bsddb.btopen(filename, mode)
            if sys.version >= '2.3':
                # need to patch bug in 2.3's set_location
                # XXX need to have a < as well once fixed!
                bsddb._DBWithCursor.set_location = set_location
            filename = shelve.BsdDbShelf(filename)
        return filename

    open = classmethod(open)

    def getExactOID(self, base):
        """Get the given OID,value pair for the given base

		This method is responsible for implementing the GET
		request, (or a GETBULK request which specifies
		inclusive operation).
		"""
        encoded = oidToSortable(base)
        if self.btree.has_key(encoded):
            return base, self.btree[encoded]
        raise errors.OIDNameError(base, message="No such OID")

    def setValue(self, oid, value):
        """Set the given oid,value pair, returning old value

		This method is responsible for implementing the SET
		request.
		"""
        old = None
        oid = oidToSortable(oid)
        if self.btree.has_key(oid):
            try:
                old = self.btree[oid]
            except KeyError:
                pass
        self.btree[oid] = value
        self.btree.sync()
        return old

    def firstOID(self):
        """Retrieve the first OID,value pair for the storage

		Raises OIDNameError if there are no pairs available
		"""
        try:
            oid, value = self.btree.first()
            return sortableToOID(oid), value
        except bsddb.error:
            raise errors.OIDNameError(
                (),
                message="""No OIDs available in this storage""",
            )

    def nextOID(self, base):
        """Get next OID,value pair after given base OID

		This method is responsible for implementing GETNEXT,
		and GETBULK requests.
		"""
        try:
            encoded = oidToSortable(base)
            oid, value = self.btree.set_location(encoded)
            oid = sortableToOID(oid)
        except KeyError, err:
            raise errors.OIDNameError(base,
                                      message="OID not found in database")
        if oid == base:
            try:
                oid, value = self.btree.next()
                oid = sortableToOID(oid)
            except KeyError, err:
                raise errors.OIDNameError(
                    base, message="OID appears to be last in database")